Search in sources :

Example 1 with PodList

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());
}
Also used : PodList(io.fabric8.kubernetes.api.model.PodList) PodResource(io.fabric8.kubernetes.client.dsl.PodResource) DoneablePod(io.fabric8.kubernetes.api.model.DoneablePod) Pod(io.fabric8.kubernetes.api.model.Pod) DoneablePod(io.fabric8.kubernetes.api.model.DoneablePod) Map(java.util.Map)

Example 2 with PodList

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);
    }
}
Also used : LabelSelectorBuilder(io.fabric8.kubernetes.api.model.LabelSelectorBuilder) PodList(io.fabric8.kubernetes.api.model.PodList) Pod(io.fabric8.kubernetes.api.model.Pod) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) Closeable(java.io.Closeable) Logger(io.fabric8.maven.docker.util.Logger) ProcessUtil(io.fabric8.maven.core.util.ProcessUtil) PodListBuilder(io.fabric8.kubernetes.api.model.PodListBuilder) OpenShiftMockServer(io.fabric8.openshift.client.server.mock.OpenShiftMockServer) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) WatchEvent(io.fabric8.kubernetes.api.model.WatchEvent) Test(org.junit.Test)

Example 3 with PodList

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;
}
Also used : PodList(io.fabric8.kubernetes.api.model.PodList) DoneablePod(io.fabric8.kubernetes.api.model.DoneablePod) Pod(io.fabric8.kubernetes.api.model.Pod) Watch(io.fabric8.kubernetes.client.Watch) LogWatch(io.fabric8.kubernetes.client.dsl.LogWatch) Watcher(io.fabric8.kubernetes.client.Watcher) LabelSelectorRequirement(io.fabric8.kubernetes.api.model.LabelSelectorRequirement)

Example 4 with PodList

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;
}
Also used : PodList(io.fabric8.kubernetes.api.model.PodList) Pod(io.fabric8.kubernetes.api.model.Pod) Watch(io.fabric8.kubernetes.client.Watch) Watcher(io.fabric8.kubernetes.client.Watcher)

Example 5 with PodList

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);
}
Also used : PodList(io.fabric8.kubernetes.api.model.PodList) Pod(io.fabric8.kubernetes.api.model.Pod)

Aggregations

PodList (io.fabric8.kubernetes.api.model.PodList)26 Pod (io.fabric8.kubernetes.api.model.Pod)24 DoneablePod (io.fabric8.kubernetes.api.model.DoneablePod)7 Watch (io.fabric8.kubernetes.client.Watch)7 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)6 Test (org.junit.Test)6 Watcher (io.fabric8.kubernetes.client.Watcher)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 PodBuilder (io.fabric8.kubernetes.api.model.PodBuilder)4 ContainerStatus (io.fabric8.kubernetes.api.model.ContainerStatus)3 Service (io.fabric8.kubernetes.api.model.Service)3 ServiceList (io.fabric8.kubernetes.api.model.ServiceList)3 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)3 PodResource (io.fabric8.kubernetes.client.dsl.PodResource)3 ArrayList (java.util.ArrayList)3 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)2 ConfigMapList (io.fabric8.kubernetes.api.model.ConfigMapList)2 DoneableConfigMap (io.fabric8.kubernetes.api.model.DoneableConfigMap)2 DoneableEndpoints (io.fabric8.kubernetes.api.model.DoneableEndpoints)2