Search in sources :

Example 46 with Controller

use of io.fabric8.kubernetes.api.Controller in project strimzi by strimzi.

the class ControllerTest method testOnTopicChanged.

/**
 * 0. ZK notifies of a change in topic config
 * 1. controller gets updated topic metadata
 * 2. controller updates k8s and topic store.
 */
@Test
public void testOnTopicChanged(TestContext context) {
    Topic kubeTopic = new Topic.Builder(topicName.toString(), 10, (short) 2, map("cleanup.policy", "bar")).build();
    Topic kafkaTopic = new Topic.Builder(topicName.toString(), 10, (short) 2, map("cleanup.policy", "baz")).build();
    Topic privateTopic = kubeTopic;
    ConfigMap cm = TopicSerialization.toConfigMap(kubeTopic, cmPredicate);
    mockKafka.setCreateTopicResponse(topicName.toString(), null).createTopic(kafkaTopic, ar -> {
    });
    mockKafka.setTopicMetadataResponse(topicName, Utils.getTopicMetadata(kafkaTopic), null);
    // mockKafka.setUpdateTopicResponse(topicName -> Future.succeededFuture());
    mockTopicStore.setCreateTopicResponse(topicName, null).create(privateTopic, ar -> {
    });
    mockTopicStore.setUpdateTopicResponse(topicName, null);
    mockK8s.setCreateResponse(mapName, null).createConfigMap(cm, ar -> {
    });
    mockK8s.setModifyResponse(mapName, null);
    Async async = context.async(3);
    controller.onTopicConfigChanged(topicName, ar -> {
        assertSucceeded(context, ar);
        context.assertEquals("baz", mockKafka.getTopicState(topicName).getConfig().get("cleanup.policy"));
        mockTopicStore.read(topicName, ar2 -> {
            assertSucceeded(context, ar2);
            context.assertEquals("baz", ar2.result().getConfig().get("cleanup.policy"));
            async.countDown();
        });
        mockK8s.getFromName(mapName, ar2 -> {
            assertSucceeded(context, ar2);
            context.assertEquals("baz", TopicSerialization.fromConfigMap(ar2.result()).getConfig().get("cleanup.policy"));
            async.countDown();
        });
        async.countDown();
    });
}
Also used : ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) Async(io.vertx.ext.unit.Async) Test(org.junit.Test)

Example 47 with Controller

use of io.fabric8.kubernetes.api.Controller in project fabric8-maven-plugin by fabric8io.

the class ApplyMojo method applyEntities.

protected void applyEntities(Controller controller, KubernetesClient kubernetes, String namespace, String fileName, Set<HasMetadata> entities) throws Exception {
    // Apply all items
    for (HasMetadata entity : entities) {
        if (entity instanceof Pod) {
            Pod pod = (Pod) entity;
            controller.applyPod(pod, fileName);
        } else if (entity instanceof Service) {
            Service service = (Service) entity;
            controller.applyService(service, fileName);
        } else if (entity instanceof ReplicationController) {
            ReplicationController replicationController = (ReplicationController) entity;
            controller.applyReplicationController(replicationController, fileName);
        } else if (entity != null) {
            controller.apply(entity, fileName);
        }
    }
    String command = clusterAccess.isOpenShiftImageStream(log) ? "oc" : "kubectl";
    log.info("[[B]]HINT:[[B]] Use the command `%s get pods -w` to watch your pods start up", command);
    Logger serviceLogger = createExternalProcessLogger("[[G]][SVC][[G]] ");
    long serviceUrlWaitTimeSeconds = this.serviceUrlWaitTimeSeconds;
    for (HasMetadata entity : entities) {
        if (entity instanceof Service) {
            Service service = (Service) entity;
            String name = getName(service);
            Resource<Service, DoneableService> serviceResource = kubernetes.services().inNamespace(namespace).withName(name);
            String url = null;
            // lets wait a little while until there is a service URL in case the exposecontroller is running slow
            for (int i = 0; i < serviceUrlWaitTimeSeconds; i++) {
                if (i > 0) {
                    Thread.sleep(1000);
                }
                Service s = serviceResource.get();
                if (s != null) {
                    url = getExternalServiceURL(s);
                    if (Strings.isNotBlank(url)) {
                        break;
                    }
                }
                if (!isExposeService(service)) {
                    break;
                }
            }
            // lets not wait for other services
            serviceUrlWaitTimeSeconds = 1;
            if (Strings.isNotBlank(url) && url.startsWith("http")) {
                serviceLogger.info("" + name + ": " + url);
            }
        }
    }
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Pod(io.fabric8.kubernetes.api.model.Pod) DoneableService(io.fabric8.kubernetes.api.model.DoneableService) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController) DoneableService(io.fabric8.kubernetes.api.model.DoneableService) Service(io.fabric8.kubernetes.api.model.Service) KubernetesHelper.createIntOrString(io.fabric8.kubernetes.api.KubernetesHelper.createIntOrString) Logger(io.fabric8.maven.docker.util.Logger)

