private void selectHosts(ContainerNetworkProvisionTaskState state, ContainerNetworkDescription networkDescription, Consumer<List<ComputeState>> callback) { // If hosts are provided use them directly to try to provision the network // (e.g. when External network CRUD operations) List<String> providedHostLinks = ContainerNetworkAllocationTaskService .getProvidedHostIdsAsSelfLinks(state); if (providedHostLinks != null) { retrieveContainerHostsByLinks(state, providedHostLinks, (hosts) -> { List<String> disabledHosts = hosts.stream().filter((host) -> { return host.powerState != PowerState.ON; }) .map(host -> host.address).collect(Collectors.toList()); if (disabledHosts.isEmpty()) { callback.accept(hosts); } else { String err = String.format( "Requested network provisioning for disabled hosts: [%s].", disabledHosts); failTask(err, null); } }); return; } selectHost(state, (host) -> { callback.accept(Collections.singletonList(host)); }); }
private void selectHost(ContainerNetworkProvisionTaskState state, Consumer<ComputeState> callback) { getContextContainerStates(state, (states) -> { getContextContainerDescriptions(states, (descriptions) -> { List<ContainerState> containerStatesForNetwork = getDependantContainerStates( descriptions, states, networkDescription); if (containerStatesForNetwork.isEmpty()) { String err = String.format( "No container states depending on network description [%s] found.", networkDescription.name); failTask(err, null); } else { String hostLink = containerStatesForNetwork.get(0).parentLink; getHost(hostLink, (host) -> { callback.accept(host); }); } }); }); }
private void getContainerNetworkDescription(ContainerNetworkProvisionTaskState state, Consumer<ContainerNetworkDescription> callbackFunction) { if (networkDescription != null) { callbackFunction.accept(networkDescription); return; } sendRequest(Operation.createGet(this, state.resourceDescriptionLink) .setCompletion( (o, e) -> { if (e != null) { failTask("Failure retrieving container network description state", e); return; } ContainerNetworkDescription desc = o .getBody(ContainerNetworkDescription.class); this.networkDescription = desc; callbackFunction.accept(desc); })); }
private void updateContainerNetworkStateWithContainerHostLink( ContainerNetworkState currentNetworkState, ComputeState host, Runnable callbackFunction) { ContainerNetworkState patch = new ContainerNetworkState(); patch.originatingHostLink = host.documentSelfLink; patch.parentLinks = new ArrayList<>(Arrays.asList(host.documentSelfLink)); if (currentNetworkState.driver == null || currentNetworkState.driver.isEmpty()) { String cluster = host.customProperties .get(ContainerHostService.DOCKER_HOST_CLUSTER_STORE_PROP_NAME); if (cluster != null && !cluster.isEmpty()) { patch.driver = ContainerNetworkDescription.NETWORK_DRIVER_OVERLAY; } } sendRequest(Operation .createPatch(this, currentNetworkState.documentSelfLink) .setBody(patch) .setCompletion( (o, e) -> { if (e != null) { logWarning("Error while updating network: %s", currentNetworkState.documentSelfLink); failTask(String.format("Error while updating network: %s", currentNetworkState.documentSelfLink), e); } else { callbackFunction.run(); } })); }
"Unexpected size of resource links and hosts, hosts should be one or equal to resource links! Actual resources - [%s], hosts - [%s]", state.resourceLinks.size(), hosts.size()); failTask(err, null);
private void getNetwork(String networkLink, Consumer<ContainerNetworkState> callback) { Operation.createGet(this, networkLink).setCompletion((op, ex) -> { if (ex != null) { failTask("Failed retrieving network: " + Utils.toString(ex), null); return; } ContainerNetworkState network = op.getBody(ContainerNetworkState.class); callback.accept(network); }).sendWith(this); }
private void getHost(String hostLink, Consumer<ComputeState> callback) { Operation.createGet(this, hostLink).setCompletion((op, ex) -> { if (ex != null) { failTask("Failed retrieving host: " + Utils.toString(ex), null); return; } ComputeState host = op.getBody(ComputeState.class); callback.accept(host); }).sendWith(this); }
logWarning("Error while updating network: %s", currentNetworkState.documentSelfLink); failTask(String.format("Error while updating network: %s", currentNetworkState.documentSelfLink), e); } else {
private void getNetworkByName(ContainerNetworkProvisionTaskState state, String networkName, Consumer<ContainerNetworkState> callback) { selectHost(state, (host) -> { List<ContainerNetworkState> networkStates = new ArrayList<ContainerNetworkState>(); QueryTask queryTask = NetworkUtils .getNetworkByHostAndNameQueryTask(host.documentSelfLink, networkName); new ServiceDocumentQuery<ContainerNetworkState>(getHost(), ContainerNetworkState.class) .query(queryTask, (r) -> { if (r.hasException()) { failTask("Failed to query for active networks by name '" + networkName + "' in host '" + host.documentSelfLink + "'!", r.getException()); } else if (r.hasResult()) { if (networkName.equals(r.getResult().name)) { networkStates.add(r.getResult()); } } else { if (networkStates.size() == 1) { callback.accept(networkStates.get(0)); return; } failTask(networkStates.size() + " active network(s) found by name '" + networkName + "' in host '" + host.documentSelfLink + "'!", null); } }); }); }
private void getContextContainerDescriptions( Map<String, List<ContainerState>> containersByDescriptionLink, Consumer<List<ContainerDescription>> callback) { if ((containersByDescriptionLink == null) || (containersByDescriptionLink.isEmpty())) { callback.accept(Collections.emptyList()); return; } QueryTask q = QueryUtil.buildQuery(ContainerDescription.class, true); QueryUtil.addExpandOption(q); QueryUtil.addListValueClause(q, ContainerDescription.FIELD_NAME_SELF_LINK, containersByDescriptionLink.keySet()); q.taskInfo.isDirect = false; List<ContainerDescription> result = new ArrayList<>(); new ServiceDocumentQuery<ContainerDescription>(getHost(), ContainerDescription.class) .query(q, (r) -> { if (r.hasException()) { failTask("Exception while selecting container descriptions", r.getException()); } else if (r.hasResult()) { result.add(r.getResult()); } else { callback.accept(result); } }); }
(r) -> { if (r.hasException()) { failTask( "Exception while selecting containers with contextId [" + contextId + "]",
private void createAndSendContainerNetworkRequest(ContainerNetworkState networkState, ContainerNetworkProvisionTaskState state, ServiceTaskCallback taskCallback, String networkSelfLink) { getContainerNetworkDescription(state, (networkDescription) -> { AdapterRequest networkRequest = new AdapterRequest(); networkRequest.resourceReference = UriUtils.buildUri(getHost(), networkSelfLink); networkRequest.serviceTaskCallback = taskCallback; if (Boolean.TRUE.equals(networkDescription.external)) { // The network is defined as external, just validate that it exists actually. networkRequest.operationTypeId = NetworkOperationType.INSPECT.id; } else { networkRequest.operationTypeId = NetworkOperationType.CREATE.id; } networkRequest.customProperties = state.customProperties; sendRequest(Operation.createPatch(getHost(), state.instanceAdapterReference.toString()) .setBody(networkRequest) .setContextId(getSelfId()) .setCompletion((o, e) -> { if (e != null) { failTask("AdapterRequest failed for network: " + networkSelfLink, e); return; } logInfo("Network '%s' request started for: %s", networkRequest.operationTypeId, networkSelfLink); })); }); }