Skip to content

Commit

Permalink
Merge pull request #4121 from camilamacedo86/create-maker-scaffold-we…
Browse files Browse the repository at this point in the history
…bhook-checks-e2e

✨ (go/v4) Add scaffold for e2e webhook checks
  • Loading branch information
k8s-ci-robot authored Sep 7, 2024
2 parents defede3 + b13bb6e commit 8f9e72c
Show file tree
Hide file tree
Showing 10 changed files with 306 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,46 @@ var _ = Describe("Manager", Ordered, func() {
))
})

It("should provisioned cert-manager", func() {
By("validating that cert-manager has the certificate Secret")
verifyCertManager := func(g Gomega) {
cmd := exec.Command("kubectl", "get", "secrets", "webhook-server-cert", "-n", namespace)
_, err := utils.Run(cmd)
g.Expect(err).NotTo(HaveOccurred())
}
Eventually(verifyCertManager).Should(Succeed())
})

It("should have CA injection for mutating webhooks", func() {
By("checking CA injection for mutating webhooks")
verifyCAInjection := func(g Gomega) {
cmd := exec.Command("kubectl", "get",
"mutatingwebhookconfigurations.admissionregistration.k8s.io",
"project-mutating-webhook-configuration",
"-o", "go-template={{ range .webhooks }}{{ .clientConfig.caBundle }}{{ end }}")
mwhOutput, err := utils.Run(cmd)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(len(mwhOutput)).To(BeNumerically(">", 10))
}
Eventually(verifyCAInjection).Should(Succeed())
})

It("should have CA injection for validating webhooks", func() {
By("checking CA injection for validating webhooks")
verifyCAInjection := func(g Gomega) {
cmd := exec.Command("kubectl", "get",
"validatingwebhookconfigurations.admissionregistration.k8s.io",
"project-validating-webhook-configuration",
"-o", "go-template={{ range .webhooks }}{{ .clientConfig.caBundle }}{{ end }}")
vwhOutput, err := utils.Run(cmd)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(len(vwhOutput)).To(BeNumerically(">", 10))
}
Eventually(verifyCAInjection).Should(Succeed())
})

// +kubebuilder:scaffold:e2e-webhooks-checks

// TODO: Customize the e2e test suite with scenarios specific to your project.
// Consider applying sample/CR(s) and check their status and/or verifying
// the reconciliation by using the metrics, i.e.:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ var _ = Describe("Manager", Ordered, func() {
))
})

// +kubebuilder:scaffold:e2e-webhooks-checks

