Search in sources :

Example 11 with Network

use of io.fabric8.maven.docker.model.Network in project syndesis-qe by syndesisio.

the class OperatorValidationSteps method checkAffinity.

@When("^check (affinity|tolerations)( not set)? for (infra|integration) pods$")
public void checkAffinity(String test, String notSet, String method) {
    List<Pod> pods = "infra".equals(method) ? ComponentUtils.getComponentPods().stream().filter(p -> !p.getMetadata().getName().contains("operator")).collect(Collectors.toList()) : OpenShiftUtils.findPodsByPredicates(p -> "integration".equals(p.getMetadata().getLabels().get("syndesis.io/type")));
    for (Pod p : pods) {
        String name = p.getMetadata().getName();
        if ("affinity".equals(test)) {
            Affinity podAffinity = p.getSpec().getAffinity();
            if (notSet == null) {
                assertThat(podAffinity).as(name + ": affinity is null").isNotNull();
                NodeAffinity nodeAffinity = podAffinity.getNodeAffinity();
                assertThat(nodeAffinity).as(name + ": node affinity is null").isNotNull();
                NodeSelector selector = nodeAffinity.getRequiredDuringSchedulingIgnoredDuringExecution();
                assertThat(selector).as(name + ": required is null").isNotNull();
                List<NodeSelectorTerm> terms = selector.getNodeSelectorTerms();
                assertThat(terms).as(name + ": node selector is null").isNotNull();
                assertThat(terms).as(name + ": node selector size isn't 1").hasSize(1);
            } else {
                assertThat(podAffinity).isNull();
            }
        } else {
            Optional<Toleration> toleration = p.getSpec().getTolerations().stream().filter(t -> "node.kubernetes.io/network-unavailable".equals(t.getKey())).findAny();
            if (notSet == null) {
                assertThat(toleration).as(name + ": Expected toleration setting is not present").isPresent();
            } else {
                assertThat(toleration).as(name + ": Toleration shouldn't be present").isNotPresent();
            }
        }
    }
}
Also used : Quantity(io.fabric8.kubernetes.api.model.Quantity) Arrays(java.util.Arrays) Enumeration(java.util.Enumeration) Date(java.util.Date) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Autowired(org.springframework.beans.factory.annotation.Autowired) TimeoutException(java.util.concurrent.TimeoutException) Random(java.util.Random) StringUtils(org.apache.commons.lang3.StringUtils) JSONObject(org.json.JSONObject) Matcher(java.util.regex.Matcher) IntegrationsEndpoint(io.syndesis.qe.endpoint.IntegrationsEndpoint) Map(java.util.Map) Addon(io.syndesis.qe.addon.Addon) ZipFile(java.util.zip.ZipFile) Syndesis(io.syndesis.qe.resource.impl.Syndesis) Path(java.nio.file.Path) ZipEntry(java.util.zip.ZipEntry) Affinity(io.fabric8.kubernetes.api.model.Affinity) AccountsDirectory(io.syndesis.qe.account.AccountsDirectory) Set(java.util.Set) DeploymentConfig(io.fabric8.openshift.api.model.DeploymentConfig) Jaeger(io.syndesis.qe.resource.impl.Jaeger) DoneablePersistentVolume(io.fabric8.kubernetes.api.model.DoneablePersistentVolume) Collectors(java.util.stream.Collectors) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Assertions.fail(org.assertj.core.api.Assertions.fail) S3BucketNameBuilder(io.syndesis.qe.utils.aws.S3BucketNameBuilder) ResourceFactory(io.syndesis.qe.resource.ResourceFactory) Optional(java.util.Optional) Lazy(org.springframework.context.annotation.Lazy) Pattern(java.util.regex.Pattern) Component(io.syndesis.qe.component.Component) OpenShiftWaitUtils(io.syndesis.qe.wait.OpenShiftWaitUtils) SoftAssertions(org.assertj.core.api.SoftAssertions) NodeSelectorTerm(io.fabric8.kubernetes.api.model.NodeSelectorTerm) Then(io.cucumber.java.en.Then) ComponentUtils(io.syndesis.qe.component.ComponentUtils) LocalPortForward(io.fabric8.kubernetes.client.LocalPortForward) HashMap(java.util.HashMap) Yaml(org.yaml.snakeyaml.Yaml) Given(io.cucumber.java.en.Given) DataTable(io.cucumber.datatable.DataTable) Node(io.fabric8.kubernetes.api.model.Node) OutputStream(java.io.OutputStream) PersistentVolumeFluent(io.fabric8.kubernetes.api.model.PersistentVolumeFluent) Endpoints(io.fabric8.kubernetes.api.model.Endpoints) Files(java.nio.file.Files) When(io.cucumber.java.en.When) S3Utils(io.syndesis.qe.utils.aws.S3Utils) FileOutputStream(java.io.FileOutputStream) Pod(io.fabric8.kubernetes.api.model.Pod) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) NodeSelector(io.fabric8.kubernetes.api.model.NodeSelector) Toleration(io.fabric8.kubernetes.api.model.Toleration) OpenShiftUtils(io.syndesis.qe.utils.OpenShiftUtils) FileInputStream(java.io.FileInputStream) Account(io.syndesis.qe.account.Account) File(java.io.File) NodeAffinity(io.fabric8.kubernetes.api.model.NodeAffinity) HTTPUtils(io.syndesis.qe.utils.http.HTTPUtils) TestUtils(io.syndesis.qe.utils.TestUtils) Paths(java.nio.file.Paths) SecretBuilder(io.fabric8.kubernetes.api.model.SecretBuilder) ExternalDatabase(io.syndesis.qe.resource.impl.ExternalDatabase) JSONArray(org.json.JSONArray) InputStream(java.io.InputStream) NodeAffinity(io.fabric8.kubernetes.api.model.NodeAffinity) Pod(io.fabric8.kubernetes.api.model.Pod) Toleration(io.fabric8.kubernetes.api.model.Toleration) Affinity(io.fabric8.kubernetes.api.model.Affinity) NodeAffinity(io.fabric8.kubernetes.api.model.NodeAffinity) NodeSelector(io.fabric8.kubernetes.api.model.NodeSelector) NodeSelectorTerm(io.fabric8.kubernetes.api.model.NodeSelectorTerm) When(io.cucumber.java.en.When)

