diff options
Diffstat (limited to 'docker-api')
3 files changed, 61 insertions, 29 deletions
diff --git a/docker-api/src/main/java/com/yahoo/vespa/hosted/dockerapi/Docker.java b/docker-api/src/main/java/com/yahoo/vespa/hosted/dockerapi/Docker.java index 2bf3f0f8d84..bc94c39d135 100644 --- a/docker-api/src/main/java/com/yahoo/vespa/hosted/dockerapi/Docker.java +++ b/docker-api/src/main/java/com/yahoo/vespa/hosted/dockerapi/Docker.java @@ -12,6 +12,11 @@ import java.util.Optional; * and to avoid OSGi exporting those classes. */ public interface Docker { + /** + * Must be called before any other method. May be called more than once. + */ + void start(); + interface CreateContainerCommand { CreateContainerCommand withLabel(String name, String value); CreateContainerCommand withEnvironment(String name, String value); diff --git a/docker-api/src/main/java/com/yahoo/vespa/hosted/dockerapi/DockerImpl.java b/docker-api/src/main/java/com/yahoo/vespa/hosted/dockerapi/DockerImpl.java index 9de2cae604f..054f466657f 100644 --- a/docker-api/src/main/java/com/yahoo/vespa/hosted/dockerapi/DockerImpl.java +++ b/docker-api/src/main/java/com/yahoo/vespa/hosted/dockerapi/DockerImpl.java @@ -59,7 +59,10 @@ public class DockerImpl implements Docker { static final String LABEL_NAME_MANAGEDBY = "com.yahoo.vespa.managedby"; private final int SECONDS_TO_WAIT_BEFORE_KILLING; + private final boolean fallbackTo123OnErrors; private static final String FRAMEWORK_CONTAINER_PREFIX = "/"; + private final DockerConfig config; + private final boolean inProduction; private Optional<DockerImageGarbageCollector> dockerImageGC = Optional.empty(); private CounterWrapper numberOfDockerDaemonFails; @@ -68,39 +71,61 @@ public class DockerImpl implements Docker { private final Set<DockerImage> scheduledPulls = new HashSet<>(); // Exposed for testing. - final DockerClient dockerClient; + DockerClient dockerClient; + + @Inject + public DockerImpl(final DockerConfig config, MetricReceiverWrapper metricReceiver) { + this(config, + true, /* fallback to 1.23 on errors */ + metricReceiver, + !config.isRunningLocally()); + } + + private DockerImpl(final DockerConfig config, + boolean fallbackTo123OnErrors, + MetricReceiverWrapper metricReceiverWrapper, + boolean inProduction) { + this.config = config; + this.fallbackTo123OnErrors = fallbackTo123OnErrors; + this.inProduction = inProduction; + if (config == null) { + this.SECONDS_TO_WAIT_BEFORE_KILLING = 10; + } else { + SECONDS_TO_WAIT_BEFORE_KILLING = config.secondsToWaitBeforeKillingContainer(); + } + if (metricReceiverWrapper != null) { + setMetrics(metricReceiverWrapper); + } + } // For testing DockerImpl(final DockerClient dockerClient) { + this(null, false, null, false); this.dockerClient = dockerClient; - this.SECONDS_TO_WAIT_BEFORE_KILLING = 10; } - DockerImpl( - final DockerConfig config, - boolean fallbackTo123OnErrors, - MetricReceiverWrapper metricReceiverWrapper) { - SECONDS_TO_WAIT_BEFORE_KILLING = config.secondsToWaitBeforeKillingContainer(); - - dockerClient = initDockerConnection(config, fallbackTo123OnErrors); - setMetrics(metricReceiverWrapper); + // For testing + DockerImpl(final DockerConfig config, + boolean fallbackTo123OnErrors, + MetricReceiverWrapper metricReceiverWrapper) { + this(config, fallbackTo123OnErrors, metricReceiverWrapper, false); } - @Inject - public DockerImpl(final DockerConfig config, MetricReceiverWrapper metricReceiver) { - this( - config, - true, /* fallback to 1.23 on errors */ - metricReceiver); - - if (!config.isRunningLocally()) { - Duration minAgeToDelete = Duration.ofMinutes(config.imageGCMinTimeToLiveMinutes()); - dockerImageGC = Optional.of(new DockerImageGarbageCollector(minAgeToDelete)); - - try { - setupDockerNetworkIfNeeded(); - } catch (Exception e) { - throw new DockerException("Could not setup docker network", e); + @Override + public void start() { + if (config != null) { + if (dockerClient == null) { + dockerClient = initDockerConnection(); + } + if (inProduction) { + Duration minAgeToDelete = Duration.ofMinutes(config.imageGCMinTimeToLiveMinutes()); + dockerImageGC = Optional.of(new DockerImageGarbageCollector(minAgeToDelete)); + + try { + setupDockerNetworkIfNeeded(); + } catch (Exception e) { + throw new DockerException("Could not setup docker network", e); + } } } } @@ -489,7 +514,7 @@ public class DockerImpl implements Docker { } } - private DockerClient initDockerConnection(final DockerConfig config, boolean fallbackTo123orErrors) { + private DockerClient initDockerConnection() { JerseyDockerCmdExecFactory dockerFactory = new JerseyDockerCmdExecFactory() .withMaxPerRouteConnections(config.maxPerRouteConnections()) .withMaxTotalConnections(config.maxTotalConnections()) @@ -508,7 +533,7 @@ public class DockerImpl implements Docker { logger.info("Found version 1.24 or newer of remote API, using 1.23."); } } catch (Exception e) { - if (!fallbackTo123orErrors) { + if (!fallbackTo123OnErrors) { throw e; } logger.log(LogLevel.ERROR, "Failed when trying to figure out remote API version of docker, using 1.23", e); diff --git a/docker-api/src/main/java/com/yahoo/vespa/hosted/dockerapi/DockerTestUtils.java b/docker-api/src/main/java/com/yahoo/vespa/hosted/dockerapi/DockerTestUtils.java index 079d6876043..549af0d85cb 100644 --- a/docker-api/src/main/java/com/yahoo/vespa/hosted/dockerapi/DockerTestUtils.java +++ b/docker-api/src/main/java/com/yahoo/vespa/hosted/dockerapi/DockerTestUtils.java @@ -46,11 +46,13 @@ public class DockerTestUtils { public static DockerImpl getDocker() { if (docker == null) { - docker = new DockerImpl( + DockerImpl tmpDocker = new DockerImpl( dockerConfig, false, /* fallback to 1.23 on errors */ new MetricReceiverWrapper(MetricReceiver.nullImplementation)); - createDockerTestNetworkIfNeeded(docker); + tmpDocker.start(); + createDockerTestNetworkIfNeeded(tmpDocker); + docker = tmpDocker; } return docker; |