@@ -26,6 +26,7 @@ import (
26
26
"k8s.io/apimachinery/pkg/util/uuid"
27
27
clientset "k8s.io/client-go/kubernetes"
28
28
imageutils "k8s.io/kubernetes/test/utils/image"
29
+ admissionapi "k8s.io/pod-security-admission/api"
29
30
)
30
31
31
32
const (
@@ -40,7 +41,7 @@ type Config struct {
40
41
PVCs []* v1.PersistentVolumeClaim
41
42
PVCsReadOnly bool
42
43
InlineVolumeSources []* v1.VolumeSource
43
- IsPrivileged bool
44
+ SecurityLevel admissionapi. Level
44
45
Command string
45
46
HostIPC bool
46
47
HostPID bool
@@ -52,8 +53,8 @@ type Config struct {
52
53
}
53
54
54
55
// CreateUnschedulablePod with given claims based on node selector
55
- func CreateUnschedulablePod (ctx context.Context , client clientset.Interface , namespace string , nodeSelector map [string ]string , pvclaims []* v1.PersistentVolumeClaim , isPrivileged bool , command string ) (* v1.Pod , error ) {
56
- pod := MakePod (namespace , nodeSelector , pvclaims , isPrivileged , command )
56
+ func CreateUnschedulablePod (ctx context.Context , client clientset.Interface , namespace string , nodeSelector map [string ]string , pvclaims []* v1.PersistentVolumeClaim , securityLevel admissionapi. Level , command string ) (* v1.Pod , error ) {
57
+ pod := MakePod (namespace , nodeSelector , pvclaims , securityLevel , command )
57
58
pod , err := client .CoreV1 ().Pods (namespace ).Create (ctx , pod , metav1.CreateOptions {})
58
59
if err != nil {
59
60
return nil , fmt .Errorf ("pod Create API error: %w" , err )
@@ -73,12 +74,12 @@ func CreateUnschedulablePod(ctx context.Context, client clientset.Interface, nam
73
74
74
75
// CreateClientPod defines and creates a pod with a mounted PV. Pod runs infinite loop until killed.
75
76
func CreateClientPod (ctx context.Context , c clientset.Interface , ns string , pvc * v1.PersistentVolumeClaim ) (* v1.Pod , error ) {
76
- return CreatePod (ctx , c , ns , nil , []* v1.PersistentVolumeClaim {pvc }, true , "" )
77
+ return CreatePod (ctx , c , ns , nil , []* v1.PersistentVolumeClaim {pvc }, admissionapi . LevelPrivileged , "" )
77
78
}
78
79
79
80
// CreatePod with given claims based on node selector
80
- func CreatePod (ctx context.Context , client clientset.Interface , namespace string , nodeSelector map [string ]string , pvclaims []* v1.PersistentVolumeClaim , isPrivileged bool , command string ) (* v1.Pod , error ) {
81
- pod := MakePod (namespace , nodeSelector , pvclaims , isPrivileged , command )
81
+ func CreatePod (ctx context.Context , client clientset.Interface , namespace string , nodeSelector map [string ]string , pvclaims []* v1.PersistentVolumeClaim , securityLevel admissionapi. Level , command string ) (* v1.Pod , error ) {
82
+ pod := MakePod (namespace , nodeSelector , pvclaims , securityLevel , command )
82
83
pod , err := client .CoreV1 ().Pods (namespace ).Create (ctx , pod , metav1.CreateOptions {})
83
84
if err != nil {
84
85
return nil , fmt .Errorf ("pod Create API error: %w" , err )
@@ -128,7 +129,7 @@ func CreateSecPodWithNodeSelection(ctx context.Context, client clientset.Interfa
128
129
129
130
// MakePod returns a pod definition based on the namespace. The pod references the PVC's
130
131
// name. A slice of BASH commands can be supplied as args to be run by the pod
131
- func MakePod (ns string , nodeSelector map [string ]string , pvclaims []* v1.PersistentVolumeClaim , isPrivileged bool , command string ) * v1.Pod {
132
+ func MakePod (ns string , nodeSelector map [string ]string , pvclaims []* v1.PersistentVolumeClaim , securityLevel admissionapi. Level , command string ) * v1.Pod {
132
133
if len (command ) == 0 {
133
134
command = "trap exit TERM; while true; do sleep 1; done"
134
135
}
@@ -147,7 +148,7 @@ func MakePod(ns string, nodeSelector map[string]string, pvclaims []*v1.Persisten
147
148
Name : "write-pod" ,
148
149
Image : GetDefaultTestImage (),
149
150
Command : GenerateScriptCmd (command ),
150
- SecurityContext : GenerateContainerSecurityContext (isPrivileged ),
151
+ SecurityContext : GenerateContainerSecurityContext (securityLevel ),
151
152
},
152
153
},
153
154
RestartPolicy : v1 .RestartPolicyOnFailure ,
@@ -157,6 +158,10 @@ func MakePod(ns string, nodeSelector map[string]string, pvclaims []*v1.Persisten
157
158
if nodeSelector != nil {
158
159
podSpec .Spec .NodeSelector = nodeSelector
159
160
}
161
+ if securityLevel == admissionapi .LevelRestricted {
162
+ podSpec = MustMixinRestrictedPodSecurity (podSpec )
163
+ }
164
+
160
165
return podSpec
161
166
}
162
167
@@ -196,6 +201,10 @@ func MakePodSpec(podConfig *Config) *v1.PodSpec {
196
201
if podConfig .ImageID != imageutils .None {
197
202
image = podConfig .ImageID
198
203
}
204
+ securityLevel := podConfig .SecurityLevel
205
+ if securityLevel == "" {
206
+ securityLevel = admissionapi .LevelBaseline
207
+ }
199
208
podSpec := & v1.PodSpec {
200
209
HostIPC : podConfig .HostIPC ,
201
210
HostPID : podConfig .HostPID ,
@@ -205,7 +214,7 @@ func MakePodSpec(podConfig *Config) *v1.PodSpec {
205
214
Name : "write-pod" ,
206
215
Image : GetTestImage (image ),
207
216
Command : GenerateScriptCmd (podConfig .Command ),
208
- SecurityContext : GenerateContainerSecurityContext (podConfig . IsPrivileged ),
217
+ SecurityContext : GenerateContainerSecurityContext (securityLevel ),
209
218
},
210
219
},
211
220
RestartPolicy : v1 .RestartPolicyOnFailure ,
0 commit comments