diff --git a/cameleer3-server-app/src/main/java/com/cameleer3/server/app/runtime/DockerRuntimeOrchestrator.java b/cameleer3-server-app/src/main/java/com/cameleer3/server/app/runtime/DockerRuntimeOrchestrator.java index 29d7034a..b45ee849 100644 --- a/cameleer3-server-app/src/main/java/com/cameleer3/server/app/runtime/DockerRuntimeOrchestrator.java +++ b/cameleer3-server-app/src/main/java/com/cameleer3/server/app/runtime/DockerRuntimeOrchestrator.java @@ -12,10 +12,6 @@ import com.github.dockerjava.api.model.HealthCheck; import com.github.dockerjava.api.model.HostConfig; import com.github.dockerjava.api.model.RestartPolicy; import com.github.dockerjava.api.model.Volume; -import com.github.dockerjava.core.DefaultDockerClientConfig; -import com.github.dockerjava.core.DockerClientImpl; -import com.github.dockerjava.zerodep.ZerodepDockerHttpClient; -import jakarta.annotation.PostConstruct; import jakarta.annotation.PreDestroy; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -29,24 +25,16 @@ import java.util.stream.Stream; public class DockerRuntimeOrchestrator implements RuntimeOrchestrator { private static final Logger log = LoggerFactory.getLogger(DockerRuntimeOrchestrator.class); - private DockerClient dockerClient; + private final DockerClient dockerClient; private ContainerLogForwarder logForwarder; - public void setLogForwarder(ContainerLogForwarder logForwarder) { - this.logForwarder = logForwarder; + public DockerRuntimeOrchestrator(DockerClient dockerClient) { + this.dockerClient = dockerClient; } - @PostConstruct - public void init() { - var config = DefaultDockerClientConfig.createDefaultConfigBuilder() - .withDockerHost("unix:///var/run/docker.sock") - .build(); - var httpClient = new ZerodepDockerHttpClient.Builder() - .dockerHost(config.getDockerHost()) - .build(); - dockerClient = DockerClientImpl.getInstance(config, httpClient); - log.info("Docker client initialized, host: {}", config.getDockerHost()); + public void setLogForwarder(ContainerLogForwarder logForwarder) { + this.logForwarder = logForwarder; } @PreDestroy diff --git a/cameleer3-server-app/src/main/java/com/cameleer3/server/app/runtime/RuntimeOrchestratorAutoConfig.java b/cameleer3-server-app/src/main/java/com/cameleer3/server/app/runtime/RuntimeOrchestratorAutoConfig.java index a63d425f..bbad1145 100644 --- a/cameleer3-server-app/src/main/java/com/cameleer3/server/app/runtime/RuntimeOrchestratorAutoConfig.java +++ b/cameleer3-server-app/src/main/java/com/cameleer3/server/app/runtime/RuntimeOrchestratorAutoConfig.java @@ -4,8 +4,13 @@ import com.cameleer3.server.app.search.ClickHouseLogStore; import com.cameleer3.server.app.storage.PostgresDeploymentRepository; import com.cameleer3.server.core.runtime.DeploymentRepository; import com.cameleer3.server.core.runtime.RuntimeOrchestrator; +import com.github.dockerjava.api.DockerClient; +import com.github.dockerjava.core.DefaultDockerClientConfig; +import com.github.dockerjava.core.DockerClientImpl; +import com.github.dockerjava.zerodep.ZerodepDockerHttpClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -17,13 +22,29 @@ public class RuntimeOrchestratorAutoConfig { private static final Logger log = LoggerFactory.getLogger(RuntimeOrchestratorAutoConfig.class); + @Bean + public DockerClient dockerClient() { + if (!Files.exists(Path.of("/var/run/docker.sock"))) { + return null; + } + var config = DefaultDockerClientConfig.createDefaultConfigBuilder() + .withDockerHost("unix:///var/run/docker.sock") + .build(); + var httpClient = new ZerodepDockerHttpClient.Builder() + .dockerHost(config.getDockerHost()) + .build(); + DockerClient client = DockerClientImpl.getInstance(config, httpClient); + log.info("Docker client initialized, host: {}", config.getDockerHost()); + return client; + } + @Bean public RuntimeOrchestrator runtimeOrchestrator( - @org.springframework.beans.factory.annotation.Autowired(required = false) - ContainerLogForwarder logForwarder) { - if (Files.exists(Path.of("/var/run/docker.sock"))) { + @Autowired(required = false) DockerClient dockerClient, + @Autowired(required = false) ContainerLogForwarder logForwarder) { + if (dockerClient != null) { log.info("Docker socket detected - enabling Docker runtime orchestrator"); - DockerRuntimeOrchestrator orchestrator = new DockerRuntimeOrchestrator(); + DockerRuntimeOrchestrator orchestrator = new DockerRuntimeOrchestrator(dockerClient); if (logForwarder != null) { orchestrator.setLogForwarder(logForwarder); } @@ -34,27 +55,30 @@ public class RuntimeOrchestratorAutoConfig { } @Bean - public DockerNetworkManager dockerNetworkManager(RuntimeOrchestrator orchestrator) { - if (orchestrator instanceof DockerRuntimeOrchestrator docker) { - return new DockerNetworkManager(docker.getDockerClient()); + public DockerNetworkManager dockerNetworkManager( + @Autowired(required = false) DockerClient dockerClient) { + if (dockerClient != null) { + return new DockerNetworkManager(dockerClient); } return null; } @Bean public DockerEventMonitor dockerEventMonitor(RuntimeOrchestrator orchestrator, + @Autowired(required = false) DockerClient dockerClient, DeploymentRepository deploymentRepository) { - if (orchestrator instanceof DockerRuntimeOrchestrator docker) { + if (orchestrator instanceof DockerRuntimeOrchestrator docker && dockerClient != null) { return new DockerEventMonitor(docker, (PostgresDeploymentRepository) deploymentRepository); } return null; } @Bean - public ContainerLogForwarder containerLogForwarder(RuntimeOrchestrator orchestrator, - ClickHouseLogStore logStore) { - if (orchestrator instanceof DockerRuntimeOrchestrator docker) { - return new ContainerLogForwarder(docker.getDockerClient(), logStore); + public ContainerLogForwarder containerLogForwarder( + @Autowired(required = false) DockerClient dockerClient, + ClickHouseLogStore logStore) { + if (dockerClient != null) { + return new ContainerLogForwarder(dockerClient, logStore); } return null; }