Skip to content

Commit d101484

Browse files
perdasilvaPer Goncalves da Silva
andauthored
✨ Add NamespaceSelector to generated webhook configs (#2076)
Signed-off-by: Per Goncalves da Silva <pegoncal@redhat.com> Co-authored-by: Per Goncalves da Silva <pegoncal@redhat.com>
1 parent 7bde7c9 commit d101484

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

internal/operator-controller/rukpak/render/registryv1/generators/generators.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
corev1 "k8s.io/api/core/v1"
1414
rbacv1 "k8s.io/api/rbac/v1"
1515
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
16+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1617
"k8s.io/apimachinery/pkg/util/intstr"
1718
"k8s.io/apimachinery/pkg/util/sets"
1819
"k8s.io/utils/ptr"
@@ -29,6 +30,8 @@ import (
2930
const (
3031
tlsCrtPath = "tls.crt"
3132
tlsKeyPath = "tls.key"
33+
34+
labelKubernetesNamespaceMetadataName = "kubernetes.io/metadata.name"
3235
)
3336

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

292295
//nolint:prealloc
293296
var objs []client.Object
297+
294298
for _, wh := range rv1.CSV.Spec.WebhookDefinitions {
295299
if wh.Type != v1alpha1.ValidatingAdmissionWebhook {
296300
continue
@@ -318,6 +322,9 @@ func BundleValidatingWebhookResourceGenerator(rv1 *bundle.RegistryV1, opts rende
318322
Port: &wh.ContainerPort,
319323
},
320324
},
325+
// It is safe to create a namespace selector even for cluster scoped CRs. A webhook
326+
// is never skipped for cluster scoped CRs.
327+
NamespaceSelector: getWebhookNamespaceSelector(opts.TargetNamespaces),
321328
},
322329
),
323330
)
@@ -367,6 +374,9 @@ func BundleMutatingWebhookResourceGenerator(rv1 *bundle.RegistryV1, opts render.
367374
},
368375
},
369376
ReinvocationPolicy: wh.ReinvocationPolicy,
377+
// It is safe to create a namespace selector even for cluster scoped CRs. A webhook
378+
// is never skipped for cluster scoped CRs.
379+
NamespaceSelector: getWebhookNamespaceSelector(opts.TargetNamespaces),
370380
},
371381
),
372382
)
@@ -535,3 +545,20 @@ func addCertVolumesToDeployment(dep *appsv1.Deployment, certSecretInfo render.Ce
535545
)
536546
}
537547
}
548+
549+
// getWebhookNamespaceSelector returns a label selector that matches any namespace in targetNamespaces.
550+
// If targetNamespaces is empty, nil, or includes "" (signifying all namespaces) nil is returned.
551+
func getWebhookNamespaceSelector(targetNamespaces []string) *metav1.LabelSelector {
552+
if len(targetNamespaces) > 0 && !slices.Contains(targetNamespaces, "") {
553+
return &metav1.LabelSelector{
554+
MatchExpressions: []metav1.LabelSelectorRequirement{
555+
{
556+
Key: labelKubernetesNamespaceMetadataName,
557+
Operator: metav1.LabelSelectorOpIn,
558+
Values: targetNamespaces,
559+
},
560+
},
561+
}
562+
}
563+
return nil
564+
}