Example 48 with Controller

use of io.fabric8.kubernetes.api.Controller in project fabric8-maven-plugin by fabric8io.

the class ApplyMojo method createController.

protected Controller createController() {
    Controller controller = new Controller(clusterAccess.createDefaultClient(log));
    controller.setThrowExceptionOnError(failOnError);
    controller.setRecreateMode(recreate);
    getLog().debug("Using recreate mode: " + recreate);
    return controller;
}
Also used : Controller(io.fabric8.kubernetes.api.Controller) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController)

Example 49 with Controller

use of io.fabric8.kubernetes.api.Controller in project fabric8-maven-plugin by fabric8io.

the class ApplyMojo method createIngress.

protected void createIngress(Controller controller, KubernetesClient kubernetesClient, Collection<HasMetadata> collection) {
    String routeDomainPostfix = this.routeDomain;
    Log log = getLog();
    String namespace = clusterAccess.getNamespace();
    List<Ingress> ingressList = null;
    // lets get the routes first to see if we should bother
    try {
        IngressList ingresses = kubernetesClient.extensions().ingresses().inNamespace(namespace).list();
        if (ingresses != null) {
            ingressList = ingresses.getItems();
        }
    } catch (Exception e) {
        log.warn("Cannot load Ingress instances. Must be an older version of Kubernetes? Error: " + e, e);
        return;
    }
    List<Ingress> ingresses = new ArrayList<>();
    for (Object object : collection) {
        if (object instanceof Service) {
            Service service = (Service) object;
            if (!serviceHasIngressRule(ingressList, service)) {
                Ingress ingress = createIngressForService(routeDomainPostfix, namespace, service);
                if (ingress != null) {
                    ingresses.add(ingress);
                    log.info("Created ingress for " + namespace + ":" + KubernetesHelper.getName(service));
                } else {
                    log.debug("No ingress required for " + namespace + ":" + KubernetesHelper.getName(service));
                }
            } else {
                log.info("Already has ingress for service " + namespace + ":" + KubernetesHelper.getName(service));
            }
        }
    }
    collection.addAll(ingresses);
}
Also used : Log(org.apache.maven.plugin.logging.Log) ArrayList(java.util.ArrayList) Ingress(io.fabric8.kubernetes.api.model.extensions.Ingress) DoneableService(io.fabric8.kubernetes.api.model.DoneableService) Service(io.fabric8.kubernetes.api.model.Service) KubernetesHelper.createIntOrString(io.fabric8.kubernetes.api.KubernetesHelper.createIntOrString) IngressList(io.fabric8.kubernetes.api.model.extensions.IngressList) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException)

Example 50 with Controller

use of io.fabric8.kubernetes.api.Controller in project fabric8-maven-plugin by fabric8io.

the class DebugMojo method portForward.

private void portForward(Controller controller, String podName) throws MojoExecutionException {
    try {
        getFabric8ServiceHub(controller).getPortForwardService().forwardPort(createExternalProcessLogger("[[B]]port-forward[[B]] "), podName, portToInt(remoteDebugPort, "remoteDebugPort"), portToInt(localDebugPort, "localDebugPort"));
        log.info("");
        log.info("Now you can start a Remote debug execution in your IDE by using localhost and the debug port " + localDebugPort);
        log.info("");
    } catch (Fabric8ServiceException e) {
        throw new MojoExecutionException("Failed to start port forwarding" + e, e);
    }
}
Also used : Fabric8ServiceException(io.fabric8.maven.core.service.Fabric8ServiceException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException)

Aggregations

Test (org.junit.Test)23 ReplicationController (io.fabric8.kubernetes.api.model.ReplicationController)16 Controller (io.fabric8.kubernetes.api.Controller)13 ResourceConfig (io.fabric8.maven.core.config.ResourceConfig)12 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)12 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)9 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)8 DefaultKubernetesClient (io.fabric8.kubernetes.client.DefaultKubernetesClient)8 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)8 Async (io.vertx.ext.unit.Async)8 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)7 Service (io.fabric8.kubernetes.api.model.Service)6 File (java.io.File)6 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)5 ReplicaSet (io.fabric8.kubernetes.api.model.extensions.ReplicaSet)5 DeploymentConfig (io.fabric8.openshift.api.model.DeploymentConfig)5 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)4