use of io.fabric8.kubernetes.api.model.PodList in project camel by apache.
the class KubernetesPodsProducer method doListPodsByLabel.
protected void doListPodsByLabel(Exchange exchange, String operation) {
Map<String, String> labels = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_PODS_LABELS, Map.class);
if (ObjectHelper.isEmpty(labels)) {
LOG.error("Get pods by labels require specify a labels set");
throw new IllegalArgumentException("Get pods by labels require specify a labels set");
}
MixedOperation<Pod, PodList, DoneablePod, PodResource<Pod, DoneablePod>> pods = getEndpoint().getKubernetesClient().pods();
for (Map.Entry<String, String> entry : labels.entrySet()) {
pods.withLabel(entry.getKey(), entry.getValue());
}
PodList podList = pods.list();
MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true);
exchange.getOut().setBody(podList.getItems());
}
use of io.fabric8.kubernetes.api.model.PodList in project fabric8-maven-plugin by fabric8io.
the class PortForwardServiceTest method testSimpleScenario.
@Test
public void testSimpleScenario() throws Exception {
// Cannot test more complex scenarios due to errors in mockwebserver
OpenShiftMockServer mockServer = new OpenShiftMockServer(false);
Pod pod1 = new PodBuilder().withNewMetadata().withName("mypod").addToLabels("mykey", "myvalue").withResourceVersion("1").endMetadata().withNewStatus().withPhase("run").endStatus().build();
PodList pods1 = new PodListBuilder().withItems(pod1).withNewMetadata().withResourceVersion("1").endMetadata().build();
mockServer.expect().get().withPath("/api/v1/namespaces/test/pods?labelSelector=mykey%3Dmyvalue").andReturn(200, pods1).always();
mockServer.expect().get().withPath("/api/v1/namespaces/test/pods").andReturn(200, pods1).always();
mockServer.expect().get().withPath("/api/v1/namespaces/test/pods?labelSelector=mykey%3Dmyvalue&watch=true").andUpgradeToWebSocket().open().waitFor(1000).andEmit(new WatchEvent(pod1, "MODIFIED")).done().always();
mockServer.expect().get().withPath("/api/v1/namespaces/test/pods?resourceVersion=1&watch=true").andUpgradeToWebSocket().open().waitFor(1000).andEmit(new WatchEvent(pod1, "MODIFIED")).done().always();
OpenShiftClient client = mockServer.createOpenShiftClient();
PortForwardService service = new PortForwardService(clientToolsService, logger, client) {
@Override
public ProcessUtil.ProcessExecutionContext forwardPortAsync(Logger externalProcessLogger, String pod, int remotePort, int localPort) throws Fabric8ServiceException {
return new ProcessUtil.ProcessExecutionContext(process, Collections.<Thread>emptyList(), logger);
}
};
try (Closeable c = service.forwardPortAsync(logger, new LabelSelectorBuilder().withMatchLabels(Collections.singletonMap("mykey", "myvalue")).build(), 8080, 9000)) {
Thread.sleep(3000);
}
}
use of io.fabric8.kubernetes.api.model.PodList in project fabric8-maven-plugin by fabric8io.
the class KubernetesClientUtil method withSelector.
public static FilterWatchListDeletable<Pod, PodList, Boolean, Watch, Watcher<Pod>> withSelector(NonNamespaceOperation<Pod, PodList, DoneablePod, PodResource<Pod, DoneablePod>> pods, LabelSelector selector, Logger log) {
FilterWatchListDeletable<Pod, PodList, Boolean, Watch, Watcher<Pod>> answer = pods;
Map<String, String> matchLabels = selector.getMatchLabels();
if (matchLabels != null && !matchLabels.isEmpty()) {
answer = answer.withLabels(matchLabels);
}
List<LabelSelectorRequirement> matchExpressions = selector.getMatchExpressions();
if (matchExpressions != null) {
for (LabelSelectorRequirement expression : matchExpressions) {
String key = expression.getKey();
List<String> values = expression.getValues();
if (Strings.isNullOrBlank(key)) {
log.warn("Ignoring empty key in selector expression %s", expression);
continue;
}
if (values == null || values.isEmpty()) {
log.warn("Ignoring empty values in selector expression %s", expression);
continue;
}
String[] valuesArray = values.toArray(new String[values.size()]);
String operator = expression.getOperator();
switch(operator) {
case "In":
answer = answer.withLabelIn(key, valuesArray);
break;
case "NotIn":
answer = answer.withLabelNotIn(key, valuesArray);
break;
default:
log.warn("Ignoring unknown operator %s in selector expression %s", operator, expression);
}
}
}
return answer;
}
use of io.fabric8.kubernetes.api.model.PodList in project fabric8-maven-plugin by fabric8io.
the class PortForwardService method getNewestPod.
private Pod getNewestPod(LabelSelector selector) {
FilterWatchListDeletable<Pod, PodList, Boolean, Watch, Watcher<Pod>> pods = KubernetesClientUtil.withSelector(kubernetes.pods(), selector, log);
PodList list = pods.list();
if (list != null) {
List<Pod> items = list.getItems();
return getNewestPod(items);
}
return null;
}
use of io.fabric8.kubernetes.api.model.PodList in project fabric8-maven-plugin by fabric8io.
the class BaseBoosterIT method waitTillApplicationPodStarts.
/**
* It watches over application pod until it becomes ready to serve.
*
* @throws Exception
*/
protected void waitTillApplicationPodStarts() throws InterruptedException {
logger.info("Waiting to application pod .... ");
int nPolls = 0;
// Keep polling till 5 minutes
while (nPolls < APPLICATION_POD_WAIT_POLLS) {
PodList podList = openShiftClient.pods().withLabel("app", testsuiteRepositoryArtifactId).list();
for (Pod pod : podList.getItems()) {
logger.info("waitTillApplicationPodStarts() -> Pod : " + pod.getMetadata().getName() + ", isReady : " + KubernetesHelper.isPodReady(pod));
if (KubernetesHelper.isPodReady(pod)) {
logger.info("OK ✓ ... Pod wait over.");
TimeUnit.SECONDS.sleep(10);
return;
}
}
TimeUnit.SECONDS.sleep(10);
nPolls++;
}
throw new AssertionError("Pod wait timeout! Could not find application pod for " + testsuiteRepositoryArtifactId);
}
Aggregations