Example 12 with Network

use of io.fabric8.maven.docker.model.Network in project docker-maven-plugin by fabric8io.

the class DockerAccessWithHcClient method removeNetwork.

@Override
public boolean removeNetwork(String networkId) throws DockerAccessException {
    try {
        String url = urlBuilder.removeNetwork(networkId);
        log.verbose(Logger.LogVerboseCategory.API, API_LOG_FORMAT_DELETE, url);
        int status = delegate.delete(url, HTTP_OK, HTTP_NO_CONTENT, HTTP_NOT_FOUND);
        return status == HTTP_OK || status == HTTP_NO_CONTENT;
    } catch (IOException e) {
        throw new DockerAccessException(e, "Unable to remove network [%s]", networkId);
    }
}
Also used : DockerAccessException(io.fabric8.maven.docker.access.DockerAccessException) IOException(java.io.IOException)

Example 13 with Network

use of io.fabric8.maven.docker.model.Network in project docker-maven-plugin by fabric8io.

the class RunService method stopStartedContainers.

/**
 * Stop all registered container
 * @param keepContainer whether to keep container or to remove them after stopping
 * @param removeVolumes whether to remove volumes after stopping
 * @throws DockerAccessException if during stopping of a container sth fails
 */
public void stopStartedContainers(boolean keepContainer, boolean removeVolumes, boolean removeCustomNetworks, GavLabel gavLabel) throws DockerAccessException, ExecException {
    List<DockerAccessException> thrownExceptions = new ArrayList<>();
    Set<Network> networksToRemove = new HashSet<>();
    for (ContainerTracker.ContainerShutdownDescriptor descriptor : tracker.removeShutdownDescriptors(gavLabel)) {
        try {
            collectCustomNetworks(networksToRemove, descriptor, removeCustomNetworks);
            shutdown(descriptor, keepContainer, removeVolumes);
        } catch (DockerAccessException exc) {
            thrownExceptions.add(exc);
        }
    }
    try {
        removeCustomNetworks(networksToRemove);
    } catch (DockerAccessException exc) {
        thrownExceptions.add(exc);
    }
    if (!thrownExceptions.isEmpty()) {
        StringJoiner description = new StringJoiner(",", "(", ")");
        for (DockerAccessException dae : thrownExceptions) {
            description.add(dae.getLocalizedMessage());
        }
        DockerAccessException exception = new DockerAccessException(description.toString());
        for (DockerAccessException dae : thrownExceptions) {
            exception.addSuppressed(dae);
        }
        throw exception;
    }
}
Also used : ContainerShutdownDescriptor(io.fabric8.maven.docker.service.ContainerTracker.ContainerShutdownDescriptor) DockerAccessException(io.fabric8.maven.docker.access.DockerAccessException) Network(io.fabric8.maven.docker.model.Network) ArrayList(java.util.ArrayList) StringJoiner(java.util.StringJoiner) HashSet(java.util.HashSet)

