public static String[] fromDockerLinksToCompositeLinks(String[] links) { if (isNullOrEmpty(links)) { return null; } for (int i = 0; i < links.length; i++) { if (!links[i].contains(":")) { links[i] = links[i] + ":" + links[i]; } } return links; }
private static void sanitizeDockerCompose(DockerCompose entity) { assertNotNull(entity, "entity"); if (!isNullOrEmpty(entity.services)) { for (DockerComposeService service : entity.services.values()) { // this could be a new serializer... Logging lc = service.logging; if (lc != null && lc.driver == null && isNullOrEmpty(lc.options)) { service.logging = null; } } } }
private static String[] fromCompositeLinksToDockerLinks(String[] links) { if (isNullOrEmpty(links)) { return null; } for (int i = 0; i < links.length; i++) { if (links[i].contains(":")) { String[] parts = links[i].split(":"); if ((parts.length == 1) || (parts[0].equals(parts[1]))) { links[i] = parts[0]; } } } return links; }
private static String[] fromCompositePortBindingsToDockerPorts(PortBinding[] portBindings) { if (isNullOrEmpty(portBindings)) { return null; } String[] ports = new String[portBindings.length]; for (int i = 0; i < portBindings.length; i++) { ports[i] = portBindings[i].toString(); } return ports; }
private void failOperation(Operation op, Map<Long, Throwable> errors, Throwable defaultError) { if (isNullOrEmpty(errors)) { op.fail(defaultError); } else { List<Throwable> throwables = errors.values().stream() .filter(e -> e != null) .collect(Collectors.toList()); op.fail(throwables.get(0)); } }
public static List<EnvVar> fromContainerDescriptionEnvsToPodContainersEnvs(String[] envs) { if (isNullOrEmpty(envs)) { return null; } List<EnvVar> envVars = new ArrayList<>(envs.length); for (int i = 0; i < envs.length; i++) { String[] keyValue = envs[i].split("="); EnvVar podContainerEnvVar = new EnvVar(); podContainerEnvVar.name = keyValue[0]; podContainerEnvVar.value = keyValue[1]; envVars.add(i, podContainerEnvVar); } return envVars; }
@SuppressWarnings({ "unchecked", "rawtypes" }) public static Map<String, Object> serializeTemplate(CompositeTemplate template, ObjectMapper objectMapper, ObjectWriter objectWriter) throws IOException { // TODO figure out why directly deserializing to Map doesn't work Map result = objectMapper.readValue(objectWriter.writeValueAsString(template), Map.class); if (!isNullOrEmpty(template.components)) { Map<String, Object> components = new HashMap<>(); result.put("components", components); for (Map.Entry<String, ComponentTemplate<?>> entry : template.components.entrySet()) { String componentKey = entry.getKey(); ComponentTemplate<?> componentTemplate = entry.getValue(); Map serializedComponentTemplate = serializeComponentTemplate(componentTemplate, objectMapper, objectWriter); components.put(componentKey, serializedComponentTemplate); } } return result; }
@SuppressWarnings("unchecked") public static CompositeTemplate deserializeTemplate(Map<String, Object> initialMap, ObjectMapper objectMapper) { Map<String, ComponentTemplate<?>> compositeTemplateComponents = new HashMap<>(); Map<String, Object> components = (Map<String, Object>) initialMap .remove(BindingUtils.COMPONENTS); if (!isNullOrEmpty(components)) { for (Map.Entry<String, Object> ce : components.entrySet()) { ComponentTemplate<?> componentTemplate = deserializeComponent( (Map<String, Object>) ce.getValue(), objectMapper); compositeTemplateComponents.put(ce.getKey(), componentTemplate); } } CompositeTemplate compositeTemplate = objectMapper .convertValue(initialMap, CompositeTemplate.class); compositeTemplate.components = compositeTemplateComponents; return compositeTemplate; }
public static List<ContainerPort> fromContainerDescriptionPortsToPodContainerPorts( PortBinding[] ports) { if (isNullOrEmpty(ports)) { return null; } List<ContainerPort> podPorts = new ArrayList<>(ports.length); for (int i = 0; i < ports.length; i++) { ContainerPort podPort = new ContainerPort(); podPort.containerPort = Integer.parseInt(ports[i].containerPort); podPort.protocol = fromCompositeProtocolToKubernetesProtocol(ports[i].protocol); podPorts.add(i, podPort); } return podPorts; }
private static ServiceNetworks fromCompositeServiceNetworksToDockerNetworks( Map<String, ServiceNetwork> networks) { if (isNullOrEmpty(networks)) { return null; } boolean asNames = true; for (ServiceNetwork network : networks.values()) { asNames = asNames && network.useDefaults(); if (!asNames) { break; } } ServiceNetworks serviceNetworks = new ServiceNetworks(); if (asNames) { serviceNetworks.values = networks.keySet().toArray(new String[] {}); } else { serviceNetworks.valuesMap = networks; } return serviceNetworks; }
private static PortBinding[] fromDockerPortsToCompositePortBindings(String[] ports) { if (isNullOrEmpty(ports)) { return null; } PortBinding[] portBindings = new PortBinding[ports.length]; for (int i = 0; i < ports.length; i++) { portBindings[i] = fromDockerPortMapping(DockerPortMapping.fromString(ports[i])); } return portBindings; }
@SuppressWarnings("unchecked") public static Map<String, Object> serializeComponentTemplate( ComponentTemplate<?> componentTemplate, ObjectMapper objectMapper, ObjectWriter objectWriter) throws IOException { // We have a special deserializer for the ComponentTemplate @SuppressWarnings("rawtypes") Map serializedComponentTemplate = objectMapper .readValue(objectWriter.writeValueAsString(componentTemplate), Map.class); serializedComponentTemplate.remove("children"); if (isNullOrEmpty(componentTemplate.children)) { return serializedComponentTemplate; } NestedState state = new NestedState(); state.object = (ServiceDocument) componentTemplate.data; state.children = componentTemplate.children; Map<String, Object> serializedDataComponentTemplate = serializeNestedState(state, objectMapper, objectWriter); serializedComponentTemplate.put("data", serializedDataComponentTemplate); return serializedComponentTemplate; }
private static void sanitizeContainerComponents(CompositeTemplate entity, boolean serialize) { for (Entry<String, ComponentTemplate<ContainerDescription>> entry : filterComponentTemplates( entity.components, ContainerDescription.class).entrySet()) { ComponentTemplate<ContainerDescription> component = entry.getValue(); normalizeContainerDescription(component, serialize); if (!entry.getKey().equals(component.data.name)) { Utils.logWarning("Container name '%s' differs from component name '%s' and " + "it will be overridden with the component name!", component.data.name, entry.getKey()); component.data.name = entry.getKey(); } component.data.tenantLinks = null; // this could be a new serializer... HealthConfig hc = component.data.healthConfig; if (hc != null && hc.protocol == null) { component.data.healthConfig = null; } // this could be a new serializer... LogConfig lc = component.data.logConfig; if (lc != null && lc.type == null && isNullOrEmpty(lc.config)) { component.data.logConfig = null; } } }
public static String serializeCompositeTemplate(CompositeTemplate entity) throws IOException { sanitizeCompositeTemplate(entity, true); Map<String, Object> stringObjectMap = TemplateSerializationUtils.serializeTemplate(entity); if (!isNullOrEmpty(entity.bindings)) { normalizeBindings(stringObjectMap, entity); } return YamlMapper.objectWriter().writeValueAsString(stringObjectMap); }
private static ComponentTemplate<ContainerNetworkDescription> fromDockerNetworkToCompositeComponent( DockerComposeNetwork network) { assertNotNull(network, "network"); ContainerNetworkDescription description = new ContainerNetworkDescription(); description.driver = network.driver; description.customProperties = network.driver_opts; description.ipam = network.ipam; if (network.external != null) { if (!isNullOrEmpty(network.external.name)) { description.externalName = network.external.name; } else if (network.external.value != null) { description.external = network.external.value; } } return fromContainerNetworkDescriptionToComponentTemplate(description); }
private static ComponentTemplate<ContainerVolumeDescription> fromDockerVolumeToCompositeComponent( DockerComposeVolume volume) { assertNotNull(volume, "volume"); ContainerVolumeDescription description = new ContainerVolumeDescription(); description.driver = volume.driver; description.options = volume.driver_opts; if (volume.external != null) { if (!isNullOrEmpty(volume.external.name)) { description.externalName = volume.external.name; } else if (volume.external.value != null) { description.external = volume.external.value; } } return fromContainerVolumeDescriptionToComponentTemplate(description); }
private static DockerComposeVolume fromCompositeComponentToDockerVolume( ComponentTemplate<ContainerVolumeDescription> component) { assertNotNull(component, "component"); ContainerVolumeDescription description = component.data; DockerComposeVolume network = new DockerComposeVolume(); network.driver = description.driver; network.driver_opts = description.options; if (!isNullOrEmpty(description.externalName)) { network.external = new VolumeExternal(); network.external.name = description.externalName; } else if (description.external != null) { network.external = new VolumeExternal(); network.external.value = description.external; } return network; }
private static void sanitizeCompositeTemplate(CompositeTemplate entity, boolean serialize) { assertNotNull(entity, "entity"); entity.id = null; entity.status = null; if (!isNullOrEmpty(entity.components)) { sanitizeContainerComponents(entity, serialize); sanitizeContainerNetworkComponents(entity); sanitizeContainerVolumeComponents(entity); normalizeClosureDescriptions(entity, serialize); } }
private static DockerComposeNetwork fromCompositeComponentToDockerNetwork( ComponentTemplate<ContainerNetworkDescription> component) { assertNotNull(component, "component"); ContainerNetworkDescription description = component.data; transformDriversFromComponentTemplate(description); DockerComposeNetwork network = new DockerComposeNetwork(); network.driver = description.driver; network.driver_opts = PropertyUtils.mergeCustomProperties(description.options, description.customProperties); network.ipam = description.ipam; if (!isNullOrEmpty(description.externalName)) { network.external = new NetworkExternal(); network.external.name = description.externalName; } else if (description.external != null) { network.external = new NetworkExternal(); network.external.value = description.external; } return network; }
@Test public void testSerializeDeserializeEmptyTemplate() throws IOException { CompositeTemplate template = new CompositeTemplate(); template.name = "empty"; String templateSerialized = serializeCompositeTemplate(template); assertNotNull(templateSerialized); assertTrue(templateSerialized.contains("empty")); CompositeTemplate templateDeserialized = deserializeCompositeTemplate(templateSerialized); assertNotNull(templateDeserialized); assertEquals(template.name, templateDeserialized.name); assertTrue(isNullOrEmpty(template.components)); assertTrue(isNullOrEmpty(templateDeserialized.components)); }