From 33a59db04798a89b08e0d519f0c718b7ec09e795 Mon Sep 17 00:00:00 2001 From: Lukas Krejci Date: Wed, 27 Apr 2022 16:06:21 +0200 Subject: [PATCH] fix: Use the labels used by the che-server to recognize workspace namespaces (#1359) Use the app.kubernetes.io/part-of=che.eclipse.org and app.kubernetes.io/component=workspaces-namespace labels to mark a namespace as workspace namespace. This should bring the operator and che-server in line with how they are marking the managed namespaces. --- controllers/usernamespace/controller.go | 4 ++-- controllers/usernamespace/namespacecache.go | 14 +++++++++++--- controllers/usernamespace/namespacecache_test.go | 16 ++++++++++++++-- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/controllers/usernamespace/controller.go b/controllers/usernamespace/controller.go index 969cdfbf9..d4fcd41b2 100644 --- a/controllers/usernamespace/controller.go +++ b/controllers/usernamespace/controller.go @@ -159,7 +159,7 @@ func isLabeledAsUserSettings(obj metav1.Object) bool { func (r *CheUserNamespaceReconciler) isInManagedNamespace(ctx context.Context, obj metav1.Object) bool { info, err := r.namespaceCache.GetNamespaceInfo(ctx, obj.GetNamespace()) - return err == nil && info != nil && info.OwnerUid != "" + return err == nil && info != nil && info.IsWorkspaceNamespace } func (r *CheUserNamespaceReconciler) triggerAllNamespaces() handler.EventHandler { @@ -195,7 +195,7 @@ func (r *CheUserNamespaceReconciler) Reconcile(ctx context.Context, req ctrl.Req return ctrl.Result{}, err } - if info == nil || info.OwnerUid == "" { + if info == nil || !info.IsWorkspaceNamespace { // we're not handling this namespace return ctrl.Result{}, nil } diff --git a/controllers/usernamespace/namespacecache.go b/controllers/usernamespace/namespacecache.go index 0e8af8bf1..43b7b8b2f 100644 --- a/controllers/usernamespace/namespacecache.go +++ b/controllers/usernamespace/namespacecache.go @@ -30,6 +30,10 @@ const ( workspaceNamespaceOwnerUidLabel string = "che.eclipse.org/workspace-namespace-owner-uid" cheNameLabel string = "che.eclipse.org/che-name" cheNamespaceLabel string = "che.eclipse.org/che-namespace" + chePartOfLabel string = "app.kubernetes.io/part-of" + chePartOfLabelValue string = "che.eclipse.org" + cheComponentLabel string = "app.kubernetes.io/component" + cheComponentLabelValue string = "workspaces-namespace" ) type namespaceCache struct { @@ -39,8 +43,8 @@ type namespaceCache struct { } type namespaceInfo struct { - OwnerUid string - CheCluster *types.NamespacedName + IsWorkspaceNamespace bool + CheCluster *types.NamespacedName } func NewNamespaceCache() *namespaceCache { @@ -117,12 +121,16 @@ func (c *namespaceCache) examineNamespaceUnsafe(ctx context.Context, ns string) labels = map[string]string{} } + // ownerUid is the legacy label that we used to use. Let's not break the existing workspace namespaces and still + // recognize it ownerUid := labels[workspaceNamespaceOwnerUidLabel] cheName := labels[cheNameLabel] cheNamespace := labels[cheNamespaceLabel] + partOfLabel := labels[chePartOfLabel] + componentLabel := labels[cheComponentLabel] ret := namespaceInfo{ - OwnerUid: ownerUid, + IsWorkspaceNamespace: ownerUid != "" || (partOfLabel == chePartOfLabelValue && componentLabel == cheComponentLabelValue), CheCluster: &types.NamespacedName{ Name: cheName, Namespace: cheNamespace, diff --git a/controllers/usernamespace/namespacecache_test.go b/controllers/usernamespace/namespacecache_test.go index 671422ebf..a304d96bd 100644 --- a/controllers/usernamespace/namespacecache_test.go +++ b/controllers/usernamespace/namespacecache_test.go @@ -110,7 +110,7 @@ func TestExamineUpdatesCache(t *testing.T) { nsi, err := nsc.GetNamespaceInfo(ctx, nsName) assert.NoError(t, err) - assert.Empty(t, nsi.OwnerUid, "Detected owner UID should be empty") + assert.False(t, nsi.IsWorkspaceNamespace, "The namespace should not be found as managed") assert.Contains(t, nsc.knownNamespaces, nsName, "The namespace info should have been cached") @@ -126,7 +126,19 @@ func TestExamineUpdatesCache(t *testing.T) { nsi, err = nsc.ExamineNamespace(ctx, nsName) assert.NoError(t, err) - assert.Equal(t, "uid", nsi.OwnerUid, "unexpected detected owner UID") + assert.True(t, nsi.IsWorkspaceNamespace, "namespace should be found as managed using the legacy user UID label") + + ns.(metav1.Object).SetLabels(map[string]string{ + chePartOfLabel: chePartOfLabelValue, + cheComponentLabel: cheComponentLabelValue, + }) + + assert.NoError(t, cl.Update(ctx, ns)) + + nsi, err = nsc.ExamineNamespace(ctx, nsName) + assert.NoError(t, err) + + assert.True(t, nsi.IsWorkspaceNamespace, "namespace should be found as managed using the part-of and component labels") } test(infrastructure.Kubernetes, &corev1.Namespace{