[Operator] Make NfsIdmapdConfigMap a managed dependent resource

Change-Id: If21674e68c37b159fe65c5f4b72bf0036d7190dc
diff --git a/operator/src/main/java/com/google/gerrit/k8s/operator/cluster/GerritClusterReconciler.java b/operator/src/main/java/com/google/gerrit/k8s/operator/cluster/GerritClusterReconciler.java
index f7de3a8..a7d00da 100644
--- a/operator/src/main/java/com/google/gerrit/k8s/operator/cluster/GerritClusterReconciler.java
+++ b/operator/src/main/java/com/google/gerrit/k8s/operator/cluster/GerritClusterReconciler.java
@@ -38,23 +38,22 @@
 @ControllerConfiguration(
     dependents = {
       @Dependent(type = GitRepositoriesPVC.class, useEventSourceWithName = PVC_EVENT_SOURCE),
-      @Dependent(type = GerritLogsPVC.class, useEventSourceWithName = PVC_EVENT_SOURCE)
+      @Dependent(type = GerritLogsPVC.class, useEventSourceWithName = PVC_EVENT_SOURCE),
+      @Dependent(
+          type = NfsIdmapdConfigMap.class,
+          reconcilePrecondition = NfsWorkaroundCondition.class)
     })
 public class GerritClusterReconciler
     implements Reconciler<GerritCluster>, EventSourceInitializer<GerritCluster> {
   public static final String PVC_EVENT_SOURCE = "pvc-event-source";
   private final KubernetesClient kubernetesClient;
 
-  private NfsIdmapdConfigMap dependentNfsImapdConfigMap;
   private PluginCachePVC dependentPluginCachePvc;
   private GerritIngress gerritIngress;
 
   public GerritClusterReconciler(KubernetesClient client) {
     this.kubernetesClient = client;
 
-    this.dependentNfsImapdConfigMap = new NfsIdmapdConfigMap();
-    this.dependentNfsImapdConfigMap.setKubernetesClient(kubernetesClient);
-
     this.dependentPluginCachePvc = new PluginCachePVC();
     this.dependentPluginCachePvc.setKubernetesClient(kubernetesClient);
 
@@ -88,7 +87,6 @@
     Map<String, EventSource> eventSources =
         EventSourceInitializer.nameEventSources(
             gerritEventSource,
-            this.dependentNfsImapdConfigMap.initEventSource(context),
             this.dependentPluginCachePvc.initEventSource(context),
             this.gerritIngress.initEventSource(context));
     eventSources.put(PVC_EVENT_SOURCE, pvcEventSource);
@@ -99,11 +97,6 @@
   @Override
   public UpdateControl<GerritCluster> reconcile(
       GerritCluster gerritCluster, Context<GerritCluster> context) {
-    if (gerritCluster.getSpec().getStorageClasses().getNfsWorkaround().isEnabled()
-        && gerritCluster.getSpec().getStorageClasses().getNfsWorkaround().getIdmapdConfig()
-            != null) {
-      dependentNfsImapdConfigMap.reconcile(gerritCluster, context);
-    }
 
     if (gerritCluster.getSpec().getPluginCacheStorage().isEnabled()) {
       dependentPluginCachePvc.reconcile(gerritCluster, context);
diff --git a/operator/src/main/java/com/google/gerrit/k8s/operator/cluster/NfsWorkaroundCondition.java b/operator/src/main/java/com/google/gerrit/k8s/operator/cluster/NfsWorkaroundCondition.java
new file mode 100644
index 0000000..7d969e8
--- /dev/null
+++ b/operator/src/main/java/com/google/gerrit/k8s/operator/cluster/NfsWorkaroundCondition.java
@@ -0,0 +1,29 @@
+// Copyright (C) 2022 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.gerrit.k8s.operator.cluster;
+
+import io.fabric8.kubernetes.api.model.ConfigMap;
+import io.javaoperatorsdk.operator.api.reconciler.Context;
+import io.javaoperatorsdk.operator.processing.dependent.workflow.Condition;
+
+public class NfsWorkaroundCondition implements Condition<ConfigMap, GerritCluster> {
+
+  @Override
+  public boolean isMet(
+      GerritCluster gerritCluster, ConfigMap secondary, Context<GerritCluster> context) {
+    NfsWorkaroundConfig cfg = gerritCluster.getSpec().getStorageClasses().getNfsWorkaround();
+    return cfg.isEnabled() && cfg.getIdmapdConfig() != null;
+  }
+}