diff --git a/.changes/unreleased/changed-20261012-121128.yaml b/.changes/unreleased/changed-20261012-121128.yaml new file mode 100644 index 0000000..7029101 --- /dev/null +++ b/.changes/unreleased/changed-20261012-121128.yaml @@ -0,0 +1,6 @@ +kind: Changed +body: |- + PodGroup webhook logs validate update at verbosity 4 instead of the default level +custom: + Author: "sam-huang1223" + Issue: "24" diff --git a/go.mod b/go.mod index 6ac3317..7819243 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/kai-scheduler/api go 1.26.3 require ( + github.com/go-logr/logr v1.4.3 github.com/onsi/ginkgo/v2 v2.28.1 github.com/onsi/gomega v1.39.1 github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.88.0 @@ -27,7 +28,6 @@ require ( github.com/evanphx/json-patch/v5 v5.9.11 // indirect github.com/fsnotify/fsnotify v1.9.0 // indirect github.com/fxamacker/cbor/v2 v2.9.0 // indirect - github.com/go-logr/logr v1.4.3 // indirect github.com/go-openapi/jsonpointer v0.21.2 // indirect github.com/go-openapi/jsonreference v0.21.0 // indirect github.com/go-openapi/swag v0.23.1 // indirect diff --git a/scheduling/v2alpha2/podgroup_webhook.go b/scheduling/v2alpha2/podgroup_webhook.go index 3008736..8ed62dc 100644 --- a/scheduling/v2alpha2/podgroup_webhook.go +++ b/scheduling/v2alpha2/podgroup_webhook.go @@ -43,7 +43,7 @@ func (_ *PodGroup) ValidateCreate(ctx context.Context, podGroup *PodGroup) (admi // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type func (_ *PodGroup) ValidateUpdate(ctx context.Context, oldPodGroup *PodGroup, podGroup *PodGroup) (admission.Warnings, error) { logger := log.FromContext(ctx) - logger.Info("validate update", "namespace", podGroup.Namespace, "name", podGroup.Name) + logger.V(4).Info("validate update", "namespace", podGroup.Namespace, "name", podGroup.Name) validationErrors := validatePodGroupSpec(&podGroup.Spec) diff --git a/scheduling/v2alpha2/podgroup_webhook_test.go b/scheduling/v2alpha2/podgroup_webhook_test.go index 66fbafc..8c3df93 100644 --- a/scheduling/v2alpha2/podgroup_webhook_test.go +++ b/scheduling/v2alpha2/podgroup_webhook_test.go @@ -9,7 +9,9 @@ import ( "strings" "testing" + "github.com/go-logr/logr/funcr" "k8s.io/utils/ptr" + "sigs.k8s.io/controller-runtime/pkg/log" ) func TestValidateSubGroups(t *testing.T) { @@ -536,3 +538,33 @@ func errorsListEqual(want, got []error) bool { } return true } + +func TestValidateUpdateLogsAboveDefaultVerbosity(t *testing.T) { + for _, tt := range []struct { + verbosity int + wantLog bool + }{ + {verbosity: 3, wantLog: false}, + {verbosity: 4, wantLog: true}, + } { + var logs strings.Builder + ctx := log.IntoContext(context.Background(), funcr.New(func(_, args string) { + logs.WriteString(args + "\n") + }, funcr.Options{Verbosity: tt.verbosity})) + podGroup := &PodGroup{} + + if _, err := podGroup.ValidateUpdate(ctx, podGroup, podGroup); err != nil { + t.Fatalf("ValidateUpdate() error = %v", err) + } + if got := strings.Contains(logs.String(), "validate update"); got != tt.wantLog { + t.Errorf("verbosity %d: logged validate update = %v, want %v", tt.verbosity, got, tt.wantLog) + } + + if _, err := podGroup.ValidateCreate(ctx, podGroup); err != nil { + t.Fatalf("ValidateCreate() error = %v", err) + } + if !strings.Contains(logs.String(), "validate create") { + t.Errorf("verbosity %d: validate create was not logged", tt.verbosity) + } + } +}