internal/operator-controller/rukpak/render/registryv1/generators/generators_test.go

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,7 +1507,7 @@ func Test_BundleValidatingWebhookResourceGenerator_Succeeds(t *testing.T) {
15071507
},
15081508
opts: render.Options{
15091509
InstallNamespace: "install-namespace",
1510-
TargetNamespaces: []string{"watch-namespace-one", "watch-namespace-two"},
1510+
TargetNamespaces: []string{""},
15111511
},
15121512
expectedResources: []client.Object{
15131513
&admissionregistrationv1.ValidatingWebhookConfiguration{
@@ -1554,6 +1554,7 @@ func Test_BundleValidatingWebhookResourceGenerator_Succeeds(t *testing.T) {
15541554
Port: ptr.To(int32(443)),
15551555
},
15561556
},
1557+
// No NamespaceSelector is set targetNamespaces = []string{""} (AllNamespaces install mode)
15571558
},
15581559
},
15591560
},
@@ -1647,6 +1648,15 @@ func Test_BundleValidatingWebhookResourceGenerator_Succeeds(t *testing.T) {
16471648
Port: ptr.To(int32(443)),
16481649
},
16491650
},
1651+
NamespaceSelector: &metav1.LabelSelector{
1652+
MatchExpressions: []metav1.LabelSelectorRequirement{
1653+
{
1654+
Key: "kubernetes.io/metadata.name",
1655+
Operator: metav1.LabelSelectorOpIn,
1656+
Values: []string{"watch-namespace-one", "watch-namespace-two"},
1657+
},
1658+
},
1659+
},
16501660
},
16511661
},
16521662
},
@@ -1694,6 +1704,15 @@ func Test_BundleValidatingWebhookResourceGenerator_Succeeds(t *testing.T) {
16941704
Port: ptr.To(int32(443)),
16951705
},
16961706
},
1707+
NamespaceSelector: &metav1.LabelSelector{
1708+
MatchExpressions: []metav1.LabelSelectorRequirement{
1709+
{
1710+
Key: "kubernetes.io/metadata.name",
1711+
Operator: metav1.LabelSelectorOpIn,
1712+
Values: []string{"watch-namespace-one", "watch-namespace-two"},
1713+
},
1714+
},
1715+
},
16971716
},
16981717
},
16991718
},
@@ -1772,7 +1791,7 @@ func Test_BundleMutatingWebhookResourceGenerator_Succeeds(t *testing.T) {
17721791
},
17731792
opts: render.Options{
17741793
InstallNamespace: "install-namespace",
1775-
TargetNamespaces: []string{"watch-namespace-one", "watch-namespace-two"},
1794+
TargetNamespaces: []string{""},
17761795
},
17771796
expectedResources: []client.Object{
17781797
&admissionregistrationv1.MutatingWebhookConfiguration{
@@ -1820,6 +1839,7 @@ func Test_BundleMutatingWebhookResourceGenerator_Succeeds(t *testing.T) {
18201839
Port: ptr.To(int32(443)),
18211840
},
18221841
},
1842+
// No NamespaceSelector is set targetNamespaces = []string{""} (AllNamespaces install mode)
18231843
},
18241844
},
18251845
},
@@ -1915,6 +1935,15 @@ func Test_BundleMutatingWebhookResourceGenerator_Succeeds(t *testing.T) {
19151935
Port: ptr.To(int32(443)),
19161936
},
19171937
},
1938+
NamespaceSelector: &metav1.LabelSelector{
1939+
MatchExpressions: []metav1.LabelSelectorRequirement{
1940+
{
1941+
Key: "kubernetes.io/metadata.name",
1942+
Operator: metav1.LabelSelectorOpIn,
1943+
Values: []string{"watch-namespace-one", "watch-namespace-two"},
1944+
},
1945+
},
1946+
},
19181947
},
19191948
},
19201949
},
@@ -1962,6 +1991,15 @@ func Test_BundleMutatingWebhookResourceGenerator_Succeeds(t *testing.T) {
19621991
Port: ptr.To(int32(443)),
19631992
},
19641993
},
1994+
NamespaceSelector: &metav1.LabelSelector{
1995+
MatchExpressions: []metav1.LabelSelectorRequirement{
1996+
{
1997+
Key: "kubernetes.io/metadata.name",
1998+
Operator: metav1.LabelSelectorOpIn,
1999+
Values: []string{"watch-namespace-one", "watch-namespace-two"},
2000+
},
2001+
},
2002+
},
19652003
},
19662004
},
19672005
},

0 commit comments

Comments
 (0)