Skip to content

Commit f8d6d02

Browse files
🌱 improve e2e tests to ensure that pods are restricted
1 parent 3dca1f7 commit f8d6d02

File tree

3 files changed

+60
-20
lines changed

3 files changed

+60
-20
lines changed

test/e2e/utils/test_context.go

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,17 @@ import (
3333
// TestContext specified to run e2e tests
3434
type TestContext struct {
3535
*CmdContext
36-
TestSuffix string
37-
Domain string
38-
Group string
39-
Version string
40-
Kind string
41-
Resources string
42-
ImageName string
43-
BinaryName string
44-
Kubectl *Kubectl
45-
K8sVersion *KubernetesVersion
36+
TestSuffix string
37+
Domain string
38+
Group string
39+
Version string
40+
Kind string
41+
Resources string
42+
ImageName string
43+
BinaryName string
44+
Kubectl *Kubectl
45+
K8sVersion *KubernetesVersion
46+
IsRestricted bool
4647
}
4748

4849
// NewTestContext init with a random suffix for test TestContext stuff,
@@ -262,6 +263,22 @@ func (t *TestContext) Destroy() {
262263
}
263264
}
264265

266+
// CreateManagerNamespace will create the namespace where the manager is deployed
267+
func (t *TestContext) CreateManagerNamespace() error {
268+
_, err := t.Kubectl.Command("create", "ns", t.Kubectl.Namespace)
269+
return err
270+
}
271+
272+
// LabelAllNamespacesToWarnAboutRestricted will label all namespaces so that we can verify
273+
// if a warning with `Warning: would violate PodSecurity` will be raised when the manifests are applied
274+
func (t *TestContext) LabelAllNamespacesToWarnAboutRestricted() error {
275+
_, err := t.Kubectl.Command("label", "--overwrite", "ns", "--all",
276+
"pod-security.kubernetes.io/audit=restricted",
277+
"pod-security.kubernetes.io/enforce-version=v1.24",
278+
"pod-security.kubernetes.io/warn=restricted")
279+
return err
280+
}
281+
265282
// LoadImageToKindCluster loads a local docker image to the kind cluster
266283
func (t *TestContext) LoadImageToKindCluster() error {
267284
cluster := "kind"

test/e2e/v3/generate_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ Count int `+"`"+`json:"count,omitempty"`+"`"+`
130130
}
131131

132132
// GenerateV3 implements a go/v3(-alpha) plugin project defined by a TestContext.
133-
func GenerateV3(kbc *utils.TestContext, crdAndWebhookVersion string, restrictive bool) {
133+
func GenerateV3(kbc *utils.TestContext, crdAndWebhookVersion string) {
134134
var err error
135135

136136
By("initializing a project")
@@ -229,7 +229,7 @@ Count int `+"`"+`json:"count,omitempty"`+"`"+`
229229
_ = pluginutil.RunCmd("Update dependencies", "go", "mod", "tidy")
230230
}
231231

232-
if restrictive {
232+
if kbc.IsRestricted {
233233
By("uncomment kustomize files to ensure that pods are restricted")
234234
uncommentPodStandards(kbc)
235235
}
@@ -252,7 +252,7 @@ func uncommentPodStandards(kbc *utils.TestContext) {
252252
}
253253

254254
// GenerateV3 implements a go/v3(-alpha) plugin project defined by a TestContext.
255-
func GenerateV3WithKustomizeV2(kbc *utils.TestContext, crdAndWebhookVersion string, restrictive bool) {
255+
func GenerateV3WithKustomizeV2(kbc *utils.TestContext, crdAndWebhookVersion string) {
256256
var err error
257257

258258
By("initializing a project")
@@ -418,4 +418,8 @@ Count int `+"`"+`json:"count,omitempty"`+"`"+`
418418
# index: 1
419419
# create: true`, "#")).To(Succeed())
420420

421+
if kbc.IsRestricted {
422+
By("uncomment kustomize files to ensure that pods are restricted")
423+
uncommentPodStandards(kbc)
424+
}
421425
}

test/e2e/v3/plugin_cluster_test.go

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"encoding/json"
2121
"fmt"
2222
"os"
23+
"os/exec"
2324
"path/filepath"
2425
"strconv"
2526
"strings"
@@ -127,7 +128,7 @@ var _ = Describe("kubebuilder", func() {
127128
srvVer.GitVersion))
128129
}
129130

130-
GenerateV3(kbc, "v1", false)
131+
GenerateV3(kbc, "v1")
131132
Run(kbc)
132133
})
133134
It("should generate a runnable project with the golang base plugin v3 and kustomize v4-alpha", func() {
@@ -136,7 +137,7 @@ var _ = Describe("kubebuilder", func() {
136137
Skip(fmt.Sprintf("cluster version %s does not support v1 CRDs or webhooks",
137138
srvVer.GitVersion))
138139
}
139-
GenerateV3WithKustomizeV2(kbc, "v1", false)
140+
GenerateV3WithKustomizeV2(kbc, "v1")
140141
Run(kbc)
141142
})
142143
It("should generate a runnable project with v1beta1 CRDs and Webhooks", func() {
@@ -148,7 +149,7 @@ var _ = Describe("kubebuilder", func() {
148149
srvVer.GitVersion))
149150
}
150151

151-
GenerateV3(kbc, "v1beta1", false)
152+
GenerateV3(kbc, "v1beta1")
152153
Run(kbc)
153154
})
154155

