Skip to content

Commit fa57f32

Browse files
committed
[processor/webhook] Replace app's namespace in webhook NamespaeSelectors
1 parent e57c93d commit fa57f32

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

pkg/processor/webhook/mutating.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/arttor/helmify/pkg/helmify"
1010
v1 "k8s.io/api/admissionregistration/v1"
11+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1112
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1213
"k8s.io/apimachinery/pkg/runtime"
1314
"k8s.io/apimachinery/pkg/runtime/schema"
@@ -55,6 +56,7 @@ func (w mwh) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstructured
5556
for i, whc := range whConf.Webhooks {
5657
whc.ClientConfig.Service.Name = appMeta.TemplatedName(whc.ClientConfig.Service.Name)
5758
whc.ClientConfig.Service.Namespace = strings.ReplaceAll(whc.ClientConfig.Service.Namespace, appMeta.Namespace(), `{{ .Release.Namespace }}`)
59+
mutateNamespaceSelector(appMeta, whc.NamespaceSelector)
5860
whConf.Webhooks[i] = whc
5961
}
6062
webhooks, _ := yaml.Marshal(whConf.Webhooks)
@@ -98,3 +100,26 @@ func (r *mwhResult) Write(writer io.Writer) error {
98100
_, err := writer.Write(r.data)
99101
return err
100102
}
103+
104+
const (
105+
nameLabel = "kubernetes.io/metadata.name"
106+
namespaceTemplate = "{{ .Release.Namespace }}"
107+
)
108+
109+
// Replace the relase namespace in a namespace selector
110+
func mutateNamespaceSelector(appMeta helmify.AppMetadata, sel *metav1.LabelSelector) {
111+
if sel == nil {
112+
return
113+
}
114+
origNamespace := appMeta.Namespace()
115+
for i, me := range sel.MatchExpressions {
116+
if me.Key == nameLabel {
117+
for vi, v := range me.Values {
118+
if v == origNamespace {
119+
me.Values[vi] = namespaceTemplate
120+
}
121+
}
122+
sel.MatchExpressions[i] = me
123+
}
124+
}
125+
}

pkg/processor/webhook/mutating_test.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package webhook
33
import (
44
"testing"
55

6+
"github.com/arttor/helmify/pkg/config"
7+
"github.com/arttor/helmify/pkg/helmify"
68
"github.com/arttor/helmify/pkg/metadata"
79

810
"github.com/arttor/helmify/internal"
@@ -26,6 +28,14 @@ webhooks:
2628
path: /mutate-ceph-example-com-v1alpha1-volume
2729
failurePolicy: Fail
2830
name: vvolume.kb.io
31+
namespaceSelector:
32+
matchExpressions:
33+
- key: kubernetes.io/metadata.name
34+
operator: NotIn
35+
values:
36+
- namespace-1
37+
- my-operator-system
38+
- namespace-3
2939
rules:
3040
- apiGroups:
3141
- test.example.com
@@ -43,7 +53,7 @@ func Test_mwh_Process(t *testing.T) {
4353

4454
t.Run("processed", func(t *testing.T) {
4555
obj := internal.GenerateObj(mwhYaml)
46-
processed, _, err := testInstance.Process(&metadata.Service{}, obj)
56+
processed, _, err := testInstance.Process(testAppMetaWithNamespace(), obj)
4757
assert.NoError(t, err)
4858
assert.Equal(t, true, processed)
4959
})
@@ -54,3 +64,14 @@ func Test_mwh_Process(t *testing.T) {
5464
assert.Equal(t, false, processed)
5565
})
5666
}
67+
68+
func testAppMetaWithNamespace() helmify.AppMetadata {
69+
// Create an empty meta Service and load a dummy namespaced object.
70+
am := metadata.New(config.Config{})
71+
am.Load(internal.GenerateObj(`apiVersion: v1
72+
kind: Service
73+
metadata:
74+
name: my-operator-controller-manager-metrics-service
75+
namespace: my-operator-system`))
76+
return am
77+
}

pkg/processor/webhook/validating.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func (w vwh) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstructured
5555
for i, whc := range whConf.Webhooks {
5656
whc.ClientConfig.Service.Name = appMeta.TemplatedName(whc.ClientConfig.Service.Name)
5757
whc.ClientConfig.Service.Namespace = strings.ReplaceAll(whc.ClientConfig.Service.Namespace, appMeta.Namespace(), `{{ .Release.Namespace }}`)
58+
mutateNamespaceSelector(appMeta, whc.NamespaceSelector)
5859
whConf.Webhooks[i] = whc
5960
}
6061
webhooks, _ := yaml.Marshal(whConf.Webhooks)

pkg/processor/webhook/validating_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ webhooks:
2626
path: /validate-ceph-example-com-v1alpha1-volume
2727
failurePolicy: Fail
2828
name: vvolume.kb.io
29+
namespaceSelector:
30+
matchExpressions:
31+
- key: kubernetes.io/metadata.name
32+
operator: NotIn
33+
values:
34+
- namespace-1
35+
- my-operator-system
36+
- namespace-3
2937
rules:
3038
- apiGroups:
3139
- test.example.com
@@ -43,7 +51,7 @@ func Test_vwh_Process(t *testing.T) {
4351

4452
t.Run("processed", func(t *testing.T) {
4553
obj := internal.GenerateObj(vwhYaml)
46-
processed, _, err := testInstance.Process(&metadata.Service{}, obj)
54+
processed, _, err := testInstance.Process(testAppMetaWithNamespace(), obj)
4755
assert.NoError(t, err)
4856
assert.Equal(t, true, processed)
4957
})

0 commit comments

Comments
 (0)