Skip to content

Commit

Permalink
Add an e2e test case
Browse files Browse the repository at this point in the history
Signed-off-by: Jian Qiu <jqiu@redhat.com>
  • Loading branch information
qiujian16 committed Apr 19, 2024
1 parent 9c987a0 commit b3e909c
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 24 deletions.
10 changes: 4 additions & 6 deletions pkg/addon/templateagent/decorator.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@ func (d *namespaceDecorator) decorate(obj *unstructured.Unstructured) (*unstruct
return obj, nil
}

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

path, ok := d.paths[obj.GetKind()]
if !ok {
Expand All @@ -72,7 +71,7 @@ func (d *namespaceDecorator) decorate(obj *unstructured.Unstructured) (*unstruct
}
}
case interface{}:
if err := setNamespaceForObject(obj, d.installNamespace); err != nil {
if err := setNamespaceForObject(f, d.installNamespace); err != nil {
return obj, err
}
}
Expand Down Expand Up @@ -145,7 +144,6 @@ func newEnvironmentDecorator(orderedValues orderedValues) podTemplateSpecDecorat
}
}
func (d *environmentDecorator) decorate(pod *corev1.PodTemplateSpec) error {
fmt.Printf("ordered value is %v\n", d.orderedValues)
envVars := make([]corev1.EnvVar, len(d.orderedValues))
for index, value := range d.orderedValues {
envVars[index] = corev1.EnvVar{
Expand Down
8 changes: 0 additions & 8 deletions pkg/addon/templateagent/decorator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,6 @@ func TestNamespaceDecorator(t *testing.T) {
testingcommon.AssertEqualNameNamespace(t, obj.GetName(), obj.GetNamespace(), "test", "default")
},
},
{
name: "cluster scoped",
object: testingcommon.NewUnstructured("v1", "ClusterRole", "", "test"),
namespace: "newns",
validateObject: func(t *testing.T, obj *unstructured.Unstructured) {
testingcommon.AssertEqualNameNamespace(t, obj.GetName(), obj.GetNamespace(), "test", "")
},
},
{
name: "namespace set",
object: testingcommon.NewUnstructured("v1", "Pod", "default", "test"),
Expand Down
88 changes: 78 additions & 10 deletions test/e2e/addonmanagement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
const (
nodePlacementDeploymentConfigName = "node-placement-deploy-config"
imageOverrideDeploymentConfigName = "image-override-deploy-config"
namespaceOverrideConfigName = "namespace-override-config"
originalImageValue = "quay.io/open-cluster-management/addon-examples:latest"
overrideImageValue = "quay.io/ocm/addon-examples:latest"
customSignerName = "example.com/signer-name"
Expand Down Expand Up @@ -476,6 +477,55 @@ var _ = ginkgo.Describe("Enable addon management feature gate", ginkgo.Ordered,

})

ginkgo.It("Template type addon should be configured by addon deployment config for namespace", func() {
ginkgo.By("Prepare a AddOnDeploymentConfig for namespace config")
overrideNamespace := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "another-addon-namespace",
},
}
_, err := t.SpokeKubeClient.CoreV1().Namespaces().Create(context.TODO(), overrideNamespace, metav1.CreateOptions{})
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Eventually(func() error {
return prepareInstallNamespace(clusterName, overrideNamespace.Name)
}, eventuallyTimeout, eventuallyInterval).ShouldNot(gomega.HaveOccurred())

ginkgo.By("Add the configs to ManagedClusterAddOn")
gomega.Eventually(func() error {
addon, err := t.AddOnClinet.AddonV1alpha1().ManagedClusterAddOns(clusterName).Get(
context.Background(), addOnName, metav1.GetOptions{})
if err != nil {
return err
}
newAddon := addon.DeepCopy()
newAddon.Spec.Configs = []addonapiv1alpha1.AddOnConfig{
{
ConfigGroupResource: addonapiv1alpha1.ConfigGroupResource{
Group: "addon.open-cluster-management.io",
Resource: "addondeploymentconfigs",
},
ConfigReferent: addonapiv1alpha1.ConfigReferent{
Namespace: clusterName,
Name: namespaceOverrideConfigName,
},
},
}
_, err = t.AddOnClinet.AddonV1alpha1().ManagedClusterAddOns(clusterName).Update(
context.Background(), newAddon, metav1.UpdateOptions{})
if err != nil {
return err
}
return nil
}, eventuallyTimeout, eventuallyInterval).ShouldNot(gomega.HaveOccurred())

ginkgo.By("Make sure addon is configured")
gomega.Eventually(func() error {
_, err := t.SpokeKubeClient.AppsV1().Deployments(overrideNamespace.Name).Get(
context.Background(), "hello-template-agent", metav1.GetOptions{})
return err
}, eventuallyTimeout, eventuallyInterval).ShouldNot(gomega.HaveOccurred())
})

ginkgo.It("Template type addon's image should be overrode by cluster annotation", func() {
ginkgo.By("Prepare cluster annotation for addon image override config")
overrideRegistries := addonapiv1alpha1.AddOnDeploymentConfigSpec{
Expand Down Expand Up @@ -564,6 +614,32 @@ var _ = ginkgo.Describe("Enable addon management feature gate", ginkgo.Ordered,

})

func prepareInstallNamespace(namespace, installNamespace string) error {
_, err := t.AddOnClinet.AddonV1alpha1().AddOnDeploymentConfigs(namespace).Get(
context.Background(), namespaceOverrideConfigName, metav1.GetOptions{})
if errors.IsNotFound(err) {
if _, err := t.AddOnClinet.AddonV1alpha1().AddOnDeploymentConfigs(namespace).Create(
context.Background(),
&addonapiv1alpha1.AddOnDeploymentConfig{
ObjectMeta: metav1.ObjectMeta{
Name: namespaceOverrideConfigName,
Namespace: namespace,
},
Spec: addonapiv1alpha1.AddOnDeploymentConfigSpec{
AgentInstallNamespace: installNamespace,
},
},
metav1.CreateOptions{},
); err != nil {
return err
}

return nil
}

return err
}

func prepareImageOverrideAddOnDeploymentConfig(namespace, installNamespace string) error {
_, err := t.AddOnClinet.AddonV1alpha1().AddOnDeploymentConfigs(namespace).Get(
context.Background(), imageOverrideDeploymentConfigName, metav1.GetOptions{})
Expand All @@ -588,11 +664,7 @@ func prepareImageOverrideAddOnDeploymentConfig(namespace, installNamespace strin
return nil
}

if err != nil {
return err
}

return nil
return err
}

func prepareNodePlacementAddOnDeploymentConfig(namespace, installNamespace string) error {
Expand Down Expand Up @@ -622,11 +694,7 @@ func prepareNodePlacementAddOnDeploymentConfig(namespace, installNamespace strin
return nil
}

if err != nil {
return err
}

return nil
return err
}

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

0 comments on commit b3e909c

Please sign in to comment.