[Operator] Store the ConfigMap versions in GerritStatus

This follows the same pattern as for Secrets and is more reliable
than to check for the reconciliation result.

Change-Id: I1321d02473956b29dbc3c2d4fa6319deb14bc8cb
diff --git a/Documentation/operator-api-reference.md b/Documentation/operator-api-reference.md
index fb7b37c..908b380 100644
--- a/Documentation/operator-api-reference.md
+++ b/Documentation/operator-api-reference.md
@@ -323,7 +323,7 @@
 ---
 
 **Group**: gerritoperator.google.com \
-**Version**: v1alpha3 \
+**Version**: v1alpha4 \
 **Kind**: Gerrit
 
 ---
@@ -340,7 +340,7 @@
 Example:
 
 ```yaml
-apiVersion: "gerritoperator.google.com/v1alpha3"
+apiVersion: "gerritoperator.google.com/v1alpha4"
 kind: Gerrit
 metadata:
   name: gerrit
@@ -893,6 +893,7 @@
 | Field | Type | Description |
 |---|---|---|
 | `ready` | `boolean` | Whether the Gerrit instance is ready |
+| `appliedConfigMapVersions` | `Map<String, String>` | Versions of each ConfigMap currently mounted into Gerrit pods |
 | `appliedSecretVersions` | `Map<String, String>` | Versions of each secret currently mounted into Gerrit pods |
 
 ## IngressConfig
diff --git a/operator/k8s/gerrit.sample.yaml b/operator/k8s/gerrit.sample.yaml
index 37339a5..8e91280 100644
--- a/operator/k8s/gerrit.sample.yaml
+++ b/operator/k8s/gerrit.sample.yaml
@@ -1,4 +1,4 @@
-apiVersion: "gerritoperator.google.com/v1alpha3"
+apiVersion: "gerritoperator.google.com/v1alpha4"
 kind: Gerrit
 metadata:
   name: gerrit
diff --git a/operator/src/main/java/com/google/gerrit/k8s/operator/gerrit/GerritReconciler.java b/operator/src/main/java/com/google/gerrit/k8s/operator/gerrit/GerritReconciler.java
index 77d0673..7f55f7e 100644
--- a/operator/src/main/java/com/google/gerrit/k8s/operator/gerrit/GerritReconciler.java
+++ b/operator/src/main/java/com/google/gerrit/k8s/operator/gerrit/GerritReconciler.java
@@ -37,13 +37,9 @@
 import io.javaoperatorsdk.operator.api.reconciler.Reconciler;
 import io.javaoperatorsdk.operator.api.reconciler.UpdateControl;
 import io.javaoperatorsdk.operator.api.reconciler.dependent.Dependent;
-import io.javaoperatorsdk.operator.api.reconciler.dependent.ReconcileResult;
-import io.javaoperatorsdk.operator.api.reconciler.dependent.ReconcileResult.Operation;
-import io.javaoperatorsdk.operator.api.reconciler.dependent.managed.ManagedDependentResourceContext;
 import io.javaoperatorsdk.operator.processing.dependent.workflow.WorkflowReconcileResult;
 import io.javaoperatorsdk.operator.processing.event.source.EventSource;
 import io.javaoperatorsdk.operator.processing.event.source.informer.InformerEventSource;
-import java.util.Collection;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Optional;
@@ -126,6 +122,30 @@
       status.setReady(false);
     }
 
+    Map<String, String> cmVersions = new HashMap<>();
+
+    cmVersions.put(
+        GerritConfigMap.getName(gerrit),
+        client
+            .configMaps()
+            .inNamespace(gerrit.getMetadata().getNamespace())
+            .withName(GerritConfigMap.getName(gerrit))
+            .get()
+            .getMetadata()
+            .getResourceVersion());
+
+    cmVersions.put(
+        GerritInitConfigMap.getName(gerrit),
+        client
+            .configMaps()
+            .inNamespace(gerrit.getMetadata().getNamespace())
+            .withName(GerritInitConfigMap.getName(gerrit))
+            .get()
+            .getMetadata()
+            .getResourceVersion());
+
+    status.setAppliedConfigMapVersions(cmVersions);
+
     Map<String, String> secretVersions = new HashMap<>();
     Optional<Secret> gerritSecret = context.getSecondaryResource(Secret.class);
     if (gerritSecret.isPresent()) {
@@ -138,21 +158,43 @@
     return gerrit;
   }
 
-  @SuppressWarnings("rawtypes")
   private boolean isGerritRestartRequired(Gerrit gerrit, Context<Gerrit> context) {
-    ManagedDependentResourceContext managedResources = context.managedDependentResourceContext();
-    Optional<WorkflowReconcileResult> reconcileResults =
-        managedResources.getWorkflowReconcileResult();
-    if (reconcileResults.isPresent()) {
-      Collection<ReconcileResult> results = reconcileResults.get().getReconcileResults().values();
-      for (ReconcileResult r : results) {
-        if (r.getSingleResource().isPresent()
-            && r.getSingleResource().get() instanceof ConfigMap
-            && (r.getSingleOperation().equals(Operation.UPDATED)
-                || r.getSingleOperation().equals(Operation.CREATED))) {
-          return true;
-        }
-      }
+    String gerritConfigMapName = GerritConfigMap.getName(gerrit);
+    String gerritConfigMapVersion =
+        client
+            .configMaps()
+            .inNamespace(gerrit.getMetadata().getNamespace())
+            .withName(gerritConfigMapName)
+            .get()
+            .getMetadata()
+            .getResourceVersion();
+    if (!gerritConfigMapVersion.equals(
+        gerrit.getStatus().getAppliedConfigMapVersions().get(gerritConfigMapName))) {
+      logger.atInfo().log(
+          "Looking up ConfigMap: %s; Installed configmap resource version: %s; Resource version known to Gerrit: %s",
+          gerritConfigMapName,
+          gerritConfigMapVersion,
+          gerrit.getStatus().getAppliedConfigMapVersions().get(gerritConfigMapName));
+      return true;
+    }
+
+    String gerritInitConfigMapName = GerritInitConfigMap.getName(gerrit);
+    String gerritInitConfigMapVersion =
+        client
+            .configMaps()
+            .inNamespace(gerrit.getMetadata().getNamespace())
+            .withName(gerritInitConfigMapName)
+            .get()
+            .getMetadata()
+            .getResourceVersion();
+    if (!gerritInitConfigMapVersion.equals(
+        gerrit.getStatus().getAppliedConfigMapVersions().get(gerritInitConfigMapName))) {
+      logger.atInfo().log(
+          "Looking up ConfigMap: %s; Installed configmap resource version: %s; Resource version known to Gerrit: %s",
+          gerritInitConfigMapName,
+          gerritInitConfigMapVersion,
+          gerrit.getStatus().getAppliedConfigMapVersions().get(gerritInitConfigMapName));
+      return true;
     }
 
     String secretName = gerrit.getSpec().getSecretRef();
diff --git a/operator/src/main/java/com/google/gerrit/k8s/operator/gerrit/model/Gerrit.java b/operator/src/main/java/com/google/gerrit/k8s/operator/gerrit/model/Gerrit.java
index 42e7a95..943eb56 100644
--- a/operator/src/main/java/com/google/gerrit/k8s/operator/gerrit/model/Gerrit.java
+++ b/operator/src/main/java/com/google/gerrit/k8s/operator/gerrit/model/Gerrit.java
@@ -23,7 +23,7 @@
 import org.apache.commons.lang3.builder.ToStringStyle;
 
 @Group("gerritoperator.google.com")
-@Version("v1alpha3")
+@Version("v1alpha4")
 @ShortNames("gcr")
 public class Gerrit extends CustomResource<GerritSpec, GerritStatus> implements Namespaced {
   private static final long serialVersionUID = 2L;
diff --git a/operator/src/main/java/com/google/gerrit/k8s/operator/gerrit/model/GerritStatus.java b/operator/src/main/java/com/google/gerrit/k8s/operator/gerrit/model/GerritStatus.java
index 825db27..aeeb403 100644
--- a/operator/src/main/java/com/google/gerrit/k8s/operator/gerrit/model/GerritStatus.java
+++ b/operator/src/main/java/com/google/gerrit/k8s/operator/gerrit/model/GerritStatus.java
@@ -19,6 +19,7 @@
 
 public class GerritStatus {
   private boolean ready = false;
+  private Map<String, String> appliedConfigMapVersions = new HashMap<>();
   private Map<String, String> appliedSecretVersions = new HashMap<>();
 
   public boolean isReady() {
@@ -29,6 +30,14 @@
     this.ready = ready;
   }
 
+  public Map<String, String> getAppliedConfigMapVersions() {
+    return appliedConfigMapVersions;
+  }
+
+  public void setAppliedConfigMapVersions(Map<String, String> appliedConfigMapVersions) {
+    this.appliedConfigMapVersions = appliedConfigMapVersions;
+  }
+
   public Map<String, String> getAppliedSecretVersions() {
     return appliedSecretVersions;
   }