From 657a7dc82e4bd97c95a649cbf7ee82071a159af9 Mon Sep 17 00:00:00 2001 From: changzhen Date: Thu, 27 May 2021 11:05:24 +0800 Subject: [PATCH] add e2e of imageOverrider Signed-off-by: changzhen --- test/e2e/overridepolicy_test.go | 270 ++++++++++++++++++++++++++++++++ test/helper/overridepolicy.go | 84 ++++++++++ 2 files changed, 354 insertions(+) create mode 100644 test/e2e/overridepolicy_test.go create mode 100644 test/helper/overridepolicy.go diff --git a/test/e2e/overridepolicy_test.go b/test/e2e/overridepolicy_test.go new file mode 100644 index 000000000000..32d43a22f0e9 --- /dev/null +++ b/test/e2e/overridepolicy_test.go @@ -0,0 +1,270 @@ +package e2e + +import ( + "context" + "fmt" + + "github.com/onsi/ginkgo" + "github.com/onsi/gomega" + appsv1 "k8s.io/api/apps/v1" + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/errors" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/util/rand" + "k8s.io/apimachinery/pkg/util/wait" + "k8s.io/klog/v2" + + "github.com/karmada-io/karmada/test/helper" +) + +var _ = ginkgo.Describe("[OverridePolicy] apply overriders testing", func() { + ginkgo.Context("Deployment imageOverride with empty predicate test", func() { + deploymentNamespace := testNamespace + deploymentName := deploymentNamePrefix + rand.String(RandomStrLength) + propagationPolicyNamespace := testNamespace + propagationPolicyName := deploymentName + overridePolicyNamespace := testNamespace + overridePolicyName := deploymentName + + deployment := helper.NewDeployment(deploymentNamespace, deploymentName) + propagationPolicy := helper.NewPolicyWithSingleDeployment(propagationPolicyNamespace, propagationPolicyName, deployment, clusterNames) + overridePolicy := helper.NewOverridePolicyWithDeployment(overridePolicyNamespace, overridePolicyName, deployment, clusterNames, helper.NewImageOverriderWithEmptyPredicate()) + + ginkgo.BeforeEach(func() { + ginkgo.By(fmt.Sprintf("creating propagationPolicy(%s/%s)", propagationPolicyNamespace, propagationPolicyName), func() { + _, err := karmadaClient.PolicyV1alpha1().PropagationPolicies(propagationPolicyNamespace).Create(context.TODO(), propagationPolicy, metav1.CreateOptions{}) + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + }) + }) + + ginkgo.BeforeEach(func() { + ginkgo.By(fmt.Sprintf("creating overridePolicy(%s/%s)", overridePolicyNamespace, overridePolicyName), func() { + _, err := karmadaClient.PolicyV1alpha1().OverridePolicies(overridePolicyNamespace).Create(context.TODO(), overridePolicy, metav1.CreateOptions{}) + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + }) + }) + + ginkgo.AfterEach(func() { + ginkgo.By(fmt.Sprintf("removing overridePolicy(%s/%s)", overridePolicyNamespace, overridePolicyName), func() { + err := karmadaClient.PolicyV1alpha1().OverridePolicies(overridePolicyNamespace).Delete(context.TODO(), overridePolicyName, metav1.DeleteOptions{}) + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + }) + }) + + ginkgo.AfterEach(func() { + ginkgo.By(fmt.Sprintf("removing propagationPolicy(%s/%s)", propagationPolicyNamespace, propagationPolicyName), func() { + err := karmadaClient.PolicyV1alpha1().PropagationPolicies(propagationPolicyNamespace).Delete(context.TODO(), propagationPolicyName, metav1.DeleteOptions{}) + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + }) + }) + + ginkgo.It("deployment imageOverride testing", func() { + ginkgo.By(fmt.Sprintf("creating deployment(%s/%s)", deploymentNamespace, deploymentName), func() { + _, err := kubeClient.AppsV1().Deployments(testNamespace).Create(context.TODO(), deployment, metav1.CreateOptions{}) + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + }) + + ginkgo.By("check if deployment present on member clusters have correct image value", func() { + for _, cluster := range clusters { + clusterClient := getClusterClient(cluster.Name) + gomega.Expect(clusterClient).ShouldNot(gomega.BeNil()) + + var deploymentInCluster *appsv1.Deployment + + klog.Infof("Waiting for deployment(%s/%s) present on cluster(%s)", deploymentNamespace, deploymentName, cluster.Name) + err := wait.Poll(pollInterval, pollTimeout, func() (done bool, err error) { + deploymentInCluster, err = clusterClient.AppsV1().Deployments(deploymentNamespace).Get(context.TODO(), deploymentName, metav1.GetOptions{}) + if err != nil { + if errors.IsNotFound(err) { + return false, nil + } + return false, err + } + return true, nil + }) + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + + for _, container := range deploymentInCluster.Spec.Template.Spec.Containers { + if container.Image != "fictional.registry.us/busybox:1.0" { + err = fmt.Errorf("deployment in cluster(%s) has not expect image value, actual: %s", cluster.Name, container.Image) + break + } + } + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + } + }) + + ginkgo.By(fmt.Sprintf("removing deployment(%s/%s)", deploymentNamespace, deploymentName), func() { + err := kubeClient.AppsV1().Deployments(testNamespace).Delete(context.TODO(), deploymentName, metav1.DeleteOptions{}) + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + }) + }) + + }) + + ginkgo.Context("Pod imageOverride test", func() { + podNamespace := testNamespace + podName := podNamePrefix + rand.String(RandomStrLength) + propagationPolicyNamespace := testNamespace + propagationPolicyName := podName + overridePolicyNamespace := testNamespace + overridePolicyName := podName + + pod := helper.NewPod(podNamespace, podName) + propagationPolicy := helper.NewPolicyWithSinglePod(propagationPolicyNamespace, propagationPolicyName, pod, clusterNames) + overridePolicy := helper.NewOverridePolicyWithPod(overridePolicyNamespace, overridePolicyName, pod, clusterNames, helper.NewImageOverriderWithEmptyPredicate()) + + ginkgo.BeforeEach(func() { + ginkgo.By(fmt.Sprintf("creating propagationPolicy(%s/%s)", propagationPolicyNamespace, propagationPolicyName), func() { + _, err := karmadaClient.PolicyV1alpha1().PropagationPolicies(propagationPolicyNamespace).Create(context.TODO(), propagationPolicy, metav1.CreateOptions{}) + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + }) + }) + + ginkgo.BeforeEach(func() { + ginkgo.By(fmt.Sprintf("creating overridePolicy(%s/%s)", overridePolicyNamespace, overridePolicyName), func() { + _, err := karmadaClient.PolicyV1alpha1().OverridePolicies(overridePolicyNamespace).Create(context.TODO(), overridePolicy, metav1.CreateOptions{}) + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + }) + }) + + ginkgo.AfterEach(func() { + ginkgo.By(fmt.Sprintf("removing overridePolicy(%s/%s)", overridePolicyNamespace, overridePolicyName), func() { + err := karmadaClient.PolicyV1alpha1().OverridePolicies(overridePolicyNamespace).Delete(context.TODO(), overridePolicyName, metav1.DeleteOptions{}) + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + }) + }) + + ginkgo.AfterEach(func() { + ginkgo.By(fmt.Sprintf("removing propagationPolicy(%s/%s)", propagationPolicyNamespace, propagationPolicyName), func() { + err := karmadaClient.PolicyV1alpha1().PropagationPolicies(propagationPolicyNamespace).Delete(context.TODO(), propagationPolicyName, metav1.DeleteOptions{}) + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + }) + }) + + ginkgo.It("pod imageOverride testing", func() { + ginkgo.By(fmt.Sprintf("creating pod(%s/%s)", podNamespace, podName), func() { + _, err := kubeClient.CoreV1().Pods(testNamespace).Create(context.TODO(), pod, metav1.CreateOptions{}) + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + }) + + ginkgo.By("check if pod present on member clusters have correct image value", func() { + for _, cluster := range clusters { + clusterClient := getClusterClient(cluster.Name) + gomega.Expect(clusterClient).ShouldNot(gomega.BeNil()) + + var podInClusters *corev1.Pod + + klog.Infof("Waiting for pod(%s/%s) present on cluster(%s)", podNamespace, podName, cluster.Name) + err := wait.Poll(pollInterval, pollTimeout, func() (done bool, err error) { + podInClusters, err = clusterClient.CoreV1().Pods(podNamespace).Get(context.TODO(), podName, metav1.GetOptions{}) + + if err != nil { + if errors.IsNotFound(err) { + return false, nil + } + return false, err + } + return true, nil + }) + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + + for _, container := range podInClusters.Spec.Containers { + if container.Image != "fictional.registry.us/busybox:1.0" { + err = fmt.Errorf("pod in cluster(%s) has not expect image value, actual: %s", cluster.Name, container.Image) + break + } + } + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + } + }) + + ginkgo.By(fmt.Sprintf("removing pod(%s/%s)", podNamespace, podName), func() { + err := kubeClient.CoreV1().Pods(testNamespace).Delete(context.TODO(), podName, metav1.DeleteOptions{}) + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + }) + }) + + }) + + ginkgo.Context("Deployment imageOverride with predicate test", func() { + deploymentNamespace := testNamespace + deploymentName := deploymentNamePrefix + rand.String(RandomStrLength) + propagationPolicyNamespace := testNamespace + propagationPolicyName := deploymentName + overridePolicyNamespace := testNamespace + overridePolicyName := deploymentName + + deployment := helper.NewDeployment(deploymentNamespace, deploymentName) + propagationPolicy := helper.NewPolicyWithSingleDeployment(propagationPolicyNamespace, propagationPolicyName, deployment, clusterNames) + overridePolicy := helper.NewOverridePolicyWithDeployment(overridePolicyNamespace, overridePolicyName, deployment, clusterNames, helper.NewImageOverriderWithPredicate()) + + ginkgo.BeforeEach(func() { + ginkgo.By(fmt.Sprintf("creating propagationPolicy(%s/%s)", propagationPolicyNamespace, propagationPolicyName), func() { + _, err := karmadaClient.PolicyV1alpha1().PropagationPolicies(propagationPolicyNamespace).Create(context.TODO(), propagationPolicy, metav1.CreateOptions{}) + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + }) + }) + + ginkgo.BeforeEach(func() { + ginkgo.By(fmt.Sprintf("creating overridePolicy(%s/%s)", overridePolicyNamespace, overridePolicyName), func() { + _, err := karmadaClient.PolicyV1alpha1().OverridePolicies(overridePolicyNamespace).Create(context.TODO(), overridePolicy, metav1.CreateOptions{}) + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + }) + }) + + ginkgo.AfterEach(func() { + ginkgo.By(fmt.Sprintf("removing overridePolicy(%s/%s)", overridePolicyNamespace, overridePolicyName), func() { + err := karmadaClient.PolicyV1alpha1().OverridePolicies(overridePolicyNamespace).Delete(context.TODO(), overridePolicyName, metav1.DeleteOptions{}) + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + }) + }) + + ginkgo.AfterEach(func() { + ginkgo.By(fmt.Sprintf("removing propagationPolicy(%s/%s)", propagationPolicyNamespace, propagationPolicyName), func() { + err := karmadaClient.PolicyV1alpha1().PropagationPolicies(propagationPolicyNamespace).Delete(context.TODO(), propagationPolicyName, metav1.DeleteOptions{}) + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + }) + }) + + ginkgo.It("deployment imageOverride testing", func() { + ginkgo.By(fmt.Sprintf("creating deployment(%s/%s)", deploymentNamespace, deploymentName), func() { + _, err := kubeClient.AppsV1().Deployments(testNamespace).Create(context.TODO(), deployment, metav1.CreateOptions{}) + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + }) + + ginkgo.By("check if deployment present on member clusters have correct image value", func() { + for _, cluster := range clusters { + clusterClient := getClusterClient(cluster.Name) + gomega.Expect(clusterClient).ShouldNot(gomega.BeNil()) + + var deploymentInCluster *appsv1.Deployment + + klog.Infof("Waiting for deployment(%s/%s) present on cluster(%s)", deploymentNamespace, deploymentName, cluster.Name) + err := wait.Poll(pollInterval, pollTimeout, func() (done bool, err error) { + deploymentInCluster, err = clusterClient.AppsV1().Deployments(deploymentNamespace).Get(context.TODO(), deploymentName, metav1.GetOptions{}) + if err != nil { + if errors.IsNotFound(err) { + return false, nil + } + return false, err + } + return true, nil + }) + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + + if deploymentInCluster.Spec.Template.Spec.Containers[0].Image != "fictional.registry.us/nginx:1.19.0" { + err = fmt.Errorf("deployment in cluster(%s) has not expect image value, actual: %s", cluster.Name, deploymentInCluster.Spec.Template.Spec.Containers[0].Image) + } + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + } + }) + + ginkgo.By(fmt.Sprintf("removing deployment(%s/%s)", deploymentNamespace, deploymentName), func() { + err := kubeClient.AppsV1().Deployments(testNamespace).Delete(context.TODO(), deploymentName, metav1.DeleteOptions{}) + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + }) + }) + + }) +}) diff --git a/test/helper/overridepolicy.go b/test/helper/overridepolicy.go new file mode 100644 index 000000000000..c2dc8e21af0a --- /dev/null +++ b/test/helper/overridepolicy.go @@ -0,0 +1,84 @@ +package helper + +import ( + appsv1 "k8s.io/api/apps/v1" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1" +) + +// NewOverridePolicyWithDeployment will build a OverridePolicy object that select with Deployment resource. +func NewOverridePolicyWithDeployment(namespace, name string, deployment *appsv1.Deployment, clusters []string, overriders policyv1alpha1.Overriders) *policyv1alpha1.OverridePolicy { + return newOverridePolicy(namespace, name, deployment.APIVersion, deployment.Kind, deployment.Name, clusters, overriders) +} + +// NewOverridePolicyWithPod will build a OverridePolicy object that select with Pod resource. +func NewOverridePolicyWithPod(namespace, name string, pod *corev1.Pod, clusters []string, overriders policyv1alpha1.Overriders) *policyv1alpha1.OverridePolicy { + return newOverridePolicy(namespace, name, pod.APIVersion, pod.Kind, pod.Name, clusters, overriders) +} + +func newOverridePolicy(namespace, policyName, apiVersion, kind, resourceName string, clusters []string, overriders policyv1alpha1.Overriders) *policyv1alpha1.OverridePolicy { + return &policyv1alpha1.OverridePolicy{ + TypeMeta: metav1.TypeMeta{ + APIVersion: "policy.karmada.io/v1alpha1", + Kind: "OverridePolicy", + }, + ObjectMeta: metav1.ObjectMeta{ + Namespace: namespace, + Name: policyName, + }, + Spec: policyv1alpha1.OverrideSpec{ + ResourceSelectors: []policyv1alpha1.ResourceSelector{ + { + APIVersion: apiVersion, + Kind: kind, + Name: resourceName, + }, + }, + TargetCluster: &policyv1alpha1.ClusterAffinity{ + ClusterNames: clusters, + }, + Overriders: overriders, + }, + } +} + +// NewImageOverriderWithEmptyPredicate will build a Overriders object with empty predicate. +func NewImageOverriderWithEmptyPredicate() policyv1alpha1.Overriders { + return policyv1alpha1.Overriders{ + ImageOverrider: []policyv1alpha1.ImageOverrider{ + { + Component: "Registry", + Operator: "replace", + Value: "fictional.registry.us", + }, + { + Component: "Repository", + Operator: "replace", + Value: "busybox", + }, + { + Component: "Tag", + Operator: "replace", + Value: "1.0", + }, + }, + } +} + +// NewImageOverriderWithPredicate will build a Overriders object with predicate. +func NewImageOverriderWithPredicate() policyv1alpha1.Overriders { + return policyv1alpha1.Overriders{ + ImageOverrider: []policyv1alpha1.ImageOverrider{ + { + Predicate: &policyv1alpha1.ImagePredicate{ + Path: "/spec/template/spec/containers/0/image", + }, + Component: "Registry", + Operator: "replace", + Value: "fictional.registry.us", + }, + }, + } +}