From 123c3743bc23766776782cd27acfa5ab64a98809 Mon Sep 17 00:00:00 2001
From: Matt Christiansen <matt@nikore.net>
Date: Tue, 24 Aug 2021 17:15:37 -0700
Subject: [PATCH 1/3] Update imagepullsecrets in service account on existing
 clusters/serviceaccounts

Signed-off-by: Matt Christiansen <matt@nikore.net>
---
 .../zookeepercluster_controller.go            |  5 ++++
 .../zookeepercluster_controller_test.go       | 30 +++++++++++++++++++
 2 files changed, 35 insertions(+)

diff --git a/pkg/controller/zookeepercluster/zookeepercluster_controller.go b/pkg/controller/zookeepercluster/zookeepercluster_controller.go
index e48c3509e..1c8218c76 100644
--- a/pkg/controller/zookeepercluster/zookeepercluster_controller.go
+++ b/pkg/controller/zookeepercluster/zookeepercluster_controller.go
@@ -235,6 +235,11 @@ func (r *ReconcileZookeeperCluster) reconcileStatefulSet(instance *zookeeperv1be
 			}
 		} else if err != nil {
 			return err
+		} else {
+			foundServiceAccount.ImagePullSecrets = serviceAccount.ImagePullSecrets
+			if err = r.client.Update(context.TODO(), foundServiceAccount); err != nil {
+				return err
+			}
 		}
 	}
 	sts := zk.MakeStatefulSet(instance)
diff --git a/pkg/controller/zookeepercluster/zookeepercluster_controller_test.go b/pkg/controller/zookeepercluster/zookeepercluster_controller_test.go
index 55b5347db..6035be7a8 100644
--- a/pkg/controller/zookeepercluster/zookeepercluster_controller_test.go
+++ b/pkg/controller/zookeepercluster/zookeepercluster_controller_test.go
@@ -250,6 +250,36 @@ var _ = Describe("ZookeeperCluster Controller", func() {
 
 		})
 
