From 342a7b6298db8be2e5cebd67f918c64d377ba39b Mon Sep 17 00:00:00 2001 From: changzhen Date: Tue, 17 Dec 2024 19:43:38 +0800 Subject: [PATCH] fix validate panic when ResourceInterpreterWebhookConfiguration .webhooks[*].clientConfig.service.port is nil Signed-off-by: changzhen --- pkg/webhook/configuration/validating.go | 4 ++++ pkg/webhook/configuration/validating_test.go | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/pkg/webhook/configuration/validating.go b/pkg/webhook/configuration/validating.go index 7c86aef866c2..9aa5dfedcd5a 100644 --- a/pkg/webhook/configuration/validating.go +++ b/pkg/webhook/configuration/validating.go @@ -27,6 +27,7 @@ import ( "k8s.io/apimachinery/pkg/util/validation/field" "k8s.io/apiserver/pkg/util/webhook" "k8s.io/klog/v2" + "k8s.io/utils/ptr" "sigs.k8s.io/controller-runtime/pkg/webhook/admission" configv1alpha1 "github.com/karmada-io/karmada/pkg/apis/config/v1alpha1" @@ -103,6 +104,9 @@ func validateWebhook(hook *configv1alpha1.ResourceInterpreterWebhook, fldPath *f case cc.URL != nil: allErrors = append(allErrors, webhook.ValidateWebhookURL(fldPath.Child("clientConfig").Child("url"), *cc.URL, true)...) case cc.Service != nil: + if cc.Service.Port == nil { + cc.Service.Port = ptr.To[int32](443) + } allErrors = append(allErrors, webhook.ValidateWebhookService(fldPath.Child("clientConfig").Child("service"), cc.Service.Name, cc.Service.Namespace, cc.Service.Path, *cc.Service.Port)...) } diff --git a/pkg/webhook/configuration/validating_test.go b/pkg/webhook/configuration/validating_test.go index 7097fcf33ade..427b68d44325 100644 --- a/pkg/webhook/configuration/validating_test.go +++ b/pkg/webhook/configuration/validating_test.go @@ -264,6 +264,17 @@ func TestValidateWebhook(t *testing.T) { }, expectedError: "service name is required", }, + { + name: "ClientConfig: valid service with nil port", + hook: &configv1alpha1.ResourceInterpreterWebhook{ + ClientConfig: admissionregistrationv1.WebhookClientConfig{ + Service: &admissionregistrationv1.ServiceReference{ + Name: "svc", + Path: strPtr("/interpreter"), + }, + }, + }, + }, { name: "invalid interpreter context versions", hook: &configv1alpha1.ResourceInterpreterWebhook{