Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automated cherry pick of #5960: Fix panic when validating ResourceInterpreterWebhookConfiguration with unspecified service port #5966

Open
wants to merge 1 commit into
base: release-1.11
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion pkg/webhook/configuration/validating.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -103,7 +104,19 @@ 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)...)
// Temporary fix: If the service port is not specified, set a default value of 443.
// This is a workaround to prevent a panic when validating ResourceInterpreterWebhookConfiguration
// with an unspecified service port. Ideally, this logic should be handled by a MutatingWebhook,
// but introducing a MutatingWebhook at this stage would require significant changes and is not
// convenient for cherry-picking to release branches. Therefore, we are temporarily setting the
// default port here. Once a MutatingWebhook is implemented, this logic can be moved there.
//
// Note: The Interpreter framework also sets the same default value (443) when processing Service,
// so the backend will not encounter issues due to missing port information.
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"))...)
Expand Down
14 changes: 14 additions & 0 deletions pkg/webhook/configuration/validating_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 but with port unspecified",
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 {
Expand Down