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

Preserve webhook namespaceSelector.matchLabels #2605

Merged
merged 2 commits into from
Oct 6, 2022
Merged
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
6 changes: 5 additions & 1 deletion webhook/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ func EnsureLabelSelectorExpressions(
}

if len(current.MatchExpressions) == 0 {
return want
return &metav1.LabelSelector{
MatchLabels: current.MatchLabels,
MatchExpressions: want.MatchExpressions,
}
}

var wantExpressions []metav1.LabelSelectorRequirement
Expand All @@ -44,6 +47,7 @@ func EnsureLabelSelectorExpressions(
}

return &metav1.LabelSelector{
MatchLabels: current.MatchLabels,
MatchExpressions: ensureLabelSelectorRequirements(
current.MatchExpressions, wantExpressions),
}
Expand Down
133 changes: 133 additions & 0 deletions webhook/resourcesemantics/validation/reconcile_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,139 @@ func TestReconcile(t *testing.T) {
}},
},
}},
}, {
Name: "secret and VWH exist, correcting namespaceSelector, preserving matchLabels",
Key: key,
Objects: []runtime.Object{secret, ns,
&admissionregistrationv1.ValidatingWebhookConfiguration{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Webhooks: []admissionregistrationv1.ValidatingWebhook{{
Name: name,
ClientConfig: admissionregistrationv1.WebhookClientConfig{
Service: &admissionregistrationv1.ServiceReference{
Namespace: system.Namespace(),
Name: "webhook",
// Path is fine.
Path: ptr.String(path),
},
// CABundle is fine.
CABundle: []byte("present"),
},
// Rules are fine.
Rules: expectedRules,
// NamespaceSelector contains non-knative things.
NamespaceSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"app.kubernetes.io/name": "knative-eventing",
},
MatchExpressions: []metav1.LabelSelectorRequirement{{
Key: "foo.knative.dev/exclude",
Operator: metav1.LabelSelectorOpDoesNotExist,
}, {
Key: "foo.bar/baz",
Operator: metav1.LabelSelectorOpDoesNotExist,
}},
},
}},
},
},
WantUpdates: []clientgotesting.UpdateActionImpl{{
Object: &admissionregistrationv1.ValidatingWebhookConfiguration{
ObjectMeta: metav1.ObjectMeta{
Name: name,
OwnerReferences: expectedOwnerReferences,
},
Webhooks: []admissionregistrationv1.ValidatingWebhook{{
Name: name,
ClientConfig: admissionregistrationv1.WebhookClientConfig{
Service: &admissionregistrationv1.ServiceReference{
Namespace: system.Namespace(),
Name: "webhook",
Path: ptr.String(path),
},
CABundle: []byte("present"),
},
Rules: expectedRules,
NamespaceSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"app.kubernetes.io/name": "knative-eventing",
},
// The knative key is added while the non-knative key is kept.
// Old knative key is removed.
MatchExpressions: []metav1.LabelSelectorRequirement{{
Key: "webhooks.knative.dev/exclude",
Operator: metav1.LabelSelectorOpDoesNotExist,
}, {
Key: "foo.bar/baz",
Operator: metav1.LabelSelectorOpDoesNotExist,
}},
},
}},
},
}},
}, {
Name: "secret and VWH exist, correcting namespaceSelector without existing matchExpressions, preserving matchLabels",
Key: key,
Objects: []runtime.Object{secret, ns,
&admissionregistrationv1.ValidatingWebhookConfiguration{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Webhooks: []admissionregistrationv1.ValidatingWebhook{{
Name: name,
ClientConfig: admissionregistrationv1.WebhookClientConfig{
Service: &admissionregistrationv1.ServiceReference{
Namespace: system.Namespace(),
Name: "webhook",
// Path is fine.
Path: ptr.String(path),
},
// CABundle is fine.
CABundle: []byte("present"),
},
// Rules are fine.
Rules: expectedRules,
// NamespaceSelector contains non-knative things.
NamespaceSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"app.kubernetes.io/name": "knative-eventing",
},
},
}},
},
},
WantUpdates: []clientgotesting.UpdateActionImpl{{
Object: &admissionregistrationv1.ValidatingWebhookConfiguration{
ObjectMeta: metav1.ObjectMeta{
Name: name,
OwnerReferences: expectedOwnerReferences,
},
Webhooks: []admissionregistrationv1.ValidatingWebhook{{
Name: name,
ClientConfig: admissionregistrationv1.WebhookClientConfig{
Service: &admissionregistrationv1.ServiceReference{
Namespace: system.Namespace(),
Name: "webhook",
Path: ptr.String(path),
},
CABundle: []byte("present"),
},
Rules: expectedRules,
NamespaceSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"app.kubernetes.io/name": "knative-eventing",
},
// The knative key is added
MatchExpressions: []metav1.LabelSelectorRequirement{{
Key: "webhooks.knative.dev/exclude",
Operator: metav1.LabelSelectorOpDoesNotExist,
}},
},
}},
},
}},
}}

table.Test(t, MakeFactory(func(ctx context.Context, listers *Listers, cmw configmap.Watcher) controller.Reconciler {
Expand Down