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

feat: add custom labels to docker services #1251

Open
wants to merge 5 commits into
base: nightly
Choose a base branch
from
Open
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
Expand Up @@ -33,6 +33,7 @@
import eu.cloudnetservice.node.command.annotation.Description;
import eu.cloudnetservice.node.command.source.CommandSource;
import jakarta.inject.Singleton;
import java.util.Map;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -165,6 +166,37 @@ public void removeExposedPort(
source.sendMessage(I18n.trans("command-tasks-remove-collection-property", "exposedPort", task.name(), port));
}

@CommandMethod("docker task <task> add label <key> <value>")
public void addLabel(
@NonNull CommandSource source,
@Argument("task") @NonNull ServiceTask task,
@Argument("key") @NonNull String key,
@Argument("value") @NonNull String value
) {
var entry = Map.entry(key, value);
this.updateTaskDockerConfig(task, ($, builder) -> builder.addLabel(entry));
source.sendMessage(I18n.trans("command-tasks-add-collection-property", "labels", task.name(), entry));
}

@CommandMethod("docker task <task> clear labels")
public void clearLabels(
@NonNull CommandSource source,
@Argument("task") @NonNull ServiceTask task
) {
this.updateTaskDockerConfig(task, ($, builder) -> builder.labels(Map.of()));
source.sendMessage(I18n.trans("command-tasks-clear-property", "labels", task.name()));
}

@CommandMethod("docker task <task> remove label <key>")
public void removeLabel(
@NonNull CommandSource source,
@Argument("task") @NonNull ServiceTask task,
@Argument("key") @NonNull String key
) {
this.updateTaskDockerConfig(task, (config, builder) -> builder.removeLabel(key));
source.sendMessage(I18n.trans("command-tasks-remove-collection-property", "labels", task.name(), key));
}