@@ -161,7 +162,8 @@ var _ = Describe("kubebuilder", func() {
161162
"and securityContext.seccompProfile", srvVer.GitVersion))
162163
}
163164

164-
GenerateV3(kbc, "v1", true)
165+
kbc.IsRestricted = true
166+
GenerateV3(kbc, "v1")
165167
Run(kbc)
166168
})
167169
It("should generate a runnable project with the golang base plugin v3 and kustomize v4-alpha"+
@@ -174,7 +176,8 @@ var _ = Describe("kubebuilder", func() {
174176
"and securityContext.seccompProfile", srvVer.GitVersion))
175177
}
176178

177-
GenerateV3WithKustomizeV2(kbc, "v1", true)
179+
kbc.IsRestricted = true
180+
GenerateV3WithKustomizeV2(kbc, "v1")
178181
Run(kbc)
179182
})
180183
It("should generate a runnable project with v1beta1 CRDs and Webhooks with restricted pods", func() {
@@ -188,7 +191,8 @@ var _ = Describe("kubebuilder", func() {
188191
"and securityContext.seccompProfile", srvVer.GitVersion))
189192
}
190193

191-
GenerateV3(kbc, "v1beta1", true)
194+
kbc.IsRestricted = true
195+
GenerateV3(kbc, "v1beta1")
192196
Run(kbc)
193197
})
194198
})
@@ -200,6 +204,14 @@ func Run(kbc *utils.TestContext) {
200204
var controllerPodName string
201205
var err error
202206

207+
By("creating manager namespace")
208+
err = kbc.CreateManagerNamespace()
209+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
210+
211+
By("labeling all namespaces to warn about restricted")
212+
err = kbc.LabelAllNamespacesToWarnAboutRestricted()
213+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
214+
203215
By("updating the go.mod")
204216
err = kbc.Tidy()
205217
ExpectWithOffset(1, err).NotTo(HaveOccurred())
@@ -218,9 +230,16 @@ func Run(kbc *utils.TestContext) {
218230
// --clusterrole=cluster-admin [email protected]
219231
// https://cloud.google.com/kubernetes-engine/docs/how-to/role-based-access-control
220232
By("deploying the controller-manager")
221-
err = kbc.Make("deploy", "IMG="+kbc.ImageName)
233+
234+
cmd := exec.Command("make", "deploy", "IMG="+kbc.ImageName)
235+
output, err := kbc.Run(cmd)
222236
ExpectWithOffset(1, err).NotTo(HaveOccurred())
223237

238+
if kbc.IsRestricted {
239+
By("validating that manager Pod/container(s) are restricted")
240+
ExpectWithOffset(1, output).NotTo(ContainSubstring("Warning: would violate PodSecurity"))
241+
}
242+
224243
By("validating that the controller-manager pod is running as expected")
225244
verifyControllerUp := func() error {
226245
// Get pod name

0 commit comments

Comments
 (0)