forked from apache/flink
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FLINK-16194][k8s] Introduce Java command decorator for the main Cont…
…ainer of the JobManager and the TaskManger Pod
- Loading branch information
1 parent
a50435d
commit 7f19c9c
Showing
8 changed files
with
753 additions
and
289 deletions.
There are no files selected for viewing
99 changes: 99 additions & 0 deletions
99
...in/java/org/apache/flink/kubernetes/kubeclient/decorators/JavaCmdJobManagerDecorator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* 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:https://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.kubeclient.decorators; | ||
|
||
import org.apache.flink.configuration.Configuration; | ||
import org.apache.flink.kubernetes.kubeclient.FlinkPod; | ||
import org.apache.flink.kubernetes.kubeclient.parameters.KubernetesJobManagerParameters; | ||
import org.apache.flink.kubernetes.utils.KubernetesUtils; | ||
import org.apache.flink.runtime.clusterframework.BootstrapTools; | ||
|
||
import io.fabric8.kubernetes.api.model.Container; | ||
import io.fabric8.kubernetes.api.model.ContainerBuilder; | ||
|
||
import java.util.Arrays; | ||
|
||
import static org.apache.flink.util.Preconditions.checkNotNull; | ||
|
||
/** | ||
* Attach the jvm command and args to the main container for running the JobManager code. | ||
*/ | ||
public class JavaCmdJobManagerDecorator extends AbstractKubernetesStepDecorator { | ||
|
||
private final KubernetesJobManagerParameters kubernetesJobManagerParameters; | ||
|
||
public JavaCmdJobManagerDecorator(KubernetesJobManagerParameters kubernetesJobManagerParameters) { | ||
this.kubernetesJobManagerParameters = checkNotNull(kubernetesJobManagerParameters); | ||
} | ||
|
||
@Override | ||
public FlinkPod decorateFlinkPod(FlinkPod flinkPod) { | ||
final String startCommand = getJobManagerStartCommand( | ||
kubernetesJobManagerParameters.getFlinkConfiguration(), | ||
kubernetesJobManagerParameters.getJobManagerMemoryMB(), | ||
kubernetesJobManagerParameters.getFlinkConfDirInPod(), | ||
kubernetesJobManagerParameters.getFlinkLogDirInPod(), | ||
kubernetesJobManagerParameters.hasLogback(), | ||
kubernetesJobManagerParameters.hasLog4j(), | ||
kubernetesJobManagerParameters.getEntrypointClass()); | ||
|
||
final Container mainContainerWithStartCmd = new ContainerBuilder(flinkPod.getMainContainer()) | ||
.withCommand(kubernetesJobManagerParameters.getContainerEntrypoint()) | ||
.withArgs(Arrays.asList("/bin/bash", "-c", startCommand)) | ||
.build(); | ||
|
||
return new FlinkPod.Builder(flinkPod) | ||
.withMainContainer(mainContainerWithStartCmd) | ||
.build(); | ||
} | ||
|
||
/** | ||
* Generates the shell command to start a jobmanager for kubernetes. | ||
* | ||
* @param flinkConfig The Flink configuration. | ||
* @param jobManagerMemoryMb JobManager heap size. | ||
* @param configDirectory The configuration directory for the flink-conf.yaml | ||
* @param logDirectory The log directory. | ||
* @param hasLogback Uses logback? | ||
* @param hasLog4j Uses log4j? | ||
* @param mainClass The main class to start with. | ||
* @return A String containing the job manager startup command. | ||
*/ | ||
private static String getJobManagerStartCommand( | ||
Configuration flinkConfig, | ||
int jobManagerMemoryMb, | ||
String configDirectory, | ||
String logDirectory, | ||
boolean hasLogback, | ||
boolean hasLog4j, | ||
String mainClass) { | ||
final int heapSize = BootstrapTools.calculateHeapSize(jobManagerMemoryMb, flinkConfig); | ||
final String jvmMemOpts = String.format("-Xms%sm -Xmx%sm", heapSize, heapSize); | ||
return KubernetesUtils.getCommonStartCommand( | ||
flinkConfig, | ||
KubernetesUtils.ClusterComponent.JOB_MANAGER, | ||
jvmMemOpts, | ||
configDirectory, | ||
logDirectory, | ||
hasLogback, | ||
hasLog4j, | ||
mainClass, | ||
null); | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
...n/java/org/apache/flink/kubernetes/kubeclient/decorators/JavaCmdTaskManagerDecorator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* 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:https://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.kubeclient.decorators; | ||
|
||
import org.apache.flink.configuration.Configuration; | ||
import org.apache.flink.kubernetes.kubeclient.FlinkPod; | ||
import org.apache.flink.kubernetes.kubeclient.parameters.KubernetesTaskManagerParameters; | ||
import org.apache.flink.kubernetes.taskmanager.KubernetesTaskExecutorRunner; | ||
import org.apache.flink.kubernetes.utils.KubernetesUtils; | ||
import org.apache.flink.runtime.clusterframework.ContaineredTaskManagerParameters; | ||
import org.apache.flink.runtime.clusterframework.TaskExecutorProcessSpec; | ||
import org.apache.flink.runtime.clusterframework.TaskExecutorProcessUtils; | ||
import org.apache.flink.runtime.entrypoint.parser.CommandLineOptions; | ||
|
||
import io.fabric8.kubernetes.api.model.Container; | ||
import io.fabric8.kubernetes.api.model.ContainerBuilder; | ||
|
||
import java.util.Arrays; | ||
|
||
import static org.apache.flink.util.Preconditions.checkNotNull; | ||
|
||
/** | ||
* Attach the jvm command and args to the main container for running the TaskManager code. | ||
*/ | ||
public class JavaCmdTaskManagerDecorator extends AbstractKubernetesStepDecorator { | ||
|
||
private final KubernetesTaskManagerParameters kubernetesTaskManagerParameters; | ||
|
||
public JavaCmdTaskManagerDecorator(KubernetesTaskManagerParameters kubernetesTaskManagerParameters) { | ||
this.kubernetesTaskManagerParameters = checkNotNull(kubernetesTaskManagerParameters); | ||
} | ||
|
||
@Override | ||
public FlinkPod decorateFlinkPod(FlinkPod flinkPod) { | ||
final Container mainContainerWithStartCmd = new ContainerBuilder(flinkPod.getMainContainer()) | ||
.withCommand(kubernetesTaskManagerParameters.getContainerEntrypoint()) | ||
.withArgs(Arrays.asList("/bin/bash", "-c", getTaskManagerStartCommand())) | ||
.build(); | ||
|
||
return new FlinkPod.Builder(flinkPod) | ||
.withMainContainer(mainContainerWithStartCmd) | ||
.build(); | ||
} | ||
|
||
private String getTaskManagerStartCommand() { | ||
final String confDirInPod = kubernetesTaskManagerParameters.getFlinkConfDirInPod(); | ||
|
||
final String logDirInPod = kubernetesTaskManagerParameters.getFlinkLogDirInPod(); | ||
|
||
final String mainClassArgs = "--" + CommandLineOptions.CONFIG_DIR_OPTION.getLongOpt() + " " + | ||
confDirInPod + " " + kubernetesTaskManagerParameters.getDynamicProperties(); | ||
|
||
return getTaskManagerStartCommand( | ||
kubernetesTaskManagerParameters.getFlinkConfiguration(), | ||
kubernetesTaskManagerParameters.getContaineredTaskManagerParameters(), | ||
confDirInPod, | ||
logDirInPod, | ||
kubernetesTaskManagerParameters.hasLogback(), | ||
kubernetesTaskManagerParameters.hasLog4j(), | ||
KubernetesTaskExecutorRunner.class.getCanonicalName(), | ||
mainClassArgs); | ||
} | ||
|
||
private static String getTaskManagerStartCommand( | ||
Configuration flinkConfig, | ||
ContaineredTaskManagerParameters tmParams, | ||
String configDirectory, | ||
String logDirectory, | ||
boolean hasLogback, | ||
boolean hasLog4j, | ||
String mainClass, | ||
String mainArgs) { | ||
final TaskExecutorProcessSpec taskExecutorProcessSpec = tmParams.getTaskExecutorProcessSpec(); | ||
final String jvmMemOpts = TaskExecutorProcessUtils.generateJvmParametersStr(taskExecutorProcessSpec); | ||
String args = TaskExecutorProcessUtils.generateDynamicConfigsStr(taskExecutorProcessSpec); | ||
if (mainArgs != null) { | ||
args += " " + mainArgs; | ||
} | ||
|
||
return KubernetesUtils.getCommonStartCommand( | ||
flinkConfig, | ||
KubernetesUtils.ClusterComponent.TASK_MANAGER, | ||
jvmMemOpts, | ||
configDirectory, | ||
logDirectory, | ||
hasLogback, | ||
hasLog4j, | ||
mainClass, | ||
args); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
flink-kubernetes/src/test/java/org/apache/flink/kubernetes/KubernetesTestUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* 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:https://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; | ||
|
||
import org.apache.flink.shaded.guava18.com.google.common.io.Files; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
/** | ||
* Utilities for the Kubernetes tests. | ||
*/ | ||
public class KubernetesTestUtils { | ||
|
||
public static void createTemporyFile(String data, File directory, String fileName) throws IOException { | ||
Files.write(data, new File(directory, fileName), StandardCharsets.UTF_8); | ||
} | ||
} |
Oops, something went wrong.