Search in sources :

Example 6 with ReplicationController

use of io.fabric8.kubernetes.api.model.ReplicationController in project camel by apache.

the class KubernetesReplicationControllersProducer method doScaleReplicationController.

protected void doScaleReplicationController(Exchange exchange, String operation) throws Exception {
    String rcName = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_REPLICATION_CONTROLLER_NAME, String.class);
    String namespaceName = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class);
    Integer replicasNumber = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_REPLICATION_CONTROLLER_REPLICAS, Integer.class);
    if (ObjectHelper.isEmpty(rcName)) {
        LOG.error("Scale a specific replication controller require specify a replication controller name");
        throw new IllegalArgumentException("Scale a specific replication controller require specify a replication controller name");
    }
    if (ObjectHelper.isEmpty(namespaceName)) {
        LOG.error("Scale a specific replication controller require specify a namespace name");
        throw new IllegalArgumentException("Scale a specific replication controller require specify a namespace name");
    }
    if (ObjectHelper.isEmpty(replicasNumber)) {
        LOG.error("Scale a specific replication controller require specify a replicas number");
        throw new IllegalArgumentException("Scale a specific replication controller require specify a replicas number");
    }
    ReplicationController rcScaled = getEndpoint().getKubernetesClient().replicationControllers().inNamespace(namespaceName).withName(rcName).scale(replicasNumber, true);
    MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true);
    exchange.getOut().setBody(rcScaled.getStatus().getReplicas());
}
Also used : DoneableReplicationController(io.fabric8.kubernetes.api.model.DoneableReplicationController) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController)

Example 7 with ReplicationController

use of io.fabric8.kubernetes.api.model.ReplicationController in project camel by apache.

the class KubernetesReplicationControllersProducer method doListReplicationControllersByLabels.

protected void doListReplicationControllersByLabels(Exchange exchange, String operation) throws Exception {
    ReplicationControllerList rcList = null;
    Map<String, String> labels = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_REPLICATION_CONTROLLERS_LABELS, Map.class);
    String namespaceName = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class);
    if (!ObjectHelper.isEmpty(namespaceName)) {
        NonNamespaceOperation<ReplicationController, ReplicationControllerList, DoneableReplicationController, RollableScallableResource<ReplicationController, DoneableReplicationController>> replicationControllers;
        replicationControllers = getEndpoint().getKubernetesClient().replicationControllers().inNamespace(namespaceName);
        for (Map.Entry<String, String> entry : labels.entrySet()) {
            replicationControllers.withLabel(entry.getKey(), entry.getValue());
        }
        rcList = replicationControllers.list();
    } else {
        MixedOperation<ReplicationController, ReplicationControllerList, DoneableReplicationController, RollableScallableResource<ReplicationController, DoneableReplicationController>> replicationControllers;
        replicationControllers = getEndpoint().getKubernetesClient().replicationControllers();
        for (Map.Entry<String, String> entry : labels.entrySet()) {
            replicationControllers.withLabel(entry.getKey(), entry.getValue());
        }
        rcList = replicationControllers.list();
    }
    MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true);
    exchange.getOut().setBody(rcList.getItems());
}
Also used : DoneableReplicationController(io.fabric8.kubernetes.api.model.DoneableReplicationController) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController) RollableScallableResource(io.fabric8.kubernetes.client.dsl.RollableScallableResource) Map(java.util.Map) ReplicationControllerList(io.fabric8.kubernetes.api.model.ReplicationControllerList) DoneableReplicationController(io.fabric8.kubernetes.api.model.DoneableReplicationController)

Example 8 with ReplicationController

use of io.fabric8.kubernetes.api.model.ReplicationController in project camel by apache.

the class KubernetesReplicationControllersProducer method doGetReplicationController.

protected void doGetReplicationController(Exchange exchange, String operation) throws Exception {
    ReplicationController rc = null;
    String rcName = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_REPLICATION_CONTROLLER_NAME, String.class);
    String namespaceName = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class);
    if (ObjectHelper.isEmpty(rcName)) {
        LOG.error("Get a specific replication controller require specify a replication controller name");
        throw new IllegalArgumentException("Get a specific replication controller require specify a replication controller name");
    }
    if (ObjectHelper.isEmpty(namespaceName)) {
        LOG.error("Get a specific replication controller require specify a namespace name");
        throw new IllegalArgumentException("Get a specific replication controller require specify a namespace name");
    }
    rc = getEndpoint().getKubernetesClient().replicationControllers().inNamespace(namespaceName).withName(rcName).get();
    MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true);
    exchange.getOut().setBody(rc);
}
Also used : DoneableReplicationController(io.fabric8.kubernetes.api.model.DoneableReplicationController) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController)

Example 9 with ReplicationController

use of io.fabric8.kubernetes.api.model.ReplicationController in project camel by apache.

the class KubernetesReplicationControllersProducer method doCreateReplicationController.

