Skip to content

✨ Add NamespaceSelector to generated webhook configs #2076

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

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
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/utils/ptr"
Expand All @@ -29,6 +30,8 @@ import (
const (
tlsCrtPath = "tls.crt"
tlsKeyPath = "tls.key"

labelKubernetesNamespaceMetadataName = "kubernetes.io/metadata.name"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this right? Should it be metadata.namespace? Because this ought to be the name of the resource, not the namespace?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Unless you're looking at namespaces?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so. We're selecting for the name of namespace. In more recent kube versions, the label "kubernetes.io/metadata.name" started to be stamped out to allow for label selection on the resource name. So, if targetNamespaces = ["ns1"], it will limit the webhook to namespaces with label "kubernetes.io/metadata.name" in ["ns1"] => the namespace with name ns1. Or, am I misunderstanding your question?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think i may have been confused, but you're looking at namespace resources for this selector, so I think we're good.

)

// volume mount name -> mount path
Expand Down Expand Up @@ -291,6 +294,7 @@ func BundleValidatingWebhookResourceGenerator(rv1 *bundle.RegistryV1, opts rende

//nolint:prealloc
var objs []client.Object

for _, wh := range rv1.CSV.Spec.WebhookDefinitions {
if wh.Type != v1alpha1.ValidatingAdmissionWebhook {
continue
Expand Down Expand Up @@ -318,6 +322,9 @@ func BundleValidatingWebhookResourceGenerator(rv1 *bundle.RegistryV1, opts rende
Port: &wh.ContainerPort,
},
},
// It is safe to create a namespace selector even for cluster scoped CRs. A webhook
// is never skipped for cluster scoped CRs.
NamespaceSelector: getWebhookNamespaceSelector(opts.TargetNamespaces),
},
),
)
Expand Down Expand Up @@ -367,6 +374,9 @@ func BundleMutatingWebhookResourceGenerator(rv1 *bundle.RegistryV1, opts render.
},
},
ReinvocationPolicy: wh.ReinvocationPolicy,
// It is safe to create a namespace selector even for cluster scoped CRs. A webhook
// is never skipped for cluster scoped CRs.
NamespaceSelector: getWebhookNamespaceSelector(opts.TargetNamespaces),
},
),
)
Expand Down Expand Up @@ -535,3 +545,20 @@ func addCertVolumesToDeployment(dep *appsv1.Deployment, certSecretInfo render.Ce
)
}
}

// getWebhookNamespaceSelector returns a label selector that matches any namespace in targetNamespaces.
// If targetNamespaces is empty, nil, or includes "" (signifying all namespaces) nil is returned.
func getWebhookNamespaceSelector(targetNamespaces []string) *metav1.LabelSelector {
if len(targetNamespaces) > 0 && !slices.Contains(targetNamespaces, "") {
return &metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Key: labelKubernetesNamespaceMetadataName,
Operator: metav1.LabelSelectorOpIn,
Values: targetNamespaces,
},
},
}
}
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -1507,7 +1507,7 @@ func Test_BundleValidatingWebhookResourceGenerator_Succeeds(t *testing.T) {
},
opts: render.Options{
InstallNamespace: "install-namespace",
TargetNamespaces: []string{"watch-namespace-one", "watch-namespace-two"},
TargetNamespaces: []string{""},
},
expectedResources: []client.Object{
&admissionregistrationv1.ValidatingWebhookConfiguration{
Expand Down Expand Up @@ -1554,6 +1554,7 @@ func Test_BundleValidatingWebhookResourceGenerator_Succeeds(t *testing.T) {
Port: ptr.To(int32(443)),
},
},
// No NamespaceSelector is set targetNamespaces = []string{""} (AllNamespaces install mode)
},
},
},
Expand Down Expand Up @@ -1647,6 +1648,15 @@ func Test_BundleValidatingWebhookResourceGenerator_Succeeds(t *testing.T) {
Port: ptr.To(int32(443)),
},
},
NamespaceSelector: &metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Key: "kubernetes.io/metadata.name",
Operator: metav1.LabelSelectorOpIn,
Values: []string{"watch-namespace-one", "watch-namespace-two"},
},
},
},
},
},
},
Expand Down Expand Up @@ -1694,6 +1704,15 @@ func Test_BundleValidatingWebhookResourceGenerator_Succeeds(t *testing.T) {
Port: ptr.To(int32(443)),
},
},
NamespaceSelector: &metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Key: "kubernetes.io/metadata.name",
Operator: metav1.LabelSelectorOpIn,
Values: []string{"watch-namespace-one", "watch-namespace-two"},
},
},
},
},
},
},
Expand Down Expand Up @@ -1772,7 +1791,7 @@ func Test_BundleMutatingWebhookResourceGenerator_Succeeds(t *testing.T) {
},
opts: render.Options{
InstallNamespace: "install-namespace",
TargetNamespaces: []string{"watch-namespace-one", "watch-namespace-two"},
TargetNamespaces: []string{""},
},
expectedResources: []client.Object{
&admissionregistrationv1.MutatingWebhookConfiguration{
Expand Down Expand Up @@ -1820,6 +1839,7 @@ func Test_BundleMutatingWebhookResourceGenerator_Succeeds(t *testing.T) {
Port: ptr.To(int32(443)),
},
},
// No NamespaceSelector is set targetNamespaces = []string{""} (AllNamespaces install mode)
},
},
},
Expand Down Expand Up @@ -1915,6 +1935,15 @@ func Test_BundleMutatingWebhookResourceGenerator_Succeeds(t *testing.T) {
Port: ptr.To(int32(443)),
},
},
NamespaceSelector: &metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Key: "kubernetes.io/metadata.name",
Operator: metav1.LabelSelectorOpIn,
Values: []string{"watch-namespace-one", "watch-namespace-two"},
},
},
},
},
},
},
Expand Down Expand Up @@ -1962,6 +1991,15 @@ func Test_BundleMutatingWebhookResourceGenerator_Succeeds(t *testing.T) {
Port: ptr.To(int32(443)),
},
},
NamespaceSelector: &metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Key: "kubernetes.io/metadata.name",
Operator: metav1.LabelSelectorOpIn,
Values: []string{"watch-namespace-one", "watch-namespace-two"},
},
},
},
},
},
},
Expand Down
Loading