Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for labels in ComposeService #1512

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 label(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").label(Map.of("a", "aa", "z", "zz")));
container.add("test", (service) -> service.label(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")));
}

}