You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by GitBox <gi...@apache.org> on 2022/02/25 05:00:20 UTC

[GitHub] [flink-kubernetes-operator] Aitozi commented on a change in pull request #24: [FLINK-26136] Extract shared deployment validation logic

Aitozi commented on a change in pull request #24:
URL: https://github.com/apache/flink-kubernetes-operator/pull/24#discussion_r814457419



##########
File path: flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/validation/DefaultDeploymentValidator.java
##########
@@ -0,0 +1,165 @@
+/*
+ * 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.validation;
+
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.configuration.MemorySize;
+import org.apache.flink.kubernetes.operator.crd.FlinkDeployment;
+import org.apache.flink.kubernetes.operator.crd.spec.FlinkDeploymentSpec;
+import org.apache.flink.kubernetes.operator.crd.spec.JobManagerSpec;
+import org.apache.flink.kubernetes.operator.crd.spec.JobSpec;
+import org.apache.flink.kubernetes.operator.crd.spec.JobState;
+import org.apache.flink.kubernetes.operator.crd.spec.Resource;
+import org.apache.flink.kubernetes.operator.crd.spec.TaskManagerSpec;
+import org.apache.flink.kubernetes.operator.crd.spec.UpgradeMode;
+
+import java.util.Map;
+import java.util.Optional;
+
+/** Default validator implementation. */
+public class DefaultDeploymentValidator implements FlinkDeploymentValidator {
+
+    private static final String[] FORBIDDEN_CONF_KEYS =
+            new String[] {"kubernetes.namespace", "kubernetes.cluster-id"};
+
+    @Override
+    public Optional<String> validate(FlinkDeployment deployment) {
+        FlinkDeploymentSpec spec = deployment.getSpec();
+        return firstPresent(
+                validateFlinkConfig(spec.getFlinkConfiguration()),
+                validateJobSpec(spec.getJob()),
+                validateJmSpec(spec.getJobManager()),
+                validateTmSpec(spec.getTaskManager()),
+                validateSpecChange(deployment));
+    }
+
+    private static Optional<String> firstPresent(Optional<String>... errOpts) {
+        for (Optional<String> opt : errOpts) {
+            if (opt.isPresent()) {
+                return opt;
+            }
+        }
+        return Optional.empty();
+    }
+
+    private Optional<String> validateFlinkConfig(Map<String, String> confMap) {
+        if (confMap == null) {
+            return Optional.empty();
+        }
+        Configuration conf = Configuration.fromMap(confMap);
+        for (String key : FORBIDDEN_CONF_KEYS) {
+            if (conf.containsKey(key)) {
+                return Optional.of("Forbidden Flink config key: " + key);
+            }
+        }
+        return Optional.empty();
+    }
+
+    private Optional<String> validateJobSpec(JobSpec job) {
+        if (job == null) {
+            return Optional.empty();
+        }
+
+        if (job.getParallelism() < 1) {
+            return Optional.of("Job parallelism must be larger than 0");
+        }
+
+        if (job.getJarURI() == null) {
+            return Optional.of("Jar URI must be defined");
+        }
+
+        return Optional.empty();
+    }
+
+    private Optional<String> validateJmSpec(JobManagerSpec jmSpec) {
+        if (jmSpec == null) {
+            return Optional.empty();
+        }
+
+        return validateResources("JobManager", jmSpec.getResource());
+    }
+
+    private Optional<String> validateTmSpec(TaskManagerSpec tmSpec) {
+        if (tmSpec == null) {
+            return Optional.empty();
+        }
+
+        return validateResources("TaskManager", tmSpec.getResource());
+    }
+
+    private Optional<String> validateResources(String component, Resource resource) {
+        if (resource == null) {
+            return Optional.empty();
+        }
+
+        String memory = resource.getMemory();
+        if (memory == null) {
+            return Optional.of(component + " resource memory must be defined.");
+        }
+
+        try {
+            MemorySize.parse(memory);
+        } catch (IllegalArgumentException iae) {
+            return Optional.of(component + " resource memory parse error: " + iae.getMessage());
+        }
+
+        return Optional.empty();
+    }
+
+    private Optional<String> validateSpecChange(FlinkDeployment deployment) {
+        if (deployment.getStatus() == null
+                || deployment.getStatus().getReconciliationStatus() == null
+                || deployment.getStatus().getReconciliationStatus().getLastReconciledSpec()
+                        == null) {
+            return Optional.empty();
+        }
+
+        FlinkDeploymentSpec newSpec = deployment.getSpec();
+        FlinkDeploymentSpec oldSpec =
+                deployment.getStatus().getReconciliationStatus().getLastReconciledSpec();
+
+        if (newSpec.getJob() != null && oldSpec.getJob() == null) {
+            return Optional.of("Cannot switch from session to job cluster");
+        }
+
+        if (newSpec.getJob() == null && oldSpec.getJob() != null) {
+            return Optional.of("Cannot switch from job to session cluster");
+        }
+
+        if (oldSpec == null) {

Review comment:
       IDE complains here, `oldSpec == null is always false` . It should change the order with the preceding check




-- 
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: commits-unsubscribe@flink.apache.org

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