@@ -48,15 +48,32 @@ var _ = Describe("Managed Cluster Validating Webhooks", Ordered, func() {
48
48
testNamespace * v1.Namespace
49
49
)
50
50
const (
51
- namespaceName = "openshift-validation-webhook"
52
- serviceName = "validation-webhook"
53
- daemonsetName = "validation-webhook"
54
- configMapName = "webhook-cert"
55
- secretName = "webhook-cert"
56
- saName = "webhook-sa"
57
- testNsName = "osde2e-temp-ns"
51
+ namespaceName = "openshift-validation-webhook"
52
+ serviceName = "validation-webhook"
53
+ daemonsetName = "validation-webhook"
54
+ configMapName = "webhook-cert"
55
+ secretName = "webhook-cert"
56
+ testNsName = "osde2e-temp-ns"
57
+ privilegedNamespace = "openshift-backplane"
58
+ unprivilegedNamespace = "openshift-logging"
58
59
)
59
60
61
+ createNS := func (ns string ) {
62
+ testNamespace = & v1.Namespace {ObjectMeta : metav1.ObjectMeta {Name : ns }}
63
+ err := client .Create (context .TODO (), testNamespace )
64
+ By ("checking the custom namespace exists" )
65
+ err = wait .For (conditions .New (client .Resources ).ResourceMatch (testNamespace , func (object k8s.Object ) bool {
66
+ return true
67
+ }))
68
+ Expect (err ).ShouldNot (HaveOccurred (), "Unable to create test namespace" )
69
+ }
70
+
71
+ deleteNS := func (ns * v1.Namespace ) {
72
+ err := client .Delete (context .TODO (), ns )
73
+ err = wait .For (conditions .New (client .Resources ).ResourceDeleted (ns ))
74
+ Expect (err ).ShouldNot (HaveOccurred (), "Unable to delete test namespace" )
75
+ }
76
+
60
77
BeforeAll (func () {
61
78
log .SetLogger (GinkgoLogr )
62
79
var err error
@@ -106,7 +123,7 @@ var _ = Describe("Managed Cluster Validating Webhooks", Ordered, func() {
106
123
Expect (err ).ToNot (HaveOccurred ())
107
124
})
108
125
109
- It ("should create a pod with the correct security context" , func () {
126
+ It ("should create a pod with the correct security context" , func (ctx context. Context ) {
110
127
pod := & v1.Pod {
111
128
ObjectMeta : metav1.ObjectMeta {
112
129
Name : "testpod" ,
@@ -132,27 +149,25 @@ var _ = Describe("Managed Cluster Validating Webhooks", Ordered, func() {
132
149
},
133
150
}
134
151
135
- err := client .Create (context .TODO (), pod )
152
+ err := client .Create (ctx , pod )
153
+ Expect (err ).NotTo (HaveOccurred ())
154
+ err = client .Delete (ctx , pod )
136
155
Expect (err ).NotTo (HaveOccurred ())
137
156
})
138
157
139
158
Describe ("sre-pod-validation" , Ordered , func () {
140
159
const (
141
- privilegedNamespace = "openshift-backplane"
142
- unprivilegedNamespace = "openshift-logging"
143
-
144
160
deletePodWaitDuration = 5 * time .Minute
145
161
createPodWaitDuration = 1 * time .Minute
146
162
)
147
163
148
164
var pod * v1.Pod
149
165
150
166
BeforeAll (func () {
151
- name := envconf .RandomName ("testpod" , 12 )
152
167
pod = & v1.Pod {
153
168
ObjectMeta : metav1.ObjectMeta {
154
- Name : name ,
155
- Namespace : testNsName ,
169
+ Name : envconf . RandomName ( "testpod" , 12 ) ,
170
+ Namespace : privilegedNamespace ,
156
171
},
157
172
Spec : v1.PodSpec {
158
173
Containers : []v1.Container {
@@ -201,42 +216,31 @@ var _ = Describe("Managed Cluster Validating Webhooks", Ordered, func() {
201
216
}
202
217
})
203
218
204
- withNamespace := func (pod * v1.Pod , namespace string ) * v1.Pod {
205
- pod .SetNamespace (namespace )
206
- return pod
207
- }
208
-
209
219
It ("blocks pods scheduled onto master/infra nodes" , func (ctx context.Context ) {
210
- err := dedicatedAdmink8s .Create (ctx , withNamespace ( pod , privilegedNamespace ) )
220
+ err := dedicatedAdmink8s .Create (ctx , pod )
211
221
Expect (errors .IsForbidden (err )).To (BeTrue ())
212
222
213
- err = userk8s .Create (ctx , withNamespace ( pod , privilegedNamespace ) )
223
+ err = userk8s .Create (ctx , pod )
214
224
Expect (errors .IsForbidden (err )).To (BeTrue ())
215
225
216
- err = userk8s .Create (ctx , withNamespace (pod , unprivilegedNamespace ))
226
+ pod .SetNamespace (unprivilegedNamespace )
227
+ err = userk8s .Create (ctx , pod )
217
228
Expect (errors .IsForbidden (err )).To (BeTrue ())
229
+ pod .SetNamespace (privilegedNamespace )
218
230
}, SpecTimeout (createPodWaitDuration .Seconds ()+ deletePodWaitDuration .Seconds ()))
219
231
220
232
It ("allows cluster-admin to schedule pods onto master/infra nodes" , func (ctx context.Context ) {
221
- sa := & v1.ServiceAccount {}
222
-
223
- err := client .Get (ctx , saName , namespaceName , sa )
224
-
225
- if err == nil {
226
- err = client .Delete (ctx , sa )
227
- Expect (err ).ToNot (HaveOccurred (), "Failed to delete existing Service Account" )
228
- }
229
-
230
- sa = & v1.ServiceAccount {
233
+ sa := & v1.ServiceAccount {
231
234
ObjectMeta : metav1.ObjectMeta {
232
- Name : saName ,
235
+ Name : "webhook-sa" ,
233
236
Namespace : namespaceName ,
234
237
},
235
238
}
236
- err = client .Create (ctx , sa )
239
+ err : = client .Create (ctx , sa )
237
240
Expect (err ).ShouldNot (HaveOccurred (), "Unable to create service account" )
241
+ err = client .Delete (ctx , sa )
242
+ Expect (err ).ShouldNot (HaveOccurred (), "Unable to delete service account" )
238
243
239
- pod = withNamespace (pod , privilegedNamespace )
240
244
err = client .Create (ctx , pod )
241
245
Expect (err ).NotTo (HaveOccurred ())
242
246
err = client .Delete (ctx , pod )
@@ -320,9 +324,11 @@ var _ = Describe("Managed Cluster Validating Webhooks", Ordered, func() {
320
324
)
321
325
322
326
BeforeAll (func (ctx context.Context ) {
323
- testNamespace = & v1.Namespace {ObjectMeta : metav1.ObjectMeta {Name : testNsName }}
324
- err := client .Create (ctx , testNamespace )
325
- Expect (err ).ShouldNot (HaveOccurred (), "Unable to create test namespace" )
327
+ createNS (testNsName )
328
+ })
329
+
330
+ AfterAll (func (ctx context.Context ) {
331
+ deleteNS (testNamespace )
326
332
})
327
333
328
334
It ("only blocks configmap/user-ca-bundle changes" , func (ctx context.Context ) {
@@ -521,7 +527,7 @@ var _ = Describe("Managed Cluster Validating Webhooks", Ordered, func() {
521
527
Expect (err ).NotTo (HaveOccurred ())
522
528
}
523
529
524
- updateNamespace := func (ctx context.Context , name string , user string , groups ... string ) error {
530
+ updateNamespace := func (ctx context.Context , name , user string , groups ... string ) error {
525
531
userk8s , err := client .Impersonate (user , groups ... )
526
532
if err != nil {
527
533
return err
@@ -585,9 +591,6 @@ var _ = Describe("Managed Cluster Validating Webhooks", Ordered, func() {
585
591
})
586
592
587
593
Describe ("sre-prometheusrule-validation" , func () {
588
- const privilegedNamespace = "openshift-backplane"
589
- const unprivilegedNamespace = "openshift-logging"
590
-
591
594
newPrometheusRule := func (namespace string ) * monitoringv1.PrometheusRule {
592
595
return & monitoringv1.PrometheusRule {
593
596
ObjectMeta : metav1.ObjectMeta {Name : "prometheus-example-app" , Namespace : namespace },
@@ -613,10 +616,11 @@ var _ = Describe("Managed Cluster Validating Webhooks", Ordered, func() {
613
616
rule := newPrometheusRule (privilegedNamespace )
614
617
err = client .Delete (ctx , rule )
615
618
Expect (err == nil || errors .IsNotFound (err )).To (BeTrue (), "Failed to ensure PrometheusRule deletion" )
619
+ createNS (testNsName )
616
620
})
617
621
618
622
AfterAll (func (ctx context.Context ) {
619
- client . Delete ( ctx , testNamespace )
623
+ deleteNS ( testNamespace )
620
624
})
621
625
622
626
DescribeTable (
@@ -650,7 +654,7 @@ var _ = Describe("Managed Cluster Validating Webhooks", Ordered, func() {
650
654
})
651
655
652
656
It ("allows non-privileged users to manage PrometheusRules in non-privileged namespaces" , func (ctx context.Context ) {
653
- rule := newPrometheusRule ("osde2e-temp-ns" )
657
+ rule := newPrometheusRule (testNsName )
654
658
655
659
err := dedicatedAdmink8s .Create (ctx , rule )
656
660
Expect (err ).NotTo (HaveOccurred ())
0 commit comments