Search in sources :

Example 61 with KubernetesClient

use of io.fabric8.kubernetes.client.KubernetesClient in project strimzi by strimzi.

the class Main method isOnOpenShift.

static Future<Boolean> isOnOpenShift(Vertx vertx, KubernetesClient client) {
    URL kubernetesApi = client.getMasterUrl();
    Future<Boolean> fut = Future.future();
    HttpClientOptions httpClientOptions = new HttpClientOptions();
    httpClientOptions.setDefaultHost(kubernetesApi.getHost());
    if (kubernetesApi.getPort() == -1) {
        httpClientOptions.setDefaultPort(kubernetesApi.getDefaultPort());
    } else {
        httpClientOptions.setDefaultPort(kubernetesApi.getPort());
    }
    if (kubernetesApi.getProtocol().equals("https")) {
        httpClientOptions.setSsl(true);
        httpClientOptions.setTrustAll(true);
    }
    HttpClient httpClient = vertx.createHttpClient(httpClientOptions);
    httpClient.getNow("/oapi", res -> {
        if (res.statusCode() == HttpResponseStatus.OK.code()) {
            log.debug("{} returned {}. We are on OpenShift.", res.request().absoluteURI(), res.statusCode());
            // We should be on OpenShift based on the /oapi result. We can now safely try isAdaptable() to be 100% sure.
            Boolean isOpenShift = Boolean.TRUE.equals(client.isAdaptable(OpenShiftClient.class));
            fut.complete(isOpenShift);
        } else {
            log.debug("{} returned {}. We are not on OpenShift.", res.request().absoluteURI(), res.statusCode());
            fut.complete(Boolean.FALSE);
        }
    });
    return fut;
}
Also used : HttpClient(io.vertx.core.http.HttpClient) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) URL(java.net.URL) HttpClientOptions(io.vertx.core.http.HttpClientOptions)

Example 62 with KubernetesClient

use of io.fabric8.kubernetes.client.KubernetesClient in project strimzi by strimzi.

the class Main method main.

public static void main(String[] args) {
    Vertx vertx = Vertx.vertx();
    KubernetesClient client = new DefaultKubernetesClient();
    isOnOpenShift(vertx, client).setHandler(os -> {
        if (os.succeeded()) {
            run(vertx, client, os.result().booleanValue(), System.getenv()).setHandler(ar -> {
                if (ar.failed()) {
                    log.error("Unable to start controller for 1 or more namespace", ar.cause());
                    System.exit(1);
                }
            });
        } else {
            log.error("Failed to distinguish between Kubernetes and OpenShift", os.cause());
            System.exit(1);
        }
    });
}
Also used : DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) Vertx(io.vertx.core.Vertx)

Example 63 with KubernetesClient

use of io.fabric8.kubernetes.client.KubernetesClient in project strimzi by strimzi.

the class StatefulSetOperatorTest method mocker.

@Override
protected void mocker(KubernetesClient mockClient, MixedOperation op) {
    AppsAPIGroupDSL mockExt = mock(AppsAPIGroupDSL.class);
    when(mockExt.statefulSets()).thenReturn(op);
    when(mockClient.apps()).thenReturn(mockExt);
}
Also used : AppsAPIGroupDSL(io.fabric8.kubernetes.client.dsl.AppsAPIGroupDSL)

Example 64 with KubernetesClient

use of io.fabric8.kubernetes.client.KubernetesClient in project strimzi by strimzi.

the class K8sImplTest method testList.

@Test
public void testList(TestContext context) {
    Async async = context.async();
    KubernetesClient mockClient = mock(KubernetesClient.class);
    MixedOperation<ConfigMap, ConfigMapList, DoneableConfigMap, Resource<ConfigMap, DoneableConfigMap>> mockConfigMaps = mock(MixedOperation.class);
    when(mockClient.configMaps()).thenReturn(mockConfigMaps);
    when(mockConfigMaps.withLabels(any())).thenReturn(mockConfigMaps);
    when(mockConfigMaps.inNamespace(any())).thenReturn(mockConfigMaps);
    when(mockConfigMaps.list()).thenReturn(new ConfigMapListBuilder().addNewItem().withKind("ConfigMap").withNewMetadata().withName("unrelated").withLabels(Collections.singletonMap("foo", "bar")).endMetadata().withData(Collections.singletonMap("foo", "bar")).endItem().addNewItem().endItem().build());
    K8sImpl k8s = new K8sImpl(vertx, mockClient, new LabelPredicate("foo", "bar"), "default");
    k8s.listMaps(ar -> {
        List<ConfigMap> list = ar.result();
        context.assertFalse(list.isEmpty());
        async.complete();
    });
}
Also used : ConfigMapList(io.fabric8.kubernetes.api.model.ConfigMapList) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) DoneableConfigMap(io.fabric8.kubernetes.api.model.DoneableConfigMap) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) Async(io.vertx.ext.unit.Async) Resource(io.fabric8.kubernetes.client.dsl.Resource) ConfigMapListBuilder(io.fabric8.kubernetes.api.model.ConfigMapListBuilder) DoneableConfigMap(io.fabric8.kubernetes.api.model.DoneableConfigMap) Test(org.junit.Test)

