Skip to content
Permalink
Browse files
add configmap and secret mount to container (#158)
  • Loading branch information
coderzc committed Dec 14, 2021
1 parent 0224cce commit e3787773bc9f6bae6723a4b1afa5e464d2365dfc
Showing 20 changed files with 233 additions and 29 deletions.
@@ -5,7 +5,7 @@
<parent>
<artifactId>hugegraph-computer</artifactId>
<groupId>com.baidu.hugegraph</groupId>
<version>0.1.1</version>
<version>0.1.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

@@ -5,7 +5,7 @@
<parent>
<artifactId>hugegraph-computer</artifactId>
<groupId>com.baidu.hugegraph</groupId>
<version>0.1.1</version>
<version>0.1.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

@@ -5,7 +5,7 @@
<parent>
<artifactId>hugegraph-computer</artifactId>
<groupId>com.baidu.hugegraph</groupId>
<version>0.1.1</version>
<version>0.1.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

@@ -24,7 +24,7 @@
<parent>
<artifactId>hugegraph-computer</artifactId>
<groupId>com.baidu.hugegraph</groupId>
<version>0.1.1</version>
<version>0.1.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

@@ -5,10 +5,10 @@
<parent>
<artifactId>hugegraph-computer</artifactId>
<groupId>com.baidu.hugegraph</groupId>
<version>0.1.1</version>
<version>0.1.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<version>0.1.1</version>
<version>0.1.2</version>

<artifactId>computer-driver</artifactId>

@@ -717,7 +717,6 @@ public static synchronized ComputerOptions instance() {
);

public static final Set<String> K8S_REQUIRED_USER_OPTIONS = ImmutableSet.of(
HUGEGRAPH_URL.name(),
ALGORITHM_PARAMS_CLASS.name()
);
}
@@ -88,6 +88,10 @@ type ComputerJobSpec struct {
// Volume mounts in the Job container.
// More info: https://kubernetes.io/docs/concepts/storage/volumes/
VolumeMounts []corev1.VolumeMount `json:"volumeMounts,omitempty"`

SecretPaths map[string]string `json:"secretPaths,omitempty"`

ConfigMapPaths map[string]string `json:"configMapPaths,omitempty"`
}

