Skip to content

Commit

Permalink
Merge pull request #1512 from eddumelendez
Browse files Browse the repository at this point in the history
* pr/1512:
  Polish "Add support for labels in ComposeService"
  Add support for labels in ComposeService

Closes gh-1512
  • Loading branch information
mhalbritter committed Feb 29, 2024
2 parents dc3f82f + 0218a97 commit 89cb191
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,6 +29,7 @@
*
* @author Stephane Nicoll
* @author Moritz Halbritter
* @author Eddú Meléndez
*/
public class ComposeFileWriter {

Expand All @@ -52,6 +53,7 @@ private void writeService(IndentingWriter writer, ComposeService service) {
writer.indented(() -> {
writer.println("image: '%s:%s'".formatted(service.getImage(), service.getImageTag()));
writerServiceEnvironment(writer, service.getEnvironment());
writerServiceLabels(writer, service.getLabels());
writerServicePorts(writer, service.getPorts());
writeServiceCommand(writer, service.getCommand());
});
Expand Down Expand Up @@ -89,4 +91,16 @@ private void writeServiceCommand(IndentingWriter writer, String command) {
writer.println("command: '%s'".formatted(command));
}

private void writerServiceLabels(IndentingWriter writer, Map<String, String> labels) {
if (labels.isEmpty()) {
return;
}
writer.println("labels:");
writer.indented(() -> {
for (Map.Entry<String, String> label : labels.entrySet()) {
writer.println("- \"%s=%s\"".formatted(label.getKey(), label.getValue()));
}
});
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,6 +29,7 @@
*
* @author Moritz Halbritter
* @author Stephane Nicoll
* @author Eddú Meléndez
*/
public final class ComposeService {

Expand All @@ -46,6 +47,8 @@ public final class ComposeService {

private final String command;

private final Map<String, String> labels;

private ComposeService(Builder builder) {
this.name = builder.name;
this.image = builder.image;
Expand All @@ -54,6 +57,7 @@ private ComposeService(Builder builder) {
this.environment = Collections.unmodifiableMap(new TreeMap<>(builder.environment));
this.ports = Collections.unmodifiableSet(new TreeSet<>(builder.ports));
this.command = builder.command;
this.labels = Collections.unmodifiableMap(new TreeMap<>(builder.labels));
}

public String getName() {
Expand Down Expand Up @@ -84,6 +88,10 @@ public String getCommand() {
return this.command;
}

public Map<String, String> getLabels() {
return this.labels;
}

/**
* Builder for {@link ComposeService}.
*/
Expand All @@ -103,6 +111,8 @@ public static class Builder {

private String command;

private final Map<String, String> labels = new TreeMap<>();

protected Builder(String name) {
this.name = name;
}
Expand Down Expand Up @@ -152,6 +162,16 @@ public Builder command(String command) {
return this;
}

public Builder label(String key, String value) {
this.labels.put(key, value);
return this;
}

public Builder labels(Map<String, String> label) {
this.labels.putAll(label);
return this;
}

/**
* Builds the {@link ComposeService} instance.
* @return the built instance
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,6 +30,7 @@
*
* @author Moritz Halbritter
* @author Stephane Nicoll
* @author Eddú Meléndez
*/
class ComposeFileWriterTests {

Expand Down Expand Up @@ -58,14 +59,17 @@ void writeDetailedService() {
.environment("ELASTIC_PASSWORD", "secret")
.environment("discovery.type", "single-node")
.ports(9200, 9300)
.command("bin/run thing"));
.command("bin/run thing")
.label("foo", "bar"));
assertThat(write(file)).isEqualToIgnoringNewLines("""
services:
elasticsearch:
image: 'elasticsearch:8.6.1'
environment:
- 'ELASTIC_PASSWORD=secret'
- 'discovery.type=single-node'
labels:
- "foo=bar"
ports:
- '9200'
- '9300'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,6 +27,7 @@
* Tests for {@link ComposeServiceContainer}.
*
* @author Stephane Nicoll
* @author Eddú Meléndez
*/
class ComposeServiceContainerTests {

Expand Down Expand Up @@ -126,6 +127,7 @@ void customizeService() {
service.environment("param", "value");
service.ports(8080);
service.command("run");
service.label("foo", "bar");
});
assertThat(container.values()).singleElement().satisfies((service) -> {
assertThat(service.getName()).isEqualTo("test");
Expand All @@ -135,6 +137,7 @@ void customizeService() {
assertThat(service.getEnvironment()).containsOnly(entry("param", "value"));
assertThat(service.getPorts()).containsOnly(8080);
assertThat(service.getCommand()).isEqualTo("run");
assertThat(service.getLabels()).containsOnly(entry("foo", "bar"));
});
}

Expand Down Expand Up @@ -184,4 +187,24 @@ void removeWithNonMatchingName() {
assertThat(container.isEmpty()).isFalse();
}

@Test
void labelKeysAreSorted() {
ComposeServiceContainer container = new ComposeServiceContainer();
container.add("test", (service) -> service.imageAndTag("my-image").label("z", "zz"));
container.add("test", (service) -> service.label("a", "aa"));
assertThat(container.values()).singleElement()
.satisfies(
(service) -> assertThat(service.getLabels()).containsExactly(entry("a", "aa"), entry("z", "zz")));
}

@Test
void labelIsMerged() {
ComposeServiceContainer container = new ComposeServiceContainer();
container.add("test", (service) -> service.imageAndTag("my-image").labels(Map.of("a", "aa", "z", "zz")));
container.add("test", (service) -> service.labels(Map.of("a", "aaa", "b", "bb")));
assertThat(container.values()).singleElement()
.satisfies((service) -> assertThat(service.getLabels()).containsExactly(entry("a", "aaa"), entry("b", "bb"),
entry("z", "zz")));
}

}

0 comments on commit 89cb191

Please sign in to comment.