Skip to content

Commit 8856ef6

Browse files
committed
Add an e2e test case
Signed-off-by: Jian Qiu <[email protected]>
1 parent 9c987a0 commit 8856ef6

File tree

5 files changed

+98
-41
lines changed

5 files changed

+98
-41
lines changed

pkg/addon/templateagent/decorator.go

+6-8
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ type namespaceDecorator struct {
2525
paths map[string]string
2626
}
2727

28-
func newNamespaceDecorator(configValues addonfactory.Values) *namespaceDecorator {
28+
func newNamespaceDecorator(privateValues addonfactory.Values) *namespaceDecorator {
2929
decorator := &namespaceDecorator{
3030
paths: map[string]string{
3131
"ClusterRoleBinding": "subjects",
3232
"RoleBinding": "subjects",
3333
},
3434
}
35-
namespace, ok := configValues["INSTALL_NAMESPACE"]
35+
namespace, ok := privateValues[InstallNamespacePrivateValueKey]
3636
if ok {
3737
decorator.installNamespace = namespace.(string)
3838
}
@@ -45,10 +45,9 @@ func (d *namespaceDecorator) decorate(obj *unstructured.Unstructured) (*unstruct
4545
return obj, nil
4646
}
4747

48-
// If obj has no namespace set, we do not mutate namespace assuming it is cluster scoped.
49-
if len(obj.GetNamespace()) > 0 {
50-
obj.SetNamespace(d.installNamespace)
51-
}
48+
// set namespace for all manifests, if the manifest is cluster scoped, namespace will be ignored when
49+
// being applied.
50+
obj.SetNamespace(d.installNamespace)
5251

5352
path, ok := d.paths[obj.GetKind()]
5453
if !ok {
@@ -72,7 +71,7 @@ func (d *namespaceDecorator) decorate(obj *unstructured.Unstructured) (*unstruct
7271
}
7372
}
7473
case interface{}:
75-
if err := setNamespaceForObject(obj, d.installNamespace); err != nil {
74+
if err := setNamespaceForObject(f, d.installNamespace); err != nil {
7675
return obj, err
7776
}
7877
}
@@ -145,7 +144,6 @@ func newEnvironmentDecorator(orderedValues orderedValues) podTemplateSpecDecorat
145144
}
146145
}
147146
func (d *environmentDecorator) decorate(pod *corev1.PodTemplateSpec) error {
148-
fmt.Printf("ordered value is %v\n", d.orderedValues)
149147
envVars := make([]corev1.EnvVar, len(d.orderedValues))
150148
for index, value := range d.orderedValues {
151149
envVars[index] = corev1.EnvVar{

pkg/addon/templateagent/decorator_test.go

+1-9
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,6 @@ func TestNamespaceDecorator(t *testing.T) {
2727
testingcommon.AssertEqualNameNamespace(t, obj.GetName(), obj.GetNamespace(), "test", "default")
2828
},
2929
},
30-
{
31-
name: "cluster scoped",
32-
object: testingcommon.NewUnstructured("v1", "ClusterRole", "", "test"),
33-
namespace: "newns",
34-
validateObject: func(t *testing.T, obj *unstructured.Unstructured) {
35-
testingcommon.AssertEqualNameNamespace(t, obj.GetName(), obj.GetNamespace(), "test", "")
36-
},
37-
},
3830
{
3931
name: "namespace set",
4032
object: testingcommon.NewUnstructured("v1", "Pod", "default", "test"),
@@ -127,7 +119,7 @@ func TestNamespaceDecorator(t *testing.T) {
127119
t.Run(tc.name, func(t *testing.T) {
128120
values := addonfactory.Values{}
129121
if len(tc.namespace) > 0 {
130-
values["INSTALL_NAMESPACE"] = tc.namespace
122+
values[InstallNamespacePrivateValueKey] = tc.namespace
131123
}
132124

133125
d := newNamespaceDecorator(values)

pkg/addon/templateagent/template_agent.go

+6-10
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ import (
2424
)
2525

2626
const (
27-
NodePlacementPrivateValueKey = "__NODE_PLACEMENT"
28-
RegistriesPrivateValueKey = "__REGISTRIES"
27+
NodePlacementPrivateValueKey = "__NODE_PLACEMENT"
28+
RegistriesPrivateValueKey = "__REGISTRIES"
29+
InstallNamespacePrivateValueKey = "__INSTALL_NAMESPACE"
2930
)
3031

3132
// templateBuiltinValues includes the built-in values for crd template agentAddon.
@@ -170,7 +171,7 @@ func (a *CRDTemplateAgentAddon) renderObjects(
170171
return objects, err
171172
}
172173

173-
object, err = a.decorateObject(template, object, presetValues, configValues, privateValues)
174+
object, err = a.decorateObject(template, object, presetValues, privateValues)
174175
if err != nil {
175176
return objects, err
176177
}
@@ -183,17 +184,12 @@ func (a *CRDTemplateAgentAddon) decorateObject(
183184
template *addonapiv1alpha1.AddOnTemplate,
184185
obj *unstructured.Unstructured,
185186
orderedValues orderedValues,
186-
configValues, privateValues addonfactory.Values) (*unstructured.Unstructured, error) {
187+
privateValues addonfactory.Values) (*unstructured.Unstructured, error) {
187188
decorators := []decorator{
188189
newDeploymentDecorator(a.addonName, template, orderedValues, privateValues),
189-
newNamespaceDecorator(configValues),
190+
newNamespaceDecorator(privateValues),
190191
}
191192

192-
a.logger.V(4).Info("decorator",
193-
"orderedValues", orderedValues,
194-
"configValues", configValues,
195-
"privateValues", privateValues)
196-
197193
var err error
198194
for _, decorator := range decorators {
199195
obj, err = decorator.decorate(obj)

pkg/addon/templateagent/values.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func ToAddOnInstallNamespacePrivateValues(config addonapiv1alpha1.AddOnDeploymen
4141
return nil, nil
4242
}
4343
return addonfactory.Values{
44-
"INSTALL_NAMESPACE": config.Spec.AgentInstallNamespace,
44+
InstallNamespacePrivateValueKey: config.Spec.AgentInstallNamespace,
4545
}, nil
4646
}
4747

@@ -69,8 +69,9 @@ func (a *CRDTemplateAgentAddon) getValues(
6969
overrideValues = addonfactory.MergeValues(overrideValues, defaultValues)
7070

7171
privateValuesKeys := map[string]struct{}{
72-
NodePlacementPrivateValueKey: {},
73-
RegistriesPrivateValueKey: {},
72+
NodePlacementPrivateValueKey: {},
73+
RegistriesPrivateValueKey: {},
74+
InstallNamespacePrivateValueKey: {},
7475
}
7576

7677
for i := 0; i < len(a.getValuesFuncs); i++ {
@@ -96,7 +97,9 @@ func (a *CRDTemplateAgentAddon) getValues(
9697
if err != nil {
9798
return presetValues, overrideValues, privateValues, nil
9899
}
99-
overrideValues = addonfactory.MergeValues(builtinValues, overrideValues)
100+
// builtinValues only contains CLUSTER_NAME, and it should override overrideValues if CLUSTER_NAME
101+
// is also set in the overrideValues, since CLUSTER_NAME should not be set externally.
102+
overrideValues = addonfactory.MergeValues(overrideValues, builtinValues)
100103

101104
for k, v := range overrideValues {
102105
_, ok := v.(string)

test/e2e/addonmanagement_test.go

+78-10
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
const (
2929
nodePlacementDeploymentConfigName = "node-placement-deploy-config"
3030
imageOverrideDeploymentConfigName = "image-override-deploy-config"
31+
namespaceOverrideConfigName = "namespace-override-config"
3132
originalImageValue = "quay.io/open-cluster-management/addon-examples:latest"
3233
overrideImageValue = "quay.io/ocm/addon-examples:latest"
3334
customSignerName = "example.com/signer-name"
@@ -476,6 +477,55 @@ var _ = ginkgo.Describe("Enable addon management feature gate", ginkgo.Ordered,
476477

477478
})
478479

480+
ginkgo.It("Template type addon should be configured by addon deployment config for namespace", func() {
481+
ginkgo.By("Prepare a AddOnDeploymentConfig for namespace config")
482+
overrideNamespace := &corev1.Namespace{
483+
ObjectMeta: metav1.ObjectMeta{
484+
Name: "another-addon-namespace",
485+
},
486+
}
487+
_, err := t.SpokeKubeClient.CoreV1().Namespaces().Create(context.TODO(), overrideNamespace, metav1.CreateOptions{})
488+
gomega.Expect(err).ToNot(gomega.HaveOccurred())
489+
gomega.Eventually(func() error {
490+
return prepareInstallNamespace(clusterName, overrideNamespace.Name)
491+
}, eventuallyTimeout, eventuallyInterval).ShouldNot(gomega.HaveOccurred())
492+
493+
ginkgo.By("Add the configs to ManagedClusterAddOn")
494+
gomega.Eventually(func() error {
495+
addon, err := t.AddOnClinet.AddonV1alpha1().ManagedClusterAddOns(clusterName).Get(
496+
context.Background(), addOnName, metav1.GetOptions{})
497+
if err != nil {
498+
return err
499+
}
500+
newAddon := addon.DeepCopy()
501+
newAddon.Spec.Configs = []addonapiv1alpha1.AddOnConfig{
502+
{
503+
ConfigGroupResource: addonapiv1alpha1.ConfigGroupResource{
504+
Group: "addon.open-cluster-management.io",
505+
Resource: "addondeploymentconfigs",
506+
},
507+
ConfigReferent: addonapiv1alpha1.ConfigReferent{
508+
Namespace: clusterName,
509+
Name: namespaceOverrideConfigName,
510+
},
511+
},
512+
}
513+
_, err = t.AddOnClinet.AddonV1alpha1().ManagedClusterAddOns(clusterName).Update(
514+
context.Background(), newAddon, metav1.UpdateOptions{})
515+
if err != nil {
516+
return err
517+
}
518+
return nil
519+
}, eventuallyTimeout, eventuallyInterval).ShouldNot(gomega.HaveOccurred())
520+
521+
ginkgo.By("Make sure addon is configured")
522+
gomega.Eventually(func() error {
523+
_, err := t.SpokeKubeClient.AppsV1().Deployments(overrideNamespace.Name).Get(
524+
context.Background(), "hello-template-agent", metav1.GetOptions{})
525+
return err
526+
}, eventuallyTimeout, eventuallyInterval).ShouldNot(gomega.HaveOccurred())
527+
})
528+
479529
ginkgo.It("Template type addon's image should be overrode by cluster annotation", func() {
480530
ginkgo.By("Prepare cluster annotation for addon image override config")
481531
overrideRegistries := addonapiv1alpha1.AddOnDeploymentConfigSpec{
@@ -564,6 +614,32 @@ var _ = ginkgo.Describe("Enable addon management feature gate", ginkgo.Ordered,
564614

565615
})
566616

617+
func prepareInstallNamespace(namespace, installNamespace string) error {
618+
_, err := t.AddOnClinet.AddonV1alpha1().AddOnDeploymentConfigs(namespace).Get(
619+
context.Background(), namespaceOverrideConfigName, metav1.GetOptions{})
620+
if errors.IsNotFound(err) {
621+
if _, err := t.AddOnClinet.AddonV1alpha1().AddOnDeploymentConfigs(namespace).Create(
622+
context.Background(),
623+
&addonapiv1alpha1.AddOnDeploymentConfig{
624+
ObjectMeta: metav1.ObjectMeta{
625+
Name: namespaceOverrideConfigName,
626+
Namespace: namespace,
627+
},
628+
Spec: addonapiv1alpha1.AddOnDeploymentConfigSpec{
629+
AgentInstallNamespace: installNamespace,
630+
},
631+
},
632+
metav1.CreateOptions{},
633+
); err != nil {
634+
return err
635+
}
636+
637+
return nil
638+
}
639+
640+
return err
641+
}
642+
567643
func prepareImageOverrideAddOnDeploymentConfig(namespace, installNamespace string) error {
568644
_, err := t.AddOnClinet.AddonV1alpha1().AddOnDeploymentConfigs(namespace).Get(
569645
context.Background(), imageOverrideDeploymentConfigName, metav1.GetOptions{})
@@ -588,11 +664,7 @@ func prepareImageOverrideAddOnDeploymentConfig(namespace, installNamespace strin
588664
return nil
589665
}
590666

591-
if err != nil {
592-
return err
593-
}
594-
595-
return nil
667+
return err
596668
}
597669

598670
func prepareNodePlacementAddOnDeploymentConfig(namespace, installNamespace string) error {
@@ -622,11 +694,7 @@ func prepareNodePlacementAddOnDeploymentConfig(namespace, installNamespace strin
622694
return nil
623695
}
624696

625-
if err != nil {
626-
return err
627-
}
628-
629-
return nil
697+
return err
630698
}
631699

632700
func copySignerSecret(ctx context.Context, kubeClient kubernetes.Interface, srcNs, srcName, dstNs, dstName string) error {

0 commit comments

Comments
 (0)