use of io.fabric8.kubernetes.api.model.LabelSelector in project fabric8-maven-plugin by fabric8io.
the class PodLogService method waitAndLogPods.
private void waitAndLogPods(final KubernetesClient kubernetes, final String namespace, LabelSelector selector, final boolean watchAddedPodsOnly, final String ctrlCMessage, final boolean followLog, Date ignorePodsOlderThan, boolean waitInCurrentThread) {
FilterWatchListDeletable<Pod, PodList, Boolean, Watch, Watcher<Pod>> pods = withSelector(kubernetes.pods().inNamespace(namespace), selector, log);
if (context.getPodName() != null) {
log.info("Watching pod with selector %s, and name %s waiting for a running pod...", selector, context.getPodName());
pods = pods.withField("metadata.name", context.getPodName());
} else {
log.info("Watching pods with selector %s waiting for a running pod...", selector);
}
Pod latestPod = null;
boolean runningPod = false;
PodList list = pods.list();
if (list != null) {
List<Pod> items = list.getItems();
if (items != null) {
for (Pod pod : items) {
PodStatusType status = getPodStatus(pod);
switch(status) {
case WAIT:
case OK:
if (latestPod == null || KubernetesResourceUtil.isNewerResource(pod, latestPod)) {
if (ignorePodsOlderThan != null) {
Date podCreateTime = KubernetesResourceUtil.getCreationTimestamp(pod);
if (podCreateTime != null && podCreateTime.compareTo(ignorePodsOlderThan) > 0) {
latestPod = pod;
}
} else {
latestPod = pod;
}
}
runningPod = true;
break;
case ERROR:
default:
continue;
}
}
}
}
// we may have missed the ADDED event so lets simulate one
if (latestPod != null) {
onPod(Watcher.Action.ADDED, latestPod, kubernetes, namespace, ctrlCMessage, followLog);
}
if (!watchAddedPodsOnly) {
// lets watch the current pods then watch for changes
if (!runningPod) {
log.warn("No pod is running yet. Are you sure you deployed your app via `fabric8:deploy`?");
log.warn("Or did you stop it via `fabric8:stop`? If so try running the `fabric8:start` goal");
}
}
podWatcher = pods.watch(new Watcher<Pod>() {
@Override
public void eventReceived(Action action, Pod pod) {
onPod(action, pod, kubernetes, namespace, ctrlCMessage, followLog);
}
@Override
public void onClose(KubernetesClientException e) {
// ignore
}
});
if (waitInCurrentThread) {
while (terminateLatch.getCount() > 0) {
try {
terminateLatch.await();
} catch (InterruptedException e) {
// ignore
}
}
}
}
use of io.fabric8.kubernetes.api.model.LabelSelector 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.LabelSelector in project fabric8 by fabric8io.
the class KubernetesConfigAdminBridgeTest method testAndOr.
@Test
public void testAndOr() {
System.setProperty("fabric8.pid.filters", "appName=A;B,database.name=my.oracle.datasource");
KubernetesMockServer plainServer = new KubernetesMockServer(false);
plainServer.expect().get().withPath("/api/v1/namespaces/test/configmaps?labelSelector=karaf.pid,appName%20in%20(A,B),database.name%20in%20(my.oracle.datasource)&watch=true").andReturnChunked(200).always();
plainServer.expect().get().withPath("/api/v1/namespaces/test/configmaps?labelSelector=karaf.pid,appName%20in%20(A,B),database.name%20in%20(my.oracle.datasource)").andReturn(200, cmEmptyList).once();
KubernetesConfigAdminBridge kcab = new KubernetesConfigAdminBridge();
kcab.bindConfigAdmin(caService);
kcab.bindKubernetesClient(plainServer.createClient());
kcab.activate();
}
use of io.fabric8.kubernetes.api.model.LabelSelector in project fabric8 by fabric8io.
the class KubernetesConfigAdminBridgeTest method testAand.
@Test
public void testAand() {
System.setProperty("fabric8.pid.filters", "appName=A,database.name=my.oracle.datasource");
KubernetesMockServer plainServer = new KubernetesMockServer(false);
plainServer.expect().get().withPath("/api/v1/namespaces/test/configmaps?labelSelector=karaf.pid,appName%20in%20(A),database.name%20in%20(my.oracle.datasource)&watch=true").andReturnChunked(200).always();
plainServer.expect().get().withPath("/api/v1/namespaces/test/configmaps?labelSelector=karaf.pid,appName%20in%20(A),database.name%20in%20(my.oracle.datasource)").andReturn(200, cmEmptyList).once();
KubernetesConfigAdminBridge kcab = new KubernetesConfigAdminBridge();
kcab.bindConfigAdmin(caService);
kcab.bindKubernetesClient(plainServer.createClient());
kcab.activate();
}
use of io.fabric8.kubernetes.api.model.LabelSelector in project fabric8 by fabric8io.
the class ReplicaSetPodsAssert method pods.
@Override
public PodSelectionAssert pods() {
spec().isNotNull().selector().isNotNull();
ReplicaSetSpec spec = this.actual.getSpec();
Integer replicas = spec.getReplicas();
LabelSelector selector = spec.getSelector();
Map<String, String> matchLabels = selector.getMatchLabels();
List<LabelSelectorRequirement> matchExpressions = selector.getMatchExpressions();
return new PodSelectionAssert(client, replicas, matchLabels, matchExpressions, "ReplicaSet " + KubernetesHelper.getName(actual));
}
Aggregations