protected void doCreateReplicationController(Exchange exchange, String operation) throws Exception {
    ReplicationController rc = null;
    String rcName = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_REPLICATION_CONTROLLER_NAME, String.class);
    String namespaceName = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class);
    ReplicationControllerSpec rcSpec = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_REPLICATION_CONTROLLER_SPEC, ReplicationControllerSpec.class);
    if (ObjectHelper.isEmpty(rcName)) {
        LOG.error("Create a specific replication controller require specify a replication controller name");
        throw new IllegalArgumentException("Create a specific replication controller require specify a replication controller name");
    }
    if (ObjectHelper.isEmpty(namespaceName)) {
        LOG.error("Create a specific replication controller require specify a namespace name");
        throw new IllegalArgumentException("Create a specific replication controller require specify a namespace name");
    }
    if (ObjectHelper.isEmpty(rcSpec)) {
        LOG.error("Create a specific replication controller require specify a replication controller spec bean");
        throw new IllegalArgumentException("Create a specific replication controller require specify a replication controller spec bean");
    }
    Map<String, String> labels = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_REPLICATION_CONTROLLERS_LABELS, Map.class);
    ReplicationController rcCreating = new ReplicationControllerBuilder().withNewMetadata().withName(rcName).withLabels(labels).endMetadata().withSpec(rcSpec).build();
    rc = getEndpoint().getKubernetesClient().replicationControllers().inNamespace(namespaceName).create(rcCreating);
    MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true);
    exchange.getOut().setBody(rc);
}
Also used : ReplicationControllerBuilder(io.fabric8.kubernetes.api.model.ReplicationControllerBuilder) DoneableReplicationController(io.fabric8.kubernetes.api.model.DoneableReplicationController) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController) ReplicationControllerSpec(io.fabric8.kubernetes.api.model.ReplicationControllerSpec)

Example 10 with ReplicationController

use of io.fabric8.kubernetes.api.model.ReplicationController in project camel by apache.

the class KubernetesReplicationControllersConsumerTest method createAndDeleteReplicationController.

@Test
public void createAndDeleteReplicationController() throws Exception {
    if (ObjectHelper.isEmpty(authToken)) {
        return;
    }
    mockResultEndpoint.expectedHeaderValuesReceivedInAnyOrder(KubernetesConstants.KUBERNETES_EVENT_ACTION, "ADDED", "DELETED", "MODIFIED", "MODIFIED", "MODIFIED");
    Exchange ex = template.request("direct:createReplicationController", new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, "default");
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_REPLICATION_CONTROLLER_NAME, "test");
            Map<String, String> labels = new HashMap<String, String>();
            labels.put("this", "rocks");
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_REPLICATION_CONTROLLERS_LABELS, labels);
            ReplicationControllerSpec rcSpec = new ReplicationControllerSpec();
            rcSpec.setReplicas(2);
            PodTemplateSpecBuilder builder = new PodTemplateSpecBuilder();
            PodTemplateSpec t = builder.withNewMetadata().withName("nginx-template").addToLabels("server", "nginx").endMetadata().withNewSpec().addNewContainer().withName("wildfly").withImage("jboss/wildfly").addNewPort().withContainerPort(80).endPort().endContainer().endSpec().build();
            rcSpec.setTemplate(t);
            Map<String, String> selectorMap = new HashMap<String, String>();
            selectorMap.put("server", "nginx");
            rcSpec.setSelector(selectorMap);
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_REPLICATION_CONTROLLER_SPEC, rcSpec);
        }
    });
    ReplicationController rc = ex.getOut().getBody(ReplicationController.class);
    assertEquals(rc.getMetadata().getName(), "test");
    ex = template.request("direct:deleteReplicationController", new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, "default");
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_REPLICATION_CONTROLLER_NAME, "test");
        }
    });
    boolean rcDeleted = ex.getOut().getBody(Boolean.class);
    assertTrue(rcDeleted);
    Thread.sleep(3000);
    mockResultEndpoint.assertIsSatisfied();
}
Also used : Exchange(org.apache.camel.Exchange) PodTemplateSpec(io.fabric8.kubernetes.api.model.PodTemplateSpec) Processor(org.apache.camel.Processor) PodTemplateSpecBuilder(io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController) HashMap(java.util.HashMap) Map(java.util.Map) ReplicationControllerSpec(io.fabric8.kubernetes.api.model.ReplicationControllerSpec) Test(org.junit.Test)

Aggregations

ReplicationController (io.fabric8.kubernetes.api.model.ReplicationController)10 Test (org.junit.Test)6 Map (java.util.Map)5 Exchange (org.apache.camel.Exchange)5 Processor (org.apache.camel.Processor)5 DoneableReplicationController (io.fabric8.kubernetes.api.model.DoneableReplicationController)4 ReplicationControllerSpec (io.fabric8.kubernetes.api.model.ReplicationControllerSpec)4 HashMap (java.util.HashMap)4 PodTemplateSpec (io.fabric8.kubernetes.api.model.PodTemplateSpec)3 PodTemplateSpecBuilder (io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder)3 ReplicationControllerBuilder (io.fabric8.kubernetes.api.model.ReplicationControllerBuilder)1 ReplicationControllerList (io.fabric8.kubernetes.api.model.ReplicationControllerList)1 RollableScallableResource (io.fabric8.kubernetes.client.dsl.RollableScallableResource)1