Skip to content

Commit 4ce8785

Browse files
committed
fix to sort makers
Signed-off-by: terasihma <[email protected]>
1 parent 9084e41 commit 4ce8785

File tree

5 files changed

+273
-1
lines changed

5 files changed

+273
-1
lines changed

pkg/webhook/parser.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,12 @@ func (g Generator) Generate(ctx *genall.GenerationContext) error {
330330
root.AddError(err)
331331
}
332332

333-
for _, cfg := range markerSet[ConfigDefinition.Name] {
333+
cfgs := markerSet[ConfigDefinition.Name]
334+
sort.SliceStable(cfgs, func(i, j int) bool {
335+
return cfgs[i].(Config).Name < cfgs[j].(Config).Name
336+
})
337+
338+
for _, cfg := range cfgs {
334339
cfg := cfg.(Config)
335340
webhookVersions, err := cfg.webhookVersions()
336341
if err != nil {

pkg/webhook/parser_integration_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,52 @@ var _ = Describe("Webhook Generation From Parsing to CustomResourceDefinition",
175175
assertSame(actualMutating, expectedMutating)
176176
assertSame(actualValidating, expectedValidating)
177177
})
178+
179+
It("should generate the ordered webhook definitions", func() {
180+
By("switching into testdata to appease go modules")
181+
cwd, err := os.Getwd()
182+
Expect(err).NotTo(HaveOccurred())
183+
Expect(os.Chdir("./testdata/valid-ordered")).To(Succeed()) // go modules are directory-sensitive
184+
defer func() { Expect(os.Chdir(cwd)).To(Succeed()) }()
185+
186+
By("loading the roots")
187+
pkgs, err := loader.LoadRoots(".")
188+
Expect(err).NotTo(HaveOccurred())
189+
Expect(pkgs).To(HaveLen(1))
190+
191+
By("setting up the parser")
192+
reg := &markers.Registry{}
193+
Expect(reg.Register(webhook.ConfigDefinition)).To(Succeed())
194+
195+
By("requesting that the manifest be generated")
196+
outputDir, err := ioutil.TempDir("", "webhook-integration-test")
197+
Expect(err).NotTo(HaveOccurred())
198+
// defer os.RemoveAll(outputDir)
199+
genCtx := &genall.GenerationContext{
200+
Collector: &markers.Collector{Registry: reg},
201+
Roots: pkgs,
202+
OutputRule: genall.OutputToDirectory(outputDir),
203+
}
204+
Expect(webhook.Generator{}.Generate(genCtx)).To(Succeed())
205+
for _, r := range genCtx.Roots {
206+
Expect(r.Errors).To(HaveLen(0))
207+
}
208+
209+
By("loading the generated v1 YAML")
210+
actualFile, err := ioutil.ReadFile(path.Join(outputDir, "manifests.yaml"))
211+
Expect(err).NotTo(HaveOccurred())
212+
actualManifest := &admissionregv1.ValidatingWebhookConfiguration{}
213+
Expect(yaml.UnmarshalStrict(actualFile, actualManifest)).To(Succeed())
214+
215+
By("loading the desired v1 YAML")
216+
expectedFile, err := ioutil.ReadFile("manifests.yaml")
217+
Expect(err).NotTo(HaveOccurred())
218+
expectedManifest := &admissionregv1.ValidatingWebhookConfiguration{}
219+
Expect(yaml.UnmarshalStrict(expectedFile, expectedManifest)).To(Succeed())
220+
221+
By("comparing the manifest")
222+
assertSame(actualManifest, expectedManifest)
223+
})
178224
})
179225

