Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SANDBOX-559 K8s 1 27 member #551

Merged
merged 27 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
05068f7
fixes for controller-runtime upgrade and breaking changes
ranakan19 Mar 5, 2024
db372af
unit test changes with deletionTimestamp and fakeClient
ranakan19 Mar 15, 2024
9e9790a
Merge branch 'master' into k8s_1_27_member
ranakan19 Mar 25, 2024
6e167ba
mod tidy, fix toolchain common replace version
ranakan19 Mar 25, 2024
0b5289e
remove commented out code
ranakan19 Mar 27, 2024
0674acb
remove unused function lint check
ranakan19 Mar 27, 2024
60a3477
replace deprecated Poll function to fix linter error
ranakan19 Mar 27, 2024
6376380
correct replace fork version, should fix snyk error too
ranakan19 Mar 27, 2024
b06b7a5
Merge branch 'master' into k8s_1_27_member
ranakan19 Apr 3, 2024
0987ad3
Merge branch 'master' into k8s_1_27_member
ranakan19 Apr 5, 2024
b1816ef
add finalizer, fix lint
ranakan19 Apr 10, 2024
c169584
Merge branch 'master' into k8s_1_27_member
ranakan19 Apr 10, 2024
525e1e4
remove emoty status in test
ranakan19 Apr 15, 2024
8d817d7
merge conflict resolve
ranakan19 Aug 5, 2024
fa0b6c6
update api and common as in master
ranakan19 Aug 20, 2024
c4ce6ed
merge conflict resolve
ranakan19 Aug 20, 2024
dbb501e
update versions
ranakan19 Aug 21, 2024
8fabc1a
Merge branch 'master' into k8s_1_27_member
ranakan19 Nov 11, 2024
2d10436
update api and common latest version
ranakan19 Nov 11, 2024
6c107bb
format
ranakan19 Nov 11, 2024
d8ff43e
update redhat-cop/operator-utils
ranakan19 Nov 11, 2024
ff31b2f
update common version
ranakan19 Nov 11, 2024
70fe12d
update kustome and controller-gen version in makefile
ranakan19 Nov 11, 2024
b2198e9
v5
ranakan19 Nov 11, 2024
8658a94
Merge branch 'master' into k8s_1_27_member
ranakan19 Nov 14, 2024
0aeb383
remove replace, update versions after merge
ranakan19 Nov 14, 2024
35f5fff
remove replace
ranakan19 Nov 14, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions controllers/idler/idler_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,11 @@ func TestReconcile(t *testing.T) {
idler := &toolchainv1alpha1.Idler{
ObjectMeta: metav1.ObjectMeta{
Name: "being-deleted",
Finalizers: []string{"toolchain.dev.openshift.com"},
DeletionTimestamp: &now,
},
Spec: toolchainv1alpha1.IdlerSpec{TimeoutSeconds: 30},
Spec: toolchainv1alpha1.IdlerSpec{TimeoutSeconds: 30},
Status: toolchainv1alpha1.IdlerStatus{Conditions: make([]toolchainv1alpha1.Condition, 0)},
alexeykazakov marked this conversation as resolved.
Show resolved Hide resolved
}
reconciler, req, _, _, _ := prepareReconcile(t, "being-deleted", getHostCluster, idler)

Expand Down Expand Up @@ -635,7 +637,7 @@ func TestEnsureIdlingFailed(t *testing.T) {
require.NoError(t, err)

// second reconcile to delete pods and create notification
cl.MockStatusUpdate = func(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error {
cl.MockStatusUpdate = func(ctx context.Context, obj client.Object, opts ...client.SubResourceUpdateOption) error {
return fmt.Errorf("cannot set status to fail")
}
_, err = reconciler.Reconcile(context.TODO(), req)
Expand Down Expand Up @@ -935,7 +937,7 @@ func TestCreateNotification(t *testing.T) {
nsTmplSet := newNSTmplSet(test.MemberOperatorNs, "alex", "advanced", "abcde11", namespaces, usernames)
mur := newMUR("alex")
reconciler, _, cl, _, _ := prepareReconcile(t, idler.Name, getHostCluster, idler, nsTmplSet, mur)
cl.MockStatusUpdate = func(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error {
cl.MockStatusUpdate = func(ctx context.Context, obj client.Object, opts ...client.SubResourceUpdateOption) error {
return errors.New("can't update condition")
}
//when
Expand Down Expand Up @@ -1327,7 +1329,7 @@ func createPods(t *testing.T, r *Reconciler, owner metav1.Object, startTime meta
return podsToTrack
}

func prepareReconcile(t *testing.T, name string, getHostClusterFunc func(fakeClient client.Client) cluster.GetHostClusterFunc, initIdlerObjs ...runtime.Object) (*Reconciler, reconcile.Request, *test.FakeClient, *test.FakeClient, *fakedynamic.FakeDynamicClient) {
func prepareReconcile(t *testing.T, name string, getHostClusterFunc func(fakeClient client.Client) cluster.GetHostClusterFunc, initIdlerObjs ...client.Object) (*Reconciler, reconcile.Request, *test.FakeClient, *test.FakeClient, *fakedynamic.FakeDynamicClient) {
s := scheme.Scheme
err := apis.AddToScheme(s)
require.NoError(t, err)
Expand Down
5 changes: 3 additions & 2 deletions controllers/memberoperatorconfig/mapper.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package memberoperatorconfig

import (
"context"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -11,8 +12,8 @@ import (
var mapperLog = ctrl.Log.WithName("SecretToMemberOperatorConfigMapper")

// MapSecretToMemberOperatorConfig maps secrets to the singular instance of MemberOperatorConfig named "config"
func MapSecretToMemberOperatorConfig() func(object client.Object) []reconcile.Request {
return func(obj client.Object) []reconcile.Request {
func MapSecretToMemberOperatorConfig() func(context context.Context, object client.Object) []reconcile.Request {
return func(ctx context.Context, obj client.Object) []reconcile.Request {
if secret, ok := obj.(*corev1.Secret); ok {
mapperLog.Info("Secret mapped to MemberOperatorConfig", "name", secret.Name)
return []reconcile.Request{{NamespacedName: types.NamespacedName{Namespace: secret.Namespace, Name: "config"}}}
Expand Down
6 changes: 4 additions & 2 deletions controllers/memberoperatorconfig/mapper_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package memberoperatorconfig

import (
"context"
"testing"

"github.com/codeready-toolchain/toolchain-common/pkg/test"
Expand All @@ -14,6 +15,7 @@ import (

func TestSecretToMemberOperatorConfigMapper(t *testing.T) {
// when
ctx := context.TODO()
secretData := map[string][]byte{
"che-admin-username": []byte("cheadmin"),
"che-admin-password": []byte("password"),
Expand All @@ -24,7 +26,7 @@ func TestSecretToMemberOperatorConfigMapper(t *testing.T) {
secret := newSecret("test-secret", secretData)

// when
req := MapSecretToMemberOperatorConfig()(secret)
req := MapSecretToMemberOperatorConfig()(ctx, secret)

// then
require.Len(t, req, 1)
Expand All @@ -39,7 +41,7 @@ func TestSecretToMemberOperatorConfigMapper(t *testing.T) {
pod := &corev1.Pod{}

// when
req := MapSecretToMemberOperatorConfig()(pod)
req := MapSecretToMemberOperatorConfig()(ctx, pod)

// then
assert.Empty(t, req)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@ import (
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
)

// SetupWithManager sets up the controller with the Manager.
func (r *Reconciler) SetupWithManager(mgr manager.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&toolchainv1alpha1.MemberOperatorConfig{}, builder.WithPredicates(predicate.GenerationChangedPredicate{})).
Watches(&source.Kind{Type: &corev1.Secret{}},
Watches(&corev1.Secret{},
handler.EnqueueRequestsFromMapFunc(MapSecretToMemberOperatorConfig())).
Complete(r)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
schedulingv1 "k8s.io/api/scheduling/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -341,7 +340,7 @@ func TestHandleWebConsolePluginDeploy(t *testing.T) {
})
}

func prepareReconcile(t *testing.T, initObjs ...runtime.Object) (*Reconciler, client.Client) {
func prepareReconcile(t *testing.T, initObjs ...client.Object) (*Reconciler, client.Client) {
os.Setenv("WATCH_NAMESPACE", test.MemberOperatorNs)
restore := test.SetEnvVarAndRestore(t, "MEMBER_OPERATOR_WEBHOOK_IMAGE", "webhookimage")
t.Cleanup(restore)
Expand Down
7 changes: 3 additions & 4 deletions controllers/memberstatus/memberstatus_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/metrics/pkg/apis/metrics/v1beta1"
Expand Down Expand Up @@ -714,7 +713,7 @@ func newGetHostClusterNotExist(fakeClient client.Client) cluster.GetHostClusterF
return NewGetHostClusterWithProbe(fakeClient, false, corev1.ConditionFalse, metav1.Now())
}

func prepareReconcile(t *testing.T, requestName string, getHostClusterFunc func(fakeClient client.Client) cluster.GetHostClusterFunc, allNamespacesClient *test.FakeClient, lastGitHubAPICall time.Time, mockedGitHubClient commonclient.GetGitHubClientFunc, initObjs ...runtime.Object) (*Reconciler, reconcile.Request, *test.FakeClient) {
func prepareReconcile(t *testing.T, requestName string, getHostClusterFunc func(fakeClient client.Client) cluster.GetHostClusterFunc, allNamespacesClient *test.FakeClient, lastGitHubAPICall time.Time, mockedGitHubClient commonclient.GetGitHubClientFunc, initObjs ...client.Object) (*Reconciler, reconcile.Request, *test.FakeClient) {
logf.SetLogger(zap.New(zap.UseDevMode(true)))
os.Setenv("WATCH_NAMESPACE", test.MemberOperatorNs)
fakeClient := test.NewFakeClient(t, initObjs...)
Expand Down Expand Up @@ -779,8 +778,8 @@ func withMemoryUsage(usage string) nodeMetricsModifier {
}
}

func newNodesAndNodeMetrics(nodeAndMetricsCreators ...nodeAndMetricsCreator) []runtime.Object {
var objects []runtime.Object
func newNodesAndNodeMetrics(nodeAndMetricsCreators ...nodeAndMetricsCreator) []client.Object {
var objects []client.Object
for _, create := range nodeAndMetricsCreators {
node, nodeMetrics := create()
objects = append(objects, node, nodeMetrics)
Expand Down
3 changes: 1 addition & 2 deletions controllers/nstemplateset/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/client-go/kubernetes/scheme"
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -330,7 +329,7 @@ func newOptionalDeployment(name, owner string) *appsv1.Deployment {
}
}

func prepareAPIClient(t *testing.T, initObjs ...runtime.Object) (*APIClient, *test.FakeClient) {
func prepareAPIClient(t *testing.T, initObjs ...runtimeclient.Object) (*APIClient, *test.FakeClient) {
s := scheme.Scheme
err := apis.AddToScheme(s)
require.NoError(t, err)
Expand Down
11 changes: 11 additions & 0 deletions controllers/nstemplateset/cluster_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,12 @@ func (r *clusterResourcesManager) deleteClusterResource(ctx context.Context, nsT
if err := r.setStatusUpdatingIfNotProvisioning(ctx, nsTmplSet); err != nil {
return false, err
}
//remove finalizer before deleting
toDelete.SetFinalizers(nil)
alexeykazakov marked this conversation as resolved.
Show resolved Hide resolved
if err := r.Client.Update(ctx, toDelete); err != nil {
return false, errs.Wrapf(err, "failed to remove finalizer on cluster resource of name '%s' and gvk '%v'",
toDelete.GetName(), toDelete.GetObjectKind().GroupVersionKind())
}
if err := r.Client.Delete(ctx, toDelete); err != nil {
return false, errs.Wrapf(err, "failed to delete an existing redundant cluster resource of name '%s' and gvk '%v'",
toDelete.GetName(), toDelete.GetObjectKind().GroupVersionKind())
Expand Down Expand Up @@ -316,6 +322,11 @@ func (r *clusterResourcesManager) delete(ctx context.Context, nsTmplSet *toolcha
}

log.FromContext(ctx).Info("deleting cluster resource", "name", toDelete.GetName(), "kind", toDelete.GetObjectKind().GroupVersionKind().Kind)
// remove finalizer before deleting
toDelete.SetFinalizers(nil)
if err := r.Client.Update(ctx, toDelete); err != nil {
return false, r.wrapErrorWithStatusUpdate(ctx, nsTmplSet, r.setStatusTerminatingFailed, err, "failed to remove finalizer from cluster resource '%s'", toDelete.GetName())
}
if err := r.Client.Delete(ctx, toDelete); err != nil && errors.IsNotFound(err) {
// ignore case where the resource did not exist anymore, move to the next one to delete
continue
Expand Down
3 changes: 2 additions & 1 deletion controllers/nstemplateset/namespaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ func TestEnsureNamespacesFail(t *testing.T) {
nsTmplSet := newNSTmplSet(namespaceName, spacename, "basic", withNamespaces("abcde11", "dev"))
devNS := newNamespace("advanced", spacename, "dev") // NS exists but is missing the resources
manager, fakeClient := prepareNamespacesManager(t, nsTmplSet, devNS)
fakeClient.MockStatusUpdate = func(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error {
fakeClient.MockStatusUpdate = func(ctx context.Context, obj client.Object, opts ...client.SubResourceUpdateOption) error {
return errors.New("unable to update NSTmplSet")
}

Expand Down Expand Up @@ -752,6 +752,7 @@ func TestDeleteNamespace(t *testing.T) {
// given namespace with deletion timestamp
timeStamp := metav1.Now()
deletedNS := codeNS.DeepCopy()
deletedNS.Finalizers = []string{toolchainv1alpha1.FinalizerName}
deletedNS.DeletionTimestamp = &timeStamp
manager, _ := prepareNamespacesManager(t, nsTmplSet, deletedNS)

Expand Down
8 changes: 4 additions & 4 deletions controllers/nstemplateset/nstemplateset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ func (r *Reconciler) SetupWithManager(mgr manager.Manager, allNamespaceCluster r
mapToOwnerByLabel := handler.EnqueueRequestsFromMapFunc(commoncontroller.MapToOwnerByLabel("", toolchainv1alpha1.SpaceLabelKey))
build := ctrl.NewControllerManagedBy(mgr).
For(&toolchainv1alpha1.NSTemplateSet{}, builder.WithPredicates(predicate.GenerationChangedPredicate{})).
Watches(&source.Kind{Type: &corev1.Namespace{}}, mapToOwnerByLabel).
Watches(source.NewKindWithCache(&rbac.Role{}, allNamespaceCluster.GetCache()), mapToOwnerByLabel, builder.WithPredicates(commonpredicates.LabelsAndGenerationPredicate{})).
Watches(source.NewKindWithCache(&rbac.RoleBinding{}, allNamespaceCluster.GetCache()), mapToOwnerByLabel, builder.WithPredicates(commonpredicates.LabelsAndGenerationPredicate{}))
Watches(&corev1.Namespace{}, mapToOwnerByLabel).
WatchesRawSource(source.Kind(allNamespaceCluster.GetCache(), &rbac.Role{}), mapToOwnerByLabel, builder.WithPredicates(commonpredicates.LabelsAndGenerationPredicate{})).
alexeykazakov marked this conversation as resolved.
Show resolved Hide resolved
WatchesRawSource(source.Kind(allNamespaceCluster.GetCache(), &rbac.RoleBinding{}), mapToOwnerByLabel, builder.WithPredicates(commonpredicates.LabelsAndGenerationPredicate{}))
// watch for all cluster resource kinds associated with an NSTemplateSet
for _, clusterResource := range clusterResourceKinds {
// only reconcile generation changes for cluster resources and only when the API group is present in the cluster
if apiGroupIsPresent(apiGroupList.Groups, clusterResource.gvk) {
build = build.Watches(&source.Kind{Type: clusterResource.object}, mapToOwnerByLabel, builder.WithPredicates(commonpredicates.LabelsAndGenerationPredicate{}))
build = build.Watches(clusterResource.object, mapToOwnerByLabel, builder.WithPredicates(commonpredicates.LabelsAndGenerationPredicate{}))
}
}

Expand Down
Loading
Loading