Skip to content

Commit dcc5bb4

Browse files
authored
Merge pull request #2732 from camilamacedo86/test-improvements
🌱 : e2e test: do tests with restricted pods
2 parents 8ec681a + 793161d commit dcc5bb4

File tree

2 files changed

+82
-7
lines changed

2 files changed

+82
-7
lines changed

test/e2e/v3/generate_test.go

Lines changed: 44 additions & 2 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) {
133+
func GenerateV3(kbc *utils.TestContext, crdAndWebhookVersion string, restrictive bool) {
134134
var err error
135135

136136
By("initializing a project")
@@ -228,10 +228,52 @@ Count int `+"`"+`json:"count,omitempty"`+"`"+`
228228
if crdAndWebhookVersion == "v1beta1" {
229229
_ = pluginutil.RunCmd("Update dependencies", "go", "mod", "tidy")
230230
}
231+
232+
if restrictive {
233+
By("uncomment kustomize files to ensure that pods are restricted")
234+
uncommentPodStandards(kbc)
235+
}
236+
}
237+
238+
func uncommentPodStandards(kbc *utils.TestContext) {
239+
configManager := filepath.Join(kbc.Dir, "config", "manager", "manager.yaml")
240+
managerAuth := filepath.Join(kbc.Dir, "config", "default", "manager_auth_proxy_patch.yaml")
241+
242+
//nolint:lll
243+
if err := pluginutil.ReplaceInFile(configManager, `# TODO(user): uncomment for common cases that do not require escalating privileges
244+
# capabilities:
245+
# drop:
246+
# - "ALL"`, ` capabilities:
247+
drop:
248+
- "ALL"`); err != nil {
249+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
250+
}
251+
252+
//nolint:lll
253+
if err := pluginutil.ReplaceInFile(managerAuth, `# TODO(user): uncomment for common cases that do not require escalating privileges
254+
# capabilities:
255+
# drop:
256+
# - "ALL"`, ` capabilities:
257+
drop:
258+
- "ALL"`); err != nil {
259+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
260+
}
261+
262+
//nolint:lll
263+
if err := pluginutil.ReplaceInFile(configManager, `# TODO(user): For common cases that do not require escalating privileges
264+
# it is recommended to ensure that all your Pods/Containers are restrictive.
265+
# More info: https://kubernetes.io/docs/concepts/security/pod-security-standards/#restricted
266+
# Please uncomment the following code if your project does NOT have to work on old Kubernetes
267+
# versions < 1.19 or on vendors versions which do NOT support this field by default (i.e. Openshift < 4.11 ).
268+
# seccompProfile:
269+
# type: RuntimeDefault`, `seccompProfile:
270+
type: RuntimeDefault`); err == nil {
271+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
272+
}
231273
}
232274

233275
// GenerateV3 implements a go/v3(-alpha) plugin project defined by a TestContext.
234-
func GenerateV3WithKustomizeV2(kbc *utils.TestContext, crdAndWebhookVersion string) {
276+
func GenerateV3WithKustomizeV2(kbc *utils.TestContext, crdAndWebhookVersion string, restrictive bool) {
235277
var err error
236278

237279
By("initializing a project")

test/e2e/v3/plugin_cluster_test.go

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,38 @@ var _ = Describe("kubebuilder", func() {
119119
})
120120

121121
It("should generate a runnable project go/v3 with v1 CRDs and Webhooks", func() {
122+
// Skip if cluster version < 1.16, when v1 CRDs and webhooks did not exist.
123+
if srvVer := kbc.K8sVersion.ServerVersion; srvVer.GetMajorInt() <= 1 && srvVer.GetMinorInt() < 16 {
124+
Skip(fmt.Sprintf("cluster version %s does not support v1 CRDs or webhooks",
125+
srvVer.GitVersion))
126+
}
127+
128+
GenerateV3(kbc, "v1", false)
129+
Run(kbc)
130+
})
131+
It("should generate a runnable project with the golang base plugin v3 and kustomize v4-alpha", func() {
132+
// Skip if cluster version < 1.16, when v1 CRDs and webhooks did not exist.
133+
if srvVer := kbc.K8sVersion.ServerVersion; srvVer.GetMajorInt() <= 1 && srvVer.GetMinorInt() < 16 {
134+
Skip(fmt.Sprintf("cluster version %s does not support v1 CRDs or webhooks",
135+
srvVer.GitVersion))
136+
}
137+
GenerateV3WithKustomizeV2(kbc, "v1", false)
138+
Run(kbc)
139+
})
140+
It("should generate a runnable project with v1beta1 CRDs and Webhooks", func() {
141+
// Skip if cluster version < 1.15, when `.spec.preserveUnknownFields` was not a v1beta1 CRD field.
142+
// Skip if cluster version >= 1.22 because pre v1 CRDs and webhooks no longer exist.
143+
if srvVer := kbc.K8sVersion.ServerVersion; srvVer.GetMajorInt() <= 1 && srvVer.GetMinorInt() < 15 ||
144+
srvVer.GetMajorInt() <= 1 && srvVer.GetMinorInt() >= 22 {
145+
Skip(fmt.Sprintf("cluster version %s does not support project defaults ",
146+
srvVer.GitVersion))
147+
}
148+
149+
GenerateV3(kbc, "v1beta1", false)
150+
Run(kbc)
151+
})
152+
153+
It("should generate a runnable project go/v3 with v1 CRDs and Webhooks with restricted pods", func() {
122154
// Skip if cluster version < 1.16, when v1 CRDs and webhooks did not exist.
123155
// Skip if cluster version < 1.19, because securityContext.seccompProfile only works from 1.19
124156
// Otherwise, unknown field "seccompProfile" in io.k8s.api.core.v1.PodSecurityContext will be faced
@@ -127,10 +159,11 @@ var _ = Describe("kubebuilder", func() {
127159
"and securityContext.seccompProfile", srvVer.GitVersion))
128160
}
129161

130-
GenerateV3(kbc, "v1")
162+
GenerateV3(kbc, "v1", true)
131163
Run(kbc)
132164
})
133-
It("should generate a runnable project with the golang base plugin v3 and kustomize v4-alpha", func() {
165+
It("should generate a runnable project with the golang base plugin v3 and kustomize v4-alpha"+
166+
" with restricted pods", func() {
134167
// Skip if cluster version < 1.16, when v1 CRDs and webhooks did not exist.
135168
// Skip if cluster version < 1.19, because securityContext.seccompProfile only works from 1.19
136169
// Otherwise, unknown field "seccompProfile" in io.k8s.api.core.v1.PodSecurityContext will be faced
@@ -139,10 +172,10 @@ var _ = Describe("kubebuilder", func() {
139172
"and securityContext.seccompProfile", srvVer.GitVersion))
140173
}
141174

142-
GenerateV3WithKustomizeV2(kbc, "v1")
175+
GenerateV3WithKustomizeV2(kbc, "v1", true)
143176
Run(kbc)
144177
})
145-
It("should generate a runnable project with v1beta1 CRDs and Webhooks", func() {
178+
It("should generate a runnable project with v1beta1 CRDs and Webhooks with restricted pods", func() {
146179
// Skip if cluster version < 1.15, when `.spec.preserveUnknownFields` was not a v1beta1 CRD field.
147180
// Skip if cluster version < 1.19, because securityContext.seccompProfile only works from 1.19
148181
// Otherwise, unknown field "seccompProfile" in io.k8s.api.core.v1.PodSecurityContext will be faced
@@ -153,7 +186,7 @@ var _ = Describe("kubebuilder", func() {
153186
"and securityContext.seccompProfile", srvVer.GitVersion))
154187
}
155188

156-
GenerateV3(kbc, "v1beta1")
189+
GenerateV3(kbc, "v1beta1", true)
157190
Run(kbc)
158191
})
159192
})

0 commit comments

Comments
 (0)