Skip to content

Commit 46bf40c

Browse files
authored
Merge pull request #3206 from wfernandes/cert-manager-sideEffects
🐛 Set sideEffects:None for cert-manager MutatingWebhookConfiguration
2 parents 9e56210 + b74df8d commit 46bf40c

File tree

3 files changed

+107
-4
lines changed

3 files changed

+107
-4
lines changed

cmd/clusterctl/client/cluster/cert_manager_test.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,111 @@ import (
2121
"time"
2222

2323
. "github.com/onsi/gomega"
24+
admissionregistration "k8s.io/api/admissionregistration/v1"
25+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2426
"k8s.io/apimachinery/pkg/util/wait"
2527
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/config"
28+
"sigs.k8s.io/cluster-api/cmd/clusterctl/internal/scheme"
2629
"sigs.k8s.io/cluster-api/cmd/clusterctl/internal/test"
2730
)
2831

32+
func Test_certManagerClient_getManifestObjects(t *testing.T) {
33+
34+
tests := []struct {
35+
name string
36+
expectErr bool
37+
assert func(*testing.T, []unstructured.Unstructured)
38+
}{
39+
{
40+
name: "it should not contain the cert-manager-leaderelection ClusterRoleBinding",
41+
expectErr: false,
42+
assert: func(t *testing.T, objs []unstructured.Unstructured) {
43+
for _, o := range objs {
44+
if o.GetKind() == "ClusterRoleBinding" && o.GetName() == "cert-manager-leaderelection" {
45+
t.Error("should not find cert-manager-leaderelection ClusterRoleBinding")
46+
}
47+
}
48+
},
49+
},
50+
{
51+
name: "the MutatingWebhookConfiguration should have sideEffects set to None ",
52+
expectErr: false,
53+
assert: func(t *testing.T, objs []unstructured.Unstructured) {
54+
found := false
55+
for i := range objs {
56+
o := objs[i]
57+
if o.GetKind() == "MutatingWebhookConfiguration" && o.GetName() == "cert-manager-webhook" {
58+
w := &admissionregistration.MutatingWebhookConfiguration{}
59+
err := scheme.Scheme.Convert(&o, w, nil)
60+
if err != nil {
61+
t.Errorf("did not expect err, got %s", err)
62+
}
63+
if len(w.Webhooks) != 1 {
64+
t.Error("expected 1 webhook to be configured")
65+
}
66+
wh := w.Webhooks[0]
67+
if wh.SideEffects != nil && *wh.SideEffects == admissionregistration.SideEffectClassNone {
68+
found = true
69+
}
70+
}
71+
}
72+
if !found {
73+
t.Error("Expected to find cert-manager-webhook MutatingWebhookConfiguration with sideEffects=None")
74+
}
75+
},
76+
},
77+
{
78+
name: "the ValidatingWebhookConfiguration should have sideEffects set to None ",
79+
expectErr: false,
80+
assert: func(t *testing.T, objs []unstructured.Unstructured) {
81+
found := false
82+
for i := range objs {
83+
o := objs[i]
84+
if o.GetKind() == "ValidatingWebhookConfiguration" && o.GetName() == "cert-manager-webhook" {
85+
w := &admissionregistration.ValidatingWebhookConfiguration{}
86+
err := scheme.Scheme.Convert(&o, w, nil)
87+
if err != nil {
88+
t.Errorf("did not expect err, got %s", err)
89+
}
90+
if len(w.Webhooks) != 1 {
91+
t.Error("expected 1 webhook to be configured")
92+
}
93+
wh := w.Webhooks[0]
94+
if wh.SideEffects != nil && *wh.SideEffects == admissionregistration.SideEffectClassNone {
95+
found = true
96+
}
97+
}
98+
}
99+
if !found {
100+
t.Error("Expected to find cert-manager-webhook ValidatingWebhookConfiguration with sideEffects=None")
101+
}
102+
},
103+
},
104+
}
105+
106+
for _, tt := range tests {
107+
t.Run(tt.name, func(t *testing.T) {
108+
g := NewWithT(t)
109+
110+
pollImmediateWaiter := func(interval, timeout time.Duration, condition wait.ConditionFunc) error {
111+
return nil
112+
}
113+
fakeConfigClient := newFakeConfig("")
114+
115+
cm := newCertMangerClient(fakeConfigClient, nil, pollImmediateWaiter)
116+
objs, err := cm.getManifestObjs()
117+
118+
if tt.expectErr {
119+
g.Expect(err).To(HaveOccurred())
120+
return
121+
}
122+
g.Expect(err).ToNot(HaveOccurred())
123+
tt.assert(t, objs)
124+
})
125+
}
126+
127+
}
128+
29129
func Test_GetTimeout(t *testing.T) {
30130

31131
pollImmediateWaiter := func(interval, timeout time.Duration, condition wait.ConditionFunc) error {

cmd/clusterctl/config/assets/cert-manager.yaml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# This is manually edited cert-manager v0.11.0 release.
2-
# We've manually removed the ClusterRoleBinding cert-manager-leaderelection.
3-
# OTHER NOTES:
2+
# DIFFERENCES:
3+
# 1. We've manually removed the ClusterRoleBinding cert-manager-leaderelection.
44
# See https://github.com/kubernetes-sigs/cluster-api/issues/2928
55
# See https://github.com/jetstack/cert-manager/pull/2207
6+
# 2. Added `sideEffects: None` to MutatingWebhookConfiguration.
7+
# See https://github.com/kubernetes-sigs/cluster-api/issues/3204
68
---
79
apiVersion: apiextensions.k8s.io/v1beta1
810
kind: CustomResourceDefinition
@@ -6271,6 +6273,7 @@ webhooks:
62716273
- challenges
62726274
- certificaterequests
62736275
failurePolicy: Fail
6276+
sideEffects: None
62746277
clientConfig:
62756278
service:
62766279
name: kubernetes

cmd/clusterctl/config/zz_generated.bindata.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)