Repository: aries-containers Updated Branches: refs/heads/master 4764c4e45 -> 5ba10fe5a
Initial Container Service API Includes unit tests for the configuration builder class. Project: http://git-wip-us.apache.org/repos/asf/aries-containers/repo Commit: http://git-wip-us.apache.org/repos/asf/aries-containers/commit/5ba10fe5 Tree: http://git-wip-us.apache.org/repos/asf/aries-containers/tree/5ba10fe5 Diff: http://git-wip-us.apache.org/repos/asf/aries-containers/diff/5ba10fe5 Branch: refs/heads/master Commit: 5ba10fe5a02589cb77b0cf6c0b05ad6c96066a80 Parents: 4764c4e Author: David Bosschaert <[email protected]> Authored: Thu May 25 12:09:30 2017 +0100 Committer: David Bosschaert <[email protected]> Committed: Thu May 25 12:09:30 2017 +0100 ---------------------------------------------------------------------- .../aries/containers/ContainerFactory.java | 8 + .../org/apache/aries/containers/Service.java | 35 ++++ .../apache/aries/containers/ServiceConfig.java | 177 +++++++++++++++++++ .../aries/containers/api/ServiceConfigTest.java | 60 +++++++ 4 files changed, 280 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/aries-containers/blob/5ba10fe5/containers-api/src/main/java/org/apache/aries/containers/ContainerFactory.java ---------------------------------------------------------------------- diff --git a/containers-api/src/main/java/org/apache/aries/containers/ContainerFactory.java b/containers-api/src/main/java/org/apache/aries/containers/ContainerFactory.java new file mode 100644 index 0000000..3bdaf8d --- /dev/null +++ b/containers-api/src/main/java/org/apache/aries/containers/ContainerFactory.java @@ -0,0 +1,8 @@ +package org.apache.aries.containers; + +import org.osgi.annotation.versioning.ProviderType; + +@ProviderType +public interface ContainerFactory { + Service getService(ServiceConfig config); +} http://git-wip-us.apache.org/repos/asf/aries-containers/blob/5ba10fe5/containers-api/src/main/java/org/apache/aries/containers/Service.java ---------------------------------------------------------------------- diff --git a/containers-api/src/main/java/org/apache/aries/containers/Service.java b/containers-api/src/main/java/org/apache/aries/containers/Service.java new file mode 100644 index 0000000..0132ac4 --- /dev/null +++ b/containers-api/src/main/java/org/apache/aries/containers/Service.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIESOR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.aries.containers; + +import org.osgi.annotation.versioning.ProviderType; + +@ProviderType +public interface Service { + /** + * Destroy the service and all its containers. + */ + void destroy(); + + /** + * Obtain the current instance count. + * @return The instance count. + */ + int getActualInstanceCount(); +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/aries-containers/blob/5ba10fe5/containers-api/src/main/java/org/apache/aries/containers/ServiceConfig.java ---------------------------------------------------------------------- diff --git a/containers-api/src/main/java/org/apache/aries/containers/ServiceConfig.java b/containers-api/src/main/java/org/apache/aries/containers/ServiceConfig.java new file mode 100644 index 0000000..85c28b2 --- /dev/null +++ b/containers-api/src/main/java/org/apache/aries/containers/ServiceConfig.java @@ -0,0 +1,177 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIESOR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.aries.containers; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.osgi.annotation.versioning.ProviderType; + +@ProviderType +public class ServiceConfig { + private final String[] commandLine; + private final String containerImage; + private final List<Integer> containerPorts; + private final String entryPoint; + private final Map<String, String> envVars; +// private final List<HealthCheck> healthChecks; + private final int mainPort; + private final double requestedCPUunits; + private final int requestedInstances; + private final double requestedMemory; // in MiB + private final String serviceName; + + + private ServiceConfig(String[] commandLine, String containerImage, List<Integer> containerPorts, String entryPoint, + Map<String, String> envVars, int mainPort, double requestedCPUunits, int requestedInstances, double requestedMemory, + String serviceName) { + this.commandLine = commandLine; + this.containerImage = containerImage; + this.containerPorts = containerPorts; + this.entryPoint = entryPoint; + this.envVars = envVars; + this.mainPort = mainPort; + this.requestedCPUunits = requestedCPUunits; + this.requestedInstances = requestedInstances; + this.requestedMemory = requestedMemory; + this.serviceName = serviceName; + } + + public String[] getCommandLine() { + return commandLine; + } + + public String getContainerImage() { + return containerImage; + } + + public List<Integer> getContainerPorts() { + return containerPorts; + } + + public String getEntryPoint() { + return entryPoint; + } + + public Map<String, String> getEnvVars() { + return envVars; + } + + public int getMainPort() { + return mainPort; + } + + public double getRequestedCpuUnits() { + return requestedCPUunits; + } + + public int getRequestedInstances() { + return requestedInstances; + } + + public double getRequestedMemory() { + return requestedMemory; + } + + public String getServiceName() { + return serviceName; + } + + public static Builder builder(String serviceName, String containerImage) { + return new Builder(serviceName, containerImage); + } + + @ProviderType + public static class Builder { + private String containerImage; + private String[] commandLine = new String [] {}; + private Map<String, String> envMap = new HashMap<>(); + private String entryPoint; + private double requestedCpuUnits = 0.5; + private int requestedInstances = 1; + private double requestedMemory = 64; + private int mainPort = -1; + private List<Integer> ports = new ArrayList<>(); + private String serviceName; + + Builder(String serviceName, String containerImage) { + this.serviceName = serviceName; + this.containerImage = containerImage; + } + + public Builder commandLine(String ... commandLine) { + this.commandLine = commandLine; + return this; + } + + public Builder cpu(double requestedCpuUnits) { + this.requestedCpuUnits = requestedCpuUnits; + return this; + } + + public Builder entryPoint(String entryPoint) { + this.entryPoint = entryPoint; + return this; + } + + public Builder env(String name, String value) { + this.envMap.put(name, value); + return this; + } + + public Builder env(Map<String, String> envMap) { + this.envMap.clear(); + this.envMap.putAll(envMap); + return this; + } + + public Builder instances(int requestedInstances) { + this.requestedInstances = requestedInstances; + return this; + } + + public Builder memory(double requestedMemory) { + this.requestedMemory = requestedMemory; + return this; + } + + public Builder port(int port) { + return port(port, false); + } + + public Builder port(int port, boolean main) { + this.ports.add(port); + if (main) { + if (this.mainPort != -1) + throw new IllegalStateException("A main port has already been set: " + mainPort); + + this.mainPort = port; + } + return this; + } + + public ServiceConfig build() { + return new ServiceConfig(commandLine, containerImage, ports, entryPoint, + envMap, mainPort, requestedCpuUnits, requestedInstances, requestedMemory, + serviceName); + } + } +} http://git-wip-us.apache.org/repos/asf/aries-containers/blob/5ba10fe5/containers-api/src/test/java/org/apache/aries/containers/api/ServiceConfigTest.java ---------------------------------------------------------------------- diff --git a/containers-api/src/test/java/org/apache/aries/containers/api/ServiceConfigTest.java b/containers-api/src/test/java/org/apache/aries/containers/api/ServiceConfigTest.java new file mode 100644 index 0000000..d8b7ef2 --- /dev/null +++ b/containers-api/src/test/java/org/apache/aries/containers/api/ServiceConfigTest.java @@ -0,0 +1,60 @@ +package org.apache.aries.containers.api; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +import org.apache.aries.containers.ServiceConfig; +import org.junit.Test; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; + +public class ServiceConfigTest { + @Test + public void testServiceConfig1() { + ServiceConfig sc = ServiceConfig.builder("svc1", "myimg").build(); + assertEquals("svc1", sc.getServiceName()); + assertEquals("myimg", sc.getContainerImage()); + } + + @Test + public void testServiceConfig2() { + ServiceConfig sc = ServiceConfig.builder("svc1", "myimg"). + commandLine("-c", "runscript.sh"). + entryPoint("/bin/sh"). + cpu(3.142). + env("foo", "bar"). + env("a", "b c d"). + instances(17). + memory(5.5). + port(8080, true). + port(9090). + build(); + + assertArrayEquals(new String[] {"-c", "runscript.sh"}, sc.getCommandLine()); + assertEquals("/bin/sh", sc.getEntryPoint()); + assertEquals(3.142, sc.getRequestedCpuUnits(), 0.001); + + Map<String, String> expectedEnv = new HashMap<>(); + expectedEnv.put("foo", "bar"); + expectedEnv.put("a", "b c d"); + assertEquals(expectedEnv, sc.getEnvVars()); + assertEquals(17, sc.getRequestedInstances()); + assertEquals(5.5, sc.getRequestedMemory(), 0.01); + assertEquals(8080, sc.getMainPort()); + assertEquals(Arrays.asList(8080, 9090), sc.getContainerPorts()); + } + + @Test + public void testServiceConfig3() { + Map<String, String> env = new HashMap<>(); + env.put("a", "b"); + env.put("c", "d"); + + ServiceConfig sc = ServiceConfig.builder("svc1", "myimg"). + env(env).build(); + assertEquals(env, sc.getEnvVars()); + } + +}
