Skip to content

Commit 51be89f

Browse files
authored
Remove redundant code (#186)
1 parent 576c30b commit 51be89f

File tree

2 files changed

+62
-77
lines changed

2 files changed

+62
-77
lines changed

controllers/postgres_controller.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,7 @@ func (r *PostgresReconciler) ensureZalandoDependencies(ctx context.Context, p *p
241241
}
242242

243243
if !isInstalled {
244-
_, err := r.InstallOrUpdateOperator(ctx, namespace)
245-
if err != nil {
244+
if err := r.InstallOrUpdateOperator(ctx, namespace); err != nil {
246245
return fmt.Errorf("error while installing zalando dependencies: %w", err)
247246
}
248247
}

pkg/operatormanager/operatormanager.go

Lines changed: 61 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -102,45 +102,40 @@ func New(conf *rest.Config, fileName string, scheme *runtime.Scheme, log logr.Lo
102102
}
103103

104104
// 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 {
108106
// 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)
112109
}
113110

114111
// 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)
118114
}
119115

120116
// 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)
124119
}
125120

126121
// Decode each YAML to `client.Object`, add the namespace to it and install it.
127122
for _, item := range m.list.Items {
128123
obj, _, err := m.Decoder.Decode(item.Raw, nil, nil)
129124
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)
131126
}
132127

133128
cltObject, ok := obj.(client.Object)
134129
if !ok {
135-
return objs, fmt.Errorf("unable to cast into client.Object")
130+
return fmt.Errorf("unable to cast into client.Object")
136131
}
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)
139134
}
140135
}
141136

142137
m.log.Info("operator installed")
143-
return objs, nil
138+
return nil
144139
}
145140

146141
// 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
257252
}
258253

259254
// 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 {
261256
// remove any unwanted annotations, uids etc. Remember, these objects come straight from the YAML.
262257
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)
264259
}
265260

266261
// use our current namespace, not the one from the YAML
267262
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)
269264
}
270265

271266
// generate a proper object key for each object
272267
key, err := m.toObjectKey(obj, namespace)
273268
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)
275270
}
276271

277272
// perform different modifications on the parsed objects based on their kind
@@ -310,37 +305,35 @@ func (m *OperatorManager) createNewClientObject(ctx context.Context, objs []clie
310305
got := &rbacv1.ClusterRoleBinding{}
311306
if err := m.Get(ctx, key, got); err != nil {
312307
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)
314309
}
315310

316311
// Create the ClusterRoleBinding
317312
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)
319314
}
320315
m.log.Info("ClusterRoleBinding created")
321316

322-
// Append the newly created obj
323-
objs = append(objs, obj)
324-
return objs, nil
317+
return nil
325318
}
326319

327320
// If the ServiceAccount already exists, return.
328321
for i := range got.Subjects {
329322
if got.Subjects[i].Kind == "ServiceAccount" && got.Subjects[i].Namespace == namespace {
330-
return objs, nil
323+
return nil
331324
}
332325
}
333326

334327
// Patch the already existing ClusterRoleBinding
335328
mergeFrom := client.MergeFrom(got.DeepCopy())
336329
got.Subjects = append(got.Subjects, v.Subjects[0])
337330
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)
339332
}
340333
m.log.Info("ClusterRoleBinding patched")
341334

342335
// 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
344337
case *v1.ConfigMap:
345338
m.log.Info("handling ConfigMap")
346339
m.editConfigMap(v, namespace)
@@ -359,31 +352,29 @@ func (m *OperatorManager) createNewClientObject(ctx context.Context, objs []clie
359352
m.log.Info("handling Deployment")
360353
err = m.Get(ctx, key, &appsv1.Deployment{})
361354
default:
362-
return objs, errs.New("unknown `client.Object`")
355+
return errs.New("unknown `client.Object`")
363356
}
364357

365358
if err != nil {
366359
if errors.IsNotFound(err) {
367360
// the object (with that objectKey) does not exist yet, so we create it
368361
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)
370363
}
371364
m.log.Info("new `client.Object` created")
372365

373-
// Append the newly created obj.
374-
objs = append(objs, obj)
375-
return objs, nil
366+
return nil
376367
}
377368
// 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)
379370
}
380371

381372
// if we made it this far, the object already exists, so we just update it
382373
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)
384375
}
385376

386-
return objs, nil
377+
return nil
387378
}
388379

389380
// editConfigMap adds info to cm
@@ -420,11 +411,11 @@ func (m *OperatorManager) ensureCleanMetadata(obj runtime.Object) error {
420411
}
421412

