Skip to content

Commit 61c34c4

Browse files
webhook: sort on version if names are equal
1 parent f468c2e commit 61c34c4

File tree

3 files changed

+51
-59
lines changed

3 files changed

+51
-59
lines changed

pkg/webhook/parser.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,7 @@ func (g Generator) Generate(ctx *genall.GenerationContext) error {
514514
}
515515

516516
versionedWebhooks := make(map[string][]interface{}, len(supportedWebhookVersions))
517+
//nolint:dupl
517518
for _, version := range supportedWebhookVersions {
518519
if cfgs, ok := mutatingCfgs[version]; ok {
519520
sort.SliceStable(cfgs, func(i, j int) bool {

pkg/webhook/parser_integration_test.go

Lines changed: 50 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -433,68 +433,67 @@ var _ = Describe("Webhook Generation From Parsing to CustomResourceDefinition",
433433
Expect(os.Chdir("./testdata/valid-crosspkg-stable")).To(Succeed()) // go modules are directory-sensitive
434434
defer func() { Expect(os.Chdir(cwd)).To(Succeed()) }()
435435

436-
By("loading the roots in one order")
437-
pkgsA, err := loader.LoadRoots("./v1", "./v1alpha1")
438-
Expect(err).NotTo(HaveOccurred())
439-
Expect(pkgsA).To(HaveLen(2))
440-
441436
By("setting up the parser")
442437
reg := &markers.Registry{}
443438
Expect(reg.Register(webhook.ConfigDefinition)).To(Succeed())
444439
Expect(reg.Register(webhook.WebhookConfigDefinition)).To(Succeed())
445440

446-
By("generating manifests for order A")
447-
outputDirA, err := os.MkdirTemp("", "webhook-integration-test-order-a")
448-
Expect(err).NotTo(HaveOccurred())
449-
defer os.RemoveAll(outputDirA)
450-
genCtxA := &genall.GenerationContext{
451-
Collector: &markers.Collector{Registry: reg},
452-
Roots: pkgsA,
453-
OutputRule: genall.OutputToDirectory(outputDirA),
454-
}
455-
Expect(webhook.Generator{}.Generate(genCtxA)).To(Succeed())
456-
for _, r := range genCtxA.Roots {
457-
Expect(r.Errors).To(HaveLen(0))
458-
}
459-
460-
By("loading the generated v1 YAML for order A")
461-
actualFileA, err := os.ReadFile(path.Join(outputDirA, "manifests.yaml"))
462-
Expect(err).NotTo(HaveOccurred())
463-
actualManifestA := &admissionregv1.ValidatingWebhookConfiguration{}
464-
Expect(yaml.UnmarshalStrict(actualFileA, actualManifestA)).To(Succeed())
465-
466-
By("loading the roots in the reverse order")
467-
pkgsB := []*loader.Package{pkgsA[1], pkgsA[0]}
468-
469-
By("generating manifests for order B")
470-
outputDirB, err := os.MkdirTemp("", "webhook-integration-test-order-b")
471-
Expect(err).NotTo(HaveOccurred())
472-
defer os.RemoveAll(outputDirB)
473-
genCtxB := &genall.GenerationContext{
474-
Collector: &markers.Collector{Registry: reg},
475-
Roots: pkgsB,
476-
OutputRule: genall.OutputToDirectory(outputDirB),
477-
}
478-
Expect(webhook.Generator{}.Generate(genCtxB)).To(Succeed())
479-
for _, r := range genCtxB.Roots {
480-
Expect(r.Errors).To(HaveLen(0))
481-
}
482-
483-
By("loading the generated v1 YAML for order B")
484-
actualFileB, err := os.ReadFile(path.Join(outputDirB, "manifests.yaml"))
485-
Expect(err).NotTo(HaveOccurred())
486-
actualManifestB := &admissionregv1.ValidatingWebhookConfiguration{}
487-
Expect(yaml.UnmarshalStrict(actualFileB, actualManifestB)).To(Succeed())
488-
489441
By("loading the desired v1 YAML")
490442
expectedFile, err := os.ReadFile("manifests.yaml")
491443
Expect(err).NotTo(HaveOccurred())
492444
expectedManifest := &admissionregv1.ValidatingWebhookConfiguration{}
493445
Expect(yaml.UnmarshalStrict(expectedFile, expectedManifest)).To(Succeed())
494446

495-
By("comparing manifests across orders and to expected")
496-
assertSame(actualManifestA, actualManifestB)
497-
assertSame(actualManifestA, expectedManifest)
447+
rootsOrders := []struct {
448+
name string
449+
outputDir string
450+
roots []string
451+
}{
452+
{
453+
name: "v1 first",
454+
outputDir: "webhook-integration-test-order-a",
455+
roots: []string{
456+
"./v1",
457+
"./v1alpha1",
458+
}},
459+
{
460+
name: "v1alpha1 first",
461+
outputDir: "webhook-integration-test-order-b",
462+
roots: []string{
463+
"./v1alpha1",
464+
"./v1",
465+
}},
466+
}
467+
468+
for _, rootsOrder := range rootsOrders {
469+
By("loading the roots in order " + rootsOrder.name)
470+
pkgs, err := loader.LoadRoots(rootsOrder.roots...)
471+
Expect(err).NotTo(HaveOccurred())
472+
Expect(pkgs).To(HaveLen(2))
473+
474+
By("requesting that the manifest be generated for order " + rootsOrder.name)
475+
outputDir, err := os.MkdirTemp("", rootsOrder.outputDir)
476+
Expect(err).NotTo(HaveOccurred())
477+
defer os.RemoveAll(outputDir)
478+
genCtx := &genall.GenerationContext{
479+
Collector: &markers.Collector{Registry: reg},
480+
Roots: pkgs,
481+
OutputRule: genall.OutputToDirectory(outputDir),
482+
}
483+
Expect(webhook.Generator{}.Generate(genCtx)).To(Succeed())
484+
for _, r := range genCtx.Roots {
485+
Expect(r.Errors).To(HaveLen(0))
486+
}
487+
488+
By("loading the generated v1 YAML for order " + rootsOrder.name)
489+
actualFile, err := os.ReadFile(path.Join(outputDir, "manifests.yaml"))
490+
Expect(err).NotTo(HaveOccurred())
491+
actualManifest := &admissionregv1.ValidatingWebhookConfiguration{}
492+
Expect(yaml.UnmarshalStrict(actualFile, actualManifest)).To(Succeed())
493+
494+
By("comparing the manifest for order " + rootsOrder.name)
495+
assertSame(actualManifest, expectedManifest)
496+
}
498497
})
499498

500499
It("should fail to generate when there are multiple `kubebuilder:webhookconfiguration` markers of the same mutation type", func() {

pkg/webhook/testdata/valid-crosspkg-stable/go.mod

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)