Skip to content

Commit

Permalink
fix: fix dockerfile building for container runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
matteo-s committed Apr 30, 2024
1 parent 865f407 commit 78ac814
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,7 @@ public V1Job build(K8sKanikoRunnable runnable) throws K8sFrameworkException {
"init-config-map",
Map.of("name", "init-config-map-" + runnable.getId())
);

if (runnableVolumesOpt.stream().noneMatch(v -> "init-config-map".equals(v.getName()))) {
coreVolumes.add(configMapVolume);
}
coreVolumes.add(configMapVolume);

// Add secret for kaniko
if (StringUtils.hasText(kanikoSecret)) {
Expand All @@ -181,7 +178,14 @@ public V1Job build(K8sKanikoRunnable runnable) throws K8sFrameworkException {
List.of(
"--dockerfile=/init-config-map/Dockerfile",
"--context=/shared",
"--destination=" + imageRegistry + "/" + imagePrefix + "-" + runnable.getImage() + ":" + runnable.getId()
"--destination=" +
imageRegistry +
"/" +
imagePrefix +
"-" +
runnable.getImage() +
":" +
runnable.getId()
)
);
// Add Kaniko args
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

/**
* ContainerJobRunner
Expand All @@ -29,6 +31,7 @@
*
* @RunnerComponent(runtime = "container", task = "job")
*/
@Slf4j
public class ContainerBuildRunner implements Runner<K8sRunnable> {

private static final String TASK = "job";
Expand All @@ -48,29 +51,41 @@ public K8sRunnable produce(Run run) {
StatusFieldAccessor statusFieldAccessor = StatusFieldAccessor.with(run.getStatus());

List<CoreEnv> coreEnvList = new ArrayList<>(
List.of(new CoreEnv("PROJECT_NAME", run.getProject()), new CoreEnv("RUN_ID", run.getId()))
List.of(new CoreEnv("PROJECT_NAME", run.getProject()), new CoreEnv("RUN_ID", run.getId()))
);

Optional.ofNullable(taskSpec.getEnvs()).ifPresent(coreEnvList::addAll);

// Generate docker file
DockerfileGenerator dockerfileGenerator = new DockerfileGenerator();

if (!StringUtils.hasText(functionSpec.getBaseImage())) {
throw new IllegalArgumentException("invalid or missing baseImage");
}

// Add image to docker file
dockerfileGenerator.addInstruction(DockerfileInstruction.FROM, "FROM " + functionSpec.getBaseImage());

dockerfileGenerator.addInstruction(DockerfileInstruction.COPY, "COPY . .");
// copy context content to workdir
dockerfileGenerator.addInstruction(DockerfileInstruction.COPY, "COPY . ./build");
dockerfileGenerator.addInstruction(DockerfileInstruction.WORKDIR, "/build");

dockerfileGenerator.addInstruction(DockerfileInstruction.RUN, "CD /shared");
if (log.isDebugEnabled()) {
//add debug instructions to docker file
dockerfileGenerator.addInstruction(
DockerfileInstruction.RUN,
"PWD=`pwd`;echo \"DEBUG: Current dir ${PWD}\";LS=`ls -R`;echo \"DEBUG: Current dir content:\" && echo \"${LS}\";"
);
}

// Add Instructions to docker file
Optional
.ofNullable(taskSpec.getInstructions())
.ifPresent(instructions ->
instructions.forEach(instruction ->
dockerfileGenerator.addInstruction(DockerfileInstruction.RUN, "RUN " + instruction)
)
);
.ofNullable(taskSpec.getInstructions())
.ifPresent(instructions ->
instructions.forEach(instruction ->
dockerfileGenerator.addInstruction(DockerfileInstruction.RUN, "RUN " + instruction)
)
);

// Generate string docker file
String dockerfile = dockerfileGenerator.generateDockerfile();
Expand All @@ -80,28 +95,28 @@ public K8sRunnable produce(Run run) {

// Build runnable
return K8sKanikoRunnable
.builder()
.id(run.getId())
.project(run.getProject())
.runtime(ContainerRuntime.RUNTIME)
.task(TASK)
.state(State.READY.name())
// Base
.image(runSpecAccessor.getProject() + "-" + runSpecAccessor.getFunction())
.envs(coreEnvList)
.secrets(groupedSecrets)
.resources(taskSpec.getResources())
.volumes(taskSpec.getVolumes())
.nodeSelector(taskSpec.getNodeSelector())
.affinity(taskSpec.getAffinity())
.tolerations(taskSpec.getTolerations())
.labels(taskSpec.getLabels())
// Task specific
.contextRefs(taskSpec.getContextRefs())
.contextSources(taskSpec.getContextSources())
.dockerFile(dockerfile)
// specific
.backoffLimit(1)
.build();
.builder()
.id(run.getId())
.project(run.getProject())
.runtime(ContainerRuntime.RUNTIME)
.task(TASK)
.state(State.READY.name())
// Base
.image(runSpecAccessor.getProject() + "-" + runSpecAccessor.getFunction())
.envs(coreEnvList)
.secrets(groupedSecrets)
.resources(taskSpec.getResources())
.volumes(taskSpec.getVolumes())
.nodeSelector(taskSpec.getNodeSelector())
.affinity(taskSpec.getAffinity())
.tolerations(taskSpec.getTolerations())
.labels(taskSpec.getLabels())
// Task specific
.contextRefs(taskSpec.getContextRefs())
.contextSources(taskSpec.getContextSources())
.dockerFile(dockerfile)
// specific
.backoffLimit(1)
.build();
}
}

0 comments on commit 78ac814

Please sign in to comment.