Search in sources :

Example 6 with ReplicationControllerSpec

use of io.fabric8.kubernetes.api.model.ReplicationControllerSpec in project fabric8 by fabric8io.

the class ReplicationControllerPodsAssert method pods.

public PodSelectionAssert pods() {
    spec().isNotNull().selector().isNotNull();
    ReplicationControllerSpec spec = this.actual.getSpec();
    Integer replicas = spec.getReplicas();
    Map<String, String> matchLabels = spec.getSelector();
    List<LabelSelectorRequirement> matchExpressions = new ArrayList<>();
    return new PodSelectionAssert(client, replicas, matchLabels, matchExpressions, "ReplicationController " + KubernetesHelper.getName(actual));
}
Also used : ArrayList(java.util.ArrayList) LabelSelectorRequirement(io.fabric8.kubernetes.api.model.LabelSelectorRequirement) ReplicationControllerSpec(io.fabric8.kubernetes.api.model.ReplicationControllerSpec)

Example 7 with ReplicationControllerSpec

use of io.fabric8.kubernetes.api.model.ReplicationControllerSpec in project fabric8 by fabric8io.

the class Controller method applyReplicationController.

public void applyReplicationController(ReplicationController replicationController, String sourceName) throws Exception {
    String namespace = getNamespace();
    String id = getName(replicationController);
    Objects.notNull(id, "No name for " + replicationController + " " + sourceName);
    if (isServicesOnlyMode()) {
        LOG.debug("Only processing Services right now so ignoring ReplicationController: " + namespace + ":" + id);
        return;
    }
    ReplicationController old = kubernetesClient.replicationControllers().inNamespace(namespace).withName(id).get();
    if (isRunning(old)) {
        if (UserConfigurationCompare.configEqual(replicationController, old)) {
            LOG.info("ReplicationController has not changed so not doing anything");
        } else {
            ReplicationControllerSpec newSpec = replicationController.getSpec();
            ReplicationControllerSpec oldSpec = old.getSpec();
            if (rollingUpgrade) {
                LOG.info("Rolling upgrade of the ReplicationController: " + namespace + "/" + id);
                // lets preserve the number of replicas currently running in the environment we are about to upgrade
                if (rollingUpgradePreserveScale && newSpec != null && oldSpec != null) {
                    Integer replicas = oldSpec.getReplicas();
                    if (replicas != null) {
                        newSpec.setReplicas(replicas);
                    }
                }
                LOG.info("rollingUpgradePreserveScale " + rollingUpgradePreserveScale + " new replicas is " + (newSpec != null ? newSpec.getReplicas() : "<null>"));
                kubernetesClient.replicationControllers().inNamespace(namespace).withName(id).rolling().replace(replicationController);
            } else if (isRecreateMode()) {
                LOG.info("Deleting ReplicationController: " + id);
                kubernetesClient.replicationControllers().inNamespace(namespace).withName(id).delete();
                doCreateReplicationController(replicationController, namespace, sourceName);
            } else {
                LOG.info("Updating ReplicationController from " + sourceName + " namespace " + namespace + " name " + getName(replicationController));
                try {
                    Object answer = kubernetesClient.replicationControllers().inNamespace(namespace).withName(id).replace(replicationController);
                    logGeneratedEntity("Updated replicationController: ", namespace, replicationController, answer);
                    if (deletePodsOnReplicationControllerUpdate) {
                        kubernetesClient.pods().inNamespace(namespace).withLabels(newSpec.getSelector()).delete();
                        LOG.info("Deleting any pods for the replication controller to ensure they use the new configuration");
                    } else {
                        LOG.info("Warning not deleted any pods so they could well be running with the old configuration!");
                    }
                } catch (Exception e) {
                    onApplyError("Failed to update ReplicationController from " + sourceName + ". " + e + ". " + replicationController, e);
                }
            }
        }
    } else {
        if (!isAllowCreate()) {
            LOG.warn("Creation disabled so not creating a ReplicationController from " + sourceName + " namespace " + namespace + " name " + getName(replicationController));
        } else {
            doCreateReplicationController(replicationController, namespace, sourceName);
        }
    }
}
Also used : ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController) JSONObject(org.json.JSONObject) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) FileNotFoundException(java.io.FileNotFoundException) OpenShiftNotAvailableException(io.fabric8.openshift.client.OpenShiftNotAvailableException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) ReplicationControllerSpec(io.fabric8.kubernetes.api.model.ReplicationControllerSpec)

Example 8 with ReplicationControllerSpec

use of io.fabric8.kubernetes.api.model.ReplicationControllerSpec in project fabric8 by fabric8io.

the class Controller method doCreateReplicationController.

protected void doCreateReplicationController(ReplicationController replicationController, String namespace, String sourceName) {
    LOG.info("Creating a ReplicationController from " + sourceName + " namespace " + namespace + " name " + getName(replicationController));
    try {
        // lets check that if secrets are required they exist
        ReplicationControllerSpec spec = replicationController.getSpec();
        if (spec != null) {
            PodTemplateSpec template = spec.getTemplate();
            if (template != null) {
                PodSpec podSpec = template.getSpec();
                validatePodSpec(podSpec, namespace);
            }
        }
        Object answer;
        if (Strings.isNotBlank(namespace)) {
            answer = kubernetesClient.replicationControllers().inNamespace(namespace).create(replicationController);
        } else {
            answer = kubernetesClient.replicationControllers().inNamespace(getNamespace()).create(replicationController);
        }
        logGeneratedEntity("Created ReplicationController: ", namespace, replicationController, answer);
    } catch (Exception e) {
        onApplyError("Failed to create ReplicationController from " + sourceName + ". " + e + ". " + replicationController, e);
    }
}
Also used : PodTemplateSpec(io.fabric8.kubernetes.api.model.PodTemplateSpec) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) JSONObject(org.json.JSONObject) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) FileNotFoundException(java.io.FileNotFoundException) OpenShiftNotAvailableException(io.fabric8.openshift.client.OpenShiftNotAvailableException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) ReplicationControllerSpec(io.fabric8.kubernetes.api.model.ReplicationControllerSpec)

Example 9 with ReplicationControllerSpec

use of io.fabric8.kubernetes.api.model.ReplicationControllerSpec 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)

Example 10 with ReplicationControllerSpec

use of io.fabric8.kubernetes.api.model.ReplicationControllerSpec 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)

Aggregations

ReplicationControllerSpec (io.fabric8.kubernetes.api.model.ReplicationControllerSpec)12 ReplicationController (io.fabric8.kubernetes.api.model.ReplicationController)10 PodTemplateSpec (io.fabric8.kubernetes.api.model.PodTemplateSpec)4 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)4 DeploymentSpec (io.fabric8.kubernetes.api.model.extensions.DeploymentSpec)4 ReplicaSet (io.fabric8.kubernetes.api.model.extensions.ReplicaSet)4 ReplicaSetSpec (io.fabric8.kubernetes.api.model.extensions.ReplicaSetSpec)4 DeploymentConfig (io.fabric8.openshift.api.model.DeploymentConfig)4 DeploymentConfigSpec (io.fabric8.openshift.api.model.DeploymentConfigSpec)4 PodTemplateSpecBuilder (io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Exchange (org.apache.camel.Exchange)3 Processor (org.apache.camel.Processor)3 Test (org.junit.Test)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 Controller (io.fabric8.kubernetes.api.Controller)2 LabelSelector (io.fabric8.kubernetes.api.model.LabelSelector)2 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)2 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)2