in computer-k8s-operator/src/main/java/org/apache/hugegraph/computer/k8s/operator/controller/ComputerJobDeployer.java [400:585]
private Container getContainer(String name, String namespace,
ComputerJobSpec spec,
Set<ContainerPort> ports,
Collection<String> command,
Collection<String> args) {
List<EnvVar> envVars = spec.getEnvVars();
if (envVars == null) {
envVars = new ArrayList<>();
}
// Add env
EnvVar podIP = new EnvVarBuilder()
.withName(Constants.ENV_POD_IP)
.withNewValueFrom()
.withNewFieldRef()
.withFieldPath(POD_IP_KEY)
.endFieldRef()
.endValueFrom()
.build();
envVars.add(podIP);
EnvVar podName = new EnvVarBuilder()
.withName(Constants.ENV_POD_NAME)
.withNewValueFrom()
.withNewFieldRef()
.withFieldPath(POD_NAME_KEY)
.endFieldRef()
.endValueFrom()
.build();
envVars.add(podName);
EnvVar podNamespace = new EnvVarBuilder()
.withName(Constants.ENV_POD_NAMESPACE)
.withNewValueFrom()
.withNewFieldRef()
.withFieldPath(POD_NAMESPACE_KEY)
.endFieldRef()
.endValueFrom()
.build();
envVars.add(podNamespace);
EnvVar confDir = new EnvVarBuilder()
.withName(Constants.ENV_CONFIG_DIR)
.withValue(Constants.CONFIG_DIR)
.build();
envVars.add(confDir);
EnvVar confPath = new EnvVarBuilder()
.withName(Constants.ENV_COMPUTER_CONF_PATH)
.withValue(Constants.COMPUTER_CONF_PATH)
.build();
envVars.add(confPath);
String log4jXml = spec.getLog4jXml();
if (StringUtils.isNotBlank(log4jXml)) {
EnvVar logConfEnv = new EnvVarBuilder()
.withName(Constants.ENV_LOG4J_CONF_PATH)
.withValue(Constants.LOG_XML_PATH)
.build();
envVars.add(logConfEnv);
}
String jarFile = spec.getJarFile();
if (StringUtils.isNotBlank(jarFile)) {
EnvVar jarFilePath = new EnvVarBuilder()
.withName(Constants.ENV_JAR_FILE_PATH)
.withValue(jarFile)
.build();
envVars.add(jarFilePath);
}
String remoteJarUri = spec.getRemoteJarUri();
if (StringUtils.isNotBlank(remoteJarUri)) {
EnvVar jobJarURI = new EnvVarBuilder()
.withName(Constants.ENV_REMOTE_JAR_URI)
.withValue(remoteJarUri)
.build();
envVars.add(jobJarURI);
}
String jvmOptions = spec.getJvmOptions();
StringBuilder jvmOptionsBuilder = jvmOptions == null ?
new StringBuilder() :
new StringBuilder(jvmOptions.trim());
jvmOptionsBuilder.append(" ").append("-Duser.timezone=")
.append(this.timezone);
EnvVar jvmOptionsEnv = new EnvVarBuilder()
.withName(Constants.ENV_JVM_OPTIONS)
.withValue(jvmOptionsBuilder.toString().trim())
.build();
envVars.add(jvmOptionsEnv);
Quantity cpu;
Quantity memory;
if (name.contains("master")) {
cpu = spec.getMasterCpu();
memory = spec.getMasterMemory();
} else {
cpu = spec.getWorkerCpu();
memory = spec.getWorkerMemory();
}
if (cpu != null) {
EnvVar cpuLimit = new EnvVarBuilder()
.withName(Constants.ENV_CPU_LIMIT)
.withValue(cpu.toString())
.build();
envVars.add(cpuLimit);
}
if (memory != null) {
EnvVar memoryLimit = new EnvVarBuilder()
.withName(Constants.ENV_MEMORY_LIMIT)
.withValue(memory.toString())
.build();
envVars.add(memoryLimit);
}
List<VolumeMount> volumeMounts = spec.getVolumeMounts();
if (volumeMounts == null) {
volumeMounts = new ArrayList<>();
} else {
volumeMounts = Lists.newArrayList(volumeMounts);
}
final KubernetesClient client;
if (!Objects.equals(this.kubeClient.getNamespace(), namespace)) {
client = this.kubeClient.inNamespace(namespace);
} else {
client = this.kubeClient;
}
// Mount configmap and secret files
Map<String, String> configMapPaths = spec.getConfigMapPaths();
if (MapUtils.isNotEmpty(configMapPaths)) {
for (String key : configMapPaths.keySet()) {
ConfigMap configMap = client.configMaps().withName(key).get();
E.checkArgument(configMap != null &&
configMap.getData().size() > 0,
"The configMap '%s' don't exist", key);
this.mountConfigMapOrSecret(volumeMounts, key,
configMapPaths.get(key),
configMap.getData());
}
}
Map<String, String> secretPaths = spec.getSecretPaths();
if (MapUtils.isNotEmpty(secretPaths)) {
for (String key : secretPaths.keySet()) {
Secret secret = client.secrets().withName(key).get();
E.checkArgument(secret != null &&
secret.getData().size() > 0,
"The secret '%s' don't exist", key);
this.mountConfigMapOrSecret(volumeMounts, key,
secretPaths.get(key),
secret.getData());
}
}
VolumeMount configMount = this.getComputerConfigMount();
volumeMounts.add(configMount);
Container container = new ContainerBuilder()
.withName(KubeUtil.containerName(name))
.withImage(spec.getImage())
.withImagePullPolicy(spec.getPullPolicy())
.withEnv(spec.getEnvVars())
.withEnvFrom(spec.getEnvFrom())
.withVolumeMounts(volumeMounts)
.addAllToCommand(command)
.addAllToArgs(args)
.addAllToPorts(ports)
.withNewResources()
.addToLimits(ResourceName.CPU.value(), cpu)
.addToLimits(ResourceName.MEMORY.value(), memory)
.endResources()
.build();
// Add security context
if (spec.getSecurityContext() != null) {
container.setSecurityContext(spec.getSecurityContext());
}
return container;
}