public static Binding toBinding(Service pod) { Binding binding = null; if (pod.getStatus() != null && pod.getSpec().getClusterIP() != null) { // Running // pod binding = new Binding(pod.getSpec().getClusterIP()); } else { // Configured pod binding = new Binding(null); } for (ServicePort port : pod.getSpec().getPorts()) { // nodePort is service equivalent of pod's hostPort binding.addPortBinding(port.getPort(), port.getNodePort()); } return binding; }
public static Binding toBinding(Pod pod) { Binding binding = null; if (pod.getStatus() != null && pod.getStatus().getHostIP() != null) { // Running // pod binding = new Binding(pod.getStatus().getHostIP()); } else { // Configured pod binding = new Binding(null); } for (Container container : pod.getSpec().getContainers()) { for (ContainerPort port : container.getPorts()) { binding.addPortBinding(port.getContainerPort(), port.getHostPort()); } } return binding; }
@Before public void setup() { Yaml yaml = new Yaml(); @SuppressWarnings("unchecked") Map<String, Object> content = (Map<String, Object>) yaml.load(CONTENT); when(cube.getId()).thenReturn(CUBE_ID); when(cube.configuration()).thenReturn(content); when(cube.getMetadata(HasPortBindings.class)).thenReturn( new TestPortBindings(new Binding("localhost").addPortBinding(8089, 8090))); when(container.getName()).thenReturn(CUBE_ID); when(container.getDeployableContainer()).thenReturn(deployableContainer); when(deployableContainer.getConfigurationClass()).thenReturn(ContainerConfiguration.class); when(container.getContainerConfiguration()).thenReturn(containerDef); when(containerRegistry.getContainers()).thenReturn(Arrays.asList(container)); registry = new LocalCubeRegistry(); registry.addCube(cube); bind(ApplicationScoped.class, CubeRegistry.class, registry); bind(ApplicationScoped.class, ContainerRegistry.class, containerRegistry); }
public static Binding binding(CubeContainer cubeConfiguration, DockerClientExecutor executor) { Binding binding = new Binding(executor.getDockerServerIp()); if (cubeConfiguration.getPortBindings() != null) { for (PortBinding cubePortBinding : cubeConfiguration.getPortBindings()) { binding.addPortBinding(cubePortBinding.getExposedPort().getExposed(), cubePortBinding.getBound()); } } return binding; } }
public static Binding binding(CubeContainer cubeConfiguration, DockerClientExecutor executor) { Binding binding = new Binding(executor.getDockerServerIp()); if (cubeConfiguration.getPortBindings() != null) { for (PortBinding cubePortBinding : cubeConfiguration.getPortBindings()) { binding.addPortBinding(cubePortBinding.getExposedPort().getExposed(), cubePortBinding.getBound()); } } return binding; } }
public static Binding binding(DockerClientExecutor executor, String cubeId) { InspectContainerResponse inspectResponse = executor.getDockerClient().inspectContainerCmd(cubeId).exec(); String dockerIp = getDockerServerIp(executor); String inernalIp = null; NetworkSettings networkSettings = inspectResponse.getNetworkSettings(); if (networkSettings != null) { inernalIp = networkSettings.getIpAddress(); } Binding binding = new Binding(dockerIp, inernalIp); HostConfig hostConfig = inspectResponse.getHostConfig(); if (hostConfig.getPortBindings() != null) { for (Entry<ExposedPort, com.github.dockerjava.api.model.Ports.Binding[]> bind : hostConfig.getPortBindings() .getBindings().entrySet()) { com.github.dockerjava.api.model.Ports.Binding[] allBindings = bind.getValue(); for (com.github.dockerjava.api.model.Ports.Binding bindings : allBindings) { binding.addPortBinding(bind.getKey().getPort(), Integer.parseInt(bindings.getHostPortSpec())); } } } else { ContainerConfig connectionConfig = inspectResponse.getConfig(); final ExposedPort[] exposedPorts = connectionConfig.getExposedPorts(); if (exposedPorts != null) { for (ExposedPort port : exposedPorts) { binding.addPortBinding(port.getPort(), -1); } } } return binding; }
public static Binding binding(DockerClientExecutor executor, String cubeId) { InspectContainerResponse inspectResponse = executor.getDockerClient().inspectContainerCmd(cubeId).exec(); String dockerIp = getDockerServerIp(executor); String inernalIp = null; NetworkSettings networkSettings = inspectResponse.getNetworkSettings(); if (networkSettings != null) { inernalIp = networkSettings.getIpAddress(); } Binding binding = new Binding(dockerIp, inernalIp); HostConfig hostConfig = inspectResponse.getHostConfig(); if (hostConfig.getPortBindings() != null) { for (Entry<ExposedPort, com.github.dockerjava.api.model.Ports.Binding[]> bind : hostConfig.getPortBindings() .getBindings().entrySet()) { com.github.dockerjava.api.model.Ports.Binding[] allBindings = bind.getValue(); for (com.github.dockerjava.api.model.Ports.Binding bindings : allBindings) { binding.addPortBinding(bind.getKey().getPort(), Integer.parseInt(bindings.getHostPortSpec())); } } } else { ContainerConfig connectionConfig = inspectResponse.getConfig(); final ExposedPort[] exposedPorts = connectionConfig.getExposedPorts(); if (exposedPorts != null) { for (ExposedPort port : exposedPorts) { binding.addPortBinding(port.getPort(), -1); } } } return binding; }
@Test // equal ip, different ports public void shouldUpdateWithPortFromDocker() throws Exception { Binding binding = new Binding(LOCALHOST); binding.addPortBinding(EXPOSED_PORT, BOUND_PORT); Mockito.when(cube.getMetadata(HasPortBindings.class)).thenReturn(new TestPortBindings(binding)); bind(ContainerScoped.class, Container.class, new ContainerImpl(CUBE_CONTAINER_NAME, deployableContainer, new ContainerDefImpl("arquillian.xml"))); ProtocolMetaData metadata = new ProtocolMetaData(); metadata.addContext(new HTTPContext(LOCALHOST, EXPOSED_PORT).add(new Servlet("A", "B"))); bind(DeploymentScoped.class, ProtocolMetaData.class, metadata); fire(metadata); ProtocolMetaData updated = getManager().getContext(DeploymentContext.class) .getObjectStore().get(ProtocolMetaData.class); Assert.assertEquals(BOUND_PORT.intValue(), updated.getContexts(HTTPContext.class).iterator().next().getPort()); Assert.assertEquals(LOCALHOST, updated.getContexts(HTTPContext.class).iterator().next().getHost()); assertEventFired(ProtocolMetaData.class, 1); // twice, but original fire is intercepted and never hit the Counter }
@Test // equal ports, different ip public void shouldUpdateWithIPFromDocker() throws Exception { Binding binding = new Binding(GATEWAY_IP); binding.addPortBinding(EXPOSED_PORT, EXPOSED_PORT); Mockito.when(cube.getMetadata(HasPortBindings.class)).thenReturn(new TestPortBindings(binding)); bind(ContainerScoped.class, Container.class, new ContainerImpl(CUBE_CONTAINER_NAME, deployableContainer, new ContainerDefImpl("arquillian.xml"))); ProtocolMetaData metadata = new ProtocolMetaData(); metadata.addContext(new HTTPContext(LOCALHOST, EXPOSED_PORT).add(new Servlet("A", "B"))); bind(DeploymentScoped.class, ProtocolMetaData.class, metadata); fire(metadata); ProtocolMetaData updated = getManager().getContext(DeploymentContext.class) .getObjectStore().get(ProtocolMetaData.class); Assert.assertEquals(EXPOSED_PORT.intValue(), updated.getContexts(HTTPContext.class).iterator().next().getPort()); Assert.assertEquals(GATEWAY_IP, updated.getContexts(HTTPContext.class).iterator().next().getHost()); assertEventFired(ProtocolMetaData.class, 1); // twice, but original fire is intercepted and never hit the Counter }
@Test public void shouldNotUpdateIfContainerNotMapped() throws Exception { Binding binding = new Binding(GATEWAY_IP); binding.addPortBinding(EXPOSED_PORT, EXPOSED_PORT); Mockito.when(cube.getMetadata(HasPortBindings.class)).thenReturn(new TestPortBindings(binding)); bind(ContainerScoped.class, Container.class, new ContainerImpl("_UNMAPPED_", deployableContainer, new ContainerDefImpl("arquillian.xml"))); ProtocolMetaData metadata = new ProtocolMetaData(); metadata.addContext(new HTTPContext(LOCALHOST, EXPOSED_PORT).add(new Servlet("A", "B"))); bind(DeploymentScoped.class, ProtocolMetaData.class, metadata); fire(metadata); ProtocolMetaData updated = getManager().getContext(DeploymentContext.class) .getObjectStore().get(ProtocolMetaData.class); Assert.assertEquals(metadata.getContexts(HTTPContext.class).iterator().next(), updated.getContexts(HTTPContext.class).iterator().next()); assertEventFired(ProtocolMetaData.class, 1); } }