Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱 : e2e test: do tests with restricted pods #2732

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions test/e2e/v3/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ Count int `+"`"+`json:"count,omitempty"`+"`"+`
}

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

By("initializing a project")
Expand Down Expand Up @@ -228,10 +228,52 @@ Count int `+"`"+`json:"count,omitempty"`+"`"+`
if crdAndWebhookVersion == "v1beta1" {
_ = pluginutil.RunCmd("Update dependencies", "go", "mod", "tidy")
}

if restrictive {
By("uncomment kustomize files to ensure that pods are restricted")
uncommentPodStandards(kbc)
}
}

func uncommentPodStandards(kbc *utils.TestContext) {
configManager := filepath.Join(kbc.Dir, "config", "manager", "manager.yaml")
managerAuth := filepath.Join(kbc.Dir, "config", "default", "manager_auth_proxy_patch.yaml")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit:

Suggested change
managerAuth := filepath.Join(kbc.Dir, "config", "default", "manager_auth_proxy_patch.yaml")
managerAuth := filepath.Join(configManager, "default", "manager_auth_proxy_patch.yaml")

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We cannot do this change.
Why?
configManager-> has the full path with the file


//nolint:lll
if err := pluginutil.ReplaceInFile(configManager, `# TODO(user): uncomment for common cases that do not require escalating privileges
camilamacedo86 marked this conversation as resolved.
Show resolved Hide resolved
# capabilities:
# drop:
# - "ALL"`, ` capabilities:
drop:
- "ALL"`); err != nil {
ExpectWithOffset(1, err).NotTo(HaveOccurred())
}

//nolint:lll
if err := pluginutil.ReplaceInFile(managerAuth, `# TODO(user): uncomment for common cases that do not require escalating privileges
# capabilities:
# drop:
# - "ALL"`, ` capabilities:
drop:
- "ALL"`); err != nil {
ExpectWithOffset(1, err).NotTo(HaveOccurred())
}

//nolint:lll
if err := pluginutil.ReplaceInFile(configManager, `# TODO(user): For common cases that do not require escalating privileges
# it is recommended to ensure that all your Pods/Containers are restrictive.
# More info: https://kubernetes.io/docs/concepts/security/pod-security-standards/#restricted
# Please uncomment the following code if your project does NOT have to work on old Kubernetes
# versions < 1.19 or on vendors versions which do NOT support this field by default (i.e. Openshift < 4.11 ).
# seccompProfile:
# type: RuntimeDefault`, `seccompProfile:
type: RuntimeDefault`); err == nil {
ExpectWithOffset(1, err).NotTo(HaveOccurred())
}
}

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

By("initializing a project")
Expand Down
43 changes: 38 additions & 5 deletions test/e2e/v3/plugin_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,38 @@ var _ = Describe("kubebuilder", func() {
})

It("should generate a runnable project go/v3 with v1 CRDs and Webhooks", func() {
// Skip if cluster version < 1.16, when v1 CRDs and webhooks did not exist.
if srvVer := kbc.K8sVersion.ServerVersion; srvVer.GetMajorInt() <= 1 && srvVer.GetMinorInt() < 16 {
Skip(fmt.Sprintf("cluster version %s does not support v1 CRDs or webhooks",
srvVer.GitVersion))
}

GenerateV3(kbc, "v1", false)
Run(kbc)
})
It("should generate a runnable project with the golang base plugin v3 and kustomize v4-alpha", func() {
// Skip if cluster version < 1.16, when v1 CRDs and webhooks did not exist.
if srvVer := kbc.K8sVersion.ServerVersion; srvVer.GetMajorInt() <= 1 && srvVer.GetMinorInt() < 16 {
Skip(fmt.Sprintf("cluster version %s does not support v1 CRDs or webhooks",
srvVer.GitVersion))
}
GenerateV3WithKustomizeV2(kbc, "v1", false)
Run(kbc)
})
It("should generate a runnable project with v1beta1 CRDs and Webhooks", func() {
// Skip if cluster version < 1.15, when `.spec.preserveUnknownFields` was not a v1beta1 CRD field.
// Skip if cluster version >= 1.22 because pre v1 CRDs and webhooks no longer exist.
if srvVer := kbc.K8sVersion.ServerVersion; srvVer.GetMajorInt() <= 1 && srvVer.GetMinorInt() < 15 ||
srvVer.GetMajorInt() <= 1 && srvVer.GetMinorInt() >= 22 {
Skip(fmt.Sprintf("cluster version %s does not support project defaults ",
srvVer.GitVersion))
}

GenerateV3(kbc, "v1beta1", false)
Run(kbc)
})

It("should generate a runnable project go/v3 with v1 CRDs and Webhooks with restricted pods", func() {
// Skip if cluster version < 1.16, when v1 CRDs and webhooks did not exist.
// Skip if cluster version < 1.19, because securityContext.seccompProfile only works from 1.19
// Otherwise, unknown field "seccompProfile" in io.k8s.api.core.v1.PodSecurityContext will be faced
Expand All @@ -127,10 +159,11 @@ var _ = Describe("kubebuilder", func() {
"and securityContext.seccompProfile", srvVer.GitVersion))
}

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

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

GenerateV3(kbc, "v1beta1")
GenerateV3(kbc, "v1beta1", true)
Run(kbc)
})
})
Expand Down