+		Context("With update to ImagePullSecrets", func() {
+			var (
+				cl  client.Client
+				err error
+			)
+
+			BeforeEach(func() {
+				z.WithDefaults()
+				z.Spec.Pod.ServiceAccountName = "zookeeper"
+				z.Status.Init()
+				next := z.DeepCopy()
+				sa := zk.MakeServiceAccount(z)
+				next.Spec.Pod.ImagePullSecrets = []corev1.LocalObjectReference{{Name: "local-key"}}
+				cl = fake.NewFakeClientWithScheme(s, []runtime.Object{next, sa}...)
+				r = &ReconcileZookeeperCluster{client: cl, scheme: s, zkClient: mockZkClient}
+				res, err = r.Reconcile(req)
+			})
+
+			It("should not raise an error", func() {
+				Ω(err).To(BeNil())
+			})
+
+			It("should update the service account", func() {
+				foundSA := &corev1.ServiceAccount{}
+				err = cl.Get(context.TODO(), types.NamespacedName{Name: "zookeeper", Namespace: Namespace}, foundSA)
+				Ω(err).To(BeNil())
+				Ω(foundSA.ImagePullSecrets).To(HaveLen(1))
+			})
+		})
+
 		Context("upgrading the image for zookeepercluster", func() {
 			var (
 				cl  client.Client

From 50ddb403d573cc7b5e53ef95c6cc1f1f68ee426e Mon Sep 17 00:00:00 2001
From: Matt Christiansen <matt@nikore.net>
Date: Wed, 25 Aug 2021 17:47:27 -0700
Subject: [PATCH 2/3] Updating unit test to cover create and update

Signed-off-by: Matt Christiansen <matt@nikore.net>
---
 .../zookeepercluster_controller_test.go       | 23 +++++++++++++++----
 1 file changed, 18 insertions(+), 5 deletions(-)

diff --git a/pkg/controller/zookeepercluster/zookeepercluster_controller_test.go b/pkg/controller/zookeepercluster/zookeepercluster_controller_test.go
index 6035be7a8..0a2af8523 100644
--- a/pkg/controller/zookeepercluster/zookeepercluster_controller_test.go
+++ b/pkg/controller/zookeepercluster/zookeepercluster_controller_test.go
@@ -252,17 +252,18 @@ var _ = Describe("ZookeeperCluster Controller", func() {
 
 		Context("With update to ImagePullSecrets", func() {
 			var (
-				cl  client.Client
-				err error
+				cl   client.Client
+				err  error
+				next *v1beta1.ZookeeperCluster
+				sa   *corev1.ServiceAccount
 			)
 
 			BeforeEach(func() {
 				z.WithDefaults()
 				z.Spec.Pod.ServiceAccountName = "zookeeper"
 				z.Status.Init()
-				next := z.DeepCopy()
-				sa := zk.MakeServiceAccount(z)
-				next.Spec.Pod.ImagePullSecrets = []corev1.LocalObjectReference{{Name: "local-key"}}
+				next = z.DeepCopy()
+				sa = zk.MakeServiceAccount(z)
 				cl = fake.NewFakeClientWithScheme(s, []runtime.Object{next, sa}...)
 				r = &ReconcileZookeeperCluster{client: cl, scheme: s, zkClient: mockZkClient}
 				res, err = r.Reconcile(req)
@@ -272,7 +273,19 @@ var _ = Describe("ZookeeperCluster Controller", func() {
 				Ω(err).To(BeNil())
 			})
 
+			It("should create the service account", func() {
+				foundSA := &corev1.ServiceAccount{}
+				err = cl.Get(context.TODO(), types.NamespacedName{Name: "zookeeper", Namespace: Namespace}, foundSA)
+				Ω(err).To(BeNil())
+				Ω(foundSA.ImagePullSecrets).To(HaveLen(0))
+			})
 			It("should update the service account", func() {
+				next.Spec.Pod.ImagePullSecrets = []corev1.LocalObjectReference{{Name: "test-pull-secret"}}
+				cl = fake.NewFakeClientWithScheme(s, []runtime.Object{next, sa}...)
+				r = &ReconcileZookeeperCluster{client: cl, scheme: s, zkClient: mockZkClient}
+				_, err := r.Reconcile(req)
+				Ω(err).To(BeNil())
+
 				foundSA := &corev1.ServiceAccount{}
 				err = cl.Get(context.TODO(), types.NamespacedName{Name: "zookeeper", Namespace: Namespace}, foundSA)
 				Ω(err).To(BeNil())

From 1b64965d8967a2abf8ba9794d166f767b2a8024e Mon Sep 17 00:00:00 2001
From: Matt Christiansen <matt@nikore.net>
Date: Wed, 25 Aug 2021 18:11:19 -0700
Subject: [PATCH 3/3] Adding logging and making code look the same as create

Signed-off-by: Matt Christiansen <matt@nikore.net>
---
 .../zookeepercluster/zookeepercluster_controller.go           | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/pkg/controller/zookeepercluster/zookeepercluster_controller.go b/pkg/controller/zookeepercluster/zookeepercluster_controller.go
index 1c8218c76..e59e8af19 100644
--- a/pkg/controller/zookeepercluster/zookeepercluster_controller.go
+++ b/pkg/controller/zookeepercluster/zookeepercluster_controller.go
@@ -237,7 +237,9 @@ func (r *ReconcileZookeeperCluster) reconcileStatefulSet(instance *zookeeperv1be
 			return err
 		} else {
 			foundServiceAccount.ImagePullSecrets = serviceAccount.ImagePullSecrets
-			if err = r.client.Update(context.TODO(), foundServiceAccount); err != nil {
+			r.log.Info("Updating ServiceAccount", "ServiceAccount.Namespace", serviceAccount.Namespace, "ServiceAccount.Name", serviceAccount.Name)
+			err = r.client.Update(context.TODO(), foundServiceAccount)
+			if err != nil {
 				return err
 			}
 		}