|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes 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 helm |
| 18 | + |
| 19 | +import ( |
| 20 | + "path/filepath" |
| 21 | + |
| 22 | + "github.com/spf13/afero" |
| 23 | + helmChartLoader "helm.sh/helm/v3/pkg/chart/loader" |
| 24 | + "sigs.k8s.io/kubebuilder/v4/pkg/config" |
| 25 | + "sigs.k8s.io/kubebuilder/v4/pkg/config/store/yaml" |
| 26 | + "sigs.k8s.io/kubebuilder/v4/pkg/machinery" |
| 27 | + pluginutil "sigs.k8s.io/kubebuilder/v4/pkg/plugin/util" |
| 28 | + helmv1alpha "sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/helm/v1alpha" |
| 29 | + "sigs.k8s.io/kubebuilder/v4/test/e2e/utils" |
| 30 | + |
| 31 | + . "github.com/onsi/ginkgo/v2" |
| 32 | + . "github.com/onsi/gomega" |
| 33 | +) |
| 34 | + |
| 35 | +var _ = Describe("kubebuilder", func() { |
| 36 | + Context("plugin helm/v1-alpha", func() { |
| 37 | + var kbc *utils.TestContext |
| 38 | + |
| 39 | + BeforeEach(func() { |
| 40 | + var err error |
| 41 | + kbc, err = utils.NewTestContext(pluginutil.KubebuilderBinName, "GO111MODULE=on") |
| 42 | + Expect(err).NotTo(HaveOccurred()) |
| 43 | + Expect(kbc.Prepare()).To(Succeed()) |
| 44 | + }) |
| 45 | + |
| 46 | + AfterEach(func() { |
| 47 | + kbc.Destroy() |
| 48 | + }) |
| 49 | + |
| 50 | + It("should extend an initialed project with helm plugin", func() { |
| 51 | + initTheProject(kbc) |
| 52 | + |
| 53 | + By("extend the project by adding helm plugin") |
| 54 | + err := kbc.Edit( |
| 55 | + "--plugins", "helm.kubebuilder.io/v1-alpha", |
| 56 | + ) |
| 57 | + Expect(err).NotTo(HaveOccurred(), "Failed to edit the project") |
| 58 | + |
| 59 | + ensureCommonHelmFilesContent(kbc, false) |
| 60 | + }) |
| 61 | + |
| 62 | + // This test is to ensure that the helm plugin can be added to a project |
| 63 | + // that has already been initialized with the go/v4 plugin. |
| 64 | + // As the project is getting extended with webhooks, |
| 65 | + // it is needed to run the `kubebuilder edit --plugins helm.kubebuilder.io/v1-alpha` command |
| 66 | + // with ` --force` again to ensure that the webhooks are enabled in the |
| 67 | + // values.yaml file. |
| 68 | + It("should extend an initialized project with helm plugin and webhooks", func() { |
| 69 | + initTheProject(kbc) |
| 70 | + |
| 71 | + By("extend the project by adding helm plugin") |
| 72 | + err := kbc.Edit( |
| 73 | + "--plugins", "helm.kubebuilder.io/v1-alpha", |
| 74 | + ) |
| 75 | + Expect(err).NotTo(HaveOccurred(), "Failed to edit the project") |
| 76 | + |
| 77 | + ensureCommonHelmFilesContent(kbc, false) |
| 78 | + extendProjectWithWebhooks(kbc) |
| 79 | + |
| 80 | + // after creating webhooks, we want to have the webhooks enabled |
| 81 | + // in the values.yaml file, so we need to run `kubebuilder edit` |
| 82 | + // with the --force flag for the helm plugin. |
| 83 | + By("re-edit the project after creating webhooks") |
| 84 | + err = kbc.Edit( |
| 85 | + "--plugins", "helm.kubebuilder.io/v1-alpha", "--force", |
| 86 | + ) |
| 87 | + Expect(err).NotTo(HaveOccurred(), "Failed to edit the project") |
| 88 | + |
| 89 | + ensureCommonHelmFilesContent(kbc, true) |
| 90 | + }) |
| 91 | + }) |
| 92 | +}) |
| 93 | + |
| 94 | +// ensureCommonHelmFilesContent tests common helm-chart files which got |
| 95 | +// generated by the helm/v1(-alpha) plugin |
| 96 | +func ensureCommonHelmFilesContent(kbc *utils.TestContext, webhookEnabled bool) { |
| 97 | + var helmConfig helmv1alpha.Plugin |
| 98 | + projectConfig := getConfigFromProjectFile(filepath.Join(kbc.Dir, "PROJECT")) |
| 99 | + |
| 100 | + By("decoding the helm plugin configuration") |
| 101 | + err := projectConfig.DecodePluginConfig("helm.kubebuilder.io/v1-alpha", &helmConfig) |
| 102 | + Expect(err).NotTo(HaveOccurred(), "Failed to decode Helm plugin configuration") |
| 103 | + |
| 104 | + // loading the generated helm chart |
| 105 | + chart, err := helmChartLoader.LoadDir(filepath.Join(kbc.Dir, "dist", "chart")) |
| 106 | + Expect(err).NotTo(HaveOccurred(), "Failed to load helm chart") |
| 107 | + |
| 108 | + // validating the helm chart metadata (Chart.yaml) |
| 109 | + err = chart.Validate() |
| 110 | + Expect(err).NotTo(HaveOccurred(), "Failed to validate helm chart") |
| 111 | + |
| 112 | + // expect the chart-name equal to the name of the PROJECT |
| 113 | + Expect(chart.Name()).To(Equal("e2e-"+kbc.TestSuffix), "Chart name doesn't match") |
| 114 | + |
| 115 | + // expecting the existence of a manager.yaml file |
| 116 | + var matchedFiles int |
| 117 | + for _, templateFile := range chart.Templates { |
| 118 | + switch templateFile.Name { |
| 119 | + case "templates/manager/manager.yaml": |
| 120 | + matchedFiles++ |
| 121 | + default: |
| 122 | + matchedFiles += 0 |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + Expect(matchedFiles).To(BeNumerically("==", 1)) |
| 127 | + |
| 128 | + // check if webhooks are enabled in the Chart.yaml |
| 129 | + if webhookEnabled { |
| 130 | + isEnabled := chart.Values["webhook"].(map[string]interface{})["enable"] |
| 131 | + Expect(isEnabled).To(Equal(webhookEnabled), "webhook isn't enabled in the Chart.yaml") |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +// extendProjectWithWebhooks is creating API and scaffolding webhooks in the project |
| 136 | +func extendProjectWithWebhooks(kbc *utils.TestContext) { |
| 137 | + By("creating API definition") |
| 138 | + err := kbc.CreateAPI( |
| 139 | + "--group", kbc.Group, |
| 140 | + "--version", kbc.Version, |
| 141 | + "--kind", kbc.Kind, |
| 142 | + "--namespaced", |
| 143 | + "--resource", |
| 144 | + "--controller", |
| 145 | + "--make=false", |
| 146 | + ) |
| 147 | + Expect(err).NotTo(HaveOccurred(), "Failed to create API") |
| 148 | + |
| 149 | + By("scaffolding mutating and validating webhooks") |
| 150 | + err = kbc.CreateWebhook( |
| 151 | + "--group", kbc.Group, |
| 152 | + "--version", kbc.Version, |
| 153 | + "--kind", kbc.Kind, |
| 154 | + "--defaulting", |
| 155 | + "--programmatic-validation", |
| 156 | + "--make=false", |
| 157 | + ) |
| 158 | + Expect(err).NotTo(HaveOccurred(), "Failed to scaffolding mutating webhook") |
| 159 | + |
| 160 | + By("run make manifests") |
| 161 | + Expect(kbc.Make("manifests")).To(Succeed()) |
| 162 | +} |
| 163 | + |
| 164 | +// initTheProject initializes a project with the go/v4 plugin and sets the domain. |
| 165 | +func initTheProject(kbc *utils.TestContext) { |
| 166 | + By("initializing a project") |
| 167 | + err := kbc.Init( |
| 168 | + "--plugins", "go/v4", |
| 169 | + "--project-version", "3", |
| 170 | + "--domain", kbc.Domain, |
| 171 | + ) |
| 172 | + Expect(err).NotTo(HaveOccurred(), "Failed to initialize project") |
| 173 | +} |
| 174 | + |
| 175 | +func getConfigFromProjectFile(projectFilePath string) config.Config { |
| 176 | + By("loading the PROJECT configuration") |
| 177 | + fs := afero.NewOsFs() |
| 178 | + store := yaml.New(machinery.Filesystem{FS: fs}) |
| 179 | + err := store.LoadFrom(projectFilePath) |
| 180 | + Expect(err).NotTo(HaveOccurred(), "Failed to load PROJECT configuration") |
| 181 | + |
| 182 | + cfg := store.Config() |
| 183 | + return cfg |
| 184 | +} |
0 commit comments