Skip to content

Commit 9e11596

Browse files
Merge pull request #26938 from s-urbaniak/pod-security-levels
util/client.go: add method to specify pod security admission level
2 parents 7d8a287 + 2d3a370 commit 9e11596

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ require (
7272
k8s.io/kubelet v0.23.0
7373
k8s.io/kubernetes v1.23.0
7474
k8s.io/legacy-cloud-providers v0.23.0
75+
k8s.io/pod-security-admission v0.23.0
7576
k8s.io/utils v0.0.0-20211208161948-7d6a63dca704
7677
sigs.k8s.io/kustomize/kyaml v0.13.0
7778
sigs.k8s.io/yaml v1.2.0

test/extended/util/client.go

+36
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"io"
1010
"io/ioutil"
11+
"k8s.io/client-go/util/retry"
1112
"net"
1213
"net/http"
1314
"os"
@@ -62,6 +63,7 @@ import (
6263
watchtools "k8s.io/client-go/tools/watch"
6364
"k8s.io/client-go/util/flowcontrol"
6465
"k8s.io/kubernetes/test/e2e/framework"
66+
admissionapi "k8s.io/pod-security-admission/api"
6567
)
6668

6769
// CLI provides function to call the OpenShift CLI and Kubernetes and OpenShift
@@ -105,8 +107,17 @@ func NewCLIWithFramework(kubeFramework *framework.Framework) *CLI {
105107
return cli
106108
}
107109

110+
// NewCLIWithPodSecurityLevel initializes the CLI the same way as `NewCLI()`
111+
// but the given pod security level is applied to the created e2e test namespace.
112+
func NewCLIWithPodSecurityLevel(project string, level admissionapi.Level) *CLI {
113+
cli := NewCLI(project)
114+
cli.kubeFramework.NamespacePodSecurityEnforceLevel = level
115+
return cli
116+
}
117+
108118
// NewCLI initializes the CLI and Kube framework helpers with the provided
109119
// namespace. Should be called outside of a Ginkgo .It() function.
120+
// This will apply the `restricted` pod security level to the given underlying namespace.
110121
func NewCLI(project string) *CLI {
111122
cli := NewCLIWithoutNamespace(project)
112123
cli.withoutNamespace = false
@@ -288,6 +299,31 @@ func (c *CLI) SetupProject() string {
288299
})
289300
o.Expect(err).NotTo(o.HaveOccurred())
290301

302+
err = retry.RetryOnConflict(retry.DefaultRetry, func() error {
303+
// once permissions are settled the underlying namespace must have been created.
304+
ns, err := c.AdminKubeClient().CoreV1().Namespaces().Get(context.Background(), newNamespace, metav1.GetOptions{})
305+
if err != nil {
306+
return err
307+
}
308+
309+
if c.kubeFramework.NamespacePodSecurityEnforceLevel != "" {
310+
// TODO(sur): set to restricted in a separate PR and fix failing tests
311+
c.kubeFramework.NamespacePodSecurityEnforceLevel = admissionapi.LevelPrivileged
312+
}
313+
if ns.Labels == nil {
314+
ns.Labels = make(map[string]string)
315+
}
316+
ns.Labels[admissionapi.EnforceLevelLabel] = string(c.kubeFramework.NamespacePodSecurityEnforceLevel)
317+
// In contrast to upstream, OpenShift sets a global default on warn and audit pod security levels.
318+
// Since this would cause unwanted audit log and warning entries, we are setting the same level as for enforcement.
319+
ns.Labels[admissionapi.WarnLevelLabel] = string(c.kubeFramework.NamespacePodSecurityEnforceLevel)
320+
ns.Labels[admissionapi.AuditLevelLabel] = string(c.kubeFramework.NamespacePodSecurityEnforceLevel)
321+
322+
_, err = c.AdminKubeClient().CoreV1().Namespaces().Update(context.Background(), ns, metav1.UpdateOptions{})
323+
return err
324+
})
325+
o.Expect(err).NotTo(o.HaveOccurred())
326+
291327
// Wait for SAs and default dockercfg Secret to be injected
292328
// TODO: it would be nice to have a shared list but it is defined in at least 3 place,
293329
// TODO: some of them not even using the constants

vendor/modules.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -3103,7 +3103,8 @@ k8s.io/metrics/pkg/client/custom_metrics/scheme
31033103
k8s.io/metrics/pkg/client/external_metrics
31043104
# k8s.io/mount-utils v0.0.0 => github.com/openshift/kubernetes/staging/src/k8s.io/mount-utils v0.0.0-20220405131139-37c5e75b4e1e
31053105
k8s.io/mount-utils
3106-
# k8s.io/pod-security-admission v0.0.0 => github.com/openshift/kubernetes/staging/src/k8s.io/pod-security-admission v0.0.0-20220405131139-37c5e75b4e1e
3106+
# k8s.io/pod-security-admission v0.23.0 => github.com/openshift/kubernetes/staging/src/k8s.io/pod-security-admission v0.0.0-20220405131139-37c5e75b4e1e
3107+
## explicit
31073108
k8s.io/pod-security-admission/admission
31083109
k8s.io/pod-security-admission/admission/api
31093110
k8s.io/pod-security-admission/admission/api/load

0 commit comments

Comments
 (0)