Search in sources :

Example 1 with NodeSelector

use of io.fabric8.kubernetes.api.model.NodeSelector in project zalenium by zalando.

the class KubernetesContainerMock method getMockedKubernetesContainerClient.

public static KubernetesContainerClient getMockedKubernetesContainerClient() {
    // Mocking the environment variable to return false for video recording enabled
    Environment environment = mock(Environment.class);
    when(environment.getStringEnvVariable("ZALENIUM_KUBERNETES_CPU_REQUEST", null)).thenReturn("250m");
    when(environment.getStringEnvVariable("ZALENIUM_KUBERNETES_CPU_LIMIT", null)).thenReturn("500m");
    when(environment.getStringEnvVariable("ZALENIUM_KUBERNETES_MEMORY_REQUEST", null)).thenReturn("1Gi");
    when(environment.getStringEnvVariable("ZALENIUM_KUBERNETES_MEMORY_LIMIT", null)).thenReturn("4Gi");
    String hostName;
    try {
        hostName = InetAddress.getLocalHost().getHostName();
    } catch (UnknownHostException e) {
        hostName = "";
    }
    KubernetesServer server = new KubernetesServer();
    server.before();
    Map<String, String> zaleniumPodLabels = new HashMap<>();
    zaleniumPodLabels.put("app", "zalenium");
    zaleniumPodLabels.put("role", "grid");
    Pod zaleniumPod = new PodBuilder().withNewMetadata().withLabels(zaleniumPodLabels).addToLabels(zaleniumPodLabels).withNamespace("test").withGenerateName(hostName).withName(hostName).and().build();
    String videosVolumeName = RandomStringUtils.randomAlphabetic(5).toLowerCase();
    String generalVolumeName = RandomStringUtils.randomAlphabetic(5).toLowerCase();
    VolumeMount volumeMountVideos = new VolumeMountBuilder().withName(videosVolumeName).withMountPath("/tmp/videos").build();
    VolumeMount volumeMountGeneral = new VolumeMountBuilder().withName(generalVolumeName).withMountPath("/tmp/mounted").build();
    Container zaleniumContainer = new ContainerBuilder().withVolumeMounts(volumeMountVideos, volumeMountGeneral).build();
    Volume videosVolume = new VolumeBuilder().withName(videosVolumeName).build();
    Volume generalVolume = new VolumeBuilder().withName(generalVolumeName).build();
    HostAlias hostAlias = new HostAliasBuilder().withHostnames("foo.local").withIp("127.0.0.1").build();
    Map<String, String> nodeSelector = new HashMap<>();
    nodeSelector.put("nodeLabelKey", "nodeLabelValue");
    PodSpec zaleniumPodSpec = new PodSpecBuilder().withContainers(zaleniumContainer).withVolumes(videosVolume, generalVolume).withHostAliases(hostAlias).withNodeSelector(nodeSelector).build();
    zaleniumPod.setSpec(zaleniumPodSpec);
    String podsPath = String.format("/api/v1/namespaces/test/pods/%s", hostName);
    server.expect().withPath(podsPath).andReturn(200, zaleniumPod).once();
    Map<String, String> dockerSeleniumPodLabels = new HashMap<>();
    dockerSeleniumPodLabels.put("createdBy", "zalenium");
    Pod dockerSeleniumPod = new PodBuilder().withNewMetadata().withLabels(dockerSeleniumPodLabels).withNamespace("test").withName(hostName).and().build();
    Container dockerSeleniumContainer = new ContainerBuilder().withEnv(new EnvVarBuilder().withName("NOVNC_PORT").withValue("40000").build()).build();
    PodSpec dockerSeleniumPodSpec = new PodSpecBuilder().withContainers(dockerSeleniumContainer).build();
    dockerSeleniumPod.setSpec(dockerSeleniumPodSpec);
    PodStatus dockerSeleniumPodStatus = new PodStatusBuilder().withPodIP("localhost").build();
    dockerSeleniumPod.setStatus(dockerSeleniumPodStatus);
    server.expect().withPath("/api/v1/namespaces/test/pods?labelSelector=createdBy%3Dzalenium").andReturn(200, new PodListBuilder().withItems(dockerSeleniumPod).build()).always();
    ServiceSpec serviceSpec = new ServiceSpecBuilder().withPorts(new ServicePortBuilder().withNodePort(40000).build()).build();
    Service service = new ServiceBuilder().withSpec(serviceSpec).build();
    server.expect().withPath("/api/v1/namespaces/test/services").andReturn(201, service).always();
    String bashCommand = String.format("/api/v1/namespaces/test/pods/%s/exec?command=bash&command=-c&command=", hostName);
    String tarCommand = String.format("/api/v1/namespaces/test/pods/%s/exec?command=tar&command=-C&command=", hostName);
    String commandSuffix = "&stdout=true&stderr=true";
    String expectedOutput = "test";
    List<String> execPaths = new ArrayList<>();
    execPaths.add(String.format("%stransfer-logs.sh%s", bashCommand, commandSuffix));
    execPaths.add(String.format("%s/var/log/cont/&command=-c&command=.%s", tarCommand, commandSuffix));
    execPaths.add(String.format("%s/videos/&command=-c&command=.%s", tarCommand, commandSuffix));
    execPaths.add(String.format("%s/videos/&command=-C&command=.%s", tarCommand, commandSuffix));
    execPaths.add(String.format("%sstop-video%s", bashCommand, commandSuffix));
    execPaths.add(String.format("%sstart-video%s", bashCommand, commandSuffix));
    execPaths.add(String.format("%stransfer-logs.sh%s", bashCommand, commandSuffix));
    execPaths.add(String.format("%scleanup-container.sh%s", bashCommand, commandSuffix));
    String notifyComplete = bashCommand.concat("notify%20%27Zalenium%27,%20%27TEST%20COMPLETED%27,%20--icon=/home/seluser/images/completed.png").concat(commandSuffix);
    String notifyTimeout = bashCommand.concat("notify%20%27Zalenium%27,%20%27TEST%20TIMED%20OUT%27,%20--icon=/home/seluser/images/timeout.png").concat(commandSuffix);
    execPaths.add(notifyComplete);
    execPaths.add(notifyTimeout);
    for (String execPath : execPaths) {
        server.expect().withPath(execPath).andUpgradeToWebSocket().open(new OutputStreamMessage(expectedOutput)).done().once();
    }
    server.expect().withPath("/api/v1/namespaces/test/pods").andReturn(201, new PodBuilder().build()).always();
    KubernetesClient client = server.getClient();
    return new KubernetesContainerClient(environment, KubernetesContainerClient::createDoneablePodDefaultImpl, client);
}
Also used : HashMap(java.util.HashMap) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) ServiceSpec(io.fabric8.kubernetes.api.model.ServiceSpec) ArrayList(java.util.ArrayList) OutputStreamMessage(io.fabric8.kubernetes.client.server.mock.OutputStreamMessage) PodStatusBuilder(io.fabric8.kubernetes.api.model.PodStatusBuilder) VolumeBuilder(io.fabric8.kubernetes.api.model.VolumeBuilder) EnvVarBuilder(io.fabric8.kubernetes.api.model.EnvVarBuilder) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder) PodStatus(io.fabric8.kubernetes.api.model.PodStatus) Container(io.fabric8.kubernetes.api.model.Container) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) PodSpecBuilder(io.fabric8.kubernetes.api.model.PodSpecBuilder) HostAliasBuilder(io.fabric8.kubernetes.api.model.HostAliasBuilder) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) UnknownHostException(java.net.UnknownHostException) Pod(io.fabric8.kubernetes.api.model.Pod) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) Service(io.fabric8.kubernetes.api.model.Service) KubernetesServer(io.fabric8.kubernetes.client.server.mock.KubernetesServer) VolumeMountBuilder(io.fabric8.kubernetes.api.model.VolumeMountBuilder) KubernetesContainerClient(de.zalando.ep.zalenium.container.kubernetes.KubernetesContainerClient) HostAlias(io.fabric8.kubernetes.api.model.HostAlias) ServiceSpecBuilder(io.fabric8.kubernetes.api.model.ServiceSpecBuilder) PodListBuilder(io.fabric8.kubernetes.api.model.PodListBuilder) Volume(io.fabric8.kubernetes.api.model.Volume) ServicePortBuilder(io.fabric8.kubernetes.api.model.ServicePortBuilder) VolumeMount(io.fabric8.kubernetes.api.model.VolumeMount)