180226
func unmarshalBothV1(in []byte) (mutating admissionregv1.MutatingWebhookConfiguration, validating admissionregv1.ValidatingWebhookConfiguration) {
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
16+
//go:generate ../../../.run-controller-gen.sh webhook paths=. output:dir=.
17+
18+
// +groupName=testdata.kubebuilder.io
19+
// +versionName=v1
20+
package cronjob
21+
22+
import (
23+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24+
)
25+
26+
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
27+
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.
28+
29+
// CronJobSpec defines the desired state of CronJob
30+
type CronJobSpec struct {
31+
// The schedule in Cron format, see https://en.wikipedia.org/wiki/Cron.
32+
Schedule string `json:"schedule"`
33+
}
34+
35+
// CronJobStatus defines the observed state of CronJob
36+
type CronJobStatus struct {
37+
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
38+
// Important: Run "make" to regenerate code after modifying this file
39+
40+
// Information when was the last time the job was successfully scheduled.
41+
// +optional
42+
LastScheduleTime *metav1.Time `json:"lastScheduleTime,omitempty"`
43+
}
44+
45+
// +kubebuilder:object:root=true
46+
// +kubebuilder:subresource:status
47+
// +kubebuilder:resource:singular=mycronjob
48+
49+
// CronJob is the Schema for the cronjobs API
50+
type CronJob struct {
51+
/*
52+
*/
53+
metav1.TypeMeta `json:",inline"`
54+
metav1.ObjectMeta `json:"metadata,omitempty"`
55+
56+
Spec CronJobSpec `json:"spec,omitempty"`
57+
Status CronJobStatus `json:"status,omitempty"`
58+
}
59+
60+
// +kubebuilder:object:root=true
61+
62+
// CronJobList contains a list of CronJob
63+
type CronJobList struct {
64+
metav1.TypeMeta `json:",inline"`
65+
metav1.ListMeta `json:"metadata,omitempty"`
66+
Items []CronJob `json:"items"`
67+
}
68+
69+
func init() {
70+
SchemeBuilder.Register(&CronJob{}, &CronJobList{})
71+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
apiVersion: admissionregistration.k8s.io/v1
3+
kind: ValidatingWebhookConfiguration
4+
metadata:
5+
creationTimestamp: null
6+
name: validating-webhook-configuration
7+
webhooks:
8+
- admissionReviewVersions:
9+
- v1
10+
- v1beta1
11+
clientConfig:
12+
service:
13+
name: webhook-service
14+
namespace: system
15+
path: /validate-testdata-kubebuilder-io-v1-cronjob
16+
failurePolicy: Fail
17+
matchPolicy: Equivalent
18+
name: cronjob.testdata.kubebuilder.io
19+
rules:
20+
- apiGroups:
21+
- testdata.kubebuiler.io
22+
apiVersions:
23+
- v1
24+
operations:
25+
- CREATE
26+
- UPDATE
27+
resources:
28+
- cronjobs
29+
sideEffects: None
30+
- admissionReviewVersions:
31+
- v1
32+
- v1beta1
33+
clientConfig:
34+
service:
35+
name: webhook-service
36+
namespace: system
37+
path: /validate-testdata-kubebuilder-io-v1-cronjoblist
38+
failurePolicy: Fail
39+
matchPolicy: Equivalent
40+
name: cronjoblist.testdata.kubebuilder.io
41+
rules:
42+
- apiGroups:
43+
- testdata.kubebuiler.io
44+
apiVersions:
45+
- v1
46+
operations:
47+
- CREATE
48+
- UPDATE
49+
resources:
50+
- cronjoblist
51+
sideEffects: None
52+
- admissionReviewVersions:
53+
- v1
54+
- v1beta1
55+
clientConfig:
56+
service:
57+
name: webhook-service
58+
namespace: system
59+
path: /validate-testdata-kubebuilder-io-v1-deployments
60+
failurePolicy: Fail
61+
matchPolicy: Equivalent
62+
name: deployment.testdata.kubebuilder.io
63+
rules:
64+
- apiGroups:
65+
- testdata.kubebuiler.io
66+
apiVersions:
67+
- v1
68+
operations:
69+
- CREATE
70+
- UPDATE
71+
resources:
72+
- deployments
73+
sideEffects: None
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
16+
package cronjob
17+
18+
import (
19+
"context"
20+
"net/http"
21+
22+
ctrl "sigs.k8s.io/controller-runtime"
23+
"sigs.k8s.io/controller-runtime/pkg/client"
24+
"sigs.k8s.io/controller-runtime/pkg/webhook"
25+
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
26+
)
27+
28+
func (c *CronJob) SetupWebhookWithManager(mgr ctrl.Manager) error {
29+
return ctrl.NewWebhookManagedBy(mgr).
30+
For(c).
31+
Complete()
32+
}
33+
34+
// +kubebuilder:webhook:webhookVersions=v1,verbs=create;update,path=/validate-testdata-kubebuilder-io-v1-cronjob,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=testdata.kubebuiler.io,resources=cronjobs,versions=v1,name=cronjob.testdata.kubebuilder.io,sideEffects=None,admissionReviewVersions=v1;v1beta1,reinvocationPolicy=Never
35+
36+
type cronjobValidator struct {
37+
client client.Client
38+
decoder *admission.Decoder
39+
}
40+
41+
func NewCronjobValidator(c client.Client, dec *admission.Decoder) http.Handler {
42+
return &webhook.Admission{Handler: &cronjobValidator{c, dec}}
43+
}
44+
45+
func (v *cronjobValidator) Handle(ctx context.Context, req admission.Request) admission.Response {
46+
return admission.Allowed("ok")
47+
}
48+
49+
// +kubebuilder:webhook:webhookVersions=v1,verbs=create;update,path=/validate-testdata-kubebuilder-io-v1-cronjoblist,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=testdata.kubebuiler.io,resources=cronjoblist,versions=v1,name=cronjoblist.testdata.kubebuilder.io,sideEffects=None,admissionReviewVersions=v1;v1beta1,reinvocationPolicy=Never
50+
51+
type cronjobListValidator struct {
52+
client client.Client
53+
decoder *admission.Decoder
54+
}
55+
56+
func NewCronjobListValidator(c client.Client, dec *admission.Decoder) http.Handler {
57+
return &webhook.Admission{Handler: &cronjobListValidator{c, dec}}
58+
}
59+
60+
func (v *cronjobListValidator) Handle(ctx context.Context, req admission.Request) admission.Response {
61+
return admission.Allowed("ok")
62+
}
63+
64+
// +kubebuilder:webhook:webhookVersions=v1,verbs=create;update,path=/validate-testdata-kubebuilder-io-v1-deployments,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=testdata.kubebuiler.io,resources=deployments,versions=v1,name=deployment.testdata.kubebuilder.io,sideEffects=None,admissionReviewVersions=v1;v1beta1,reinvocationPolicy=Never
65+
66+
type deploymentValidator struct {
67+
client client.Client
68+
decoder *admission.Decoder
69+
}
70+
71+
func NewDeploymentValidator(c client.Client, dec *admission.Decoder) http.Handler {
72+
return &webhook.Admission{Handler: &deploymentValidator{c, dec}}
73+
}
74+
75+
func (v *deploymentValidator) Handle(ctx context.Context, req admission.Request) admission.Response {
76+
return admission.Allowed("ok")
77+
}

0 commit comments

Comments
 (0)