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));
}
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);
}
}
}
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);
}
}
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();
}
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);
}
Aggregations