Example 2 with NodeSelector

use of io.fabric8.kubernetes.api.model.NodeSelector in project zalenium by zalando.

the class KubernetesContainerClient method createContainer.

@Override
public ContainerCreationStatus createContainer(String zaleniumContainerName, String image, Map<String, String> envVars, String nodePort) {
    String containerIdPrefix = String.format("%s-%s-", zaleniumAppName, nodePort);
    // Convert the environment variables into the Kubernetes format.
    List<EnvVar> flattenedEnvVars = envVars.entrySet().stream().map(e -> new EnvVar(e.getKey(), e.getValue(), null)).collect(Collectors.toList());
    Map<String, String> podSelector = new HashMap<>();
    PodConfiguration config = new PodConfiguration();
    config.setNodePort(nodePort);
    config.setClient(client);
    config.setContainerIdPrefix(containerIdPrefix);
    config.setImage(image);
    config.setEnvVars(flattenedEnvVars);
    Map<String, String> labels = new HashMap<>();
    labels.putAll(createdByZaleniumMap);
    labels.putAll(appLabelMap);
    labels.putAll(podSelector);
    config.setLabels(labels);
    config.setMountedSharedFoldersMap(mountedSharedFoldersMap);
    config.setHostAliases(hostAliases);
    config.setNodeSelector(nodeSelector);
    config.setPodLimits(seleniumPodLimits);
    config.setPodRequests(seleniumPodRequests);
    DoneablePod doneablePod = createDoneablePod.apply(config);
    // Create the container
    Pod createdPod = doneablePod.done();
    String containerName = createdPod.getMetadata() == null ? containerIdPrefix : createdPod.getMetadata().getName();
    return new ContainerCreationStatus(true, containerName, nodePort);
}
Also used : Quantity(io.fabric8.kubernetes.api.model.Quantity) VolumeMount(io.fabric8.kubernetes.api.model.VolumeMount) Arrays(java.util.Arrays) StringUtils(org.apache.commons.lang.StringUtils) DoneablePod(io.fabric8.kubernetes.api.model.DoneablePod) EnvVar(io.fabric8.kubernetes.api.model.EnvVar) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Environment(de.zalando.ep.zalenium.util.Environment) URL(java.net.URL) ContainerClientRegistration(de.zalando.ep.zalenium.container.ContainerClientRegistration) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) HostAlias(io.fabric8.kubernetes.api.model.HostAlias) Map(java.util.Map) Response(okhttp3.Response) ExecListener(io.fabric8.kubernetes.client.dsl.ExecListener) ContainerClient(de.zalando.ep.zalenium.container.ContainerClient) Volume(io.fabric8.kubernetes.api.model.Volume) Logger(org.slf4j.Logger) Pod(io.fabric8.kubernetes.api.model.Pod) ExecWatch(io.fabric8.kubernetes.client.dsl.ExecWatch) UnknownHostException(java.net.UnknownHostException) Collectors(java.util.stream.Collectors) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) PodList(io.fabric8.kubernetes.api.model.PodList) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) Optional(java.util.Optional) ContainerCreationStatus(de.zalando.ep.zalenium.container.ContainerCreationStatus) InputStream(java.io.InputStream) DoneablePod(io.fabric8.kubernetes.api.model.DoneablePod) Pod(io.fabric8.kubernetes.api.model.Pod) HashMap(java.util.HashMap) ContainerCreationStatus(de.zalando.ep.zalenium.container.ContainerCreationStatus) DoneablePod(io.fabric8.kubernetes.api.model.DoneablePod) EnvVar(io.fabric8.kubernetes.api.model.EnvVar)

Example 3 with NodeSelector

use of io.fabric8.kubernetes.api.model.NodeSelector 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)

Aggregations

Pod (io.fabric8.kubernetes.api.model.Pod)3 HashMap (java.util.HashMap)3 HostAlias (io.fabric8.kubernetes.api.model.HostAlias)2 Quantity (io.fabric8.kubernetes.api.model.Quantity)2 Volume (io.fabric8.kubernetes.api.model.Volume)2 VolumeMount (io.fabric8.kubernetes.api.model.VolumeMount)2 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)2 InputStream (java.io.InputStream)2 UnknownHostException (java.net.UnknownHostException)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 ContainerClient (de.zalando.ep.zalenium.container.ContainerClient)1 ContainerClientRegistration (de.zalando.ep.zalenium.container.ContainerClientRegistration)1 ContainerCreationStatus (de.zalando.ep.zalenium.container.ContainerCreationStatus)1 KubernetesContainerClient (de.zalando.ep.zalenium.container.kubernetes.KubernetesContainerClient)1 Environment (de.zalando.ep.zalenium.util.Environment)1