Skip to content

Commit

Permalink
Merge pull request #3093 from Poor12/add-validate
Browse files Browse the repository at this point in the history
Add schedulerName validation
  • Loading branch information
karmada-bot authored Feb 2, 2023
2 parents 1d32ab6 + b9e32de commit c9886a0
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
4 changes: 4 additions & 0 deletions cmd/scheduler/app/options/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,9 @@ func (o *Options) Validate() field.ErrorList {
errs = append(errs, field.Invalid(newPath.Child("SchedulerEstimatorTimeout"), o.SchedulerEstimatorTimeout, "must be greater than or equal to 0"))
}

if o.SchedulerName == "" {
errs = append(errs, field.Invalid(newPath.Child("SchedulerName"), o.SchedulerName, "should not be empty"))
}

return errs
}
16 changes: 12 additions & 4 deletions cmd/scheduler/app/options/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func New(modifyOptions ModifyOptions) Options {
EnableSchedulerEstimator: false,
SchedulerEstimatorTimeout: metav1.Duration{Duration: 1 * time.Second},
SchedulerEstimatorPort: 9001,
SchedulerName: "default-scheduler",
}

if modifyOptions != nil {
Expand All @@ -45,10 +46,11 @@ func TestValidateKarmadaSchedulerConfiguration(t *testing.T) {
LeaderElection: componentbaseconfig.LeaderElectionConfiguration{
LeaderElect: false,
},
BindAddress: "127.0.0.1",
SecurePort: 9000,
KubeAPIQPS: 40,
KubeAPIBurst: 30,
BindAddress: "127.0.0.1",
SecurePort: 9000,
KubeAPIQPS: 40,
KubeAPIBurst: 30,
SchedulerName: "default-scheduler",
},
}

Expand Down Expand Up @@ -87,6 +89,12 @@ func TestValidateKarmadaSchedulerConfiguration(t *testing.T) {
}),
expectedErrs: field.ErrorList{field.Invalid(newPath.Child("SchedulerEstimatorTimeout"), metav1.Duration{Duration: -1 * time.Second}, "must be greater than or equal to 0")},
},
"invalid SchedulerName": {
opt: New(func(option *Options) {
option.SchedulerName = ""
}),
expectedErrs: field.ErrorList{field.Invalid(newPath.Child("SchedulerName"), "", "should not be empty")},
},
}

for _, testCase := range testCases {
Expand Down
16 changes: 16 additions & 0 deletions pkg/webhook/clusterpropagationpolicy/validating.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package clusterpropagationpolicy

import (
"context"
"fmt"
"net/http"

admissionv1 "k8s.io/api/admission/v1"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

Expand Down Expand Up @@ -31,6 +33,20 @@ func (v *ValidatingAdmission) Handle(ctx context.Context, req admission.Request)
}
klog.V(2).Infof("Validating ClusterPropagationPolicy(%s) for request: %s", policy.Name, req.Operation)

if req.Operation == admissionv1.Update {
oldPolicy := &policyv1alpha1.ClusterPropagationPolicy{}
err = v.decoder.DecodeRaw(req.OldObject, oldPolicy)
if err != nil {
return admission.Errored(http.StatusBadRequest, err)
}

if policy.Spec.SchedulerName != oldPolicy.Spec.SchedulerName {
err = fmt.Errorf("the schedulerName should not be updated")
klog.Error(err)
return admission.Denied(err.Error())
}
}

if err := validation.ValidateSpreadConstraint(policy.Spec.Placement.SpreadConstraints); err != nil {
klog.Error(err)
return admission.Denied(err.Error())
Expand Down
16 changes: 16 additions & 0 deletions pkg/webhook/propagationpolicy/validating.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package propagationpolicy

import (
"context"
"fmt"
"net/http"

admissionv1 "k8s.io/api/admission/v1"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

Expand Down Expand Up @@ -31,6 +33,20 @@ func (v *ValidatingAdmission) Handle(ctx context.Context, req admission.Request)
}
klog.V(2).Infof("Validating PropagationPolicy(%s/%s) for request: %s", policy.Namespace, policy.Name, req.Operation)

if req.Operation == admissionv1.Update {
oldPolicy := &policyv1alpha1.PropagationPolicy{}
err = v.decoder.DecodeRaw(req.OldObject, oldPolicy)
if err != nil {
return admission.Errored(http.StatusBadRequest, err)
}

if policy.Spec.SchedulerName != oldPolicy.Spec.SchedulerName {
err = fmt.Errorf("the schedulerName should not be updated")
klog.Error(err)
return admission.Denied(err.Error())
}
}

if err := validation.ValidateSpreadConstraint(policy.Spec.Placement.SpreadConstraints); err != nil {
klog.Error(err)
return admission.Denied(err.Error())
Expand Down

0 comments on commit c9886a0

Please sign in to comment.