Skip to content

Commit

Permalink
Add special handling for windows path and docker path (#258)
Browse files Browse the repository at this point in the history
  • Loading branch information
sschnabe authored Sep 10, 2023
1 parent 04b7437 commit ad582ef
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/main/java/io/kokuwa/maven/k3s/mojo/ApplyMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void execute() throws MojoExecutionException {
if (getDocker().getContainer().isEmpty()) {
throw new MojoExecutionException("No k3s container found");
}
getDocker().copyToContainer(manifests, path);
getDocker().copyToContainer(manifests, toLinuxPath(path));

// wait for service account, see https://github.com/kubernetes/kubernetes/issues/66689

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/kokuwa/maven/k3s/mojo/ImageMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ private boolean tar(Map<String, Map<String, ?>> existingImages, Path tarFile) {

// import tar into ctr

var destination = Paths.get("/tmp").resolve(tarFile.getFileName() + "_" + System.nanoTime());
var destination = "/tmp/" + tarFile.getFileName() + "_" + System.nanoTime();
var outputPattern = Pattern.compile("^unpacking (?<image>.*) \\(sha256:[0-9a-f]{64}\\).*$");

getDocker().copyToContainer(tarFile, destination);
Expand Down Expand Up @@ -232,7 +232,7 @@ private boolean docker(Map<String, Map<String, ?>> existingImages, String image)

var filename = Paths.get(image.hashCode() + ".tar");
var source = Paths.get(System.getProperty("java.io.tmpdir")).resolve(filename);
var destination = Paths.get("/tmp").resolve(filename);
var destination = "/tmp/" + filename;
try {
getDocker().saveImage(image, source);
getDocker().copyToContainer(source, destination);
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/io/kokuwa/maven/k3s/mojo/K3sMojo.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.kokuwa.maven.k3s.mojo;

import java.nio.file.Path;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.Parameter;
Expand Down Expand Up @@ -57,6 +59,11 @@ public Docker getDocker() {
return docker == null ? docker = new Docker(containerName, volumeName, getLog()) : docker;
}

public String toLinuxPath(Path path) {
// ugly hack for windows - docker path inside k3s needs to be a kinux path
return path.toString().replace("\\", "/");
}

// setter

public void setDebug(boolean debug) {
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/io/kokuwa/maven/k3s/mojo/RunMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -294,7 +293,7 @@ public void execute() throws MojoExecutionException {
getDocker().waitForLog(await, output -> output.stream().anyMatch(l -> l.contains("k3s is up and running")));
}

getDocker().copyFromContainer(Paths.get("/etc/rancher/k3s/k3s.yaml"), kubeconfig);
getDocker().copyFromContainer("/etc/rancher/k3s/k3s.yaml", kubeconfig);
getLog().info("k3s ready: KUBECONFIG=" + kubeconfig + " kubectl get all --all-namespaces");
}

Expand Down
13 changes: 4 additions & 9 deletions src/main/java/io/kokuwa/maven/k3s/util/Docker.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ public void removeContainer() throws MojoExecutionException {
Task.of(log, "docker", "rm", containerName, "--force", "--volumes").run();
}

public void copyFromContainer(Path source, Path destination) throws MojoExecutionException {
Task.of(log, "docker", "cp", containerName + ":" + toLinuxPath(source), destination.toString()).run();
public void copyFromContainer(String source, Path destination) throws MojoExecutionException {
Task.of(log, "docker", "cp", containerName + ":" + source, destination.toString()).run();
}

public void copyToContainer(Path source, Path destination) throws MojoExecutionException {
Task.of(log, "docker", "cp", source.toString(), containerName + ":" + toLinuxPath(destination)).run();
public void copyToContainer(Path source, String destination) throws MojoExecutionException {
Task.of(log, "docker", "cp", source.toString(), containerName + ":" + destination).run();
}

public void waitForLog(Await await, Function<List<String>, Boolean> checker) throws MojoExecutionException {
Expand Down Expand Up @@ -168,11 +168,6 @@ public void removeImage(String image) throws MojoExecutionException {

// internal

private String toLinuxPath(Path path) {
// ugly hack for windows - docker path inside k3s needs to be a kinux path
return path.toString().replace("\\", "/");
}

private <T> T readValue(Class<T> type, String output) {
try {
return mapper.readValue(output, type);
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/io/kokuwa/maven/k3s/util/DockerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ void copy() throws MojoExecutionException, IOException {
Files.deleteIfExists(sourceFile);
Files.deleteIfExists(returnFile);
Files.write(sourceFile, initialContent.toString().getBytes());
docker.copyToContainer(sourceDir, containerDir);
docker.copyFromContainer(containerDir, returnDir);
docker.copyToContainer(sourceDir, containerDir.toString());
docker.copyFromContainer(containerDir.toString(), returnDir);
assertEquals(initialContent, Files.readString(returnFile));

// write changed file and copy to container
Expand All @@ -136,8 +136,8 @@ void copy() throws MojoExecutionException, IOException {
Files.deleteIfExists(sourceFile);
Files.deleteIfExists(returnFile);
Files.write(sourceFile, changedContent.toString().getBytes());
docker.copyToContainer(sourceDir, containerDir);
docker.copyFromContainer(containerDir, returnDir);
docker.copyToContainer(sourceDir, containerDir.toString());
docker.copyFromContainer(containerDir.toString(), returnDir);
assertEquals(changedContent, Files.readString(returnFile));
}
}

0 comments on commit ad582ef

Please sign in to comment.