@@ -22,6 +22,7 @@ import (
22
22
listersbatchv1 "k8s.io/client-go/listers/batch/v1"
23
23
listerscorev1 "k8s.io/client-go/listers/core/v1"
24
24
listersrbacv1 "k8s.io/client-go/listers/rbac/v1"
25
+ "k8s.io/utils/pointer"
25
26
26
27
"github.com/operator-framework/api/pkg/operators/reference"
27
28
operatorsv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
@@ -85,7 +86,7 @@ func newBundleUnpackResult(lookup *operatorsv1alpha1.BundleLookup) *BundleUnpack
85
86
}
86
87
}
87
88
88
- func (c * ConfigMapUnpacker ) job (cmRef * corev1.ObjectReference , bundlePath string , secrets []corev1.LocalObjectReference , annotationUnpackTimeout time.Duration ) * batchv1.Job {
89
+ func (c * ConfigMapUnpacker ) job (cmRef * corev1.ObjectReference , bundlePath string , secrets []corev1.LocalObjectReference , annotationUnpackTimeout time.Duration , runAsUser int64 ) * batchv1.Job {
89
90
job := & batchv1.Job {
90
91
Spec : batchv1.JobSpec {
91
92
//ttlSecondsAfterFinished: 0 // can use in the future to not have to clean up job
@@ -101,6 +102,12 @@ func (c *ConfigMapUnpacker) job(cmRef *corev1.ObjectReference, bundlePath string
101
102
// See: https://kubernetes.io/docs/concepts/workloads/controllers/job/#pod-backoff-failure-policy
102
103
RestartPolicy : corev1 .RestartPolicyNever ,
103
104
ImagePullSecrets : secrets ,
105
+ SecurityContext : & corev1.PodSecurityContext {
106
+ RunAsNonRoot : pointer .Bool (true ),
107
+ SeccompProfile : & corev1.SeccompProfile {
108
+ Type : corev1 .SeccompProfileTypeRuntimeDefault ,
109
+ },
110
+ },
104
111
Containers : []corev1.Container {
105
112
{
106
113
Name : "extract" ,
@@ -129,6 +136,12 @@ func (c *ConfigMapUnpacker) job(cmRef *corev1.ObjectReference, bundlePath string
129
136
corev1 .ResourceMemory : resource .MustParse ("50Mi" ),
130
137
},
131
138
},
139
+ SecurityContext : & corev1.SecurityContext {
140
+ AllowPrivilegeEscalation : pointer .Bool (false ),
141
+ Capabilities : & corev1.Capabilities {
142
+ Drop : []corev1.Capability {"ALL" },
143
+ },
144
+ },
132
145
},
133
146
},
134
147
InitContainers : []corev1.Container {
@@ -148,6 +161,12 @@ func (c *ConfigMapUnpacker) job(cmRef *corev1.ObjectReference, bundlePath string
148
161
corev1 .ResourceMemory : resource .MustParse ("50Mi" ),
149
162
},
150
163
},
164
+ SecurityContext : & corev1.SecurityContext {
165
+ AllowPrivilegeEscalation : pointer .Bool (false ),
166
+ Capabilities : & corev1.Capabilities {
167
+ Drop : []corev1.Capability {"ALL" },
168
+ },
169
+ },
151
170
},
152
171
{
153
172
Name : "pull" ,
@@ -170,6 +189,12 @@ func (c *ConfigMapUnpacker) job(cmRef *corev1.ObjectReference, bundlePath string
170
189
corev1 .ResourceMemory : resource .MustParse ("50Mi" ),
171
190
},
172
191
},
192
+ SecurityContext : & corev1.SecurityContext {
193
+ AllowPrivilegeEscalation : pointer .Bool (false ),
194
+ Capabilities : & corev1.Capabilities {
195
+ Drop : []corev1.Capability {"ALL" },
196
+ },
197
+ },
173
198
},
174
199
},
175
200
Volumes : []corev1.Volume {
@@ -193,7 +218,9 @@ func (c *ConfigMapUnpacker) job(cmRef *corev1.ObjectReference, bundlePath string
193
218
job .SetNamespace (cmRef .Namespace )
194
219
job .SetName (cmRef .Name )
195
220
job .SetOwnerReferences ([]metav1.OwnerReference {ownerRef (cmRef )})
196
-
221
+ if runAsUser > 0 {
222
+ job .Spec .Template .Spec .SecurityContext .RunAsUser = & runAsUser
223
+ }
197
224
// By default the BackoffLimit is set to 6 which with exponential backoff 10s + 20s + 40s ...
198
225
// translates to ~10m of waiting time.
199
226
// We want to fail faster than that when we have repeated failures from the bundle unpack pod
@@ -229,7 +256,7 @@ func (c *ConfigMapUnpacker) job(cmRef *corev1.ObjectReference, bundlePath string
229
256
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . Unpacker
230
257
231
258
type Unpacker interface {
232
- UnpackBundle (lookup * operatorsv1alpha1.BundleLookup , timeout time.Duration ) (result * BundleUnpackResult , err error )
259
+ UnpackBundle (lookup * operatorsv1alpha1.BundleLookup , timeout time.Duration , runAsUser int64 ) (result * BundleUnpackResult , err error )
233
260
}
234
261
235
262
type ConfigMapUnpacker struct {
@@ -383,7 +410,7 @@ const (
383
410
NotUnpackedMessage = "bundle contents have not yet been persisted to installplan status"
384
411
)
385
412
386
- func (c * ConfigMapUnpacker ) UnpackBundle (lookup * operatorsv1alpha1.BundleLookup , timeout time.Duration ) (result * BundleUnpackResult , err error ) {
413
+ func (c * ConfigMapUnpacker ) UnpackBundle (lookup * operatorsv1alpha1.BundleLookup , timeout time.Duration , runAsUser int64 ) (result * BundleUnpackResult , err error ) {
387
414
result = newBundleUnpackResult (lookup )
388
415
389
416
// if bundle lookup failed condition already present, then there is nothing more to do
@@ -445,7 +472,7 @@ func (c *ConfigMapUnpacker) UnpackBundle(lookup *operatorsv1alpha1.BundleLookup,
445
472
secrets = append (secrets , corev1.LocalObjectReference {Name : secretName })
446
473
}
447
474
var job * batchv1.Job
448
- job , err = c .ensureJob (cmRef , result .Path , secrets , timeout )
475
+ job , err = c .ensureJob (cmRef , result .Path , secrets , timeout , runAsUser )
449
476
if err != nil || job == nil {
450
477
// ensureJob can return nil if the job present does not match the expected job (spec and ownerefs)
451
478
// The current job is deleted in that case so UnpackBundle needs to be retried
@@ -584,8 +611,8 @@ func (c *ConfigMapUnpacker) ensureConfigmap(csRef *corev1.ObjectReference, name
584
611
return
585
612
}
586
613
587
- func (c * ConfigMapUnpacker ) ensureJob (cmRef * corev1.ObjectReference , bundlePath string , secrets []corev1.LocalObjectReference , timeout time.Duration ) (job * batchv1.Job , err error ) {
588
- fresh := c .job (cmRef , bundlePath , secrets , timeout )
614
+ func (c * ConfigMapUnpacker ) ensureJob (cmRef * corev1.ObjectReference , bundlePath string , secrets []corev1.LocalObjectReference , timeout time.Duration , runAsUser int64 ) (job * batchv1.Job , err error ) {
615
+ fresh := c .job (cmRef , bundlePath , secrets , timeout , runAsUser )
589
616
job , err = c .jobLister .Jobs (fresh .GetNamespace ()).Get (fresh .GetName ())
590
617
if err != nil {
591
618
if apierrors .IsNotFound (err ) {
0 commit comments