Skip to content

Commit

Permalink
Maintenance: use parameter logging (#292)
Browse files Browse the repository at this point in the history
  • Loading branch information
sschnabe authored Sep 16, 2024
1 parent 80c336d commit 02ddd3f
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 44 deletions.
8 changes: 4 additions & 4 deletions src/main/java/io/kokuwa/maven/k3s/mojo/ApplyMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ public void execute() throws MojoExecutionException {
var oldMissing = missing.getAndSet(newMissing);
if (!newMissing.isEmpty()) {
if (oldMissing.equals(newMissing)) {
log.debug("Still waiting for: " + missing);
log.debug("Still waiting for: {}", missing);
} else {
log.info("Still waiting for: " + missing);
log.info("Still waiting for: {}", missing);
}
}
}
Expand Down Expand Up @@ -207,9 +207,9 @@ private Stream<Entry<String, Callable<Boolean>>> waitFor(Entry<String, List<Stri
var representation = "default".equals(namespace) ? name : namespace + "/" + name;
return Map.entry(representation, (Callable<Boolean>) () -> {
try {
log.debug(kind + " " + representation + " ... waiting");
log.debug("{} {} ... waiting", kind, representation);
getDocker().exec(timeout.plusSeconds(10), tmp);
log.info(kind + " " + representation + " ... ready");
log.info("{} {} ... ready", kind, representation);
return true;
} catch (MojoExecutionException e) {
getDocker().exec("kubectl", "get", "--output=yaml", "--namespace=" + namespace, kind, name);
Expand Down
44 changes: 21 additions & 23 deletions src/main/java/io/kokuwa/maven/k3s/mojo/ImageMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public void execute() throws MojoExecutionException {
private boolean tar(Map<String, Map<String, ?>> existingImages, Path tarFile) {

if (!Files.isRegularFile(tarFile)) {
log.error("Tar not found: " + tarFile);
log.error("Tar not found: {}", tarFile);
return false;
}

Expand All @@ -141,13 +141,12 @@ private boolean tar(Map<String, Map<String, ?>> existingImages, Path tarFile) {
.map(l -> l.get(labelChecksum)).filter(Objects::nonNull)
.findAny().orElse(null);
if (oldChecksum == null) {
log.debug("Tar " + tarFile + " does not exists in ctr.");
log.debug("Tar {} does not exists in ctr.", tarFile);
} else if (oldChecksum.equals(newChecksum)) {
log.info("Tar " + tarFile + " present in ctr with checksum " + newChecksum + ", skip.");
log.info("Tar {} present in ctr with checksum {}, skip.", tarFile, newChecksum);
return true;
} else {
log.debug("Tar " + tarFile + " present in ctr with checksum " + oldChecksum + ", new is: "
+ newChecksum);
log.debug("Tar {} present in ctr with checksum {}, new is: {}", tarFile, oldChecksum, newChecksum);
}

// import tar into ctr
Expand All @@ -163,30 +162,30 @@ private boolean tar(Map<String, Map<String, ?>> existingImages, Path tarFile) {
getDocker().exec("ctr", "image", "label", matcher.group("image"),
labelChecksum + "=" + newChecksum);
} else {
log.warn("Tar " + tarFile + " import output cannot be parsed: " + output);
log.warn("Tar {} import output cannot be parsed: {}", tarFile, output);
}
}
} catch (MojoExecutionException | IOException e) {
log.error("Failed to import tar: " + tarFile, e);
log.error("Failed to import tar: {}", tarFile, e);
return false;
}

log.info("Imported tar from " + tarFile);
log.info("Imported tar from {}", tarFile);
return true;
}

private boolean ctr(Map<String, Map<String, ?>> existingImages, String image) throws MojoExecutionException {

if (existingImages.containsKey(image)) {
log.debug("Image " + image + " found in ctr, skip pulling");
log.debug("Image {} found in ctr, skip pulling", image);
return true;
}

log.info("Image " + image + " not found, start pulling");
log.info("Image {} not found, start pulling", image);
// use crictl instead of cri, because crictl honors custom registry.yaml
// see https://github.com/k3s-io/k3s/issues/5277
getDocker().exec(pullTimeout, "crictl", "pull", image);
log.info("Image " + image + " pulled");
log.info("Image {} pulled", image);

return true;
}
Expand All @@ -198,19 +197,19 @@ private boolean docker(Map<String, Map<String, ?>> existingImages, String image)
var digest = getDocker().getImage(image).map(ContainerImage::getDigest).orElse(null);
if (dockerPullAlways || digest == null) {
if (digest != null) {
log.debug("Image " + image + " found in docker, pull always ...");
log.debug("Image {} found in docker, pull always ...", image);
} else {
log.debug("Image " + image + " not found in docker, pulling ...");
log.debug("Image {} not found in docker, pulling ...", image);
}
try {
getDocker().pullImage(image, pullTimeout);
} catch (MojoExecutionException e) {
log.error("Failed to pull docker image " + image, e);
log.error("Failed to pull docker image {}", image, e);
return false;
}
digest = getDocker().getImage(image).map(ContainerImage::getDigest).orElse(null);
} else {
log.debug("Image " + image + " found in docker");
log.debug("Image {} found in docker", image);
}

// skip if image is already present in ctr
Expand All @@ -219,13 +218,12 @@ private boolean docker(Map<String, Map<String, ?>> existingImages, String image)
var label = "k3s-maven-digest";
var oldDigest = existingImages.getOrDefault(normalizedImage, Map.of()).get(label);
if (oldDigest == null) {
log.debug("Image " + image + " does not exists in ctr.");
log.debug("Image {} does not exists in ctr.", image);
} else if (oldDigest.equals(digest)) {
log.info("Image " + image + " present in ctr with digest " + digest + ", skip.");
log.info("Image {} present in ctr with digest {}, skip.", image, digest);
return true;
} else {
log.debug(
"Image " + image + " present in ctr with digest " + oldDigest + ", new digest is: " + digest);
log.debug("Image {} present in ctr with digest {}, new digest is: {}", image, oldDigest, digest);
}

// move from docker to ctr
Expand All @@ -239,11 +237,11 @@ private boolean docker(Map<String, Map<String, ?>> existingImages, String image)
getDocker().exec(pullTimeout, "ctr", "image", "import", destination.toString());
getDocker().exec("ctr", "image", "label", normalizedImage, label + "=" + digest);
} catch (MojoExecutionException e) {
log.error("Failed to import tar " + source, e);
log.error("Failed to import tar {}", source, e);
return false;
}

log.info("Image " + image + " copied from docker deamon");
log.info("Image {} copied from docker deamon", image);
return true;
}

Expand All @@ -259,14 +257,14 @@ private boolean docker(Map<String, Map<String, ?>> existingImages, String image)
.filter(parts -> {
var matches = parts.length == 7;
if (!matches) {
log.warn("Unexpected output of `ctr image list`: " + List.of(parts));
log.warn("Unexpected output of `ctr image list`: {}", List.of(parts));
}
return matches;
})
.map(parts -> Map.entry(parts[0], Stream.of(parts[6].split(",")).map(s -> s.split("="))
.filter(s -> !"io.cri-containerd.image".equals(s[0]))
.collect(Collectors.toMap(s -> s[0], s -> s[1]))))
.peek(entry -> log.debug("Found ctr image: " + entry))
.peek(entry -> log.debug("Found ctr image: {}", entry))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/io/kokuwa/maven/k3s/mojo/RestartMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private Callable<Boolean> restart(String resoure) {

var matcher = resourcePattern.matcher(resoure);
if (!matcher.matches()) {
log.error("Failed to parse resoure: " + resoure);
log.error("Failed to parse resoure: {}", resoure);
return () -> false;
}

Expand All @@ -119,10 +119,10 @@ private Callable<Boolean> restart(String resoure) {
return () -> {
try {
getDocker().exec("kubectl", "rollout", "restart", kind, name, "--namespace=" + namespace);
log.info(kind + " " + namespace + "/" + name + " restarted");
log.info("{} {}/{} restarted", kind, namespace, name);
getDocker().exec("kubectl", "rollout", "status", kind, name, "--namespace=" + namespace,
"--timeout=" + timeout.getSeconds() + "s");
log.info(kind + " " + namespace + "/" + name + " restart finished");
log.info("{} {}/{} restart finished", kind, namespace, name);
return true;
} catch (MojoExecutionException e) {
getDocker().exec("kubectl", "get", "--output=yaml", "--namespace=" + namespace, kind, name);
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/io/kokuwa/maven/k3s/mojo/RunMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,9 @@ public void execute() throws MojoExecutionException {
if (dnsResolverCheck) {
try {
var address = InetAddress.getByName(dnsResolverDomain).getHostAddress();
log.debug("DNS resolved " + dnsResolverDomain + " to " + address + ".");
log.debug("DNS resolved {} to {}.", dnsResolverDomain, address);
} catch (UnknownHostException e) {
log.warn("DNS was unable to resolve " + dnsResolverDomain + ". Custom domains may not work!");
log.warn("DNS was unable to resolve {}. Custom domains may not work!", dnsResolverDomain);
}
}

Expand All @@ -250,14 +250,14 @@ public void execute() throws MojoExecutionException {
throw new MojoExecutionException("Container with id '" + container.id
+ "' found. Please remove that container or set 'k3s.failIfExists' to false.");
} else if (replaceIfExists) {
log.info("Container with id '" + container.id + "' found, replacing");
log.info("Container with id '{}' found, replacing", container.id);
getDocker().removeContainer();
} else if (!container.isRunning()) {
log.warn("Container with id '" + container.id + "' found in stopped state, restart container");
log.warn("Container with id '{}' found in stopped state, restart container", container.id);
create = false;
restart = true;
} else {
log.warn("Container with id '" + container.id + "' found, skip creating");
log.warn("Container with id '{}' found, skip creating", container.id);
create = false;
}
}
Expand All @@ -283,7 +283,7 @@ public void execute() throws MojoExecutionException {
}

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

private void createAndStartK3sContainer() throws MojoExecutionException {
Expand Down Expand Up @@ -331,7 +331,7 @@ private void createAndStartK3sContainer() throws MojoExecutionException {
if (disableTraefik) {
command.add("--disable=traefik");
}
log.info("k3s " + command.stream().collect(Collectors.joining(" ")));
log.info("k3s {}", command.stream().collect(Collectors.joining(" ")));

// create container

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/kokuwa/maven/k3s/util/Await.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ public <V> V until(Callable<V> supplier, Function<V, Boolean> check) throws Mojo
lastException = null;
} catch (Exception e) {
lastException = e;
log.debug("Await " + text + " failed with exception " + e.getMessage());
log.debug("Await {} failed with exception {}", text, e.getMessage());
}
wait(interval);
}

if (lastException != null) {
log.error("Await " + text + " had exception while waiting", lastException);
log.error("Await {} had exception while waiting", text, lastException);
}

onTimeout.run();
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/io/kokuwa/maven/k3s/util/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public List<String> run() throws MojoExecutionException {

public Task start() throws MojoExecutionException {

log.debug(">>> " + this);
log.debug(">>> {}", this);

try {
var builder = new ProcessBuilder(command);
Expand Down Expand Up @@ -102,8 +102,8 @@ public Task waitFor() throws MojoExecutionException {
public Task verify() throws MojoExecutionException {
var exitCode = process.exitValue();
if (exitCode != 0) {
log.error(">>> " + this);
output.forEach(line -> log.error("<<< " + line));
log.error(">>> {}", this);
output.forEach(line -> log.error("<<< {}", line));
throw new MojoExecutionException("Command failed with exit code " + exitCode + ": " + this);
}
return this;
Expand All @@ -128,10 +128,10 @@ private void collectLogs(String stream, InputStream inputStream) {
String line;
while ((line = reader.readLine()) != null) {
output.add(line);
log.debug("<<< [" + stream + "] " + line);
log.debug("<<< [{}] {}", stream, line);
}
} catch (IOException e) {
log.debug("Stream " + stream + " closed unexpected: " + e.getMessage());
log.debug("Stream {} closed unexpected: {}", stream, e.getMessage());
}
});
thread.start();
Expand Down

0 comments on commit 02ddd3f

Please sign in to comment.