Example 65 with KubernetesClient

use of io.fabric8.kubernetes.client.KubernetesClient in project strimzi by strimzi.

the class KafkaClusterTest method testKafkaScaleUpScaleDown.

@Test
@KafkaCluster(name = "my-cluster", kafkaNodes = 3)
public void testKafkaScaleUpScaleDown() {
    // kafka cluster already deployed via annotation
    String clusterName = "my-cluster";
    LOGGER.info("Running kafkaScaleUpScaleDown {}", clusterName);
    // kubeClient.waitForStatefulSet(kafkaStatefulSetName(clusterName), 3);
    KubernetesClient client = new DefaultKubernetesClient();
    final int initialReplicas = client.apps().statefulSets().inNamespace(NAMESPACE).withName(kafkaStatefulSetName(clusterName)).get().getStatus().getReplicas();
    assertEquals(3, initialReplicas);
    // scale up
    final int scaleTo = initialReplicas + 1;
    final int newPodId = initialReplicas;
    final int newBrokerId = newPodId;
    final String newPodName = kafkaPodName(clusterName, newPodId);
    final String firstPodName = kafkaPodName(clusterName, 0);
    LOGGER.info("Scaling Kafka up to {}", scaleTo);
    replaceCm(clusterName, "kafka-nodes", String.valueOf(initialReplicas + 1));
    kubeClient.waitForStatefulSet(kafkaStatefulSetName(clusterName), initialReplicas + 1);
    // Test that the new broker has joined the kafka cluster by checking it knows about all the other broker's API versions
    // (execute bash because we want the env vars expanded in the pod)
    String versions = getBrokerApiVersions(newPodName);
    for (int brokerId = 0; brokerId < scaleTo; brokerId++) {
        assertTrue(versions, versions.indexOf("(id: " + brokerId + " rack: ") >= 0);
    }
    // TODO Check for k8s events, logs for errors
    // scale down
    LOGGER.info("Scaling down");
    // client.apps().statefulSets().inNamespace(NAMESPACE).withName(kafkaStatefulSetName(clusterName)).scale(initialReplicas, true);
    replaceCm(clusterName, "kafka-nodes", String.valueOf(initialReplicas));
    kubeClient.waitForStatefulSet(kafkaStatefulSetName(clusterName), initialReplicas);
    final int finalReplicas = client.apps().statefulSets().inNamespace(NAMESPACE).withName(kafkaStatefulSetName(clusterName)).get().getStatus().getReplicas();
    assertEquals(initialReplicas, finalReplicas);
    versions = getBrokerApiVersions(firstPodName);
    assertTrue("Expect the added broker, " + newBrokerId + ",  to no longer be present in output of kafka-broker-api-versions.sh", versions.indexOf("(id: " + newBrokerId + " rack: ") == -1);
// TODO Check for k8s events, logs for errors
}
Also used : DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) KafkaCluster(io.strimzi.test.KafkaCluster) Test(org.junit.Test)

Aggregations

KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)62 DefaultKubernetesClient (io.fabric8.kubernetes.client.DefaultKubernetesClient)40 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)21 HashMap (java.util.HashMap)19 Pod (io.fabric8.kubernetes.api.model.Pod)17 Service (io.fabric8.kubernetes.api.model.Service)16 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)14 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)13 IOException (java.io.IOException)12 ReplicationController (io.fabric8.kubernetes.api.model.ReplicationController)10 File (java.io.File)10 ArrayList (java.util.ArrayList)9 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)8 BuildConfig (io.fabric8.openshift.api.model.BuildConfig)8 Test (org.junit.Test)8 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)7 DeploymentConfig (io.fabric8.openshift.api.model.DeploymentConfig)7 Map (java.util.Map)7 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)7 Controller (io.fabric8.kubernetes.api.Controller)6