use of io.fabric8.kubernetes.api.model.PodSpec in project fabric8 by fabric8io.
the class Controller method doCreatePod.
protected void doCreatePod(Pod pod, String namespace, String sourceName) {
LOG.info("Creating a Pod from " + sourceName + " namespace " + namespace + " name " + getName(pod));
try {
PodSpec podSpec = pod.getSpec();
if (podSpec != null) {
validatePodSpec(podSpec, namespace);
}
Object answer;
if (Strings.isNotBlank(namespace)) {
answer = kubernetesClient.pods().inNamespace(namespace).create(pod);
} else {
answer = kubernetesClient.pods().inNamespace(getNamespace()).create(pod);
}
LOG.info("Created Pod result: " + answer);
} catch (Exception e) {
onApplyError("Failed to create Pod from " + sourceName + ". " + e + ". " + pod, e);
}
}
use of io.fabric8.kubernetes.api.model.PodSpec in project flink by apache.
the class KubernetesJobManagerFactoryTest method testPodSpec.
@Test
public void testPodSpec() throws IOException {
kubernetesJobManagerSpecification = KubernetesJobManagerFactory.buildKubernetesJobManagerSpecification(flinkPod, kubernetesJobManagerParameters);
final PodSpec resultPodSpec = this.kubernetesJobManagerSpecification.getDeployment().getSpec().getTemplate().getSpec();
assertEquals(1, resultPodSpec.getContainers().size());
assertEquals(SERVICE_ACCOUNT_NAME, resultPodSpec.getServiceAccountName());
assertEquals(3, resultPodSpec.getVolumes().size());
final Container resultedMainContainer = resultPodSpec.getContainers().get(0);
assertEquals(Constants.MAIN_CONTAINER_NAME, resultedMainContainer.getName());
assertEquals(CONTAINER_IMAGE, resultedMainContainer.getImage());
assertEquals(CONTAINER_IMAGE_PULL_POLICY.name(), resultedMainContainer.getImagePullPolicy());
assertEquals(3, resultedMainContainer.getEnv().size());
assertTrue(resultedMainContainer.getEnv().stream().anyMatch(envVar -> envVar.getName().equals("key1")));
assertEquals(3, resultedMainContainer.getPorts().size());
final Map<String, Quantity> requests = resultedMainContainer.getResources().getRequests();
assertEquals(Double.toString(JOB_MANAGER_CPU), requests.get("cpu").getAmount());
assertEquals(String.valueOf(JOB_MANAGER_MEMORY), requests.get("memory").getAmount());
assertEquals(1, resultedMainContainer.getCommand().size());
// The args list is [bash, -c, 'java -classpath $FLINK_CLASSPATH ...'].
assertEquals(3, resultedMainContainer.getArgs().size());
assertEquals(3, resultedMainContainer.getVolumeMounts().size());
}
use of io.fabric8.kubernetes.api.model.PodSpec in project flink by apache.
the class KubernetesJobManagerFactoryTest method testExistingHadoopConfigMap.
@Test
public void testExistingHadoopConfigMap() throws IOException {
flinkConfig.set(KubernetesConfigOptions.HADOOP_CONF_CONFIG_MAP, EXISTING_HADOOP_CONF_CONFIG_MAP);
kubernetesJobManagerSpecification = KubernetesJobManagerFactory.buildKubernetesJobManagerSpecification(flinkPod, kubernetesJobManagerParameters);
assertFalse(kubernetesJobManagerSpecification.getAccompanyingResources().stream().anyMatch(resource -> resource.getMetadata().getName().equals(HadoopConfMountDecorator.getHadoopConfConfigMapName(CLUSTER_ID))));
final PodSpec podSpec = kubernetesJobManagerSpecification.getDeployment().getSpec().getTemplate().getSpec();
assertTrue(podSpec.getVolumes().stream().anyMatch(volume -> volume.getConfigMap().getName().equals(EXISTING_HADOOP_CONF_CONFIG_MAP)));
}
use of io.fabric8.kubernetes.api.model.PodSpec 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.PodSpec in project fabric8 by fabric8io.
the class PodWatcher method onPod.
protected void onPod(String name, Pod pod) {
PodAsserter asserter = podAsserts.get(name);
if (asserter == null) {
asserter = new PodAsserter(this, name, pod);
podAsserts.put(name, asserter);
} else {
asserter.updated(pod);
}
int restartCount = getRestartCount(pod);
PodSpec spec = pod.getSpec();
if (spec != null) {
if (KubernetesHelper.isPodRunning(pod)) {
List<Container> containers = spec.getContainers();
for (Container container : containers) {
File logFileName = LogHelpers.getLogFileName(getBaseDir(), name, container, restartCount);
String key = logFileName.getName();
PodLogWatcher logWatcher = podLogWatchers.get(key);
if (logWatcher == null) {
try {
String containerName = container.getName();
logWatcher = new PodLogWatcher(this, name, pod, containerName, logFileName);
podLogWatchers.put(key, logWatcher);
LOG.info("Watching pod " + name + " container " + containerName + " log at file: " + logFileName.getAbsolutePath());
} catch (Exception e) {
LOG.warn("Failed to create PodLogWatcher: " + e, e);
}
}
}
}
}
File yamlFile = new File(getBaseDir(), "target/test-pod-status/" + name + ".yml");
yamlFile.getParentFile().mkdirs();
try {
KubernetesHelper.saveYaml(pod, yamlFile);
} catch (IOException e) {
LOG.warn("Failed to write " + yamlFile + ". " + e, e);
}
}
Aggregations