Search in sources :

Example 6 with PodSpecBuilder

use of io.fabric8.kubernetes.api.model.PodSpecBuilder in project fabric8-maven-plugin by fabric8io.

the class KubernetesResourceUtil method mergePodSpec.

public static void mergePodSpec(PodSpecBuilder builder, PodSpec defaultPodSpec, String defaultName) {
    List<Container> containers = builder.buildContainers();
    List<Container> defaultContainers = defaultPodSpec.getContainers();
    int size = defaultContainers.size();
    if (size > 0) {
        if (containers == null || containers.isEmpty()) {
            builder.addToContainers(defaultContainers.toArray(new Container[size]));
        } else {
            int idx = 0;
            for (Container defaultContainer : defaultContainers) {
                Container container;
                if (idx < containers.size()) {
                    container = containers.get(idx);
                } else {
                    container = new Container();
                    containers.add(container);
                }
                mergeSimpleFields(container, defaultContainer);
                List<EnvVar> defaultEnv = defaultContainer.getEnv();
                if (defaultEnv != null) {
                    for (EnvVar envVar : defaultEnv) {
                        ensureHasEnv(container, envVar);
                    }
                }
                List<ContainerPort> defaultPorts = defaultContainer.getPorts();
                if (defaultPorts != null) {
                    for (ContainerPort port : defaultPorts) {
                        ensureHasPort(container, port);
                    }
                }
                if (container.getReadinessProbe() == null) {
                    container.setReadinessProbe(defaultContainer.getReadinessProbe());
                }
                if (container.getLivenessProbe() == null) {
                    container.setLivenessProbe(defaultContainer.getLivenessProbe());
                }
                if (container.getSecurityContext() == null) {
                    container.setSecurityContext(defaultContainer.getSecurityContext());
                }
                idx++;
            }
            builder.withContainers(containers);
        }
    } else if (!containers.isEmpty()) {
        // lets default the container name if there's none specified in the custom yaml file
        Container container = containers.get(0);
        if (isNullOrBlank(container.getName())) {
            container.setName(defaultName);
        }
        builder.withContainers(containers);
    }
}
Also used : Container(io.fabric8.kubernetes.api.model.Container) ContainerPort(io.fabric8.kubernetes.api.model.ContainerPort) EnvVar(io.fabric8.kubernetes.api.model.EnvVar)

Example 7 with PodSpecBuilder

use of io.fabric8.kubernetes.api.model.PodSpecBuilder in project ephemerals by LivePersonInc.

the class DefaultKubernetesDeploymentStrategy method pod.

@Override
public PodSpec pod(Deployment deployment) {
    PodSpecBuilder podSpec = new PodSpecBuilder();
    ContainerizedDeploymentUnit deploymentUnit = (ContainerizedDeploymentUnit) deployment.getDeploymentUnit();
    List<ContainerPort> containerPortList = deployment.getDeploymentUnit().getPorts().stream().map(port -> new ContainerPortBuilder().withContainerPort(port.getPort()).withName(port.getName()).build()).collect(Collectors.toList());
    Probe healthProbe = null;
    Probe readinessProbe = null;
    if (deploymentUnit.getHealthProbe() != null) {
        healthProbe = DefaultKubernetesDeploymentStrategy.probe(deploymentUnit.getHealthProbe());
    }
    if (deploymentUnit.getReadinessProbe() != null) {
        readinessProbe = DefaultKubernetesDeploymentStrategy.probe(deploymentUnit.getReadinessProbe());
    }
    ContainerBuilder containerBuilder = new ContainerBuilder();
    if (healthProbe != null) {
        containerBuilder.withLivenessProbe(healthProbe);
    }
    if (readinessProbe != null) {
        containerBuilder.withReadinessProbe(readinessProbe);
    }
    Container container = containerBuilder.withName(deployment.getId()).withPorts(containerPortList).withImage(deploymentUnit.getImage()).withEnv(envVars(deploymentUnit.getEnvVars())).withResources(new ResourceRequirementsBuilder().addToLimits("cpu", new QuantityBuilder().withAmount(String.valueOf(deploymentUnit.getCpu() * 1000) + "m").build()).addToLimits("memory", new QuantityBuilder().withAmount(String.valueOf(deploymentUnit.getMem()) + "Mi").build()).build()).build();
    container.setVolumeMounts(deploymentUnit.getVolumes().entrySet().stream().map(pair -> new VolumeMountBuilder().withName(pair.getKey().getName()).withMountPath(pair.getKey().getMountPath()).build()).collect(Collectors.toList()));
    podSpec.addToContainers(container);
    podSpec.withVolumes(deploymentUnit.getVolumes().entrySet().stream().filter(pair -> pair != null).map(pair -> {
        Volume volume = volume(pair.getValue());
        volume.setName(pair.getKey().getName());
        return volume;
    }).collect(Collectors.toList()));
    return podSpec.build();
}
Also used : Probe(io.fabric8.kubernetes.api.model.Probe) Objects(java.util.Objects) List(java.util.List) com.liveperson.ephemerals.deploy.probe(com.liveperson.ephemerals.deploy.probe) Map(java.util.Map) io.fabric8.kubernetes.api.model(io.fabric8.kubernetes.api.model) DEPLOYMENT_LABEL_KEY(com.liveperson.ephemerals.provider.kubernetes.KubernetesDeploymentHandler.DEPLOYMENT_LABEL_KEY) Deployment(com.liveperson.ephemerals.deploy.Deployment) com.liveperson.ephemerals.deploy.volume(com.liveperson.ephemerals.deploy.volume) Collectors(java.util.stream.Collectors) ContainerizedDeploymentUnit(com.liveperson.ephemerals.deploy.unit.ContainerizedDeploymentUnit) Volume(io.fabric8.kubernetes.api.model.Volume) ContainerizedDeploymentUnit(com.liveperson.ephemerals.deploy.unit.ContainerizedDeploymentUnit) Probe(io.fabric8.kubernetes.api.model.Probe) Volume(io.fabric8.kubernetes.api.model.Volume)

Example 8 with PodSpecBuilder

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

Aggregations

PodSpecBuilder (io.fabric8.kubernetes.api.model.PodSpecBuilder)6 ContainerBuilder (io.fabric8.kubernetes.api.model.ContainerBuilder)4 PodSpec (io.fabric8.kubernetes.api.model.PodSpec)4 DeploymentBuilder (io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder)4 Container (io.fabric8.kubernetes.api.model.Container)3 Service (io.fabric8.kubernetes.api.model.Service)3 ServiceBuilder (io.fabric8.kubernetes.api.model.ServiceBuilder)3 ServicePortBuilder (io.fabric8.kubernetes.api.model.ServicePortBuilder)3 ServiceSpecBuilder (io.fabric8.kubernetes.api.model.ServiceSpecBuilder)3 VolumeBuilder (io.fabric8.kubernetes.api.model.VolumeBuilder)3 VolumeMountBuilder (io.fabric8.kubernetes.api.model.VolumeMountBuilder)3 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)3 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)3 ContainerPortBuilder (io.fabric8.kubernetes.api.model.ContainerPortBuilder)2 EnvVar (io.fabric8.kubernetes.api.model.EnvVar)2 IntOrString (io.fabric8.kubernetes.api.model.IntOrString)2 ObjectMetaBuilder (io.fabric8.kubernetes.api.model.ObjectMetaBuilder)2 Pod (io.fabric8.kubernetes.api.model.Pod)2 PodBuilder (io.fabric8.kubernetes.api.model.PodBuilder)2 Probe (io.fabric8.kubernetes.api.model.Probe)2