|
| 1 | +/* |
| 2 | +Copyright 2025 The Karmada Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package e2e |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + "time" |
| 24 | + |
| 25 | + "github.com/onsi/ginkgo/v2" |
| 26 | + "github.com/onsi/gomega" |
| 27 | + appsv1 "k8s.io/api/apps/v1" |
| 28 | + corev1 "k8s.io/api/core/v1" |
| 29 | + "k8s.io/apimachinery/pkg/api/meta" |
| 30 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 31 | + "k8s.io/apimachinery/pkg/util/rand" |
| 32 | + "k8s.io/client-go/kubernetes" |
| 33 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 34 | + |
| 35 | + operatorv1alpha1 "github.com/karmada-io/karmada/operator/pkg/apis/operator/v1alpha1" |
| 36 | + "github.com/karmada-io/karmada/operator/pkg/constants" |
| 37 | + operatorutil "github.com/karmada-io/karmada/operator/pkg/util" |
| 38 | + clusterv1alpha1 "github.com/karmada-io/karmada/pkg/apis/cluster/v1alpha1" |
| 39 | + policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1" |
| 40 | + karmada "github.com/karmada-io/karmada/pkg/generated/clientset/versioned" |
| 41 | + "github.com/karmada-io/karmada/pkg/util/gclient" |
| 42 | + "github.com/karmada-io/karmada/test/e2e/framework" |
| 43 | + testhelper "github.com/karmada-io/karmada/test/helper" |
| 44 | +) |
| 45 | + |
| 46 | +const karmadactlTimeout = time.Second * 60 |
| 47 | + |
| 48 | +var karmadaContext = fmt.Sprintf("%s@%s", constants.UserName, constants.ClusterName) |
| 49 | + |
| 50 | +var _ = ginkgo.Describe("Base E2E: deploy a karmada instance and do a propagation testing", func() { |
| 51 | + var karmadaName string |
| 52 | + var controlPlaneConfig client.Client |
| 53 | + var kubeClient kubernetes.Interface |
| 54 | + var karmadaClient karmada.Interface |
| 55 | + var karmadaConfigFilePath string |
| 56 | + |
| 57 | + var pushModeClusterName string |
| 58 | + var pushModeKubeConfigPath string |
| 59 | + var pushModeClusterClient kubernetes.Interface |
| 60 | + var targetClusters []string |
| 61 | + |
| 62 | + var deploymentNamespace string |
| 63 | + var deploymentName string |
| 64 | + var policyName string |
| 65 | + |
| 66 | + ginkgo.Context("Karmada Operator base testing", func() { |
| 67 | + ginkgo.BeforeEach(func() { |
| 68 | + karmadaName = KarmadaInstanceNamePrefix + rand.String(RandomStrLength) |
| 69 | + InitializeKarmadaInstance(operatorClient, testNamespace, karmadaName, func(karmada *operatorv1alpha1.Karmada) { |
| 70 | + karmada.Spec.Components.KarmadaAPIServer.ServiceType = corev1.ServiceTypeNodePort |
| 71 | + }) |
| 72 | + }) |
| 73 | + |
| 74 | + ginkgo.BeforeEach(func() { |
| 75 | + homeDir := os.Getenv("HOME") |
| 76 | + karmadaConfigFilePath = fmt.Sprintf("%s/.kube/karmada-%s.config", homeDir, karmadaName) |
| 77 | + secret, err := hostClient.CoreV1().Secrets(testNamespace).Get(context.Background(), operatorutil.AdminKarmadaConfigSecretName(karmadaName), metav1.GetOptions{}) |
| 78 | + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
| 79 | + karmadaConfigBytes := secret.Data["karmada.config"] |
| 80 | + err = writeKubeConfigToFile(karmadaConfigBytes, karmadaConfigFilePath) |
| 81 | + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
| 82 | + karmadaRestConfig, err := framework.LoadRESTClientConfig(karmadaConfigFilePath, karmadaContext) |
| 83 | + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
| 84 | + controlPlaneConfig = gclient.NewForConfigOrDie(karmadaRestConfig) |
| 85 | + kubeClient = kubernetes.NewForConfigOrDie(karmadaRestConfig) |
| 86 | + karmadaClient = karmada.NewForConfigOrDie(karmadaRestConfig) |
| 87 | + |
| 88 | + pushModeClusterName = os.Getenv("PUSH_MODE_CLUSTER_NAME") |
| 89 | + pushModeKubeConfigPath = os.Getenv("PUSH_MODE_KUBECONFIG_PATH") |
| 90 | + pushModeClusterRestConfig, err := framework.LoadRESTClientConfig(pushModeKubeConfigPath, pushModeClusterName) |
| 91 | + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
| 92 | + pushModeClusterClient = kubernetes.NewForConfigOrDie(pushModeClusterRestConfig) |
| 93 | + |
| 94 | + targetClusters = []string{pushModeClusterName} |
| 95 | + }) |
| 96 | + |
| 97 | + ginkgo.AfterEach(func() { |
| 98 | + // Clean up resources |
| 99 | + framework.RemoveDeployment(kubeClient, deploymentNamespace, deploymentName) |
| 100 | + framework.RemovePropagationPolicy(karmadaClient, deploymentNamespace, policyName) |
| 101 | + framework.RemoveNamespace(kubeClient, deploymentNamespace) |
| 102 | + |
| 103 | + // Unjoin the push mode cluster |
| 104 | + cmd := framework.NewKarmadactlCommand(karmadaConfigFilePath, karmadaContext, karmadactlPath, "", karmadactlTimeout, |
| 105 | + "unjoin", "--cluster-kubeconfig", pushModeKubeConfigPath, "--cluster-context", pushModeClusterName, "--cluster-namespace", "karmada-cluster", |
| 106 | + "--v", "4", pushModeClusterName) |
| 107 | + _, err := cmd.ExecOrDie() |
| 108 | + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
| 109 | + |
| 110 | + // Delete the karmada instance |
| 111 | + err = operatorClient.OperatorV1alpha1().Karmadas(testNamespace).Delete(context.Background(), karmadaName, metav1.DeleteOptions{}) |
| 112 | + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
| 113 | + os.Remove(karmadaConfigFilePath) |
| 114 | + }) |
| 115 | + |
| 116 | + ginkgo.It("Deploy a karmada instance and do a propagation testing", func() { |
| 117 | + ginkgo.By("join a push mode cluster", func() { |
| 118 | + cmd := framework.NewKarmadactlCommand(karmadaConfigFilePath, karmadaContext, karmadactlPath, "", karmadactlTimeout, "join", |
| 119 | + "--cluster-kubeconfig", pushModeKubeConfigPath, "--cluster-context", pushModeClusterName, "--cluster-namespace", "karmada-cluster", |
| 120 | + "--v", "4", pushModeClusterName) |
| 121 | + _, err := cmd.ExecOrDie() |
| 122 | + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
| 123 | + }) |
| 124 | + |
| 125 | + ginkgo.By("Wait for the new cluster to be ready", func() { |
| 126 | + framework.WaitClusterFitWith(controlPlaneConfig, pushModeClusterName, func(cluster *clusterv1alpha1.Cluster) bool { |
| 127 | + return meta.IsStatusConditionPresentAndEqual(cluster.Status.Conditions, clusterv1alpha1.ClusterConditionReady, metav1.ConditionTrue) |
| 128 | + }) |
| 129 | + }) |
| 130 | + |
| 131 | + ginkgo.By("Do a simple propagation testing", func() { |
| 132 | + deploymentNamespace = "deployment-" + rand.String(RandomStrLength) |
| 133 | + framework.CreateNamespace(kubeClient, testhelper.NewNamespace(deploymentNamespace)) |
| 134 | + |
| 135 | + policyName = "deployment" + rand.String(RandomStrLength) |
| 136 | + deploymentName = policyName |
| 137 | + |
| 138 | + deployment := testhelper.NewDeployment(deploymentNamespace, deploymentName) |
| 139 | + policy := testhelper.NewPropagationPolicy(deploymentNamespace, policyName, []policyv1alpha1.ResourceSelector{ |
| 140 | + { |
| 141 | + APIVersion: deployment.APIVersion, |
| 142 | + Kind: deployment.Kind, |
| 143 | + Name: deployment.Name, |
| 144 | + }, |
| 145 | + }, policyv1alpha1.Placement{ |
| 146 | + ClusterAffinity: &policyv1alpha1.ClusterAffinity{ |
| 147 | + ClusterNames: targetClusters, |
| 148 | + }, |
| 149 | + }) |
| 150 | + |
| 151 | + framework.CreateDeployment(kubeClient, deployment) |
| 152 | + framework.CreatePropagationPolicy(karmadaClient, policy) |
| 153 | + framework.WaitDeploymentFitWith(pushModeClusterClient, deployment.Namespace, deployment.Name, |
| 154 | + func(*appsv1.Deployment) bool { |
| 155 | + return true |
| 156 | + }) |
| 157 | + }) |
| 158 | + }) |
| 159 | + }) |
| 160 | +}) |
| 161 | + |
| 162 | +func writeKubeConfigToFile(config []byte, filePath string) error { |
| 163 | + // Write the config to the specified file path |
| 164 | + err := os.WriteFile(filePath, config, 0600) |
| 165 | + if err != nil { |
| 166 | + return fmt.Errorf("failed to write kubeconfig to file: %v", err) |
| 167 | + } |
| 168 | + return nil |
| 169 | +} |
0 commit comments