@@ -102,45 +102,40 @@ func New(conf *rest.Config, fileName string, scheme *runtime.Scheme, log logr.Lo
102
102
}
103
103
104
104
// InstallOrUpdateOperator installs or updates the operator Stored in `OperatorManager`
105
- func (m * OperatorManager ) InstallOrUpdateOperator (ctx context.Context , namespace string ) ([]client.Object , error ) {
106
- objs := []client.Object {}
107
-
105
+ func (m * OperatorManager ) InstallOrUpdateOperator (ctx context.Context , namespace string ) error {
108
106
// Make sure the namespace exists.
109
- objs , err := m .createNamespace (ctx , namespace , objs )
110
- if err != nil {
111
- return objs , fmt .Errorf ("error while ensuring the existence of namespace %v: %w" , namespace , err )
107
+ if err := m .createNamespace (ctx , namespace ); err != nil {
108
+ return fmt .Errorf ("error while ensuring the existence of namespace %v: %w" , namespace , err )
112
109
}
113
110
114
111
// Add our (initially empty) custom pod environment configmap
115
- objs , err = m .createPodEnvironmentConfigMap (ctx , namespace , objs )
116
- if err != nil {
117
- return objs , fmt .Errorf ("error while creating pod environment configmap %v: %w" , namespace , err )
112
+ if err := m .createPodEnvironmentConfigMap (ctx , namespace ); err != nil {
113
+ return fmt .Errorf ("error while creating pod environment configmap %v: %w" , namespace , err )
118
114
}
119
115
120
116
// Add our sidecars configmap
121
- objs , err = m .createOrUpdateSidecarsConfig (ctx , namespace , objs )
122
- if err != nil {
123
- return objs , fmt .Errorf ("error while creating sidecars config %v: %w" , namespace , err )
117
+ if err := m .createOrUpdateSidecarsConfig (ctx , namespace ); err != nil {
118
+ return fmt .Errorf ("error while creating sidecars config %v: %w" , namespace , err )
124
119
}
125
120
126
121
// Decode each YAML to `client.Object`, add the namespace to it and install it.
127
122
for _ , item := range m .list .Items {
128
123
obj , _ , err := m .Decoder .Decode (item .Raw , nil , nil )
129
124
if err != nil {
130
- return objs , fmt .Errorf ("error while converting yaml to `client.Object`: %w" , err )
125
+ return fmt .Errorf ("error while converting yaml to `client.Object`: %w" , err )
131
126
}
132
127
133
128
cltObject , ok := obj .(client.Object )
134
129
if ! ok {
135
- return objs , fmt .Errorf ("unable to cast into client.Object" )
130
+ return fmt .Errorf ("unable to cast into client.Object" )
136
131
}
137
- if objs , err := m .createNewClientObject (ctx , objs , cltObject , namespace ); err != nil {
138
- return objs , fmt .Errorf ("error while creating the `client.Object`: %w" , err )
132
+ if err := m .createNewClientObject (ctx , cltObject , namespace ); err != nil {
133
+ return fmt .Errorf ("error while creating the `client.Object`: %w" , err )
139
134
}
140
135
}
141
136
142
137
m .log .Info ("operator installed" )
143
- return objs , nil
138
+ return nil
144
139
}
145
140
146
141
// IsOperatorDeletable returns true when there's no running instance operated by the operator
@@ -257,21 +252,21 @@ func (m *OperatorManager) UninstallOperator(ctx context.Context, namespace strin
257
252
}
258
253
259
254
// createNewClientObject adds namespace to obj and creates or patches it
260
- func (m * OperatorManager ) createNewClientObject (ctx context.Context , objs []client. Object , obj client.Object , namespace string ) ([]client. Object , error ) {
255
+ func (m * OperatorManager ) createNewClientObject (ctx context.Context , obj client.Object , namespace string ) error {
261
256
// remove any unwanted annotations, uids etc. Remember, these objects come straight from the YAML.
262
257
if err := m .ensureCleanMetadata (obj ); err != nil {
263
- return objs , fmt .Errorf ("error while ensuring the metadata of the `client.Object` is clean: %w" , err )
258
+ return fmt .Errorf ("error while ensuring the metadata of the `client.Object` is clean: %w" , err )
264
259
}
265
260
266
261
// use our current namespace, not the one from the YAML
267
262
if err := m .SetNamespace (obj , namespace ); err != nil {
268
- return objs , fmt .Errorf ("error while setting the namespace of the `client.Object` to %v: %w" , namespace , err )
263
+ return fmt .Errorf ("error while setting the namespace of the `client.Object` to %v: %w" , namespace , err )
269
264
}
270
265
271
266
// generate a proper object key for each object
272
267
key , err := m .toObjectKey (obj , namespace )
273
268
if err != nil {
274
- return objs , fmt .Errorf ("error while making the object key: %w" , err )
269
+ return fmt .Errorf ("error while making the object key: %w" , err )
275
270
}
276
271
277
272
// perform different modifications on the parsed objects based on their kind
@@ -310,37 +305,35 @@ func (m *OperatorManager) createNewClientObject(ctx context.Context, objs []clie
310
305
got := & rbacv1.ClusterRoleBinding {}
311
306
if err := m .Get (ctx , key , got ); err != nil {
312
307
if ! errors .IsNotFound (err ) {
313
- return objs , fmt .Errorf ("error while fetching %v: %w" , v , err )
308
+ return fmt .Errorf ("error while fetching %v: %w" , v , err )
314
309
}
315
310
316
311
// Create the ClusterRoleBinding
317
312
if err := m .Create (ctx , v ); err != nil {
318
- return objs , fmt .Errorf ("error while creating %v: %w" , v , err )
313
+ return fmt .Errorf ("error while creating %v: %w" , v , err )
319
314
}
320
315
m .log .Info ("ClusterRoleBinding created" )
321
316
322
- // Append the newly created obj
323
- objs = append (objs , obj )
324
- return objs , nil
317
+ return nil
325
318
}
326
319
327
320
// If the ServiceAccount already exists, return.
328
321
for i := range got .Subjects {
329
322
if got .Subjects [i ].Kind == "ServiceAccount" && got .Subjects [i ].Namespace == namespace {
330
- return objs , nil
323
+ return nil
331
324
}
332
325
}
333
326
334
327
// Patch the already existing ClusterRoleBinding
335
328
mergeFrom := client .MergeFrom (got .DeepCopy ())
336
329
got .Subjects = append (got .Subjects , v .Subjects [0 ])
337
330
if err := m .Patch (ctx , got , mergeFrom ); err != nil {
338
- return objs , fmt .Errorf ("error while patching the `ClusterRoleBinding`: %w" , err )
331
+ return fmt .Errorf ("error while patching the `ClusterRoleBinding`: %w" , err )
339
332
}
340
333
m .log .Info ("ClusterRoleBinding patched" )
341
334
342
335
// we already patched the object, no need to go through the update path at the bottom of this function
343
- return objs , nil
336
+ return nil
344
337
case * v1.ConfigMap :
345
338
m .log .Info ("handling ConfigMap" )
346
339
m .editConfigMap (v , namespace )
@@ -359,31 +352,29 @@ func (m *OperatorManager) createNewClientObject(ctx context.Context, objs []clie
359
352
m .log .Info ("handling Deployment" )
360
353
err = m .Get (ctx , key , & appsv1.Deployment {})
361
354
default :
362
- return objs , errs .New ("unknown `client.Object`" )
355
+ return errs .New ("unknown `client.Object`" )
363
356
}
364
357
365
358
if err != nil {
366
359
if errors .IsNotFound (err ) {
367
360
// the object (with that objectKey) does not exist yet, so we create it
368
361
if err := m .Create (ctx , obj ); err != nil {
369
- return objs , fmt .Errorf ("error while creating the `client.Object`: %w" , err )
362
+ return fmt .Errorf ("error while creating the `client.Object`: %w" , err )
370
363
}
371
364
m .log .Info ("new `client.Object` created" )
372
365
373
- // Append the newly created obj.
374
- objs = append (objs , obj )
375
- return objs , nil
366
+ return nil
376
367
}
377
368
// something else went horribly wrong, abort
378
- return objs , fmt .Errorf ("error while fetching the `client.Object`: %w" , err )
369
+ return fmt .Errorf ("error while fetching the `client.Object`: %w" , err )
379
370
}
380
371
381
372
// if we made it this far, the object already exists, so we just update it
382
373
if err := m .Update (ctx , obj ); err != nil {
383
- return objs , fmt .Errorf ("error while updating the `client.Object`: %w" , err )
374
+ return fmt .Errorf ("error while updating the `client.Object`: %w" , err )
384
375
}
385
376
386
- return objs , nil
377
+ return nil
387
378
}
388
379
389
380
// editConfigMap adds info to cm
@@ -420,11 +411,11 @@ func (m *OperatorManager) ensureCleanMetadata(obj runtime.Object) error {
420
411
}
421
412
422
413
// createNamespace ensures namespace exists
423
- func (m * OperatorManager ) createNamespace (ctx context.Context , namespace string , objs []client. Object ) ([]client. Object , error ) {
414
+ func (m * OperatorManager ) createNamespace (ctx context.Context , namespace string ) error {
424
415
if err := m .Get (ctx , client.ObjectKey {Name : namespace }, & corev1.Namespace {}); err != nil {
425
416
// errors other than `not found`
426
417
if ! errors .IsNotFound (err ) {
427
- return nil , fmt .Errorf ("error while fetching namespace %v: %w" , namespace , err )
418
+ return fmt .Errorf ("error while fetching namespace %v: %w" , namespace , err )
428
419
}
429
420
430
421
// Create the namespace.
@@ -434,19 +425,16 @@ func (m *OperatorManager) createNamespace(ctx context.Context, namespace string,
434
425
pg .ManagedByLabelName : pg .ManagedByLabelValue ,
435
426
}
436
427
if err := m .Create (ctx , nsObj ); err != nil {
437
- return nil , fmt .Errorf ("error while creating namespace %v: %w" , namespace , err )
428
+ return fmt .Errorf ("error while creating namespace %v: %w" , namespace , err )
438
429
}
439
430
m .log .Info ("namespace created" , "name" , namespace )
440
-
441
- // Append the created namespace to the list of the created `client.Object`s.
442
- objs = append (objs , nsObj )
443
431
}
444
432
445
- return objs , nil
433
+ return nil
446
434
}
447
435
448
436
// createPodEnvironmentConfigMap creates a new ConfigMap with additional environment variables for the pods
449
- func (m * OperatorManager ) createPodEnvironmentConfigMap (ctx context.Context , namespace string , objs []client. Object ) ([]client. Object , error ) {
437
+ func (m * OperatorManager ) createPodEnvironmentConfigMap (ctx context.Context , namespace string ) error {
450
438
ns := types.NamespacedName {
451
439
Namespace : namespace ,
452
440
Name : PodEnvCMName ,
@@ -455,26 +443,26 @@ func (m *OperatorManager) createPodEnvironmentConfigMap(ctx context.Context, nam
455
443
// configmap already exists, nothing to do here
456
444
// we will update the configmap with the correct S3 config in the postgres controller
457
445
m .log .Info ("Pod Environment ConfigMap already exists" )
458
- return objs , nil
446
+ return nil
459
447
}
460
448
461
449
cm := & v1.ConfigMap {}
462
450
if err := m .SetName (cm , PodEnvCMName ); err != nil {
463
- return objs , fmt .Errorf ("error while setting the name of the new Pod Environment ConfigMap to %v: %w" , namespace , err )
451
+ return fmt .Errorf ("error while setting the name of the new Pod Environment ConfigMap to %v: %w" , namespace , err )
464
452
}
465
453
if err := m .SetNamespace (cm , namespace ); err != nil {
466
- return objs , fmt .Errorf ("error while setting the namespace of the new Pod Environment ConfigMap to %v: %w" , namespace , err )
454
+ return fmt .Errorf ("error while setting the namespace of the new Pod Environment ConfigMap to %v: %w" , namespace , err )
467
455
}
468
456
469
457
if err := m .Create (ctx , cm ); err != nil {
470
- return objs , fmt .Errorf ("error while creating the new Pod Environment ConfigMap: %w" , err )
458
+ return fmt .Errorf ("error while creating the new Pod Environment ConfigMap: %w" , err )
471
459
}
472
460
m .log .Info ("new Pod Environment ConfigMap created" )
473
461
474
- return objs , nil
462
+ return nil
475
463
}
476
464
477
- func (m * OperatorManager ) createOrUpdateSidecarsConfig (ctx context.Context , namespace string , objs []client. Object ) ([]client. Object , error ) {
465
+ func (m * OperatorManager ) createOrUpdateSidecarsConfig (ctx context.Context , namespace string ) error {
478
466
// try to fetch the global sidecars configmap
479
467
cns := types.NamespacedName {
480
468
// TODO don't use string literals here! name is dependent of the release name of the helm chart!
@@ -485,31 +473,29 @@ func (m *OperatorManager) createOrUpdateSidecarsConfig(ctx context.Context, name
485
473
if err := m .Get (ctx , cns , c ); err != nil {
486
474
// configmap with configuration does not exists, nothing we can do here...
487
475
m .log .Error (err , "could not fetch config for sidecars" )
488
- return objs , err
476
+ return err
489
477
}
490
478
491
479
// Add our sidecars configmap
492
- objs , err := m .createOrUpdateSidecarsConfigMap (ctx , namespace , c , objs )
493
- if err != nil {
494
- return objs , fmt .Errorf ("error while creating sidecars configmap %v: %w" , namespace , err )
480
+ if err := m .createOrUpdateSidecarsConfigMap (ctx , namespace , c ); err != nil {
481
+ return fmt .Errorf ("error while creating sidecars configmap %v: %w" , namespace , err )
495
482
}
496
483
497
484
// Add services for our sidecars
498
- objs , err = m .createOrUpdateExporterSidecarService (ctx , namespace , c , objs )
499
- if err != nil {
500
- return objs , fmt .Errorf ("error while creating sidecars services %v: %w" , namespace , err )
485
+ if err := m .createOrUpdateExporterSidecarService (ctx , namespace , c ); err != nil {
486
+ return fmt .Errorf ("error while creating sidecars services %v: %w" , namespace , err )
501
487
}
502
488
503
- return objs , nil
489
+ return nil
504
490
}
505
491
506
- func (m * OperatorManager ) createOrUpdateSidecarsConfigMap (ctx context.Context , namespace string , c * v1.ConfigMap , objs []client. Object ) ([]client. Object , error ) {
492
+ func (m * OperatorManager ) createOrUpdateSidecarsConfigMap (ctx context.Context , namespace string , c * v1.ConfigMap ) error {
507
493
sccm := & v1.ConfigMap {}
508
494
if err := m .SetName (sccm , sidecarsCMName ); err != nil {
509
- return objs , fmt .Errorf ("error while setting the name of the new Sidecars ConfigMap to %v: %w" , namespace , err )
495
+ return fmt .Errorf ("error while setting the name of the new Sidecars ConfigMap to %v: %w" , namespace , err )
510
496
}
511
497
if err := m .SetNamespace (sccm , namespace ); err != nil {
512
- return objs , fmt .Errorf ("error while setting the namespace of the new Sidecars ConfigMap to %v: %w" , namespace , err )
498
+ return fmt .Errorf ("error while setting the namespace of the new Sidecars ConfigMap to %v: %w" , namespace , err )
513
499
}
514
500
515
501
// initialize map
@@ -536,24 +522,24 @@ func (m *OperatorManager) createOrUpdateSidecarsConfigMap(ctx context.Context, n
536
522
if err := m .Get (ctx , ns , & v1.ConfigMap {}); err == nil {
537
523
// local configmap aleady exists, updating it
538
524
if err := m .Update (ctx , sccm ); err != nil {
539
- return objs , fmt .Errorf ("error while updating the new Sidecars ConfigMap: %w" , err )
525
+ return fmt .Errorf ("error while updating the new Sidecars ConfigMap: %w" , err )
540
526
}
541
527
m .log .Info ("Sidecars ConfigMap updated" )
542
- return objs , nil
528
+ return nil
543
529
}
544
530
// todo: handle errors other than `NotFound`
545
531
546
532
// local configmap does not exist, creating it
547
533
if err := m .Create (ctx , sccm ); err != nil {
548
- return objs , fmt .Errorf ("error while creating the new Sidecars ConfigMap: %w" , err )
534
+ return fmt .Errorf ("error while creating the new Sidecars ConfigMap: %w" , err )
549
535
}
550
536
m .log .Info ("new Sidecars ConfigMap created" )
551
537
552
- return objs , nil
538
+ return nil
553
539
}
554
540
555
541
// createOrUpdateExporterSidecarService ensures the neccessary services to acces the sidecars exist
556
- func (m * OperatorManager ) createOrUpdateExporterSidecarService (ctx context.Context , namespace string , c * v1.ConfigMap , objs []client. Object ) ([]client. Object , error ) {
542
+ func (m * OperatorManager ) createOrUpdateExporterSidecarService (ctx context.Context , namespace string , c * v1.ConfigMap ) error {
557
543
exporterServicePort , error := strconv .ParseInt (c .Data ["postgres-exporter-service-port" ], 10 , 32 )
558
544
if error != nil {
559
545
// todo log error
@@ -569,17 +555,17 @@ func (m *OperatorManager) createOrUpdateExporterSidecarService(ctx context.Conte
569
555
pes := & v1.Service {}
570
556
571
557
if err := m .SetName (pes , postgresExporterServiceName ); err != nil {
572
- return objs , fmt .Errorf ("error while setting the name of the postgres-exporter service to %v: %w" , namespace , err )
558
+ return fmt .Errorf ("error while setting the name of the postgres-exporter service to %v: %w" , namespace , err )
573
559
}
574
560
if err := m .SetNamespace (pes , namespace ); err != nil {
575
- return objs , fmt .Errorf ("error while setting the namespace of the postgres-exporter service to %v: %w" , namespace , err )
561
+ return fmt .Errorf ("error while setting the namespace of the postgres-exporter service to %v: %w" , namespace , err )
576
562
}
577
563
labels := map [string ]string {
578
564
// "application": "spilo", // TODO check if we still need that label, IsOperatorDeletable won't work anymore if we set it.
579
565
"app" : "postgres-exporter" ,
580
566
}
581
567
if err := m .SetLabels (pes , labels ); err != nil {
582
- return objs , fmt .Errorf ("error while setting the namespace of the postgres-exporter service to %v: %w" , namespace , err )
568
+ return fmt .Errorf ("error while setting the namespace of the postgres-exporter service to %v: %w" , namespace , err )
583
569
}
584
570
585
571
pes .Spec .Ports = []v1.ServicePort {
@@ -607,20 +593,20 @@ func (m *OperatorManager) createOrUpdateExporterSidecarService(ctx context.Conte
607
593
pes .Spec .ClusterIP = old .Spec .ClusterIP
608
594
pes .ObjectMeta .ResourceVersion = old .GetObjectMeta ().GetResourceVersion ()
609
595
if err := m .Update (ctx , pes ); err != nil {
610
- return objs , fmt .Errorf ("error while updating the postgres-exporter service: %w" , err )
596
+ return fmt .Errorf ("error while updating the postgres-exporter service: %w" , err )
611
597
}
612
598
m .log .Info ("postgres-exporter service updated" )
613
- return objs , nil
599
+ return nil
614
600
}
615
601
// todo: handle errors other than `NotFound`
616
602
617
603
// local configmap does not exist, creating it
618
604
if err := m .Create (ctx , pes ); err != nil {
619
- return objs , fmt .Errorf ("error while creating the postgres-exporter service: %w" , err )
605
+ return fmt .Errorf ("error while creating the postgres-exporter service: %w" , err )
620
606
}
621
607
m .log .Info ("postgres-exporter service created" )
622
608
623
- return objs , nil
609
+ return nil
624
610
}
625
611
626
612
func (m * OperatorManager ) deletePodEnvironmentConfigMap (ctx context.Context , namespace string ) error {
@@ -689,7 +675,7 @@ func (m *OperatorManager) UpdateAllOperators(ctx context.Context) error {
689
675
// update each namespace
690
676
for _ , ns := range nsList .Items {
691
677
m .log .Info ("Updating postgres operator installation" , "namespace" , ns .Name )
692
- if _ , err := m .InstallOrUpdateOperator (ctx , ns .Name ); err != nil {
678
+ if err := m .InstallOrUpdateOperator (ctx , ns .Name ); err != nil {
693
679
return err
694
680
}
695
681
}
0 commit comments