// TODO: Customize the e2e test suite with scenarios specific to your project.
// Consider applying sample/CR(s) and check their status and/or verifying
// the reconciliation by using the metrics, i.e.:
Expand Down
1 change: 1 addition & 0 deletions pkg/plugins/golang/v4/scaffolds/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ func (s *initScaffolder) Scaffold() error {
&templates.Readme{},
&templates.Golangci{},
&e2e.Test{},
&e2e.WebhookTestUpdater{WireWebhook: false},
&e2e.SuiteTest{},
&utils.Utils{},
&templates.DevContainer{},
Expand Down
115 changes: 113 additions & 2 deletions pkg/plugins/golang/v4/scaffolds/internal/templates/test/e2e/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,18 @@ limitations under the License.
package e2e

import (
"fmt"
"path/filepath"

"sigs.k8s.io/kubebuilder/v4/pkg/machinery"
)

var _ machinery.Template = &SuiteTest{}
var _ machinery.Template = &Test{}
var _ machinery.Inserter = &WebhookTestUpdater{}

const webhookChecksMarker = "e2e-webhooks-checks"

// Test defines the basic setup for the e2e test
type Test struct {
machinery.TemplateMixin
machinery.BoilerplateMixin
Expand All @@ -31,13 +38,115 @@ type Test struct {

func (f *Test) SetTemplateDefaults() error {
if f.Path == "" {
f.Path = "test/e2e/e2e_test.go"
f.Path = filepath.Join("test", "e2e", "e2e_test.go")
}

// This is where the template body is defined with markers
f.TemplateBody = TestTemplate

return nil
}

// WebhookTestUpdater updates e2e_test.go to insert additional webhook validation tests
type WebhookTestUpdater struct {
machinery.RepositoryMixin
machinery.ProjectNameMixin
machinery.ResourceMixin
WireWebhook bool
}

// GetPath implements file.Builder
func (*WebhookTestUpdater) GetPath() string {
return filepath.Join("test", "e2e", "e2e_test.go")
}

// GetIfExistsAction implements file.Builder
func (*WebhookTestUpdater) GetIfExistsAction() machinery.IfExistsAction {
return machinery.OverwriteFile // Ensures only the marker is replaced
}

// GetMarkers implements file.Inserter
func (f *WebhookTestUpdater) GetMarkers() []machinery.Marker {
return []machinery.Marker{
machinery.NewMarkerFor(f.GetPath(), webhookChecksMarker),
}
}

// GetCodeFragments implements file.Inserter
func (f *WebhookTestUpdater) GetCodeFragments() machinery.CodeFragmentsMap {
codeFragments := machinery.CodeFragmentsMap{}
if !f.WireWebhook {
return nil
}
codeFragments[machinery.NewMarkerFor(f.GetPath(), webhookChecksMarker)] = append(
codeFragments[machinery.NewMarkerFor(f.GetPath(), webhookChecksMarker)],
webhookChecksFragment,
)

if f.Resource != nil && f.Resource.HasDefaultingWebhook() {
mutatingWebhookCode := fmt.Sprintf(mutatingWebhookChecksFragment, f.ProjectName)
codeFragments[machinery.NewMarkerFor(f.GetPath(), webhookChecksMarker)] = append(
codeFragments[machinery.NewMarkerFor(f.GetPath(), webhookChecksMarker)],
mutatingWebhookCode,
)
}

if f.Resource.HasValidationWebhook() {
validatingWebhookCode := fmt.Sprintf(validatingWebhookChecksFragment, f.ProjectName)
codeFragments[machinery.NewMarkerFor(f.GetPath(), webhookChecksMarker)] = append(
codeFragments[machinery.NewMarkerFor(f.GetPath(), webhookChecksMarker)],
validatingWebhookCode,
)
}

return codeFragments
}

const webhookChecksFragment = `It("should provisioned cert-manager", func() {
By("validating that cert-manager has the certificate Secret")
verifyCertManager := func(g Gomega) {
cmd := exec.Command("kubectl", "get", "secrets", "webhook-server-cert", "-n", namespace)
_, err := utils.Run(cmd)
g.Expect(err).NotTo(HaveOccurred())
}
Eventually(verifyCertManager).Should(Succeed())
})
`

const mutatingWebhookChecksFragment = `It("should have CA injection for mutating webhooks", func() {
By("checking CA injection for mutating webhooks")
verifyCAInjection := func(g Gomega) {
cmd := exec.Command("kubectl", "get",
"mutatingwebhookconfigurations.admissionregistration.k8s.io",
"%s-mutating-webhook-configuration",
"-o", "go-template={{ range .webhooks }}{{ .clientConfig.caBundle }}{{ end }}")
mwhOutput, err := utils.Run(cmd)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(len(mwhOutput)).To(BeNumerically(">", 10))
}
Eventually(verifyCAInjection).Should(Succeed())
})
`

// nolint:lll
const validatingWebhookChecksFragment = `It("should have CA injection for validating webhooks", func() {
By("checking CA injection for validating webhooks")
verifyCAInjection := func(g Gomega) {
cmd := exec.Command("kubectl", "get",
"validatingwebhookconfigurations.admissionregistration.k8s.io",
"%s-validating-webhook-configuration",
"-o", "go-template={{ range .webhooks }}{{ .clientConfig.caBundle }}{{ end }}")
vwhOutput, err := utils.Run(cmd)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(len(vwhOutput)).To(BeNumerically(">", 10))
}
Eventually(verifyCAInjection).Should(Succeed())
})
`

var TestTemplate = `{{ .Boilerplate }}
Expand Down Expand Up @@ -208,6 +317,8 @@ var _ = Describe("Manager", Ordered, func() {
))
})
// +kubebuilder:scaffold:e2e-webhooks-checks
// TODO: Customize the e2e test suite with scenarios specific to your project.
// Consider applying sample/CR(s) and check their status and/or verifying
// the reconciliation by using the metrics, i.e.:
Expand Down
2 changes: 2 additions & 0 deletions pkg/plugins/golang/v4/scaffolds/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/golang/v4/scaffolds/internal/templates"
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/golang/v4/scaffolds/internal/templates/api"
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/golang/v4/scaffolds/internal/templates/hack"
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/golang/v4/scaffolds/internal/templates/test/e2e"
)

var _ plugins.Scaffolder = &webhookScaffolder{}
Expand Down Expand Up @@ -86,6 +87,7 @@ func (s *webhookScaffolder) Scaffold() error {

if err := scaffold.Execute(
&api.Webhook{Force: s.force},
&e2e.WebhookTestUpdater{WireWebhook: true},
&templates.MainUpdater{WireWebhook: true},
&api.WebhookTest{Force: s.force},
); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,46 @@ var _ = Describe("Manager", Ordered, func() {
))
})

It("should provisioned cert-manager", func() {
By("validating that cert-manager has the certificate Secret")
verifyCertManager := func(g Gomega) {
cmd := exec.Command("kubectl", "get", "secrets", "webhook-server-cert", "-n", namespace)
_, err := utils.Run(cmd)
g.Expect(err).NotTo(HaveOccurred())
}
Eventually(verifyCertManager).Should(Succeed())
})

It("should have CA injection for mutating webhooks", func() {
By("checking CA injection for mutating webhooks")
verifyCAInjection := func(g Gomega) {
cmd := exec.Command("kubectl", "get",
"mutatingwebhookconfigurations.admissionregistration.k8s.io",
"project-v4-multigroup-with-deploy-image-mutating-webhook-configuration",
"-o", "go-template={{ range .webhooks }}{{ .clientConfig.caBundle }}{{ end }}")
mwhOutput, err := utils.Run(cmd)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(len(mwhOutput)).To(BeNumerically(">", 10))
}
Eventually(verifyCAInjection).Should(Succeed())
})

It("should have CA injection for validating webhooks", func() {
By("checking CA injection for validating webhooks")
verifyCAInjection := func(g Gomega) {
cmd := exec.Command("kubectl", "get",
"validatingwebhookconfigurations.admissionregistration.k8s.io",
"project-v4-multigroup-with-deploy-image-validating-webhook-configuration",
"-o", "go-template={{ range .webhooks }}{{ .clientConfig.caBundle }}{{ end }}")
vwhOutput, err := utils.Run(cmd)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(len(vwhOutput)).To(BeNumerically(">", 10))
}
Eventually(verifyCAInjection).Should(Succeed())
})

// +kubebuilder:scaffold:e2e-webhooks-checks

// TODO: Customize the e2e test suite with scenarios specific to your project.
// Consider applying sample/CR(s) and check their status and/or verifying
// the reconciliation by using the metrics, i.e.:
Expand Down
40 changes: 40 additions & 0 deletions testdata/project-v4-multigroup/test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,46 @@ var _ = Describe("Manager", Ordered, func() {
))
})

It("should provisioned cert-manager", func() {
By("validating that cert-manager has the certificate Secret")
verifyCertManager := func(g Gomega) {
cmd := exec.Command("kubectl", "get", "secrets", "webhook-server-cert", "-n", namespace)
_, err := utils.Run(cmd)
g.Expect(err).NotTo(HaveOccurred())
}
Eventually(verifyCertManager).Should(Succeed())
})

It("should have CA injection for mutating webhooks", func() {
By("checking CA injection for mutating webhooks")
verifyCAInjection := func(g Gomega) {
cmd := exec.Command("kubectl", "get",
"mutatingwebhookconfigurations.admissionregistration.k8s.io",
"project-v4-multigroup-mutating-webhook-configuration",
"-o", "go-template={{ range .webhooks }}{{ .clientConfig.caBundle }}{{ end }}")
mwhOutput, err := utils.Run(cmd)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(len(mwhOutput)).To(BeNumerically(">", 10))
}
Eventually(verifyCAInjection).Should(Succeed())
})

It("should have CA injection for validating webhooks", func() {
By("checking CA injection for validating webhooks")
verifyCAInjection := func(g Gomega) {
cmd := exec.Command("kubectl", "get",
"validatingwebhookconfigurations.admissionregistration.k8s.io",
"project-v4-multigroup-validating-webhook-configuration",
"-o", "go-template={{ range .webhooks }}{{ .clientConfig.caBundle }}{{ end }}")
vwhOutput, err := utils.Run(cmd)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(len(vwhOutput)).To(BeNumerically(">", 10))
}
Eventually(verifyCAInjection).Should(Succeed())
})

// +kubebuilder:scaffold:e2e-webhooks-checks

// TODO: Customize the e2e test suite with scenarios specific to your project.
// Consider applying sample/CR(s) and check their status and/or verifying
// the reconciliation by using the metrics, i.e.:
Expand Down
26 changes: 26 additions & 0 deletions testdata/project-v4-with-deploy-image/test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,32 @@ var _ = Describe("Manager", Ordered, func() {
))
})

It("should provisioned cert-manager", func() {
By("validating that cert-manager has the certificate Secret")
verifyCertManager := func(g Gomega) {
cmd := exec.Command("kubectl", "get", "secrets", "webhook-server-cert", "-n", namespace)
_, err := utils.Run(cmd)
g.Expect(err).NotTo(HaveOccurred())
}
Eventually(verifyCertManager).Should(Succeed())
})

It("should have CA injection for validating webhooks", func() {
By("checking CA injection for validating webhooks")
verifyCAInjection := func(g Gomega) {
cmd := exec.Command("kubectl", "get",
"validatingwebhookconfigurations.admissionregistration.k8s.io",
"project-v4-with-deploy-image-validating-webhook-configuration",
"-o", "go-template={{ range .webhooks }}{{ .clientConfig.caBundle }}{{ end }}")
vwhOutput, err := utils.Run(cmd)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(len(vwhOutput)).To(BeNumerically(">", 10))
}
Eventually(verifyCAInjection).Should(Succeed())
})

// +kubebuilder:scaffold:e2e-webhooks-checks

// TODO: Customize the e2e test suite with scenarios specific to your project.
// Consider applying sample/CR(s) and check their status and/or verifying
// the reconciliation by using the metrics, i.e.:
Expand Down
2 changes: 2 additions & 0 deletions testdata/project-v4-with-grafana/test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ var _ = Describe("Manager", Ordered, func() {
))
})

// +kubebuilder:scaffold:e2e-webhooks-checks

// TODO: Customize the e2e test suite with scenarios specific to your project.
// Consider applying sample/CR(s) and check their status and/or verifying
// the reconciliation by using the metrics, i.e.:
Expand Down
Loading

0 comments on commit 8f9e72c

Please sign in to comment.