Skip to content

Commit

Permalink
[bp-2.2] orphan the agent install namespace (#38)
Browse files Browse the repository at this point in the history
* orphan the open-cluster-management-agent-addon namespace

Signed-off-by: zhujian <[email protected]>

* orphan the agent install namespace

Signed-off-by: zhujian <[email protected]>

---------

Signed-off-by: zhujian <[email protected]>
  • Loading branch information
zhujian7 authored Nov 6, 2023
1 parent 0c0b050 commit 36d315f
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
103 changes: 103 additions & 0 deletions pkg/addon/manager/addon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
fakekube "k8s.io/client-go/kubernetes/fake"
clienttesting "k8s.io/client-go/testing"
"open-cluster-management.io/addon-framework/pkg/addonfactory"
Expand Down Expand Up @@ -90,6 +91,94 @@ func TestManifestAddonAgent(t *testing.T) {
}
}

func TestManifestOrphan(t *testing.T) {
clusterName := "cluster1"
addonName := "addon1"
imageName := "imageName1"

cases := []struct {
name string
getValuesFunc []addonfactory.GetValuesFunc
installNamespace string
validate func(t *testing.T, manifests []runtime.Object)
}{
{
name: "orphan the install namespace and not overwrite the pull secret",
getValuesFunc: []addonfactory.GetValuesFunc{GetDefaultValues(imageName, newTestImagePullSecret())},
installNamespace: "open-cluster-management-agent-addon",
validate: func(t *testing.T, manifests []runtime.Object) {
nsFound := false
secretFound := false
for _, manifest := range manifests {
obj, ok := manifest.(metav1.ObjectMetaAccessor)
assert.True(t, ok, "invalid manifest")

namespace, nok := obj.(*corev1.Namespace)
if nok {
assert.Equal(t, map[string]string{"addon.open-cluster-management.io/deletion-orphan": ""},
namespace.Annotations, "invalid namespace annotations")
nsFound = true
continue
}

secret, sok := obj.(*corev1.Secret)
if sok {
if secret.Name == "open-cluster-management-image-pull-credentials" {
secretFound = true
}
}
}
assert.True(t, nsFound, "namespace not found")
assert.False(t, secretFound, "pull secret found")
},
},
{
name: "orphan the install namespace and not overwrite the pull secret",
getValuesFunc: []addonfactory.GetValuesFunc{GetDefaultValues(imageName, newTestImagePullSecret())},
installNamespace: "test",
validate: func(t *testing.T, manifests []runtime.Object) {
nsFound := false
secretFound := false
for _, manifest := range manifests {
obj, ok := manifest.(metav1.ObjectMetaAccessor)
assert.True(t, ok, "invalid manifest")

namespace, nok := obj.(*corev1.Namespace)
if nok {
assert.Equal(t, map[string]string{"addon.open-cluster-management.io/deletion-orphan": ""},
namespace.Annotations, "invalid namespace annotations")
nsFound = true
continue
}

secret, sok := obj.(*corev1.Secret)
if sok {
if secret.Name == "open-cluster-management-image-pull-credentials" {
secretFound = true
}
}
}
assert.True(t, nsFound, "namespace not found")
assert.True(t, secretFound, "pull secret not found")
},
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
agentFactory := addonfactory.NewAgentAddonFactory(common.AddonName, FS, "manifests/templates").
WithGetValuesFuncs(c.getValuesFunc...)

addOnAgent, err := agentFactory.BuildTemplateAgentAddon()
assert.NoError(t, err)

manifests, err := addOnAgent.Manifests(newTestCluster(clusterName),
newTestAddOnWithInstallNamespace(addonName, clusterName, c.installNamespace))
assert.NoError(t, err)
c.validate(t, manifests)
})
}
}

func newTestImagePullSecret() *corev1.Secret {
return &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -121,3 +210,17 @@ func newTestAddOn(name, namespace string) *addonv1alpha1.ManagedClusterAddOn {
},
}
}

func newTestAddOnWithInstallNamespace(name, namespace, installNamespace string) *addonv1alpha1.ManagedClusterAddOn {
addon := &addonv1alpha1.ManagedClusterAddOn{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Spec: addonv1alpha1.ManagedClusterAddOnSpec{
InstallNamespace: name,
},
}
addon.Spec.InstallNamespace = installNamespace
return addon
}
3 changes: 3 additions & 0 deletions pkg/addon/manager/manifests/templates/image_pull_secret.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# should not overwrite the pull secret if it is the default namespace
{{ if ne .AddonInstallNamespace "open-cluster-management-agent-addon" }}
{{ if .ImagePullSecretData }}
apiVersion: v1
kind: Secret
Expand All @@ -8,3 +10,4 @@ type: kubernetes.io/dockerconfigjson
data:
".dockerconfigjson": {{ .ImagePullSecretData }}
{{ end }}
{{ end }}
2 changes: 2 additions & 0 deletions pkg/addon/manager/manifests/templates/namespace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ apiVersion: v1
kind: Namespace
metadata:
name: {{ .AddonInstallNamespace }}
annotations:
addon.open-cluster-management.io/deletion-orphan: ""

0 comments on commit 36d315f

Please sign in to comment.