use of io.fabric8.kubernetes.api.model.apps.Deployment in project iobserve-analysis by research-iobserve.
the class DeploymentExecutor method execute.
@Override
public void execute(final DeployComponentAction action) {
// Can't be loaded earlier because it references the other models received via TCP
final CorrespondenceModel correspondenceModel = new FileModelHandler<CorrespondenceModel>(this.resourceSet, CorrespondencePackage.eINSTANCE).load(URI.createFileURI(this.correspondenceModelFile.getAbsolutePath()));
final KubernetesClient client = new DefaultKubernetesClient();
final String rcName = this.normalizeComponentName(action.getTargetAllocationContext().getResourceContainer_AllocationContext().getEntityName());
final Deployment deployment = client.extensions().deployments().inNamespace(this.namespace).withName(rcName).get();
final AssemblyContext targetAssemblyContext = action.getTargetAllocationContext().getAssemblyContext_AllocationContext();
final String imageName = this.getImageName(correspondenceModel, targetAssemblyContext);
if (deployment != null) {
// Increase number of replicas if pod is already deployed...
final int replicas = deployment.getSpec().getReplicas();
deployment.getSpec().setReplicas(replicas + 1);
client.extensions().deployments().inNamespace(this.namespace).withName(rcName).replace(deployment);
if (DeploymentExecutor.LOGGER.isDebugEnabled()) {
DeploymentExecutor.LOGGER.debug("Scaled pod deployment of " + deployment.getMetadata().getName() + " to " + (replicas + 1));
}
} else {
// ... deploy new pod if this is not the case
final Deployment newDeployment = this.podsToDeploy.get(rcName);
final String imageLocator = newDeployment.getSpec().getTemplate().getSpec().getContainers().get(0).getImage();
newDeployment.getSpec().getTemplate().getSpec().getContainers().get(0).setImage(imageLocator + "/" + imageName);
newDeployment.getSpec().getTemplate().getSpec().getContainers().get(0).setName(this.normalizeComponentName(targetAssemblyContext.getEntityName()));
client.extensions().deployments().inNamespace(this.namespace).create(newDeployment);
if (DeploymentExecutor.LOGGER.isDebugEnabled()) {
DeploymentExecutor.LOGGER.debug("Image set to " + imageLocator + "/" + imageName);
DeploymentExecutor.LOGGER.debug("Created new pod deployment " + newDeployment.getMetadata().getName());
}
}
// Wait until deployed instance is ready
while (!client.extensions().deployments().inNamespace(this.namespace).withName(rcName).isReady()) {
if (DeploymentExecutor.LOGGER.isDebugEnabled()) {
DeploymentExecutor.LOGGER.debug(rcName + " is not ready yet.");
}
}
client.close();
}
use of io.fabric8.kubernetes.api.model.apps.Deployment in project iobserve-analysis by research-iobserve.
the class DeallocationExecutor method execute.
@Override
public void execute(final DeallocateNodeAction action) {
final KubernetesClient client = new DefaultKubernetesClient();
final String rcName = this.normalizeComponentName(action.getTargetResourceContainer().getEntityName());
client.extensions().deployments().inNamespace(this.namespace).withName(rcName).delete();
client.close();
if (DeallocationExecutor.LOGGER.isDebugEnabled()) {
DeallocationExecutor.LOGGER.debug("Successfully deleted pod deployment with name " + rcName);
}
}
use of io.fabric8.kubernetes.api.model.apps.Deployment in project iobserve-analysis by research-iobserve.
the class UndeploymentExecutor method execute.
@Override
public void execute(final UndeployComponentAction action) {
final KubernetesClient client = new DefaultKubernetesClient();
final String rcName = this.normalizeComponentName(action.getTargetAllocationContext().getResourceContainer_AllocationContext().getEntityName());
final Deployment deployment = client.extensions().deployments().inNamespace(this.namespace).withName(rcName).get();
final int replicas = deployment.getSpec().getReplicas();
// deployment is deleted in the DeallocationExecutor.
if (replicas > 0) {
deployment.getSpec().setReplicas(replicas - 1);
client.extensions().deployments().inNamespace(this.namespace).withName(rcName).replace(deployment);
}
client.close();
if (UndeploymentExecutor.LOGGER.isDebugEnabled()) {
UndeploymentExecutor.LOGGER.debug("Scaled pod deployment of " + deployment.getMetadata().getName() + " to " + (replicas - 1));
}
}
use of io.fabric8.kubernetes.api.model.apps.Deployment in project fabric8-maven-plugin by fabric8io.
the class DefaultControllerEnricher method addMissingResources.
@Override
public void addMissingResources(KubernetesListBuilder builder) {
final String name = getConfig(Config.name, MavenUtil.createDefaultResourceName(getProject()));
final ResourceConfig config = new ResourceConfig.Builder().controllerName(name).imagePullPolicy(getConfig(Config.pullPolicy)).withReplicas(Configs.asInt(getConfig(Config.replicaCount))).build();
final List<ImageConfiguration> images = getImages();
// Check if at least a replica set is added. If not add a default one
if (!KubernetesResourceUtil.checkForKind(builder, POD_CONTROLLER_KINDS)) {
// At least one image must be present, otherwise the resulting config will be invalid
if (!Lists.isNullOrEmpty(images)) {
String type = getConfig(Config.type);
if ("deployment".equalsIgnoreCase(type)) {
log.info("Adding a default Deployment");
builder.addToDeploymentItems(deployHandler.getDeployment(config, images));
} else if ("statefulSet".equalsIgnoreCase(type)) {
log.info("Adding a default StatefulSet");
builder.addToStatefulSetItems(statefulSetHandler.getStatefulSet(config, images));
} else if ("daemonSet".equalsIgnoreCase(type)) {
log.info("Adding a default DaemonSet");
builder.addToDaemonSetItems(daemonSetHandler.getDaemonSet(config, images));
} else if ("replicaSet".equalsIgnoreCase(type)) {
log.info("Adding a default ReplicaSet");
builder.addToReplicaSetItems(rsHandler.getReplicaSet(config, images));
} else if ("replicationController".equalsIgnoreCase(type)) {
log.info("Adding a default ReplicationController");
builder.addToReplicationControllerItems(rcHandler.getReplicationController(config, images));
} else if ("job".equalsIgnoreCase(type)) {
log.info("Adding a default Job");
builder.addToJobItems(jobHandler.getJob(config, images));
}
}
} else if (KubernetesResourceUtil.checkForKind(builder, "StatefulSet")) {
final StatefulSetSpec spec = statefulSetHandler.getStatefulSet(config, images).getSpec();
if (spec != null) {
builder.accept(new TypedVisitor<StatefulSetBuilder>() {
@Override
public void visit(StatefulSetBuilder statefulSetBuilder) {
statefulSetBuilder.editOrNewSpec().editOrNewTemplate().editOrNewSpec().endSpec().endTemplate().endSpec();
mergeStatefulSetSpec(statefulSetBuilder, spec);
}
});
if (spec.getTemplate() != null && spec.getTemplate().getSpec() != null) {
final PodSpec podSpec = spec.getTemplate().getSpec();
builder.accept(new TypedVisitor<PodSpecBuilder>() {
@Override
public void visit(PodSpecBuilder builder) {
KubernetesResourceUtil.mergePodSpec(builder, podSpec, name);
}
});
}
}
} else {
final DeploymentSpec spec = deployHandler.getDeployment(config, images).getSpec();
if (spec != null) {
builder.accept(new TypedVisitor<DeploymentBuilder>() {
@Override
public void visit(DeploymentBuilder deploymentBuilder) {
deploymentBuilder.editOrNewSpec().editOrNewTemplate().editOrNewSpec().endSpec().endTemplate().endSpec();
mergeDeploymentSpec(deploymentBuilder, spec);
}
});
if (spec.getTemplate() != null && spec.getTemplate().getSpec() != null) {
final PodSpec podSpec = spec.getTemplate().getSpec();
builder.accept(new TypedVisitor<PodSpecBuilder>() {
@Override
public void visit(PodSpecBuilder builder) {
KubernetesResourceUtil.mergePodSpec(builder, podSpec, name);
}
});
}
}
}
}
use of io.fabric8.kubernetes.api.model.apps.Deployment in project fabric8-maven-plugin by fabric8io.
the class MergeResourceTest method testMergeDeploymentTemplateMetadata.
@Test
public void testMergeDeploymentTemplateMetadata() throws Exception {
Deployment resource = new DeploymentBuilder().withNewMetadata().withName("cheese").endMetadata().withNewSpec().withNewTemplate().withNewSpec().addNewContainer().withImage("cheese-image").endContainer().endSpec().withNewMetadata().addToAnnotations("overwriteKey", "originalValue").addToAnnotations("unchangedKey", "shouldNotChange").addToAnnotations("unchangedBlankKey", "").addToAnnotations("deletedKey", "shouldBeDeleted").endMetadata().endTemplate().endSpec().build();
Deployment override = new DeploymentBuilder().withNewMetadata().withName("cheese").endMetadata().withNewSpec().withNewTemplate().withNewSpec().addNewContainer().addToEnv(new EnvVarBuilder().withName("ENV_FOO").withValue("FOO_VALUE").build()).endContainer().endSpec().withNewMetadata().addToAnnotations("overwriteKey", "newValue").addToAnnotations("deletedKey", "").endMetadata().endTemplate().endSpec().build();
HasMetadata answer = KubernetesResourceUtil.mergeResources(resource, override, log, false);
assertNotNull(answer);
log.info("Override metadata on Deployment generated: " + KubernetesHelper.toYaml(answer));
assertThat(answer).describedAs("mergeResult").isInstanceOf(Deployment.class);
Deployment deployment = (Deployment) answer;
Map<String, String> annotations = deployment.getSpec().getTemplate().getMetadata().getAnnotations();
assertDataModified(annotations, "Deployment.spec.template.metadata.annotations");
assertDataNotModified(resource.getSpec().getTemplate().getMetadata().getAnnotations(), "Original Deployment.spec.template.metadata.annotations");
}
Aggregations