gyfora commented on code in PR #351:
URL: 
https://github.com/apache/flink-kubernetes-operator/pull/351#discussion_r953822021


##########
flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/reconciler/deployment/AbstractFlinkResourceReconciler.java:
##########
@@ -122,16 +123,23 @@ public final void reconcile(CR cr, Context<?> ctx) throws 
Exception {
                 
cr.getStatus().getReconciliationStatus().deserializeLastReconciledSpec();
         SPEC currentDeploySpec = cr.getSpec();
 
+        var specDiff = currentDeploySpec.diff(lastReconciledSpec);
+
+        var flinkService = getFlinkService(cr, ctx);
+
         boolean specChanged =
-                reconciliationStatus.getState() == 
ReconciliationState.UPGRADING
-                        || !currentDeploySpec.equals(lastReconciledSpec);
+                DiffType.IGNORE != specDiff.getType()
+                        || reconciliationStatus.getState() == 
ReconciliationState.UPGRADING;
+
         var observeConfig = getObserveConfig(cr, ctx);
-        var flinkService = getFlinkService(cr, ctx);
+
         if (specChanged) {
+
             if (checkNewSpecAlreadyDeployed(cr, deployConfig)) {
                 return;
             }
-            LOG.info(MSG_SPEC_CHANGED);
+            LOG.info(String.format(MSG_SPEC_CHANGED, specDiff.getType()));
+            LOG.info(specDiff.toString());

Review Comment:
   We should log this in a single line



##########
flink-kubernetes-operator/src/test/java/org/apache/flink/kubernetes/operator/reconciler/diff/SpecDiffTest.java:
##########
@@ -0,0 +1,171 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.flink.kubernetes.operator.reconciler.diff;
+
+import org.apache.flink.configuration.CoreOptions;
+import org.apache.flink.kubernetes.operator.TestUtils;
+import org.apache.flink.kubernetes.operator.crd.spec.AbstractFlinkSpec;
+import org.apache.flink.kubernetes.operator.crd.spec.FlinkDeploymentSpec;
+import org.apache.flink.kubernetes.operator.crd.spec.FlinkSessionJobSpec;
+import org.apache.flink.kubernetes.operator.crd.spec.UpgradeMode;
+import org.apache.flink.kubernetes.operator.reconciler.ReconciliationUtils;
+
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.stream.Stream;
+
+import static 
org.apache.flink.kubernetes.operator.config.KubernetesOperatorConfigOptions.OPERATOR_RECONCILE_INTERVAL;
+import static 
org.apache.flink.kubernetes.operator.metrics.KubernetesOperatorMetricOptions.SCOPE_NAMING_KUBERNETES_OPERATOR;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.params.provider.Arguments.arguments;
+
+/** Spec diff test. */
+public class SpecDiffTest {
+
+    @ParameterizedTest
+    @MethodSource("applicationTestParams")
+    public void testIgnore(AbstractFlinkSpec left, AbstractFlinkSpec right) {
+        var diff = left.diff(right);
+        assertEquals(DiffType.IGNORE, diff.getType());
+        assertEquals(0, diff.getNumDiffs());
+        if (right.getJob() != null) {
+            right.getJob().setUpgradeMode(UpgradeMode.LAST_STATE);
+            right.getJob().setAllowNonRestoredState(true);
+            right.getJob().setInitialSavepointPath("local:///tmp");
+            right.getJob().setSavepointTriggerNonce(123L);
+            diff = left.diff(right);
+            assertEquals(DiffType.IGNORE, diff.getType());
+            assertEquals(4, diff.getNumDiffs());
+        }
+    }
+
+    @ParameterizedTest
+    @MethodSource("applicationTestParams")
+    public void testScale(AbstractFlinkSpec left, AbstractFlinkSpec right) {
+        var diff = left.diff(right);
+        assertEquals(DiffType.IGNORE, diff.getType());
+        assertEquals(0, diff.getNumDiffs());
+
+        if (right.getJob() != null) {
+            right.getJob().setParallelism(100);
+            diff = left.diff(right);
+            assertEquals(DiffType.SCALE, diff.getType());
+            assertEquals(1, diff.getNumDiffs());
+
+            right.getJob().setUpgradeMode(UpgradeMode.LAST_STATE);
+            right.getJob().setAllowNonRestoredState(true);
+            right.getJob().setInitialSavepointPath("local:///tmp");
+            right.getJob().setSavepointTriggerNonce(123L);
+            diff = left.diff(right);
+            assertEquals(DiffType.SCALE, diff.getType());
+            assertEquals(5, diff.getNumDiffs());
+        }
+    }
+
+    @ParameterizedTest
+    @MethodSource("applicationTestParams")
+    public void testReconcile(AbstractFlinkSpec left, AbstractFlinkSpec right) 
{
+        var diff = left.diff(right);
+        assertEquals(DiffType.IGNORE, diff.getType());
+        assertEquals(0, diff.getNumDiffs());
+
+        if (left instanceof FlinkDeploymentSpec) {
+            ((FlinkDeploymentSpec) right).setImage("flink:greatest");
+            assertEquals(DiffType.UPGRADE, left.diff(right).getType());
+            diff = left.diff(right);
+            assertEquals(DiffType.UPGRADE, diff.getType());
+            assertEquals(1, diff.getNumDiffs());
+        }
+

Review Comment:
   It's a bit strange to use `ParameterizedTest` here because you actually have 
3 different inner ifs for the 3 different params. I would personally prefer to 
have single test cases because the parameterization just breaks up the logic  
(hides what is tested) and its harder to understand.



##########
flink-kubernetes-operator/src/test/java/org/apache/flink/kubernetes/operator/reconciler/diff/SpecDiffTest.java:
##########
@@ -0,0 +1,171 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.flink.kubernetes.operator.reconciler.diff;
+
+import org.apache.flink.configuration.CoreOptions;
+import org.apache.flink.kubernetes.operator.TestUtils;
+import org.apache.flink.kubernetes.operator.crd.spec.AbstractFlinkSpec;
+import org.apache.flink.kubernetes.operator.crd.spec.FlinkDeploymentSpec;
+import org.apache.flink.kubernetes.operator.crd.spec.FlinkSessionJobSpec;
+import org.apache.flink.kubernetes.operator.crd.spec.UpgradeMode;
+import org.apache.flink.kubernetes.operator.reconciler.ReconciliationUtils;
+
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.stream.Stream;
+
+import static 
org.apache.flink.kubernetes.operator.config.KubernetesOperatorConfigOptions.OPERATOR_RECONCILE_INTERVAL;
+import static 
org.apache.flink.kubernetes.operator.metrics.KubernetesOperatorMetricOptions.SCOPE_NAMING_KUBERNETES_OPERATOR;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.params.provider.Arguments.arguments;
+
+/** Spec diff test. */
+public class SpecDiffTest {
+
+    @ParameterizedTest
+    @MethodSource("applicationTestParams")
+    public void testIgnore(AbstractFlinkSpec left, AbstractFlinkSpec right) {
+        var diff = left.diff(right);
+        assertEquals(DiffType.IGNORE, diff.getType());
+        assertEquals(0, diff.getNumDiffs());
+        if (right.getJob() != null) {
+            right.getJob().setUpgradeMode(UpgradeMode.LAST_STATE);
+            right.getJob().setAllowNonRestoredState(true);
+            right.getJob().setInitialSavepointPath("local:///tmp");
+            right.getJob().setSavepointTriggerNonce(123L);
+            diff = left.diff(right);
+            assertEquals(DiffType.IGNORE, diff.getType());
+            assertEquals(4, diff.getNumDiffs());
+        }
+    }
+
+    @ParameterizedTest
+    @MethodSource("applicationTestParams")
+    public void testScale(AbstractFlinkSpec left, AbstractFlinkSpec right) {
+        var diff = left.diff(right);
+        assertEquals(DiffType.IGNORE, diff.getType());
+        assertEquals(0, diff.getNumDiffs());
+
+        if (right.getJob() != null) {
+            right.getJob().setParallelism(100);
+            diff = left.diff(right);
+            assertEquals(DiffType.SCALE, diff.getType());
+            assertEquals(1, diff.getNumDiffs());
+
+            right.getJob().setUpgradeMode(UpgradeMode.LAST_STATE);
+            right.getJob().setAllowNonRestoredState(true);
+            right.getJob().setInitialSavepointPath("local:///tmp");
+            right.getJob().setSavepointTriggerNonce(123L);
+            diff = left.diff(right);
+            assertEquals(DiffType.SCALE, diff.getType());
+            assertEquals(5, diff.getNumDiffs());
+        }
+    }
+
+    @ParameterizedTest
+    @MethodSource("applicationTestParams")
+    public void testReconcile(AbstractFlinkSpec left, AbstractFlinkSpec right) 
{
+        var diff = left.diff(right);
+        assertEquals(DiffType.IGNORE, diff.getType());
+        assertEquals(0, diff.getNumDiffs());
+
+        if (left instanceof FlinkDeploymentSpec) {
+            ((FlinkDeploymentSpec) right).setImage("flink:greatest");
+            assertEquals(DiffType.UPGRADE, left.diff(right).getType());
+            diff = left.diff(right);
+            assertEquals(DiffType.UPGRADE, diff.getType());
+            assertEquals(1, diff.getNumDiffs());
+        }
+
+        if (left instanceof FlinkSessionJobSpec) {
+            ((FlinkSessionJobSpec) right).setDeploymentName("does-non-exist");
+            assertEquals(DiffType.UPGRADE, left.diff(right).getType());
+            diff = left.diff(right);
+            assertEquals(DiffType.UPGRADE, diff.getType());
+            assertEquals(1, diff.getNumDiffs());
+        }
+
+        if (right.getJob() != null) {
+            right.getJob().setParallelism(100);
+            right.getJob().setUpgradeMode(UpgradeMode.LAST_STATE);
+            assertEquals(DiffType.UPGRADE, left.diff(right).getType());
+            diff = left.diff(right);
+            assertEquals(DiffType.UPGRADE, diff.getType());
+            assertEquals(3, diff.getNumDiffs());
+        }

Review Comment:
   It would also be nice to include 1-2 more complex diff scenarios such us 
some inner array of the podTemplate (like env variables) have changed so that 
we get full confidence in the logic we copied



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@flink.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to