|
| 1 | +/* |
| 2 | +Copyright 2026 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 init |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + "path/filepath" |
| 23 | + "time" |
| 24 | + |
| 25 | + "github.com/karmada-io/karmada/test/e2e/framework" |
| 26 | + "github.com/karmada-io/karmada/test/e2e/framework/resource/cmdinit" |
| 27 | + "github.com/onsi/ginkgo/v2" |
| 28 | + "github.com/onsi/gomega" |
| 29 | +) |
| 30 | + |
| 31 | +const configTemplate = `apiVersion: config.karmada.io/v1alpha1 |
| 32 | +kind: KarmadaInitConfig |
| 33 | +spec: |
| 34 | + hostCluster: |
| 35 | + kubeconfig: "${KUBECONFIG_PATH}" |
| 36 | + etcd: |
| 37 | + local: |
| 38 | + dataPath: "${ETCD_DATA_PATH}" |
| 39 | + components: |
| 40 | + karmadaControllerManager: |
| 41 | + repository: "${REGISTRY}/karmada-controller-manager" |
| 42 | + tag: "${VERSION}" |
| 43 | + karmadaScheduler: |
| 44 | + repository: "${REGISTRY}/karmada-scheduler" |
| 45 | + tag: "${VERSION}" |
| 46 | + karmadaWebhook: |
| 47 | + repository: "${REGISTRY}/karmada-webhook" |
| 48 | + tag: "${VERSION}" |
| 49 | + karmadaAggregatedAPIServer: |
| 50 | + repository: "${REGISTRY}/karmada-aggregated-apiserver" |
| 51 | + tag: "${VERSION}" |
| 52 | + karmadaAPIServer: |
| 53 | + networking: |
| 54 | + port: ${KARMADA_APISERVER_NODE_PORT} |
| 55 | + karmadaDataPath: "${KARMADA_DATA_PATH}" |
| 56 | + karmadaPkiPath: "${KARMADA_PKI_PATH}" |
| 57 | + karmadaCrds: "${KARMADA_CRDS}"` |
| 58 | + |
| 59 | +// config variable value should be consistent with template placeholder name |
| 60 | +const ( |
| 61 | + configVarKubeconfigPath = "KUBECONFIG_PATH" |
| 62 | + configVarRegistry = "REGISTRY" |
| 63 | + configVarVersion = "VERSION" |
| 64 | + configVarKarmadaDataPath = "KARMADA_DATA_PATH" |
| 65 | + configVarKarmadaPkiPath = "KARMADA_PKI_PATH" |
| 66 | + configVarKarmadaCrds = "KARMADA_CRDS" |
| 67 | + configVarEtcdDataPath = "ETCD_DATA_PATH" |
| 68 | + configVarKarmadaAPIServerNodePort = "KARMADA_APISERVER_NODE_PORT" |
| 69 | +) |
| 70 | + |
| 71 | +const karmadactlInitTimeout = time.Second * 120 |
| 72 | + |
| 73 | +var _ = ginkgo.Describe("Custom E2E: initialize karmada control plane with config file", func() { |
| 74 | + var tempPki string |
| 75 | + var etcdDataPath string |
| 76 | + |
| 77 | + var configFilePath string |
| 78 | + var configVariables map[string]any |
| 79 | + |
| 80 | + ginkgo.Context("Karmadactl init with config file testing", func() { |
| 81 | + ginkgo.BeforeEach(func() { |
| 82 | + tempPki = filepath.Join(karmadaDataPath, "pki") |
| 83 | + etcdDataPath = filepath.Join(karmadaDataPath, "etcd-data") |
| 84 | + |
| 85 | + configFilePath = filepath.Join(karmadaDataPath, "karmada-config.yaml") |
| 86 | + }) |
| 87 | + }) |
| 88 | + |
| 89 | + ginkgo.It("Deploy karmada control plane with config file", func() { |
| 90 | + ginkgo.By("Generate karmada init config file with template", func() { |
| 91 | + configVariables = map[string]any{ |
| 92 | + configVarKubeconfigPath: kubeconfig, |
| 93 | + configVarRegistry: registry, |
| 94 | + configVarVersion: version, |
| 95 | + configVarKarmadaDataPath: karmadaDataPath, |
| 96 | + configVarKarmadaPkiPath: tempPki, |
| 97 | + configVarKarmadaCrds: crdsPath, |
| 98 | + configVarEtcdDataPath: etcdDataPath, |
| 99 | + configVarKarmadaAPIServerNodePort: karmadaAPIServerNodePort, |
| 100 | + } |
| 101 | + |
| 102 | + renderedContent := renderTemplateWithGenericMap(configTemplate, configVariables) |
| 103 | + err := os.WriteFile(configFilePath, []byte(renderedContent), 0600) |
| 104 | + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
| 105 | + }) |
| 106 | + |
| 107 | + ginkgo.By("Execute the command karmadactl init", func() { |
| 108 | + args := []string{"init", |
| 109 | + "--config", configFilePath, |
| 110 | + "--v", "4", |
| 111 | + } |
| 112 | + |
| 113 | + cmd := framework.NewKarmadactlCommand( |
| 114 | + "", // kubeconfig is not specified as it will use the kubeconfig specified in karmada config file |
| 115 | + "", |
| 116 | + karmadactlPath, |
| 117 | + testNamespace, |
| 118 | + karmadactlInitTimeout, |
| 119 | + args..., |
| 120 | + ) |
| 121 | + _, err := cmd.ExecOrDie() |
| 122 | + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
| 123 | + }) |
| 124 | + |
| 125 | + ginkgo.By("Waiting for components to be ready", func() { |
| 126 | + cmdinit.WaitAllKarmadaComponentReady(hostClient, testNamespace) |
| 127 | + }) |
| 128 | + }) |
| 129 | +}) |
| 130 | + |
| 131 | +func renderTemplateWithGenericMap(templateContent string, variables map[string]any) string { |
| 132 | + mapper := func(placeholderName string) string { |
| 133 | + val, ok := variables[placeholderName] |
| 134 | + if !ok { |
| 135 | + return "" |
| 136 | + } |
| 137 | + return fmt.Sprintf("%v", val) |
| 138 | + } |
| 139 | + |
| 140 | + return os.Expand(templateContent, mapper) |
| 141 | +} |
0 commit comments