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);
}
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);
}
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();
}
}
}
}
Aggregations