controller-tools icon indicating copy to clipboard operation
controller-tools copied to clipboard

controller-gen: Deterministic ordering for webhook manifests

Open terassyi opened this issue 3 years ago • 0 comments

When I generate webhook manifests with controller-gen, I get different ordered manifests every time. The order of the objects of the webhook array in the generated YAML output is randomized each time.

(snip)
webhooks:
- admissionReviewVersions:
  - v1
  - v1beta1
  name: cronjoblist.testdata.kubebuilder.io
  (snip)
- admissionReviewVersions:
  - v1
  - v1beta1
  name: cronjob.testdata.kubebuilder.io
  (snip)

I think it would be better if we can get manifests ordered by the webhook name every time because it makes easy to check.

This is the example code that gets its output. Two validator webhooks are defined in this file.

// +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

type cronjobValidator struct {
	client  client.Client
	decoder *admission.Decoder
}

func (v *cronjobValidator) Handle(ctx context.Context, req admission.Request) admission.Response {
	return admission.Allowed("ok")
}

// +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

type cronjobListValidator struct {
	client  client.Client
	decoder *admission.Decoder
}

func (v *cronjobListValidator) Handle(ctx context.Context, req admission.Request) admission.Response {
	return admission.Allowed("ok")
}

And I run controller-gen with this multiple times as shown bellow, I can get different ordered output every time.

$ ./controller-gen webhook paths="." | cat config/webhook/manifests.yaml | yq .webhooks[].name
cronjoblist.testdata.kubebuilder.io
cronjob.testdata.kubebuilder.io

$ ./controller-gen webhook paths="." | cat config/webhook/manifests.yaml | yq .webhooks[].name
cronjob.testdata.kubebuilder.io
cronjoblist.testdata.kubebuilder.io

$ ./controller-gen webhook paths="." | cat config/webhook/manifests.yaml | yq .webhooks[].name
cronjob.testdata.kubebuilder.io
cronjoblist.testdata.kubebuilder.io

terassyi avatar Sep 30 '22 06:09 terassyi