|
| 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 | + pluginutil "sigs.k8s.io/kubebuilder/v4/pkg/plugin/util" |
| 23 | + |
| 24 | + . "github.com/onsi/ginkgo/v2" |
| 25 | + |
| 26 | + . "github.com/onsi/gomega" |
| 27 | + |
| 28 | + "sigs.k8s.io/kubebuilder/v4/test/e2e/utils" |
| 29 | +) |
| 30 | + |
| 31 | +var _ = Describe("kubebuilder", func() { |
| 32 | + Context("plugin helm/v1-alpha", func() { |
| 33 | + var kbc *utils.TestContext |
| 34 | + |
| 35 | + BeforeEach(func() { |
| 36 | + var err error |
| 37 | + kbc, err = utils.NewTestContext(pluginutil.KubebuilderBinName, "GO111MODULE=on") |
| 38 | + Expect(err).NotTo(HaveOccurred()) |
| 39 | + Expect(kbc.Prepare()).To(Succeed()) |
| 40 | + }) |
| 41 | + |
| 42 | + AfterEach(func() { |
| 43 | + kbc.Destroy() |
| 44 | + }) |
| 45 | + |
| 46 | + It("should extend a runnable project with helm plugin", func() { |
| 47 | + initTheProject(kbc) |
| 48 | + generateProject(kbc) |
| 49 | + }) |
| 50 | + |
| 51 | + It("should extend a runnable project with helm plugin and webhooks", func() { |
| 52 | + initTheProject(kbc) |
| 53 | + generateProject(kbc) |
| 54 | + extendProjectWithWebhooks(kbc) |
| 55 | + |
| 56 | + By("re-edit the project after creating webhooks") |
| 57 | + err := kbc.Edit( |
| 58 | + "--plugins", "helm.kubebuilder.io/v1-alpha", "--force", |
| 59 | + ) |
| 60 | + Expect(err).NotTo(HaveOccurred(), "Failed to edit the project") |
| 61 | + |
| 62 | + fileContainsExpr, err := pluginutil.HasFileContentWith( |
| 63 | + filepath.Join(kbc.Dir, "dist", "chart", "values.yaml"), |
| 64 | + `webhook: |
| 65 | + enable: true`) |
| 66 | + Expect(err).NotTo(HaveOccurred(), "Failed to read values.yaml file") |
| 67 | + Expect(fileContainsExpr).To(BeTrue(), "Failed to get enabled webhook value from values.yaml file") |
| 68 | + }) |
| 69 | + |
| 70 | + // Without --force, the webhooks should not be enabled in the values.yaml file. |
| 71 | + It("should extend a runnable project with helm plugin but not running with --force", func() { |
| 72 | + initTheProject(kbc) |
| 73 | + generateProject(kbc) |
| 74 | + extendProjectWithWebhooks(kbc) |
| 75 | + |
| 76 | + By("re-edit the project after creating webhooks without --force") |
| 77 | + err := kbc.Edit( |
| 78 | + "--plugins", "helm.kubebuilder.io/v1-alpha", |
| 79 | + ) |
| 80 | + Expect(err).NotTo(HaveOccurred(), "Failed to edit the project") |
| 81 | + |
| 82 | + fileContainsExpr, err := pluginutil.HasFileContentWith( |
| 83 | + filepath.Join(kbc.Dir, "dist", "chart", "values.yaml"), |
| 84 | + `webhook: |
| 85 | + enable: true`) |
| 86 | + Expect(err).NotTo(HaveOccurred(), "Failed to read values.yaml file") |
| 87 | + Expect(fileContainsExpr).To(BeFalse(), "Failed to get enabled webhook value from values.yaml file") |
| 88 | + }) |
| 89 | + |
| 90 | + }) |
| 91 | +}) |
| 92 | + |
| 93 | +// generateProject implements a helm/v1(-alpha) plugin project defined by a TestContext. |
| 94 | +func generateProject(kbc *utils.TestContext) { |
| 95 | + var err error |
| 96 | + |
| 97 | + By("editing a project") |
| 98 | + err = kbc.Edit( |
| 99 | + "--plugins", "helm.kubebuilder.io/v1-alpha", |
| 100 | + ) |
| 101 | + Expect(err).NotTo(HaveOccurred(), "Failed to edit the project") |
| 102 | + |
| 103 | + fileContainsExpr, err := pluginutil.HasFileContentWith( |
| 104 | + filepath.Join(kbc.Dir, "PROJECT"), |
| 105 | + `helm.kubebuilder.io/v1-alpha: {}`) |
| 106 | + Expect(err).NotTo(HaveOccurred(), "Failed to read PROJECT file") |
| 107 | + Expect(fileContainsExpr).To(BeTrue(), "Failed to find helm plugin in PROJECT file") |
| 108 | + |
| 109 | + fileContainsExpr, err = pluginutil.HasFileContentWith( |
| 110 | + filepath.Join(kbc.Dir, "PROJECT"), |
| 111 | + "projectName: e2e-"+kbc.TestSuffix) |
| 112 | + Expect(err).NotTo(HaveOccurred(), "Failed to read PROJECT file") |
| 113 | + Expect(fileContainsExpr).To(BeTrue(), "Failed to find projectName in PROJECT file") |
| 114 | + |
| 115 | + fileContainsExpr, err = pluginutil.HasFileContentWith( |
| 116 | + filepath.Join(kbc.Dir, "dist", "chart", "Chart.yaml"), |
| 117 | + "name: e2e-"+kbc.TestSuffix) |
| 118 | + Expect(err).NotTo(HaveOccurred(), "Failed to read Chart.yaml file") |
| 119 | + Expect(fileContainsExpr).To(BeTrue(), "Failed to find name in Chart.yaml file") |
| 120 | + |
| 121 | + fileContainsExpr, err = pluginutil.HasFileContentWith( |
| 122 | + filepath.Join(kbc.Dir, "dist", "chart", "templates", "manager", "manager.yaml"), |
| 123 | + `metadata: |
| 124 | + name: e2e-`+kbc.TestSuffix) |
| 125 | + Expect(err).NotTo(HaveOccurred(), "Failed to read manager.yaml file") |
| 126 | + Expect(fileContainsExpr).To(BeTrue(), "Failed to find name in helm template manager.yaml file") |
| 127 | +} |
| 128 | + |
| 129 | +// extendProjectWithWebhooks is creating API and scaffolding webhooks in the project |
| 130 | +func extendProjectWithWebhooks(kbc *utils.TestContext) { |
| 131 | + By("creating API definition") |
| 132 | + err := kbc.CreateAPI( |
| 133 | + "--group", kbc.Group, |
| 134 | + "--version", kbc.Version, |
| 135 | + "--kind", kbc.Kind, |
| 136 | + "--namespaced", |
| 137 | + "--resource", |
| 138 | + "--controller", |
| 139 | + "--make=false", |
| 140 | + ) |
| 141 | + Expect(err).NotTo(HaveOccurred(), "Failed to create API") |
| 142 | + |
| 143 | + By("scaffolding mutating and validating webhooks") |
| 144 | + err = kbc.CreateWebhook( |
| 145 | + "--group", kbc.Group, |
| 146 | + "--version", kbc.Version, |
| 147 | + "--kind", kbc.Kind, |
| 148 | + "--defaulting", |
| 149 | + "--programmatic-validation", |
| 150 | + "--make=false", |
| 151 | + ) |
| 152 | + Expect(err).NotTo(HaveOccurred(), "Failed to scaffolding mutating webhook") |
| 153 | + |
| 154 | + By("run make manifests") |
| 155 | + Expect(kbc.Make("manifests")).To(Succeed()) |
| 156 | +} |
| 157 | + |
| 158 | +// initTheProject initializes a project with the go/v4 plugin and sets the domain. |
| 159 | +func initTheProject(kbc *utils.TestContext) { |
| 160 | + By("initializing a project") |
| 161 | + err := kbc.Init( |
| 162 | + "--plugins", "go/v4", |
| 163 | + "--project-version", "3", |
| 164 | + "--domain", kbc.Domain, |
| 165 | + ) |
| 166 | + Expect(err).NotTo(HaveOccurred(), "Failed to initialize project") |
| 167 | +} |
0 commit comments