Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add possibility to mount config map on ephemeral job agent #1505

Merged
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package org.terrakube.api.plugin.scheduler.job.tcl.executor.ephemeral;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.fabric8.kubernetes.api.model.EnvFromSource;
import io.fabric8.kubernetes.api.model.EnvVar;
import io.fabric8.kubernetes.api.model.SecretEnvSource;
import io.fabric8.kubernetes.api.model.*;
import io.fabric8.kubernetes.api.model.batch.v1.JobBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;
import lombok.AllArgsConstructor;
Expand All @@ -30,6 +28,7 @@ public boolean sendToEphemeralExecutor(Job job, ExecutorContext executorContext)
final String jobName = "job-" + job.getId();
deleteEphemeralJob(job);
log.info("Ephemeral Executor Image {}, Job: {}, Namespace: {}, NodeSelector: {}", ephemeralConfiguration.getImage(), jobName, ephemeralConfiguration.getNamespace(), ephemeralConfiguration.getNodeSelector());

SecretEnvSource secretEnvSource = new SecretEnvSource();
secretEnvSource.setName(ephemeralConfiguration.getSecret());
EnvFromSource envFromSource = new EnvFromSource();
Expand All @@ -49,7 +48,6 @@ public boolean sendToEphemeralExecutor(Job job, ExecutorContext executorContext)
} catch (Exception e) {
log.error(e.getMessage());
}

final List<EnvVar> executorEnvVarFlags = Arrays.asList(executorFlagBatch, executorFlagBatchJsonContent);

Optional<String> nodeSelector = Optional.ofNullable(executorContext.getEnvironmentVariables().containsKey(NODE_SELECTOR) ? executorContext.getEnvironmentVariables().get(NODE_SELECTOR) : null);
Expand Down Expand Up @@ -80,6 +78,31 @@ public boolean sendToEphemeralExecutor(Job job, ExecutorContext executorContext)
executorContext.getEnvironmentVariables().containsKey(SERVICE_ACCOUNT) ? executorContext.getEnvironmentVariables().get(SERVICE_ACCOUNT) : null);
String serviceAccount = serviceAccountInfo.isPresent() ? serviceAccountInfo.get() : null;

// Volume and VolumeMount for ConfigMap if specified
List<Volume> volumes = new ArrayList<>();
List<VolumeMount> volumeMounts = new ArrayList<>();

Optional<String> configMapNameOpt = Optional.ofNullable(executorContext.getEnvironmentVariables().get("CONFIG_MAP_NAME"));
Optional<String> configMapMountPathOpt = Optional.ofNullable(executorContext.getEnvironmentVariables().get("CONFIG_MAP_MOUNT_PATH"));
if (configMapNameOpt.isPresent()) {
String configMapName = configMapNameOpt.get();
String mountPath = configMapMountPathOpt.orElse("/home/"); // Default mount path if not specified
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would move the default mount path to /tmp or /mnt. From my POV /home is not safe to be used as a mount point.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I set it as default because it is used by AWS for instance to fetch its .aws/config folder but you're right as a safe point i'll move to tmp !


// Define ConfigMap volume
Volume configMapVolume = new Volume();
configMapVolume.setName("config-volume");
ConfigMapVolumeSource configMapVolumeSource = new ConfigMapVolumeSource();
configMapVolumeSource.setName(configMapName);
configMapVolume.setConfigMap(configMapVolumeSource);
volumes.add(configMapVolume);

// Define VolumeMount for the container
VolumeMount configMapMount = new VolumeMount();
configMapMount.setName("config-volume");
configMapMount.setMountPath(mountPath);
volumeMounts.add(configMapMount);
}

io.fabric8.kubernetes.api.model.batch.v1.Job k8sJob = new JobBuilder()
.withApiVersion("batch/v1")
.withNewMetadata()
Expand All @@ -95,11 +118,13 @@ public boolean sendToEphemeralExecutor(Job job, ExecutorContext executorContext)
.withNewSpec()
.withNodeSelector(nodeSelectorInfo)
.withServiceAccountName(serviceAccount)
.withVolumes(volumes)
.addNewContainer()
.withName(jobName)
.withEnvFrom(executorEnvVarFromSecret)
.withImage(ephemeralConfiguration.getImage())
.withEnv(executorEnvVarFlags)
.withVolumeMounts(volumeMounts)
.endContainer()
.withRestartPolicy("Never")
.endSpec()
Expand Down
Loading