Skip to content

Commit

Permalink
Add parameter k3s.registries to k3s:run for custom `registries.ya…
Browse files Browse the repository at this point in the history
…ml` (#251)
  • Loading branch information
sschnabe authored Aug 14, 2023
1 parent aba6d38 commit 31e056a
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 7 deletions.
1 change: 1 addition & 0 deletions docs/goal/run.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ Start and run k3s container.
| `replaceIfExists` | `k3s.replaceIfExists` | Replace existing docker container from previous run. | false |
| `nodeTimeout` | `k3s.nodeTimeout` | Timeout in seconds to wait for nodes getting ready. | 30 |
| `kubeconfig` | `k3s.kubeconfig` | Path where to place kubectl config for external usage. | ${project.build.directory}/k3s.yaml |
| `registries` | `k3s.registries` | Path to "registry.yaml" to mount to "/etc/rancher/k3s/registries.yaml". | `null` |
| `skipRun` | `skipRun` | Skip running of k3s. | false |
| `debug` | `k3s.debug` | Stream logs of docker and kubectl. | false |
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>

<artifactId>k3s-maven-plugin</artifactId>
<version>1.0.4-SNAPSHOT</version>
<version>1.1.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>

<name>k3s Maven Plugin</name>
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/io/kokuwa/maven/k3s/mojo/RunMojo.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.kokuwa.maven.k3s.mojo;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
Expand Down Expand Up @@ -144,6 +145,14 @@ public class RunMojo extends K3sMojo {
@Parameter(property = "k3s.kubeconfig", defaultValue = "${project.build.directory}/k3s.yaml")
private Path kubeconfig;

/**
* Path to "registry.yaml" to mount to "/etc/rancher/k3s/registries.yaml".
*
* @since 1.1.0
*/
@Parameter(property = "k3s.registries")
private Path registries;

/**
* Timeout in seconds to wait for nodes getting ready.
*
Expand Down Expand Up @@ -223,9 +232,12 @@ public void execute() throws MojoExecutionException {

// create container

if (registries != null && !Files.isRegularFile(registries)) {
throw new MojoExecutionException("Registries file '" + registries + "' not found.");
}
var ports = new ArrayList<>(portBindings);
ports.add(portKubeApi + ":" + portKubeApi);
getDocker().createContainer(image, ports, command);
getDocker().createContainer(image, ports, command, registries);
getDocker().createVolume();

// wait for k3s api to be ready
Expand Down Expand Up @@ -307,4 +319,8 @@ public void setReplaceIfExists(boolean replaceIfExists) {
public void setSkipRun(boolean skipRun) {
this.skipRun = skipRun;
}

public void setRegistries(File registries) {
this.registries = registries == null ? null : registries.toPath().toAbsolutePath();
}
}
6 changes: 5 additions & 1 deletion src/main/java/io/kokuwa/maven/k3s/util/Docker.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,18 @@ public Optional<Container> getContainer() throws MojoExecutionException {
.findAny();
}

public void createContainer(String image, List<String> ports, List<String> k3s) throws MojoExecutionException {
public void createContainer(String image, List<String> ports, List<String> k3s, Path registries)
throws MojoExecutionException {
var command = new ArrayList<String>();
command.add("docker");
command.add("run");
command.add("--name=" + containerName);
command.add("--privileged");
command.add("--detach");
command.add("--volume=" + volumeName + ":/var/lib/rancher/k3s/agent");
if (registries != null) {
command.add("--volume=" + registries + ":/etc/rancher/k3s/registries.yaml");
}
ports.stream().map(port -> "--publish=" + port).forEach(command::add);
command.add(image);
command.addAll(k3s);
Expand Down
28 changes: 26 additions & 2 deletions src/test/java/io/kokuwa/maven/k3s/mojo/RunMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertThrowsExactly;

import java.io.File;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import io.kokuwa.maven.k3s.test.AbstractTest;
import io.kokuwa.maven.k3s.util.Await;

/**
* Test for {@link RunMojo}.
Expand Down Expand Up @@ -44,9 +49,10 @@ void withSkip(RunMojo runMojo) throws MojoExecutionException {
void withFailIfExists(RunMojo runMojo) throws MojoExecutionException {
runMojo.setFailIfExists(true);
assertDoesNotThrow(runMojo::execute);
var message = "Container with id '" + docker.getContainer().get().id
var expectedMessage = "Container with id '" + docker.getContainer().get().id
+ "' found. Please remove that container or set 'k3s.failIfExists' to false.";
assertThrowsExactly(MojoExecutionException.class, runMojo::execute, () -> message);
var actualMessage = assertThrows(MojoExecutionException.class, runMojo::execute).getMessage();
assertEquals(expectedMessage, actualMessage, "exception message");
}

@DisplayName("with replace on existing container")
Expand All @@ -72,4 +78,22 @@ void withoutFailIfExists(RunMojo runMojo) throws MojoExecutionException {
var containerAfter = docker.getContainer().orElseThrow();
assertEquals(containerBefore.id, containerAfter.id, "container shouldn't be replaced");
}

@DisplayName("with custom registries.yaml")
@Test
void withRegistries(RunMojo runMojo, Log log) throws MojoExecutionException {
runMojo.setRegistries(new File("src/test/resources/registries.yaml"));
assertDoesNotThrow(runMojo::execute);
docker.waitForLog(Await.await(log, "registries.yaml used"), logs -> logs.stream()
.anyMatch(l -> l.contains("Using private registry config file at /etc/rancher/k3s/registries.yaml")));
}

@DisplayName("with custom registries.yaml (missing)")
@Test
void withRegistriesMissing(RunMojo runMojo) {
var file = new File("src/test/resources/nope.yaml");
runMojo.setRegistries(file);
var actualMessage = assertThrowsExactly(MojoExecutionException.class, runMojo::execute).getMessage();
assertEquals("Registries file '" + file.getAbsolutePath() + "' not found.", actualMessage, "exception message");
}
}
4 changes: 2 additions & 2 deletions src/test/java/io/kokuwa/maven/k3s/util/DockerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void container(Log log) throws MojoExecutionException {

var ports = List.of("9001:9001", "9002:9002");
docker.createVolume();
docker.createContainer("rancher/k3s", ports, List.of("server"));
docker.createContainer("rancher/k3s", ports, List.of("server"), null);
assertTrue(docker.getContainer().isPresent(), "container not found after creating");
assertTrue(docker.getContainer().map(Container::isRunnding).orElse(null));
docker.waitForLog(Await.await(log, "k3s"), o -> o.stream().anyMatch(l -> l.contains("k3s is up and running")));
Expand Down Expand Up @@ -108,7 +108,7 @@ void copy() throws MojoExecutionException, IOException {
// start container

docker.createVolume();
docker.createContainer("rancher/k3s", List.of(), List.of("server"));
docker.createContainer("rancher/k3s", List.of(), List.of("server"), null);

// define test data

Expand Down
4 changes: 4 additions & 0 deletions src/test/resources/registries.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mirrors:
docker.io:
endpoint:
- https://registry.example.com:5000

0 comments on commit 31e056a

Please sign in to comment.