Search in sources :

Example 36 with KubernetesClient

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

the class WatchServicesExample method main.

public static void main(String... args) throws Exception {
    KubernetesClient client = new DefaultKubernetesClient();
    client.services().watch(new io.fabric8.kubernetes.client.Watcher<Service>() {

        @Override
        public void eventReceived(Action action, Service service) {
            System.out.println(action + ": " + service);
        }

        @Override
        public void onClose(KubernetesClientException e) {
            System.out.println("Closed: " + e);
        }
    });
    client.close();
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) Service(io.fabric8.kubernetes.api.model.Service) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 37 with KubernetesClient

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

the class ConfigurationTest method testEnvironmentKeyButNoConfigMap.

@Test
public void testEnvironmentKeyButNoConfigMap() {
    String devNamespace = "myproject";
    String environmentKey = "testing";
    String testNamespace = devNamespace;
    Map<String, String> data = new HashMap<>();
    data.put("staging", "    name: Staging\n" + "    namespace: myproject-staging\n" + "    order: 0");
    server.expect().withPath("/api/v1/namespaces/" + devNamespace + "/configmaps/fabric8-environments").andReturn(404, "Not found").once();
    Map<String, String> map = new HashMap<>();
    map.put(FABRIC8_ENVIRONMENT, environmentKey);
    map.put(DEVELOPMENT_NAMESPACE, devNamespace);
    KubernetesClient kubernetesClient = getKubernetesClient();
    Config config = new Config();
    config.setNamespace(devNamespace);
    config.setMasterUrl(kubernetesClient.getMasterUrl().toString());
    DefaultKubernetesClient clientWithDefaultNamespace = new DefaultKubernetesClient(config);
    Configuration configuration = Configuration.fromMap(map, clientWithDefaultNamespace);
    assertEquals(testNamespace, configuration.getNamespace());
    assertTrue(configuration.isAnsiLoggerEnabled());
    assertTrue(configuration.isEnvironmentInitEnabled());
    assertTrue(configuration.isNamespaceLazyCreateEnabled());
    assertFalse(configuration.isNamespaceCleanupEnabled());
    assertFalse(configuration.isCreateNamespaceForTest());
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) HashMap(java.util.HashMap) Config(io.fabric8.kubernetes.client.Config) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) Test(org.junit.Test)

Example 38 with KubernetesClient

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

the class ConfigurationTest method testNamespaceNotFoundFromConfigMap.

@Ignore
public void testNamespaceNotFoundFromConfigMap() {
    String devNamespace = "myproject";
    String environmentKey = "testing";
    String testNamespace = devNamespace;
    Map<String, String> data = new HashMap<>();
    data.put("staging", "    name: Staging\n" + "    namespace: myproject-staging\n" + "    order: 0");
    server.expect().withPath("/api/v1/namespaces/" + devNamespace + "/configmaps/fabric8-environments").andReturn(200, new ConfigMapBuilder().withNewMetadata().withName("fabric8-environments").endMetadata().withData(data).build()).once();
    Map<String, String> map = new HashMap<>();
    map.put(FABRIC8_ENVIRONMENT, environmentKey);
    map.put(DEVELOPMENT_NAMESPACE, devNamespace);
    Configuration configuration = Configuration.fromMap(map, kubernetesClient);
    assertEquals(testNamespace, configuration.getNamespace());
    assertTrue(configuration.isAnsiLoggerEnabled());
    assertTrue(configuration.isEnvironmentInitEnabled());
    assertTrue(configuration.isNamespaceLazyCreateEnabled());
    assertFalse(configuration.isNamespaceCleanupEnabled());
    assertFalse(configuration.isCreateNamespaceForTest());
}
Also used : HashMap(java.util.HashMap) ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder) Ignore(org.junit.Ignore)

Example 39 with KubernetesClient

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

the class KubernetesHolder method getClient.

public static synchronized KubernetesClient getClient() {
    if (client != null) {
        return client;
    }
    BeanManager beanManager = getBeanManager();
    if (beanManager != null) {
        Set<Bean<?>> beans = beanManager.getBeans(KubernetesClient.class);
        if (beans.isEmpty()) {
            throw new IllegalStateException("Could not find client beans!");
        } else {
            CreationalContext ctx = beanManager.createCreationalContext(null);
            client = (KubernetesClient) beanManager.getReference(beans.iterator().next(), KubernetesClient.class, ctx);
        }
    } else {
        client = new DefaultKubernetesClient();
    }
    return client;
}
Also used : CreationalContext(javax.enterprise.context.spi.CreationalContext) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) BeanManager(javax.enterprise.inject.spi.BeanManager) Bean(javax.enterprise.inject.spi.Bean)

Example 40 with KubernetesClient

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

the class SessionListener method generateSecrets.

private Set<Secret> generateSecrets(KubernetesClient client, Session session, ObjectMeta meta) {
    Set<Secret> secrets = new HashSet<>();
    Map<String, String> annotations = meta.getAnnotations();
    if (annotations != null && !annotations.isEmpty()) {
        for (Map.Entry<String, String> entry : annotations.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            if (SecretKeys.isSecretKey(key)) {
                SecretKeys keyType = SecretKeys.fromValue(key);
                for (String name : Secrets.getNames(value)) {
                    Map<String, String> data = new HashMap<>();
                    Secret secret = null;
                    try {
                        secret = client.secrets().inNamespace(session.getNamespace()).withName(name).get();
                    } catch (Exception e) {
                    // ignore - probably doesn't exist
                    }
                    if (secret == null) {
                        for (String c : Secrets.getContents(value, name)) {
                            data.put(c, keyType.generate());
                        }
                        secret = client.secrets().inNamespace(session.getNamespace()).createNew().withNewMetadata().withName(name).endMetadata().withData(data).done();
                        secrets.add(secret);
                    }
                }
            }
        }
    }
    return secrets;
}
Also used : Util.readAsString(io.fabric8.arquillian.utils.Util.readAsString) SecretKeys(io.fabric8.arquillian.utils.SecretKeys) MultiException(io.fabric8.utils.MultiException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException)

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