wangyang0918 commented on a change in pull request #10245: [FLINK-10936][kubernetes] Implement Kubernetes command line tools to support session cluster. URL: https://github.com/apache/flink/pull/10245#discussion_r355179479
########## File path: flink-kubernetes/src/main/java/org/apache/flink/kubernetes/cli/FlinkKubernetesCustomCli.java ########## @@ -0,0 +1,294 @@ +/* + * 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.cli; + +import org.apache.flink.client.cli.AbstractCustomCommandLine; +import org.apache.flink.client.cli.CliArgsException; +import org.apache.flink.client.deployment.ClusterClientFactory; +import org.apache.flink.client.deployment.ClusterClientServiceLoader; +import org.apache.flink.client.deployment.ClusterDescriptor; +import org.apache.flink.client.deployment.DefaultClusterClientServiceLoader; +import org.apache.flink.client.program.ClusterClient; +import org.apache.flink.configuration.Configuration; +import org.apache.flink.configuration.DeploymentOptions; +import org.apache.flink.configuration.GlobalConfiguration; +import org.apache.flink.configuration.JobManagerOptions; +import org.apache.flink.configuration.MemorySize; +import org.apache.flink.configuration.TaskManagerOptions; +import org.apache.flink.kubernetes.configuration.KubernetesConfigOptions; +import org.apache.flink.kubernetes.configuration.KubernetesConfigOptionsInternal; +import org.apache.flink.kubernetes.executors.KubernetesSessionClusterExecutor; +import org.apache.flink.kubernetes.kubeclient.FlinkKubeClient; +import org.apache.flink.kubernetes.kubeclient.KubeClientFactory; +import org.apache.flink.runtime.security.SecurityUtils; +import org.apache.flink.util.FlinkException; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.Options; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.Properties; + +import static org.apache.flink.client.cli.CliFrontendParser.DETACHED_OPTION; +import static org.apache.flink.configuration.HighAvailabilityOptions.HA_CLUSTER_ID; +import static org.apache.flink.kubernetes.cli.KubernetesCliOptions.CLUSTER_ID_OPTION; +import static org.apache.flink.kubernetes.cli.KubernetesCliOptions.DYNAMIC_PROPERTY_OPTION; +import static org.apache.flink.kubernetes.cli.KubernetesCliOptions.HELP_OPTION; +import static org.apache.flink.kubernetes.cli.KubernetesCliOptions.IMAGE_OPTION; +import static org.apache.flink.kubernetes.cli.KubernetesCliOptions.JOB_CLASS_NAME_OPTION; +import static org.apache.flink.kubernetes.cli.KubernetesCliOptions.JOB_ID_OPTION; + +/** + * Kubernetes customized commandline. + */ +public class FlinkKubernetesCustomCli extends AbstractCustomCommandLine { + + private static final Logger LOG = LoggerFactory.getLogger(FlinkKubernetesCustomCli.class); + + private static final String KUBERNETES_CLUSTER_HELP = "Available commands:\n" + + "help - show these commands\n" + + "stop - stop the kubernetes cluster"; + + private static final String ID = "kubernetes-cluster"; + + // Options with short prefix(k) or long prefix(kubernetes) + private final Option imageOption; + private final Option clusterIdOption; + private final Option jobManagerMemeoryOption; + private final Option taskManagerMemeoryOption; + private final Option taskManagerSlotsOption; + private final Option dynamicPropertiesOption; + private final Option helpOption; + + private final Option jobClassOption; + private final Option jobIdOption; + + private final ClusterClientServiceLoader clusterClientServiceLoader; + + public FlinkKubernetesCustomCli( + Configuration configuration, + String shortPrefix, + String longPrefix) { + this(configuration, new DefaultClusterClientServiceLoader(), shortPrefix, longPrefix); + } + + public FlinkKubernetesCustomCli( + Configuration configuration, + ClusterClientServiceLoader clusterClientServiceLoader, + String shortPrefix, + String longPrefix) { + super(configuration); + + this.clusterClientServiceLoader = clusterClientServiceLoader; + + this.imageOption = KubernetesCliOptions.getOptionWithPrefix(IMAGE_OPTION, shortPrefix, longPrefix); + this.clusterIdOption = KubernetesCliOptions.getOptionWithPrefix(CLUSTER_ID_OPTION, shortPrefix, longPrefix); + this.dynamicPropertiesOption = KubernetesCliOptions.getOptionWithPrefix( + DYNAMIC_PROPERTY_OPTION, shortPrefix, longPrefix); + + this.jobManagerMemeoryOption = KubernetesCliOptions.getOptionWithPrefix( + KubernetesCliOptions.JOB_MANAGER_MEMORY_OPTION, shortPrefix, longPrefix); + this.taskManagerMemeoryOption = KubernetesCliOptions.getOptionWithPrefix( + KubernetesCliOptions.TASK_MANAGER_MEMORY_OPTION, shortPrefix, longPrefix); + this.taskManagerSlotsOption = KubernetesCliOptions.getOptionWithPrefix( + KubernetesCliOptions.TASK_MANAGER_SLOTS_OPTION, shortPrefix, longPrefix); + + this.jobClassOption = KubernetesCliOptions.getOptionWithPrefix(JOB_CLASS_NAME_OPTION, shortPrefix, longPrefix); + this.jobIdOption = KubernetesCliOptions.getOptionWithPrefix(JOB_ID_OPTION, shortPrefix, longPrefix); + + this.helpOption = KubernetesCliOptions.getOptionWithPrefix(HELP_OPTION, shortPrefix, longPrefix); + } + + /** + * active if commandline contains option -m kubernetes-cluster or -kid. + */ + @Override + public boolean isActive(CommandLine commandLine) { + final String jobManagerOption = commandLine.getOptionValue(addressOption.getOpt(), null); + final boolean k8sJobManager = ID.equals(jobManagerOption); + final boolean k8sClusterId = commandLine.hasOption(clusterIdOption.getOpt()); + return k8sJobManager || k8sClusterId; + } + + @Override + public void addRunOptions(Options baseOptions) { + baseOptions.addOption(DETACHED_OPTION) + .addOption(imageOption) + .addOption(clusterIdOption) + .addOption(jobManagerMemeoryOption) + .addOption(taskManagerMemeoryOption) + .addOption(taskManagerSlotsOption) + .addOption(dynamicPropertiesOption) + .addOption(helpOption); + } + + @Override + public void addGeneralOptions(Options baseOptions) { Review comment: Because we do not need the following run options. I also think the they are unnecessary for yarn-cluster. Because they are so confusing. ``` -m,--jobmanager <arg> Address of the JobManager (master) to which to connect. Use this flag to connect to a different JobManager than the one specified in the configuration. -z,--zookeeperNamespace <arg> Namespace to create the Zookeeper sub-paths for high availability mode ``` ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
