-
Notifications
You must be signed in to change notification settings - Fork 126
[WIP] OCPBUGS-62726: add descheduler validation plugin #2491
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| replace k8s.io/kube-openapi => k8s.io/kube-openapi v0.0.0-20250318190949-c8a335a9a2ff |
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,107 @@ | ||||||||||
| package descheduler | ||||||||||
|
|
||||||||||
| import ( | ||||||||||
| "context" | ||||||||||
| "fmt" | ||||||||||
| "io" | ||||||||||
|
|
||||||||||
| "k8s.io/apimachinery/pkg/api/validation" | ||||||||||
| "k8s.io/apimachinery/pkg/runtime" | ||||||||||
| "k8s.io/apimachinery/pkg/runtime/schema" | ||||||||||
| "k8s.io/apimachinery/pkg/util/validation/field" | ||||||||||
| "k8s.io/apiserver/pkg/admission" | ||||||||||
|
|
||||||||||
| configv1 "github.com/openshift/api/config/v1" | ||||||||||
| deschedulerv1 "github.com/openshift/cluster-kube-descheduler-operator/pkg/apis/descheduler/v1" | ||||||||||
| "k8s.io/kubernetes/openshift-kube-apiserver/admission/customresourcevalidation" | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| const PluginName = "config.openshift.io/ValidateDescheduler" | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. KubeDescheduler is in the |
||||||||||
|
|
||||||||||
| // Register plugin. | ||||||||||
| func Register(plugins *admission.Plugins) { | ||||||||||
| plugins.Register(PluginName, func(config io.Reader) (admission.Interface, error) { | ||||||||||
| return customresourcevalidation.NewValidator( | ||||||||||
| map[schema.GroupResource]bool{ | ||||||||||
| configv1.Resource("deschedulers"): true, | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This won't resolve correctly for a couple reasons:
I think instead you probably want:
Suggested change
|
||||||||||
| }, | ||||||||||
| map[schema.GroupVersionKind]customresourcevalidation.ObjectValidator{ | ||||||||||
| configv1.GroupVersion.WithKind("Descheduler"): deschedulerV1{}, | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly to my comment above, you probably want:
Suggested change
|
||||||||||
| }) | ||||||||||
| }) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| func toDeschedulerV1(uncastObj runtime.Object) (*deschedulerv1.KubeDescheduler, field.ErrorList) { | ||||||||||
| if uncastObj == nil { | ||||||||||
| return nil, nil | ||||||||||
| } | ||||||||||
|
|
||||||||||
| allErrs := field.ErrorList{} | ||||||||||
|
|
||||||||||
| obj, ok := uncastObj.(*deschedulerv1.KubeDescheduler) | ||||||||||
|
|
||||||||||
| if !ok { | ||||||||||
| return nil, append(allErrs, | ||||||||||
| field.NotSupported(field.NewPath("kind"), fmt.Sprintf("%T", uncastObj), []string{"Descheduler"}), | ||||||||||
| field.NotSupported(field.NewPath("apiVersion"), fmt.Sprintf("%T", uncastObj), []string{"config.openshift.io/v1"})) | ||||||||||
|
Comment on lines
+45
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| } | ||||||||||
|
|
||||||||||
| return obj, nil | ||||||||||
| } | ||||||||||
|
|
||||||||||
| type deschedulerV1 struct{} | ||||||||||
|
|
||||||||||
| func validateDeschedulerSpec(spec deschedulerv1.KubeDeschedulerSpec) field.ErrorList { | ||||||||||
| allErrs := field.ErrorList{} | ||||||||||
|
|
||||||||||
| if name := spec.Policy.Name; len(name) > 0 { | ||||||||||
| for _, msg := range validation.NameIsDNSSubdomain(spec.Policy.Name, false) { | ||||||||||
| allErrs = append(allErrs, field.Invalid(field.NewPath("spec.Policy.name"), name, msg)) | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
Comment on lines
+57
to
+61
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see where the |
||||||||||
|
|
||||||||||
| return allErrs | ||||||||||
| } | ||||||||||
|
Comment on lines
+54
to
+64
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need to validate the Enforcing additional validations on the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There isn't a specific reason to validate, and there does not exist a I will remove this. :) |
||||||||||
|
|
||||||||||
| func (deschedulerV1) ValidateCreate(_ context.Context, uncastObj runtime.Object) field.ErrorList { | ||||||||||
| obj, allErrs := toDeschedulerV1(uncastObj) | ||||||||||
| if len(allErrs) > 0 { | ||||||||||
| return allErrs | ||||||||||
| } | ||||||||||
|
|
||||||||||
| allErrs = append(allErrs, validation.ValidateObjectMeta(&obj.ObjectMeta, false, customresourcevalidation.RequireNameCluster, field.NewPath("metadata"))...) | ||||||||||
| allErrs = append(allErrs, validateDeschedulerSpec(obj.Spec)...) | ||||||||||
|
|
||||||||||
| return allErrs | ||||||||||
| } | ||||||||||
|
|
||||||||||
| func (deschedulerV1) ValidateUpdate(_ context.Context, uncastObj runtime.Object, uncastOldObj runtime.Object) field.ErrorList { | ||||||||||
| obj, allErrs := toDeschedulerV1(uncastObj) | ||||||||||
| if len(allErrs) > 0 { | ||||||||||
| return allErrs | ||||||||||
| } | ||||||||||
| oldObj, allErrs := toDeschedulerV1(uncastOldObj) | ||||||||||
| if len(allErrs) > 0 { | ||||||||||
| return allErrs | ||||||||||
| } | ||||||||||
|
|
||||||||||
| allErrs = append(allErrs, validation.ValidateObjectMetaUpdate(&obj.ObjectMeta, &oldObj.ObjectMeta, field.NewPath("metadata"))...) | ||||||||||
| allErrs = append(allErrs, validateDeschedulerSpec(obj.Spec)...) | ||||||||||
|
|
||||||||||
| return allErrs | ||||||||||
| } | ||||||||||
|
|
||||||||||
| func (deschedulerV1) ValidateStatusUpdate(_ context.Context, uncastObj runtime.Object, uncastOldObj runtime.Object) field.ErrorList { | ||||||||||
| obj, errs := toDeschedulerV1(uncastObj) | ||||||||||
| if len(errs) > 0 { | ||||||||||
| return errs | ||||||||||
| } | ||||||||||
| oldObj, errs := toDeschedulerV1(uncastOldObj) | ||||||||||
| if len(errs) > 0 { | ||||||||||
| return errs | ||||||||||
| } | ||||||||||
|
|
||||||||||
| errs = append(errs, validation.ValidateObjectMetaUpdate(&obj.ObjectMeta, &oldObj.ObjectMeta, field.NewPath("metadata"))...) | ||||||||||
| errs = append(errs, validateDeschedulerSpec(obj.Spec)...) | ||||||||||
| return errs | ||||||||||
| } | ||||||||||
|
Comment on lines
+78
to
+107
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure we need to validate these operations. I think we would only care to validate that the I believe by default, Kubernetes will enforce immutability on things like custom resource |
||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason why it is necessary is without it, Go is unable to obtain the correct version. It tries to get
v0.30.0or something like that if I remember correctly.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.