private static Map<String, ServiceNetwork> fromDockerNetworksToCompositeServiceNetworks( ServiceNetworks networks) { if (networks == null) { return null; } if (networks.values != null) { Map<String, ServiceNetwork> map = new LinkedHashMap<>(); for (String value : networks.values) { map.put(value, new ServiceNetwork()); } return map; } else { return networks.valuesMap; } }
/** * Creates a {@link ServiceNetwork} instance with the provided properties */ private ServiceNetwork createServiceNetwork(String name, String ipv4, String ipv6, String[] aliases, String[] links) { ServiceNetwork network = new ServiceNetwork(); network.name = name; network.ipv4_address = ipv4; network.ipv6_address = ipv6; network.aliases = aliases; network.links = links; return network; }
public static ServiceNetwork fromMap(Map<?,?> map) { ServiceNetwork result = new ServiceNetwork(); if (map.containsKey(FIELD_NAME)) { result.name = (String) map.get(FIELD_NAME); } if (map.containsKey(FIELD_NAME_ALIASES)) { result.aliases = (String[]) ((List) map.get(FIELD_NAME_ALIASES)).stream() .toArray(String[]::new); } if (map.containsKey(FIELD_NAME_IP4_ADDRESS)) { result.ipv4_address = (String) map.get(FIELD_NAME_IP4_ADDRESS); } if (map.containsKey(FIELD_NAME_IP6_ADDRESS)) { result.ipv6_address = (String) map.get(FIELD_NAME_IP6_ADDRESS); } return result; } }
private static Map<String, ServiceNetwork> mapNetworks(ContainerDescription cd, HostSelection hostSelection) { if (cd.networks == null) { return null; } Map<String, ServiceNetwork> result = new HashMap<>(); for (Entry<String, ServiceNetwork> entry : cd.networks.entrySet()) { String resolvedNetworkName = hostSelection.mapNames(new String[] { entry.getKey() })[0]; ServiceNetwork config = entry.getValue(); if (config == null) { config = new ServiceNetwork(); } ServiceNetwork newConfig = new ServiceNetwork(); newConfig.ipv4_address = config.ipv4_address; newConfig.ipv6_address = config.ipv6_address; List<String> newAliases = new ArrayList<>(); if (config.aliases != null) { Collections.addAll(newAliases, config.aliases); } newAliases.add(cd.name); newConfig.aliases = newAliases.toArray(new String[0]); result.put(resolvedNetworkName, newConfig); } return result; }
/** * Process a single network a container is connected to * * @param networks * the list of networks in a container state, usually retrieved by * <code>containerState.networks</code> * @param networkName * the name of the network * @param networkProps * the properties of this network that were returned by an inspect command */ private static void mapNetwork(Map<String, ServiceNetwork> networks, String networkName, Map<String, Object> networkProps) { ServiceNetwork network = new ServiceNetwork(); List<Object> aliasesList = getList(networkProps, DOCKER_CONTAINER_NETWORK_ALIASES_PROP_NAME); List<Object> linksList = getList(networkProps, DOCKER_CONTAINER_NETWORK_LINKS_PROP_NAME); network.name = networkName; network.aliases = aliasesList == null ? null : aliasesList.toArray(new String[0]); network.links = linksList == null ? null : linksList.toArray(new String[0]); network.ipv4_address = (String) networkProps .get(DOCKER_CONTAINER_NETWORK_IPV4_ADDRESS_PROP_NAME); network.ipv6_address = (String) networkProps .get(DOCKER_CONTAINER_NETWORK_IPV6_ADDRESS_PROP_NAME); networks.put(networkName, network); }
private ContainerDescription createDescription(String[] networkNames) throws Throwable { ContainerDescription desc = TestRequestStateFactory.createContainerDescription(); desc.documentSelfLink = UUID.randomUUID().toString(); desc.name = desc.documentSelfLink; Map<String, ServiceNetwork> serviceNetworks = new HashMap<String, ServiceNetwork>(); for (String networkName : networkNames) { serviceNetworks.put(networkName, new ServiceNetwork()); } desc.networks = serviceNetworks; desc = doPost(desc, ContainerDescriptionService.FACTORY_LINK); assertNotNull(desc); addForDeletion(desc); return desc; }
@SuppressWarnings("unchecked") @Test public void testMapContainerNetworkToNetworkConfig() { ServiceNetwork network = new ServiceNetwork(); Map<String, Object> endpointConfig = new HashMap<>(); DockerAdapterUtils.mapContainerNetworkToNetworkConfig(network, endpointConfig); assertNull(endpointConfig.get( DOCKER_CONTAINER_NETWORKING_CONNECT_CONFIG.ENDPOINT_CONFIG.IPAM_CONFIG_PROP_NAME)); assertNull(endpointConfig.get( DOCKER_CONTAINER_NETWORKING_CONNECT_CONFIG.ENDPOINT_CONFIG.ALIASES)); assertNull(endpointConfig.get( DOCKER_CONTAINER_NETWORKING_CONNECT_CONFIG.ENDPOINT_CONFIG.LINKS)); network.ipv4_address = "foo"; network.ipv6_address = "bar"; network.aliases = new String[] { "alias1", "alias2", "alias3" }; network.links = new String[] { "link1", "link2", "link3" }; DockerAdapterUtils.mapContainerNetworkToNetworkConfig(network, endpointConfig); assertNotNull(endpointConfig.get( DOCKER_CONTAINER_NETWORKING_CONNECT_CONFIG.ENDPOINT_CONFIG.IPAM_CONFIG_PROP_NAME)); assertEquals(network.ipv4_address, ((Map<String, Object>) endpointConfig .get(DOCKER_CONTAINER_NETWORKING_CONNECT_CONFIG.ENDPOINT_CONFIG.IPAM_CONFIG_PROP_NAME)) .get(DOCKER_CONTAINER_NETWORKING_CONNECT_CONFIG.ENDPOINT_CONFIG.IPAM_CONFIG.IPV4_CONFIG)); assertEquals(network.ipv6_address, ((Map<String, Object>) endpointConfig .get(DOCKER_CONTAINER_NETWORKING_CONNECT_CONFIG.ENDPOINT_CONFIG.IPAM_CONFIG_PROP_NAME)) .get(DOCKER_CONTAINER_NETWORKING_CONNECT_CONFIG.ENDPOINT_CONFIG.IPAM_CONFIG.IPV6_CONFIG)); assertEquals(network.aliases, endpointConfig.get( DOCKER_CONTAINER_NETWORKING_CONNECT_CONFIG.ENDPOINT_CONFIG.ALIASES)); assertEquals(network.links, endpointConfig.get( DOCKER_CONTAINER_NETWORKING_CONNECT_CONFIG.ENDPOINT_CONFIG.LINKS)); }
ServiceNetwork sn = containerState.networks.get(snKey); if (sn == null) { sn = new ServiceNetwork(); containerState.networks.put(snKey, sn);
@Test public void testCreatedContainerShouldBeInspectedOnNetworkOperationFailure() throws Throwable { // delete provisioned containers removeContainer(); // create a new state that has never been data collected createContainerState(); // update the container state with a network that does not exist ServiceNetwork network = new ServiceNetwork(); network.name = "network"; ContainerState patch = new ContainerState(); patch.networks = new HashMap<>(); patch.networks.put(network.name, network); patchContainerState(patch); sendGetContainerStateRequest(); assertNull("Container ID should have been null before creation on host", containerState.id); // This should fail on the attempt to connect the container to the network createContainer(true /* expect error */); // The createContainer method will verify that the id has been collected }
new ServiceNetwork()); container2Desc.networks = Collections.singletonMap(networkDesc.name, new ServiceNetwork());
container1Desc.documentSelfLink = UUID.randomUUID().toString(); container1Desc.networks = new HashMap<>(); container1Desc.networks.put(networkName, new ServiceNetwork()); container2Desc.affinity = new String[] { "!Container1:hard" }; container2Desc.networks = new HashMap<>(); container2Desc.networks.put(networkName, new ServiceNetwork());
container1Desc.name = "Container1"; container1Desc.networks = new HashMap<>(); container1Desc.networks.put(networkName, new ServiceNetwork()); container2Desc.affinity = new String[] { "!Container1:hard" }; container2Desc.networks = new HashMap<>(); container2Desc.networks.put(networkName, new ServiceNetwork()); container2Desc.customProperties.put(MockDockerAdapterService.FAILURE_EXPECTED, Boolean.TRUE.toString());
container1Desc.name = "container1"; container1Desc.networks = new HashMap<>(); container1Desc.networks.put(networkName, new ServiceNetwork()); container2Desc.affinity = new String[] { "!container1:hard" }; container2Desc.networks = new HashMap<>(); container2Desc.networks.put(networkName, new ServiceNetwork());
container1Desc.name = "container1"; container1Desc.networks = new HashMap<>(); container1Desc.networks.put(networkName, new ServiceNetwork()); container2Desc.affinity = new String[] { "!container1:hard" }; container2Desc.networks = new HashMap<>(); container2Desc.networks.put(networkName, new ServiceNetwork());
container1Desc.name = "Container1"; container1Desc.networks = new HashMap<>(); container1Desc.networks.put(networkName, new ServiceNetwork()); container2Desc.affinity = new String[] { "!Container1:hard" }; container2Desc.networks = new HashMap<>(); container2Desc.networks.put(networkName, new ServiceNetwork());
container1Desc._cluster = 2; container1Desc.networks = new HashMap<>(); container1Desc.networks.put(networkName, new ServiceNetwork());
container1Desc.name = "Container1"; container1Desc.networks = new HashMap<>(); container1Desc.networks.put(networkName, new ServiceNetwork()); container2Desc.name = "Container2"; container2Desc.networks = new HashMap<>(); container2Desc.networks.put(networkName, new ServiceNetwork());
container1Desc.name = "Container1"; container1Desc.networks = new HashMap<>(); container1Desc.networks.put(networkName, new ServiceNetwork()); container2Desc.name = "Container2"; container2Desc.networks = new HashMap<>(); container2Desc.networks.put(networkName, new ServiceNetwork());
@Test public void testToContainerDescription() { ComponentDescription cd = new ComponentDescription(); cd.name = "toContainer"; ContainerDescription container = new ContainerDescription(); container.name = "name1"; container.env = new String[] { "hello" }; container.image = "library/hello-world"; container.networks = new HashMap<>(); ServiceNetwork serviceNetwork = new ServiceNetwork(); serviceNetwork.hashCode(); container.networks.put("net1", serviceNetwork); cd.type = ResourceType.CONTAINER_TYPE.getName(); cd.updateServiceDocument(container); ComponentDescription serializedCd = Utils.fromJson(Utils.toJson(cd), ComponentDescription.class); assertEquals(cd.name, serializedCd.name); assertTrue(serializedCd.getServiceDocument() instanceof ContainerDescription); ContainerDescription serializedContainer = (ContainerDescription) serializedCd .getServiceDocument(); assertEquals(container.name, serializedContainer.name); assertEquals(container.env[0], serializedContainer.env[0]); assertEquals(container.image, serializedContainer.image); assertEquals(container.networks, serializedContainer.networks); }