422413
// 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 {
424415
if err := m.Get(ctx, client.ObjectKey{Name: namespace}, &corev1.Namespace{}); err != nil {
425416
// errors other than `not found`
426417
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)
428419
}
429420

430421
// Create the namespace.
@@ -434,19 +425,16 @@ func (m *OperatorManager) createNamespace(ctx context.Context, namespace string,
434425
pg.ManagedByLabelName: pg.ManagedByLabelValue,
435426
}
436427
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)
438429
}
439430
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)
443431
}
444432

445-
return objs, nil
433+
return nil
446434
}
447435

448436
// 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 {
450438
ns := types.NamespacedName{
451439
Namespace: namespace,
452440
Name: PodEnvCMName,
@@ -455,26 +443,26 @@ func (m *OperatorManager) createPodEnvironmentConfigMap(ctx context.Context, nam
455443
// configmap already exists, nothing to do here
456444
// we will update the configmap with the correct S3 config in the postgres controller
457445
m.log.Info("Pod Environment ConfigMap already exists")
458-
return objs, nil
446+
return nil
459447
}
460448

461449
cm := &v1.ConfigMap{}
462450
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)
464452
}
465453
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)
467455
}
468456

469457
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)
471459
}
472460
m.log.Info("new Pod Environment ConfigMap created")
473461

474-
return objs, nil
462+
return nil
475463
}
476464

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 {
478466
// try to fetch the global sidecars configmap
479467
cns := types.NamespacedName{
480468
// 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
485473
if err := m.Get(ctx, cns, c); err != nil {
486474
// configmap with configuration does not exists, nothing we can do here...
487475
m.log.Error(err, "could not fetch config for sidecars")
488-
return objs, err
476+
return err
489477
}
490478

491479
// 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)
495482
}
496483

497484
// 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)
501487
}
502488

503-
return objs, nil
489+
return nil
504490
}
505491

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 {
507493
sccm := &v1.ConfigMap{}
508494
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)
510496
}
511497
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)
513499
}
514500

515501
// initialize map
@@ -536,24 +522,24 @@ func (m *OperatorManager) createOrUpdateSidecarsConfigMap(ctx context.Context, n
536522
if err := m.Get(ctx, ns, &v1.ConfigMap{}); err == nil {
537523
// local configmap aleady exists, updating it
538524
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)
540526
}
541527
m.log.Info("Sidecars ConfigMap updated")
542-
return objs, nil
528+
return nil
543529
}
544530
// todo: handle errors other than `NotFound`
545531

546532
// local configmap does not exist, creating it
547533
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)
549535
}
550536
m.log.Info("new Sidecars ConfigMap created")
551537

552-
return objs, nil
538+
return nil
553539
}
554540

555541
// 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 {
557543
exporterServicePort, error := strconv.ParseInt(c.Data["postgres-exporter-service-port"], 10, 32)
558544
if error != nil {
559545
// todo log error
@@ -569,17 +555,17 @@ func (m *OperatorManager) createOrUpdateExporterSidecarService(ctx context.Conte
569555
pes := &v1.Service{}
570556

571557
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)
573559
}
574560
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)
576562
}
577563
labels := map[string]string{
578564
// "application": "spilo", // TODO check if we still need that label, IsOperatorDeletable won't work anymore if we set it.
579565
"app": "postgres-exporter",
580566
}
581567
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)
583569
}
584570

585571
pes.Spec.Ports = []v1.ServicePort{
@@ -607,20 +593,20 @@ func (m *OperatorManager) createOrUpdateExporterSidecarService(ctx context.Conte
607593
pes.Spec.ClusterIP = old.Spec.ClusterIP
608594
pes.ObjectMeta.ResourceVersion = old.GetObjectMeta().GetResourceVersion()
609595
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)
611597
}
612598
m.log.Info("postgres-exporter service updated")
613-
return objs, nil
599+
return nil
614600
}
615601
// todo: handle errors other than `NotFound`
616602

617603
// local configmap does not exist, creating it
618604
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)
620606
}
621607
m.log.Info("postgres-exporter service created")
622608

623-
return objs, nil
609+
return nil
624610
}
625611

626612
func (m *OperatorManager) deletePodEnvironmentConfigMap(ctx context.Context, namespace string) error {
@@ -689,7 +675,7 @@ func (m *OperatorManager) UpdateAllOperators(ctx context.Context) error {
689675
// update each namespace
690676
for _, ns := range nsList.Items {
691677
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 {
693679
return err
694680
}
695681
}

0 commit comments

Comments
 (0)