use of io.fabric8.kubernetes.api.model.extensions.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");
}
use of io.fabric8.kubernetes.api.model.extensions.Deployment in project fabric8-maven-plugin by fabric8io.
the class KubernetesClientUtil method resizeApp.
public static void resizeApp(KubernetesClient kubernetes, String namespace, Set<HasMetadata> entities, int replicas, Logger log) {
for (HasMetadata entity : entities) {
String name = getName(entity);
Scaleable<?> scalable = null;
if (entity instanceof Deployment) {
scalable = kubernetes.extensions().deployments().inNamespace(namespace).withName(name);
} else if (entity instanceof ReplicaSet) {
scalable = kubernetes.extensions().replicaSets().inNamespace(namespace).withName(name);
} else if (entity instanceof ReplicationController) {
scalable = kubernetes.replicationControllers().inNamespace(namespace).withName(name);
} else if (entity instanceof DeploymentConfig) {
OpenShiftClient openshiftClient = new Controller(kubernetes).getOpenShiftClientOrNull();
if (openshiftClient == null) {
log.warn("Ignoring DeploymentConfig %s as not connected to an OpenShift cluster", name);
continue;
}
scalable = openshiftClient.deploymentConfigs().inNamespace(namespace).withName(name);
}
if (scalable != null) {
log.info("Scaling " + getKind(entity) + " " + namespace + "/" + name + " to replicas: " + replicas);
scalable.scale(replicas, true);
}
}
}
use of io.fabric8.kubernetes.api.model.extensions.Deployment in project fabric8-maven-plugin by fabric8io.
the class KubernetesResourceUtil method mergeDeployments.
protected static HasMetadata mergeDeployments(Deployment resource1, Deployment resource2, Logger log, boolean switchOnLocalCustomisation) {
Deployment resource1OrCopy = resource1;
if (!switchOnLocalCustomisation) {
// lets copy the original to avoid modifying it
resource1OrCopy = new DeploymentBuilder(resource1OrCopy).build();
}
HasMetadata answer = resource1OrCopy;
DeploymentSpec spec1 = resource1OrCopy.getSpec();
DeploymentSpec spec2 = resource2.getSpec();
if (spec1 == null) {
resource1OrCopy.setSpec(spec2);
} else {
PodTemplateSpec template1 = spec1.getTemplate();
PodTemplateSpec template2 = null;
if (spec2 != null) {
template2 = spec2.getTemplate();
}
if (template1 != null && template2 != null) {
mergeMetadata(template1, template2);
}
if (template1 == null) {
spec1.setTemplate(template2);
} else {
PodSpec podSpec1 = template1.getSpec();
PodSpec podSpec2 = null;
if (template2 != null) {
podSpec2 = template2.getSpec();
}
if (podSpec1 == null) {
template1.setSpec(podSpec2);
} else {
String defaultName = null;
PodTemplateSpec updateTemplate = template1;
if (switchOnLocalCustomisation) {
HasMetadata override = resource2;
if (isLocalCustomisation(podSpec1)) {
updateTemplate = template2;
PodSpec tmp = podSpec1;
podSpec1 = podSpec2;
podSpec2 = tmp;
} else {
answer = resource2;
override = resource1OrCopy;
}
mergeMetadata(answer, override);
} else {
mergeMetadata(resource1OrCopy, resource2);
}
if (updateTemplate != null) {
if (podSpec2 == null) {
updateTemplate.setSpec(podSpec1);
} else {
PodSpecBuilder podSpecBuilder = new PodSpecBuilder(podSpec1);
mergePodSpec(podSpecBuilder, podSpec2, defaultName);
updateTemplate.setSpec(podSpecBuilder.build());
}
}
return answer;
}
}
}
log.info("Merging 2 resources for " + getKind(resource1OrCopy) + " " + getName(resource1OrCopy) + " from " + getSourceUrlAnnotation(resource1OrCopy) + " and " + getSourceUrlAnnotation(resource2) + " and removing " + getSourceUrlAnnotation(resource1OrCopy));
return resource1OrCopy;
}
use of io.fabric8.kubernetes.api.model.extensions.Deployment in project fabric8-maven-plugin by fabric8io.
the class OpenShiftDependencyResources method convertKubernetesItemToOpenShift.
/**
* Returns the OpenShift dependency for the given resource if there is one
*/
public HasMetadata convertKubernetesItemToOpenShift(HasMetadata item) {
KindAndName key = new KindAndName(item);
HasMetadata answer = openshiftDependencyResources.get(key);
if (answer == null && item instanceof Deployment) {
key = new KindAndName("DeploymentConfig", getName(item));
answer = openshiftDependencyResources.get(key);
}
return answer;
}
use of io.fabric8.kubernetes.api.model.extensions.Deployment in project fabric8-maven-plugin by fabric8io.
the class DeploymentHandlerTest method deploymentTemplateHandlerTest.
@Test
public void deploymentTemplateHandlerTest() {
ContainerHandler containerHandler = new ContainerHandler(project, envVarHandler, probeHandler);
PodTemplateHandler podTemplateHandler = new PodTemplateHandler(containerHandler);
DeploymentHandler deploymentHandler = new DeploymentHandler(podTemplateHandler);
ResourceConfig config = new ResourceConfig.Builder().imagePullPolicy("IfNotPresent").controllerName("testing").withServiceAccount("test-account").withReplicas(5).volumes(volumes1).build();
Deployment deployment = deploymentHandler.getDeployment(config, images);
// Assertion
assertNotNull(deployment.getSpec());
assertNotNull(deployment.getMetadata());
assertEquals(5, deployment.getSpec().getReplicas().intValue());
assertNotNull(deployment.getSpec().getTemplate());
assertEquals("testing", deployment.getMetadata().getName());
assertEquals("test-account", deployment.getSpec().getTemplate().getSpec().getServiceAccountName());
assertFalse(deployment.getSpec().getTemplate().getSpec().getVolumes().isEmpty());
assertEquals("test", deployment.getSpec().getTemplate().getSpec().getVolumes().get(0).getName());
assertEquals("/test/path", deployment.getSpec().getTemplate().getSpec().getVolumes().get(0).getHostPath().getPath());
assertNotNull(deployment.getSpec().getTemplate().getSpec().getContainers());
}
Aggregations