Example 14 with Network

use of io.fabric8.maven.docker.model.Network in project docker-maven-plugin by fabric8io.

the class DockerAccessWithHcClient method createNetwork.

@Override
public String createNetwork(NetworkCreateConfig networkConfig) throws DockerAccessException {
    String createJson = networkConfig.toJson();
    log.debug("Network create config: " + createJson);
    try {
        String url = urlBuilder.createNetwork();
        log.verbose(Logger.LogVerboseCategory.API, API_LOG_FORMAT_POST_WITH_REQUEST, url, createJson);
        String response = delegate.post(url, createJson, new ApacheHttpClientDelegate.BodyResponseHandler(), HTTP_CREATED);
        log.debug(response);
        JsonObject json = JsonFactory.newJsonObject(response);
        if (json.has("Warnings")) {
            logWarnings(json);
        }
        // only need first 12 to id a container
        return json.get("Id").getAsString().substring(0, 12);
    } catch (IOException e) {
        throw new DockerAccessException(e, "Unable to create network for [%s]", networkConfig.getName());
    }
}
Also used : DockerAccessException(io.fabric8.maven.docker.access.DockerAccessException) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException)

Example 15 with Network

use of io.fabric8.maven.docker.model.Network in project docker-maven-plugin by fabric8io.

the class StopMojo method stopContainers.

private void stopContainers(QueryService queryService, RunService runService, GavLabel gavLabel) throws MojoExecutionException, IOException, ExecException {
    Collection<Network> networksToRemove = getNetworksToRemove(queryService, gavLabel);
    List<DockerAccessException> thrownExceptions = new ArrayList<>();
    for (ImageConfiguration image : getResolvedImages()) {
        Collection<Container> existingContainers = getContainersForImage(queryService, image);
        for (Container container : existingContainers) {
            if (shouldStopContainer(container, gavLabel)) {
                try {
                    runService.stopContainer(container.getId(), image, keepContainer, removeVolumes);
                } catch (DockerAccessException exc) {
                    thrownExceptions.add(exc);
                }
            }
        }
    }
    // If the mojo has a stopNamePattern, check to see if there are matching containers
    for (Container container : getContainersForMojo(queryService)) {
        if (shouldStopContainer(container, gavLabel)) {
            try {
                runService.stopContainer(container.getId(), new ImageConfiguration.Builder().name(container.getImage()).build(), keepContainer, removeVolumes);
            } catch (DockerAccessException exc) {
                thrownExceptions.add(exc);
            }
        }
    }
    try {
        runService.removeCustomNetworks(networksToRemove);
    } catch (DockerAccessException exc) {
        thrownExceptions.add(exc);
    }
    if (!thrownExceptions.isEmpty()) {
        DockerAccessException exception = new DockerAccessException("At least one exception thrown during container removal.");
        for (DockerAccessException dae : thrownExceptions) {
            exception.addSuppressed(dae);
        }
        throw exception;
    }
}
Also used : Container(io.fabric8.maven.docker.model.Container) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) Network(io.fabric8.maven.docker.model.Network) DockerAccessException(io.fabric8.maven.docker.access.DockerAccessException) ArrayList(java.util.ArrayList)

Aggregations

Network (io.fabric8.maven.docker.model.Network)6 DockerAccessException (io.fabric8.maven.docker.access.DockerAccessException)5 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)5 ArrayList (java.util.ArrayList)5 NetworkConfig (io.fabric8.maven.docker.config.NetworkConfig)4 Container (io.fabric8.maven.docker.model.Container)4 IOException (java.io.IOException)4 RunImageConfiguration (io.fabric8.maven.docker.config.RunImageConfiguration)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 Container (io.fabric8.api.Container)2 Profile (io.fabric8.api.Profile)2 Pod (io.fabric8.kubernetes.api.model.Pod)2 Test (org.junit.Test)2 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 DataTable (io.cucumber.datatable.DataTable)1 Given (io.cucumber.java.en.Given)1 Then (io.cucumber.java.en.Then)1 When (io.cucumber.java.en.When)1