type ComputerJobState struct {
@@ -56,6 +56,10 @@ spec:
additionalProperties:
type: string
type: object
configMapPaths:
additionalProperties:
type: string
type: object
envFrom:
description: Environment variables injected from a source, shared by all Master and Worker.
items:
@@ -216,6 +220,10 @@ spec:
remoteJarUri:
pattern: ^(http|https):\/\/([\w.]+\/?)\S*$
type: string
secretPaths:
additionalProperties:
type: string
type: object
volumeMounts:
description: 'Volume mounts in the Job container. More info: https://kubernetes.io/docs/concepts/storage/volumes/'
items:
@@ -57,6 +57,10 @@ spec:
additionalProperties:
type: string
type: object
configMapPaths:
additionalProperties:
type: string
type: object
envFrom:
description: Environment variables injected from a source, shared by all Master and Worker.
items:
@@ -217,6 +221,10 @@ spec:
remoteJarUri:
pattern: ^(http|https):\/\/([\w.]+\/?)\S*$
type: string
secretPaths:
additionalProperties:
type: string
type: object
volumeMounts:
description: 'Volume mounts in the Job container. More info: https://kubernetes.io/docs/concepts/storage/volumes/'
items:
@@ -5,7 +5,7 @@
<parent>
<artifactId>hugegraph-computer</artifactId>
<groupId>com.baidu.hugegraph</groupId>
<version>0.1.1</version>
<version>0.1.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

@@ -28,6 +28,7 @@
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;

@@ -85,8 +86,8 @@ public class ComputerJobDeployer {
private static final int DEFAULT_TRANSPORT_PORT = 8099;
private static final int DEFAULT_RPC_PORT = 8090;
private static final int DEFAULT_TRANSPORT_THREADS = 8;

private static final String CONFIG_MAP_VOLUME = "config-map-volume";
private static final String COMPUTER_CONFIG_MAP_VOLUME =
"computer-config-map-volume";

private static final String POD_IP_KEY = "status.podIP";
private static final String POD_NAMESPACE_KEY = "metadata.namespace";
@@ -300,16 +301,16 @@ public Job desiredWorkerJob(HugeGraphComputerJob computerJob,

private Job getJob(String crName, ObjectMeta meta, ComputerJobSpec spec,
int instances, List<Container> containers) {

String configMapName = KubeUtil.configMapName(crName);

List<Volume> volumes = spec.getVolumes();
if (volumes == null) {
volumes = new ArrayList<>();
} else {
volumes = Lists.newArrayList(volumes);
}
Volume configVolume = this.getConfigVolume(configMapName);
volumes.addAll(this.getConfigMapAndSecretVolumes(spec));

String configMapName = KubeUtil.configMapName(crName);
Volume configVolume = this.getComputerConfigVolume(configMapName);
volumes.add(configVolume);

PodSpec podSpec = new PodSpecBuilder()
@@ -335,6 +336,40 @@ private Job getJob(String crName, ObjectMeta meta, ComputerJobSpec spec,
.build();
}

private List<Volume> getConfigMapAndSecretVolumes(ComputerJobSpec spec) {
List<Volume> volumes = new ArrayList<>();

Map<String, String> configMapPaths = spec.getConfigMapPaths();
if (MapUtils.isNotEmpty(configMapPaths)) {
Set<String> configMapNames = configMapPaths.keySet();
for (String configMapName : configMapNames) {
Volume volume = new VolumeBuilder()
.withName(this.volumeName(configMapName))
.withNewConfigMap()
.withName(configMapName)
.endConfigMap()
.build();
volumes.add(volume);
}
}

Map<String, String> secretPaths = spec.getSecretPaths();
if (MapUtils.isNotEmpty(secretPaths)) {
Set<String> secretNames = secretPaths.keySet();
for (String secretName : secretNames) {
Volume volume = new VolumeBuilder()
.withName(this.volumeName(secretName))
.withNewSecret()
.withSecretName(secretName)
.endSecret()
.build();
volumes.add(volume);
}
}

return volumes;
}

private Container getContainer(String name, ComputerJobSpec spec,
Set<ContainerPort> ports,
Collection<String> command,
@@ -442,7 +477,28 @@ private Container getContainer(String name, ComputerJobSpec spec,
} else {
volumeMounts = Lists.newArrayList(volumeMounts);
}
VolumeMount configMount = this.getConfigMount();

// Mount configmap and secret files
Map<String, String> paths = new HashMap<>();
Map<String, String> configMapPaths = spec.getConfigMapPaths();
if (MapUtils.isNotEmpty(configMapPaths)) {
paths.putAll(configMapPaths);
}
Map<String, String> secretPaths = spec.getSecretPaths();
if (MapUtils.isNotEmpty(secretPaths)) {
paths.putAll(secretPaths);
}
Set<Map.Entry<String, String>> entries = paths.entrySet();
for (Map.Entry<String, String> entry : entries) {
VolumeMount volumeMount = new VolumeMountBuilder()
.withName(this.volumeName(entry.getKey()))
.withMountPath(entry.getValue())
.withReadOnly(true)
.build();
volumeMounts.add(volumeMount);
}

VolumeMount configMount = this.getComputerConfigMount();
volumeMounts.add(configMount);

return new ContainerBuilder()
@@ -462,16 +518,16 @@ private Container getContainer(String name, ComputerJobSpec spec,
.build();
}

private VolumeMount getConfigMount() {
private VolumeMount getComputerConfigMount() {
return new VolumeMountBuilder()
.withName(CONFIG_MAP_VOLUME)
.withName(COMPUTER_CONFIG_MAP_VOLUME)
.withMountPath(Constants.CONFIG_DIR)
.build();
}

private Volume getConfigVolume(String configMapName) {
private Volume getComputerConfigVolume(String configMapName) {
return new VolumeBuilder()
.withName(CONFIG_MAP_VOLUME)
.withName(COMPUTER_CONFIG_MAP_VOLUME)
.withNewConfigMap()
.withName(configMapName)
.endConfigMap()
@@ -501,4 +557,8 @@ public ObjectMeta getMetadata(HugeGraphComputerJob computerJob,
.withOwnerReferences(ownerReference)
.build();
}

private String volumeName(String name) {
return name + "-volume";
}
}
@@ -5,7 +5,7 @@
<parent>
<artifactId>hugegraph-computer</artifactId>
<groupId>com.baidu.hugegraph</groupId>
<version>0.1.1</version>
<version>0.1.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

@@ -55,6 +55,10 @@
"type": "object",
"existingJavaType": "java.util.Map\u003cString,String\u003e"
},
"configMapPaths": {
"type": "object",
"existingJavaType": "java.util.Map\u003cString,String\u003e"
},
"envFrom": {
"type": "array",
"javaOmitEmpty": true,
@@ -123,6 +127,10 @@
"type": "string",
"existingJavaType": "String"
},
"secretPaths": {
"type": "object",
"existingJavaType": "java.util.Map\u003cString,String\u003e"
},
"volumeMounts": {
"type": "array",
"javaOmitEmpty": true,
@@ -24,13 +24,15 @@
import static com.baidu.hugegraph.config.OptionChecker.rangeInt;

import java.util.Map;
import java.util.Set;

import com.baidu.hugegraph.computer.driver.config.DriverConfigOption;
import com.baidu.hugegraph.computer.k8s.Constants;
import com.baidu.hugegraph.config.ConfigListOption;
import com.baidu.hugegraph.config.ConfigOption;
import com.baidu.hugegraph.config.OptionHolder;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;

import io.fabric8.kubernetes.api.model.Quantity;

@@ -176,6 +178,22 @@ public static boolean checkQuantity(String value) {
String.class
);

public static final ConfigListOption<String> CONFIG_MAP_PATHS =
new ConfigListOption<>(
"k8s.config_map_paths",
"The map of k8s-configmap' name and mount path.",
null,
""
);

public static final ConfigListOption<String> SECRET_PATHS =
new ConfigListOption<>(
"k8s.secret_paths",
"The map of k8s-secret' name and mount path.",
null,
""
);

public static final Map<String, ConfigOption<?>> ALLOW_USER_SETTINGS =
new ImmutableMap.Builder<String, ConfigOption<?>>()
.put(MASTER_CPU.name(), MASTER_CPU)
@@ -185,5 +203,12 @@ public static boolean checkQuantity(String value) {
.put(WORKER_INSTANCES.name(), WORKER_INSTANCES)
.put(JVM_OPTIONS.name(), JVM_OPTIONS)
.put(REMOTE_JAR_URI.name(), REMOTE_JAR_URI)
.put(CONFIG_MAP_PATHS.name(), CONFIG_MAP_PATHS)
.put(SECRET_PATHS.name(), SECRET_PATHS)
.build();

public static final Set<ConfigOption<?>> MAP_TYPE_CONFIGS = ImmutableSet.of(
CONFIG_MAP_PATHS,
SECRET_PATHS
);
}

0 comments on commit e378777

Please sign in to comment.