@CommandMethod("docker config network <network>")
public void setNetwork(@NonNull CommandSource source, @Argument("network") @NonNull String network) {
this.updateDockerConfig(($, builder) -> builder.network(network));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.github.dockerjava.api.DockerClient;
import eu.cloudnetservice.driver.event.EventManager;
import eu.cloudnetservice.driver.provider.ServiceTaskProvider;
import eu.cloudnetservice.driver.service.ServiceConfiguration;
import eu.cloudnetservice.modules.docker.config.DockerConfiguration;
import eu.cloudnetservice.node.TickLoop;
Expand All @@ -36,6 +37,7 @@ public class DockerizedLocalCloudServiceFactory extends BaseLocalCloudServiceFac
protected final TickLoop mainThread;
protected final EventManager eventManager;
protected final DockerClient dockerClient;
protected final ServiceTaskProvider taskProvider;
protected final DockerConfiguration dockerConfiguration;
protected final CloudServiceManager cloudServiceManager;

Expand All @@ -47,13 +49,15 @@ public DockerizedLocalCloudServiceFactory(
@NonNull EventManager eventManager,
@NonNull ServiceVersionProvider versionProvider,
@NonNull DockerClient dockerClient,
@NonNull ServiceTaskProvider taskProvider,
@NonNull DockerConfiguration configuration
) {
super(nodeConfig, versionProvider);
this.mainThread = tickLoop;
this.eventManager = eventManager;
this.cloudServiceManager = cloudServiceManager;
this.dockerClient = dockerClient;
this.taskProvider = taskProvider;
this.dockerConfiguration = configuration;
}

Expand All @@ -70,6 +74,7 @@ public DockerizedLocalCloudServiceFactory(
return new DockerizedService(
this.mainThread,
this.configuration,
this.taskProvider,
config,
manager,
this.eventManager,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
import com.google.common.collect.Lists;
import eu.cloudnetservice.common.util.StringUtil;
import eu.cloudnetservice.driver.event.EventManager;
import eu.cloudnetservice.driver.provider.ServiceTaskProvider;
import eu.cloudnetservice.driver.service.ServiceConfiguration;
import eu.cloudnetservice.driver.service.ServiceTask;
import eu.cloudnetservice.modules.docker.config.DockerConfiguration;
import eu.cloudnetservice.modules.docker.config.DockerImage;
import eu.cloudnetservice.modules.docker.config.TaskDockerConfig;
Expand All @@ -51,6 +53,7 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -83,6 +86,7 @@ public class DockerizedService extends JVMService {
Capability.NET_BIND_SERVICE
).toArray(Capability[]::new);

protected final ServiceTask selfTask;
protected final DockerClient dockerClient;
protected final DockerConfiguration configuration;
protected final DockerizedServiceLogCache logCache;
Expand All @@ -96,6 +100,7 @@ public class DockerizedService extends JVMService {
protected DockerizedService(
@NonNull TickLoop tickLoop,
@NonNull Configuration nodeConfig,
@NonNull ServiceTaskProvider taskProvider,
@NonNull ServiceConfiguration configuration,
@NonNull CloudServiceManager manager,
@NonNull EventManager eventManager,
Expand All @@ -106,6 +111,7 @@ protected DockerizedService(
) {
super(tickLoop, nodeConfig, configuration, manager, eventManager, versionProvider, serviceConfigurationPreparer);

this.selfTask = taskProvider.serviceTask(configuration.serviceId().taskName());
this.dockerClient = dockerClient;
this.configuration = dockerConfiguration;

Expand Down Expand Up @@ -197,6 +203,19 @@ protected void doStartProcess(
// an isolated, single java installation available which is always accessible via 'java'
arguments.set(0, "java");

// build the default docker labels and add the configured labels later on to make sure
// that the user can override any default labels
var labels = new HashMap<>(Map.of(
"Service", "CloudNet",
"Name", this.serviceId().name(),
"Uid", this.serviceId().uniqueId().toString(),
"Id", Integer.toString(this.serviceId().taskServiceId())));

var taskLabels = this.readFromTaskConfig(TaskDockerConfig::labels);
if (taskLabels != null) {
labels.putAll(taskLabels);
}

// create the container and store the container id
this.containerId = this.dockerClient.createContainerCmd(image.imageName())
.withEnv(env)
Expand All @@ -216,11 +235,7 @@ protected void doStartProcess(
.withRestartPolicy(RestartPolicy.noRestart())
.withNetworkMode(this.configuration.network())
.withLogConfig(new LogConfig(LogConfig.LoggingType.LOCAL, LOGGING_OPTIONS)))
.withLabels(Map.of(
"Service", "CloudNet",
"Name", this.serviceId().name(),
"Uid", this.serviceId().uniqueId().toString(),
"Id", Integer.toString(this.serviceId().taskServiceId())))
.withLabels(labels)
.exec()
.getId();
}
Expand Down Expand Up @@ -317,7 +332,13 @@ public void doDelete() {
}

protected @Nullable <T> T readFromTaskConfig(@NonNull Function<TaskDockerConfig, T> reader) {
var config = this.serviceConfiguration.propertyHolder().readObject("dockerConfig", TaskDockerConfig.class);
// it is possible to start a service with a task name that is not linked to a real task therefore we need to
// make sure that the task exists before proceeding
if (this.selfTask == null) {
return null;
}

var config = this.selfTask.propertyHolder().readObject("dockerConfig", TaskDockerConfig.class);
return config == null ? null : reader.apply(config);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,9 @@ public static final class Builder {
this.factoryName,
this.network,
this.javaImage,
this.volumes,
this.binds,
this.exposedPorts,
Set.copyOf(this.volumes),
Set.copyOf(this.binds),
Set.copyOf(this.exposedPorts),
this.dockerHost,
this.dockerCertPath,
this.registryUsername,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package eu.cloudnetservice.modules.docker.config;

import com.github.dockerjava.api.model.ExposedPort;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import lombok.NonNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -26,7 +28,8 @@ public record TaskDockerConfig(
@Nullable DockerImage javaImage,
@NonNull Set<String> volumes,
@NonNull Set<String> binds,
@NonNull Set<ExposedPort> exposedPorts
@NonNull Set<ExposedPort> exposedPorts,
@NonNull Map<String, String> labels
) {

public static @NonNull Builder builder() {
Expand All @@ -38,7 +41,8 @@ public record TaskDockerConfig(
.javaImage(config.javaImage())
.volumes(config.volumes())
.binds(config.binds())
.exposedPorts(config.exposedPorts());
.exposedPorts(config.exposedPorts())
.labels(config.labels());
}

public static class Builder {
Expand All @@ -47,6 +51,7 @@ public static class Builder {
private Set<String> volumes = new HashSet<>();
private Set<String> binds = new HashSet<>();
private Set<ExposedPort> exposedPorts = new HashSet<>();
private Map<String, String> labels = new HashMap<>();

public @NonNull Builder javaImage(@Nullable DockerImage javaImage) {
this.javaImage = javaImage;
Expand Down Expand Up @@ -83,8 +88,23 @@ public static class Builder {
return this;
}

public @NonNull Builder labels(@NonNull Map<String, String> labels) {
this.labels = new HashMap<>(labels);
return this;
}

public @NonNull Builder addLabel(@NonNull Map.Entry<String, String> label) {
this.labels.put(label.getKey(), label.getValue());
return this;
}

public @NonNull Builder removeLabel(@NonNull String label) {
this.labels.remove(label);
return this;
}

public @NonNull TaskDockerConfig build() {
return new TaskDockerConfig(this.javaImage, this.volumes, this.binds, this.exposedPorts);
return new TaskDockerConfig(this.javaImage, this.volumes, this.binds, this.exposedPorts, this.labels);
}
}
}
Loading