Search in sources :

Example 56 with OpenShiftClient

use of io.fabric8.openshift.client.OpenShiftClient in project carbon-apimgt by wso2.

the class ServiceDiscovererKubernetesTestCase method testListServices.

@Test(description = "Test .listServices() method")
public void testListServices() throws Exception {
    OpenShiftClient openShiftClient = Mockito.mock(OpenShiftClient.class, Mockito.RETURNS_DEEP_STUBS);
    ServiceDiscovererKubernetes sdKubernetes = new ServiceDiscovererKubernetes();
    sdKubernetes.setClient(openShiftClient);
    // Include ClusterIPs
    sdKubernetes.setIncludeClusterIP(true);
    // Include ExternalNames
    sdKubernetes.setIncludeExternalNameTypeServices(true);
    NonNamespaceOperation nonNamespaceOperation = Mockito.mock(NonNamespaceOperation.class);
    Mockito.when(openShiftClient.services().inNamespace(null)).thenReturn(nonNamespaceOperation);
    Mockito.when(nonNamespaceOperation.list()).thenReturn(createServiceList());
    Mockito.when(openShiftClient.getMasterUrl()).thenReturn(new URL(MASTER_URL));
    List<Endpoint> endpoints = sdKubernetes.listServices();
    Assert.assertEquals(endpoints.size(), 10);
    // 2 NodePort URL endpoints
    Mockito.verify(openShiftClient, Mockito.times(2)).getMasterUrl();
}
Also used : Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) NonNamespaceOperation(io.fabric8.kubernetes.client.dsl.NonNamespaceOperation) URL(java.net.URL) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 57 with OpenShiftClient

use of io.fabric8.openshift.client.OpenShiftClient in project carbon-apimgt by wso2.

the class ServiceDiscovererKubernetesTestCase method testInitWhileExternalTokenFileNameGiven.

@Test(description = "Test init method with external service account token file name")
public void testInitWhileExternalTokenFileNameGiven() throws Exception {
    OpenShiftClient openShiftClient = Mockito.mock(OpenShiftClient.class);
    ServiceDiscovererKubernetes sdKubernetes = new ServiceDiscovererKubernetes();
    sdKubernetes.setClient(openShiftClient);
    try {
        sdKubernetes.initImpl(createImplParametersMap("TestK8Token"));
    } catch (ServiceDiscoveryException e) {
        Assert.assertEquals(e.getCause().getMessage(), "File to decrypt does not exist");
    }
}
Also used : OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) ServiceDiscoveryException(org.wso2.carbon.apimgt.core.exception.ServiceDiscoveryException) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 58 with OpenShiftClient

use of io.fabric8.openshift.client.OpenShiftClient in project vertx-openshift-it by cescoffier.

the class Deployment method deployIfNeeded.

public static String deployIfNeeded(KubernetesClient client, File input) {
    OpenShiftClient oc = oc(client);
    assertThat(input).isFile();
    try {
        byte[] bytes = Files.readBytes(input);
        try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes)) {
            DeploymentConfig deploymentConfig = oc.deploymentConfigs().load(bis).get();
            assertThat(deploymentConfig).isNotNull();
            if (oc.deploymentConfigs().withName(deploymentConfig.getMetadata().getName()).get() != null) {
                System.out.println("Skipping the creation of dc/" + deploymentConfig.getMetadata().getName());
                return deploymentConfig.getMetadata().getName();
            }
            oc.deploymentConfigs().create(deploymentConfig);
            OC.execute("deploy", deploymentConfig.getMetadata().getName(), "--latest", "-n", oc.getNamespace());
            return deploymentConfig.getMetadata().getName();
        }
    } catch (Exception e) {
        throw new RuntimeException("Unable to deploy deployment config " + input.getAbsolutePath(), e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) DeploymentConfig(io.fabric8.openshift.api.model.DeploymentConfig)

Example 59 with OpenShiftClient

use of io.fabric8.openshift.client.OpenShiftClient in project vertx-openshift-it by cescoffier.

the class Kube method setReplicasAndWait.

public static DeploymentConfig setReplicasAndWait(KubernetesClient client, String name, int number) {
    OpenShiftClient oc = oc(client);
    DeploymentConfig config = oc.deploymentConfigs().withName(name).get();
    if (config == null) {
        fail("Unable to find the deployment config " + name);
        return null;
    }
    if (config.getSpec().getReplicas() == number) {
        return config;
    }
    config = oc.deploymentConfigs().withName(name).edit().editSpec().withReplicas(number).endSpec().done();
    if (number == 0) {
        // Wait until no pods
        await().atMost(duration()).until(() -> getPodsForDeploymentConfig(client, name).size() == 0);
    } else {
        // Wait until the right number of pods
        await().atMost(duration()).until(() -> getPodsForDeploymentConfig(client, name).size() == number);
        // Wait for readiness
        await().atMost(duration()).until(() -> getPodsForDeploymentConfig(client, name).stream().filter(KubernetesHelper::isPodReady).count() == number);
    }
    return config;
}
Also used : OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) DeploymentConfig(io.fabric8.openshift.api.model.DeploymentConfig)

Example 60 with OpenShiftClient

use of io.fabric8.openshift.client.OpenShiftClient in project fabric8-maven-plugin by fabric8io.

the class AbstractFabric8Mojo method getOpenShiftClientOrJenkinsShift.

protected OpenShiftClient getOpenShiftClientOrJenkinsShift(KubernetesClient kubernetes, String namespace) throws MojoExecutionException {
    OpenShiftClient openShiftClient = getOpenShiftClientOrNull(kubernetes);
    if (openShiftClient == null) {
        String jenkinshiftUrl = getJenkinShiftUrl(kubernetes, namespace);
        log.debug("Using jenkinshift URL: " + jenkinshiftUrl);
        if (jenkinshiftUrl == null) {
            throw new MojoExecutionException("Could not find the service `" + ServiceNames.JENKINSHIFT + "` im namespace `" + namespace + "` on this kubernetes cluster " + kubernetes.getMasterUrl());
        }
        return KubernetesHelper.createJenkinshiftOpenShiftClient(jenkinshiftUrl);
    }
    return openShiftClient;
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient)

Aggregations

OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)65 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)18 IOException (java.io.IOException)18 Test (org.junit.Test)17 NonNamespaceOperation (io.fabric8.kubernetes.client.dsl.NonNamespaceOperation)15 DefaultOpenShiftClient (io.fabric8.openshift.client.DefaultOpenShiftClient)14 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)12 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)12 API (org.wso2.carbon.apimgt.core.models.API)12 FileNotFoundException (java.io.FileNotFoundException)11 Service (io.fabric8.kubernetes.api.model.Service)10 DeploymentConfig (io.fabric8.openshift.api.model.DeploymentConfig)10 OpenShiftNotAvailableException (io.fabric8.openshift.client.OpenShiftNotAvailableException)10 BeforeTest (org.testng.annotations.BeforeTest)9 Test (org.testng.annotations.Test)9 Controller (io.fabric8.kubernetes.api.Controller)8 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)8 BuildConfig (io.fabric8.openshift.api.model.BuildConfig)8 Route (io.fabric8.openshift.api.model.Route)8 JSONObject (org.json.JSONObject)8