Skip to content

Commit 92f5bde

Browse files
authored
Merge pull request #5967 from seanlaii/automated-cherry-pick-of-#5960-upstream-release-1.12
Automated cherry pick of #5960: Fix panic when validating ResourceInterpreterWebhookConfiguration with unspecified service port
2 parents 7efb13b + 096e936 commit 92f5bde

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

pkg/webhook/configuration/validating.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"k8s.io/apimachinery/pkg/util/validation/field"
2828
"k8s.io/apiserver/pkg/util/webhook"
2929
"k8s.io/klog/v2"
30+
"k8s.io/utils/ptr"
3031
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
3132

3233
configv1alpha1 "github.com/karmada-io/karmada/pkg/apis/config/v1alpha1"
@@ -103,7 +104,19 @@ func validateWebhook(hook *configv1alpha1.ResourceInterpreterWebhook, fldPath *f
103104
case cc.URL != nil:
104105
allErrors = append(allErrors, webhook.ValidateWebhookURL(fldPath.Child("clientConfig").Child("url"), *cc.URL, true)...)
105106
case cc.Service != nil:
106-
allErrors = append(allErrors, webhook.ValidateWebhookService(fldPath.Child("clientConfig").Child("service"), cc.Service.Name, cc.Service.Namespace, cc.Service.Path, *cc.Service.Port)...)
107+
// Temporary fix: If the service port is not specified, set a default value of 443.
108+
// This is a workaround to prevent a panic when validating ResourceInterpreterWebhookConfiguration
109+
// with an unspecified service port. Ideally, this logic should be handled by a MutatingWebhook,
110+
// but introducing a MutatingWebhook at this stage would require significant changes and is not
111+
// convenient for cherry-picking to release branches. Therefore, we are temporarily setting the
112+
// default port here. Once a MutatingWebhook is implemented, this logic can be moved there.
113+
//
114+
// Note: The Interpreter framework also sets the same default value (443) when processing Service,
115+
// so the backend will not encounter issues due to missing port information.
116+
if cc.Service.Port == nil {
117+
cc.Service.Port = ptr.To[int32](443)
118+
}
119+
allErrors = append(allErrors, webhook.ValidateWebhookService(fldPath.Child("clientConfig").Child("service"), cc.Service.Namespace, cc.Service.Name, cc.Service.Path, *cc.Service.Port)...)
107120
}
108121

109122
allErrors = append(allErrors, validateInterpreterContextVersions(hook.InterpreterContextVersions, fldPath.Child("interpreterContextVersions"))...)

pkg/webhook/configuration/validating_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,20 @@ func TestValidateWebhook(t *testing.T) {
271271
},
272272
expectedError: fmt.Sprintf("must include at least one of %v", strings.Join(acceptedInterpreterContextVersions, ", ")),
273273
},
274+
{
275+
name: "valid webhook configuration: use Service in ClientConfig but with port unspecified",
276+
hook: &configv1alpha1.ResourceInterpreterWebhook{
277+
Name: "workloads.karmada.io",
278+
ClientConfig: admissionregistrationv1.WebhookClientConfig{
279+
Service: &admissionregistrationv1.ServiceReference{
280+
Namespace: "default",
281+
Name: "svc",
282+
Path: strPtr("/interpreter"),
283+
},
284+
},
285+
InterpreterContextVersions: []string{"v1alpha1"},
286+
},
287+
},
274288
}
275289

276290
for _, test := range tests {

0 commit comments

Comments
 (0)