diff --git a/pkg/webhook/configuration/validating.go b/pkg/webhook/configuration/validating.go index 7c86aef866c2..54c11ed78b47 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,7 +104,13 @@ 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: - allErrors = append(allErrors, webhook.ValidateWebhookService(fldPath.Child("clientConfig").Child("service"), cc.Service.Name, cc.Service.Namespace, cc.Service.Path, *cc.Service.Port)...) + // This modification breaks the validating programming paradigm: only resources are verified. + // However, the current changes have minimal impact on users, and the backend also handles + // exceptions when processing this value to prevent panic. + if cc.Service.Port == nil { + cc.Service.Port = ptr.To[int32](443) + } + allErrors = append(allErrors, webhook.ValidateWebhookService(fldPath.Child("clientConfig").Child("service"), cc.Service.Namespace, cc.Service.Name, cc.Service.Path, *cc.Service.Port)...) } allErrors = append(allErrors, validateInterpreterContextVersions(hook.InterpreterContextVersions, fldPath.Child("interpreterContextVersions"))...) diff --git a/pkg/webhook/configuration/validating_test.go b/pkg/webhook/configuration/validating_test.go index 7097fcf33ade..9de1fc8bf597 100644 --- a/pkg/webhook/configuration/validating_test.go +++ b/pkg/webhook/configuration/validating_test.go @@ -271,6 +271,20 @@ func TestValidateWebhook(t *testing.T) { }, expectedError: fmt.Sprintf("must include at least one of %v", strings.Join(acceptedInterpreterContextVersions, ", ")), }, + { + name: "valid webhook configuration: use Service in ClientConfig, which port is nil", + hook: &configv1alpha1.ResourceInterpreterWebhook{ + Name: "workloads.karmada.io", + ClientConfig: admissionregistrationv1.WebhookClientConfig{ + Service: &admissionregistrationv1.ServiceReference{ + Namespace: "default", + Name: "svc", + Path: strPtr("/interpreter"), + }, + }, + InterpreterContextVersions: []string{"v1alpha1"}, + }, + }, } for _, test := range tests {