@@ -25,8 +25,7 @@ import (
25
25
// configMapCatalogSourceDecorator wraps CatalogSource to add additional methods
26
26
type configMapCatalogSourceDecorator struct {
27
27
* v1alpha1.CatalogSource
28
- Reconciler * ConfigMapRegistryReconciler
29
- runAsUser int64
28
+ runAsUser int64
30
29
}
31
30
32
31
const (
@@ -110,32 +109,15 @@ func (s *configMapCatalogSourceDecorator) Service() (*corev1.Service, error) {
110
109
return svc , nil
111
110
}
112
111
113
- func (s * configMapCatalogSourceDecorator ) getNamespaceSecurityContextConfig ( ) (v1alpha1. SecurityConfig , error ) {
114
- namespace := s . GetNamespace ()
115
- if config , ok := s . Reconciler . namespacePSAConfigCache [ namespace ]; ok {
116
- return config , nil
112
+ func (s * configMapCatalogSourceDecorator ) Pod ( image string , options ... PodOptionFunc ) (* corev1. Pod , error ) {
113
+ // define defaults and apply options
114
+ opts := & PodOption {
115
+ SecurityContextConfig : v1alpha1 . Restricted ,
117
116
}
118
- // Retrieve the client from the reconciler
119
- client := s .Reconciler .OpClient
120
-
121
- ns , err := client .KubernetesInterface ().CoreV1 ().Namespaces ().Get (context .TODO (), s .GetNamespace (), metav1.GetOptions {})
122
- if err != nil {
123
- return "" , fmt .Errorf ("error fetching namespace: %v" , err )
124
- }
125
- // 'pod-security.kubernetes.io/enforce' is the label used for enforcing namespace level security,
126
- // and 'restricted' is the value indicating a restricted security policy.
127
- if val , exists := ns .Labels ["pod-security.kubernetes.io/enforce" ]; exists && val == "restricted" {
128
- return v1alpha1 .Restricted , nil
117
+ for _ , o := range options {
118
+ o (opts )
129
119
}
130
-
131
- return v1alpha1 .Legacy , nil
132
- }
133
- func (s * configMapCatalogSourceDecorator ) Pod (image string ) (* corev1.Pod , error ) {
134
- securityContextConfig , err := s .getNamespaceSecurityContextConfig ()
135
- if err != nil {
136
- return nil , err
137
- }
138
- pod , err := Pod (s .CatalogSource , "configmap-registry-server" , "" , "" , image , nil , s .Labels (), s .Annotations (), 5 , 5 , s .runAsUser , securityContextConfig )
120
+ pod , err := Pod (s .CatalogSource , "configmap-registry-server" , "" , "" , image , nil , s .Labels (), s .Annotations (), 5 , 5 , s .runAsUser , opts .SecurityContextConfig )
139
121
if err != nil {
140
122
return nil , err
141
123
}
@@ -208,12 +190,11 @@ func (s *configMapCatalogSourceDecorator) RoleBinding() *rbacv1.RoleBinding {
208
190
}
209
191
210
192
type ConfigMapRegistryReconciler struct {
211
- now nowFunc
212
- Lister operatorlister.OperatorLister
213
- OpClient operatorclient.ClientInterface
214
- Image string
215
- createPodAsUser int64
216
- namespacePSAConfigCache map [string ]v1alpha1.SecurityConfig
193
+ now nowFunc
194
+ Lister operatorlister.OperatorLister
195
+ OpClient operatorclient.ClientInterface
196
+ Image string
197
+ createPodAsUser int64
217
198
}
218
199
219
200
var _ RegistryEnsurer = & ConfigMapRegistryReconciler {}
@@ -264,8 +245,8 @@ func (c *ConfigMapRegistryReconciler) currentRoleBinding(source configMapCatalog
264
245
return roleBinding
265
246
}
266
247
267
- func (c * ConfigMapRegistryReconciler ) currentPods (source configMapCatalogSourceDecorator , image string ) ([]* corev1.Pod , error ) {
268
- protoPod , err := source .Pod (image )
248
+ func (c * ConfigMapRegistryReconciler ) currentPods (source configMapCatalogSourceDecorator , image string , podSecurityConfig v1alpha1. SecurityConfig ) ([]* corev1.Pod , error ) {
249
+ protoPod , err := source .Pod (image , WithSecurityContextConfig ( podSecurityConfig ) )
269
250
if err != nil {
270
251
return nil , err
271
252
}
@@ -281,8 +262,8 @@ func (c *ConfigMapRegistryReconciler) currentPods(source configMapCatalogSourceD
281
262
return pods , nil
282
263
}
283
264
284
- func (c * ConfigMapRegistryReconciler ) currentPodsWithCorrectResourceVersion (source configMapCatalogSourceDecorator , image string ) ([]* corev1.Pod , error ) {
285
- protoPod , err := source .Pod (image )
265
+ func (c * ConfigMapRegistryReconciler ) currentPodsWithCorrectResourceVersion (source configMapCatalogSourceDecorator , image string , podSecurityConfig v1alpha1. SecurityConfig ) ([]* corev1.Pod , error ) {
266
+ protoPod , err := source .Pod (image , WithSecurityContextConfig ( podSecurityConfig ) )
286
267
if err != nil {
287
268
return nil , err
288
269
}
@@ -300,10 +281,7 @@ func (c *ConfigMapRegistryReconciler) currentPodsWithCorrectResourceVersion(sour
300
281
301
282
// EnsureRegistryServer ensures that all components of registry server are up to date.
302
283
func (c * ConfigMapRegistryReconciler ) EnsureRegistryServer (logger * logrus.Entry , catalogSource * v1alpha1.CatalogSource ) error {
303
- if c .namespacePSAConfigCache == nil {
304
- c .namespacePSAConfigCache = make (map [string ]v1alpha1.SecurityConfig )
305
- }
306
- source := configMapCatalogSourceDecorator {catalogSource , c , c .createPodAsUser }
284
+ source := configMapCatalogSourceDecorator {catalogSource , c .createPodAsUser }
307
285
308
286
image := c .Image
309
287
if source .Spec .SourceType == "grpc" {
@@ -317,6 +295,11 @@ func (c *ConfigMapRegistryReconciler) EnsureRegistryServer(logger *logrus.Entry,
317
295
overwrite := source .Status .RegistryServiceStatus == nil
318
296
overwritePod := overwrite
319
297
298
+ defaultPodSecurityConfig , err := getNamespaceSecurityContextConfig (c .OpClient , catalogSource .GetNamespace ())
299
+ if err != nil {
300
+ return err
301
+ }
302
+
320
303
if source .Spec .SourceType == v1alpha1 .SourceTypeConfigmap || source .Spec .SourceType == v1alpha1 .SourceTypeInternal {
321
304
// fetch configmap first, exit early if we can't find it
322
305
// we use the live client here instead of a lister since our listers are scoped to objects with the olm.managed label,
@@ -340,7 +323,7 @@ func (c *ConfigMapRegistryReconciler) EnsureRegistryServer(logger *logrus.Entry,
340
323
}
341
324
342
325
// recreate the pod if no existing pod is serving the latest image
343
- current , err := c .currentPodsWithCorrectResourceVersion (source , image )
326
+ current , err := c .currentPodsWithCorrectResourceVersion (source , image , defaultPodSecurityConfig )
344
327
if err != nil {
345
328
return err
346
329
}
@@ -359,11 +342,11 @@ func (c *ConfigMapRegistryReconciler) EnsureRegistryServer(logger *logrus.Entry,
359
342
if err := c .ensureRoleBinding (source , overwrite ); err != nil {
360
343
return errors .Wrapf (err , "error ensuring rolebinding: %s" , source .RoleBinding ().GetName ())
361
344
}
362
- pod , err := source .Pod (image )
345
+ pod , err := source .Pod (image , WithSecurityContextConfig ( defaultPodSecurityConfig ) )
363
346
if err != nil {
364
347
return err
365
348
}
366
- if err := c .ensurePod (source , overwritePod ); err != nil {
349
+ if err := c .ensurePod (source , defaultPodSecurityConfig , overwritePod ); err != nil {
367
350
return errors .Wrapf (err , "error ensuring pod: %s" , pod .GetName ())
368
351
}
369
352
service , err := source .Service ()
@@ -429,12 +412,12 @@ func (c *ConfigMapRegistryReconciler) ensureRoleBinding(source configMapCatalogS
429
412
return err
430
413
}
431
414
432
- func (c * ConfigMapRegistryReconciler ) ensurePod (source configMapCatalogSourceDecorator , overwrite bool ) error {
433
- pod , err := source .Pod (c .Image )
415
+ func (c * ConfigMapRegistryReconciler ) ensurePod (source configMapCatalogSourceDecorator , podSecurityConfig v1alpha1. SecurityConfig , overwrite bool ) error {
416
+ pod , err := source .Pod (c .Image , WithSecurityContextConfig ( podSecurityConfig ) )
434
417
if err != nil {
435
418
return err
436
419
}
437
- currentPods , err := c .currentPods (source , c .Image )
420
+ currentPods , err := c .currentPods (source , c .Image , podSecurityConfig )
438
421
if err != nil {
439
422
return err
440
423
}
@@ -478,7 +461,7 @@ func (c *ConfigMapRegistryReconciler) ensureService(source configMapCatalogSourc
478
461
479
462
// CheckRegistryServer returns true if the given CatalogSource is considered healthy; false otherwise.
480
463
func (c * ConfigMapRegistryReconciler ) CheckRegistryServer (logger * logrus.Entry , catalogSource * v1alpha1.CatalogSource ) (healthy bool , err error ) {
481
- source := configMapCatalogSourceDecorator {catalogSource , c , c .createPodAsUser }
464
+ source := configMapCatalogSourceDecorator {catalogSource , c .createPodAsUser }
482
465
483
466
image := c .Image
484
467
if source .Spec .SourceType == "grpc" {
@@ -489,6 +472,11 @@ func (c *ConfigMapRegistryReconciler) CheckRegistryServer(logger *logrus.Entry,
489
472
return
490
473
}
491
474
475
+ podSecurityConfig , err := getNamespaceSecurityContextConfig (c .OpClient , catalogSource .GetNamespace ())
476
+ if err != nil {
477
+ return false , err
478
+ }
479
+
492
480
if source .Spec .SourceType == v1alpha1 .SourceTypeConfigmap || source .Spec .SourceType == v1alpha1 .SourceTypeInternal {
493
481
// we use the live client here instead of a lister since our listers are scoped to objects with the olm.managed label,
494
482
// and this configmap is a user-provided input to the catalog source and will not have that label
@@ -502,7 +490,7 @@ func (c *ConfigMapRegistryReconciler) CheckRegistryServer(logger *logrus.Entry,
502
490
}
503
491
504
492
// recreate the pod if no existing pod is serving the latest image
505
- current , err := c .currentPodsWithCorrectResourceVersion (source , image )
493
+ current , err := c .currentPodsWithCorrectResourceVersion (source , image , podSecurityConfig )
506
494
if err != nil {
507
495
return false , err
508
496
}
@@ -518,7 +506,7 @@ func (c *ConfigMapRegistryReconciler) CheckRegistryServer(logger *logrus.Entry,
518
506
if err != nil {
519
507
return false , err
520
508
}
521
- pods , err := c .currentPods (source , c .Image )
509
+ pods , err := c .currentPods (source , c .Image , podSecurityConfig )
522
510
if err != nil {
523
511
return false , err
524
512
}
0 commit comments