Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
hantmac committed Feb 24, 2024
1 parent 2679827 commit 83f9acc
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,6 @@ type DaemonSetCreateUpdateHandler struct {
Decoder *admission.Decoder
}

func (h *DaemonSetCreateUpdateHandler) validatingDaemonSetFn(ctx context.Context, obj *appsv1alpha1.DaemonSet) (bool, string, error) {
allErrs := validateDaemonSet(obj)
if len(allErrs) != 0 {
return false, "", allErrs.ToAggregate()
}
return true, "allowed to be admitted", nil
}

func (h *DaemonSetCreateUpdateHandler) validateDaemonSetUpdate(ds, oldDs *appsv1alpha1.DaemonSet) field.ErrorList {
allErrs := apivalidation.ValidateObjectMetaUpdate(&ds.ObjectMeta, &oldDs.ObjectMeta, field.NewPath("metadata"))
daemonset := ds.DeepCopy()
Expand Down Expand Up @@ -78,7 +70,7 @@ func (h *DaemonSetCreateUpdateHandler) Handle(ctx context.Context, req admission
if err != nil {
return admission.Errored(http.StatusBadRequest, err)
}
allowed, reason, err := h.validatingDaemonSetFn(ctx, obj)
allowed, reason, err := validatingDaemonSetFn(ctx, obj)
if err != nil {
klog.Warningf("ds %s/%s action %v fail:%s", obj.Namespace, obj.Name, req.AdmissionRequest.Operation, err.Error())
return admission.Errored(http.StatusInternalServerError, err)
Expand Down
9 changes: 9 additions & 0 deletions pkg/webhook/daemonset/validating/daemonset_validation.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package validating

import (
"context"
"fmt"
"strconv"

Expand All @@ -20,6 +21,14 @@ import (
"github.com/openkruise/kruise/pkg/webhook/util/convertor"
)

func validatingDaemonSetFn(ctx context.Context, obj *appsv1alpha1.DaemonSet) (bool, string, error) {
allErrs := validateDaemonSet(obj)
if len(allErrs) != 0 {
return false, "", allErrs.ToAggregate()
}
return true, "allowed to be admitted", nil
}

func validateDaemonSet(ds *appsv1alpha1.DaemonSet) field.ErrorList {
allErrs := genericvalidation.ValidateObjectMeta(&ds.ObjectMeta, true, ValidateDaemonSetName, field.NewPath("metadata"))
allErrs = append(allErrs, validateDaemonSetSpec(&ds.Spec, field.NewPath("spec"))...)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ func newDaemonset(name string) *appsv1alpha1.DaemonSet {
}

func TestValidateDaemonSet(t *testing.T) {
handler := DaemonSetCreateUpdateHandler{}

for _, c := range []struct {
Title string
Expand Down Expand Up @@ -98,7 +97,7 @@ func TestValidateDaemonSet(t *testing.T) {
true,
},
} {
result, _, err := handler.validatingDaemonSetFn(context.TODO(), c.Ds)
result, _, err := validatingDaemonSetFn(context.TODO(), c.Ds)
if !reflect.DeepEqual(c.ExpectAllowResult, result) {
t.Fatalf("case: %s, expected result: %v, got: %v, error: %v", c.Title, c.ExpectAllowResult, result, err)
}
Expand Down

0 comments on commit 83f9acc

Please sign in to comment.