use of io.fabric8.kubernetes.api.model.apps.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.apps.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.apps.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.apps.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());
}
use of io.fabric8.kubernetes.api.model.apps.Deployment in project fabric8-maven-plugin by fabric8io.
the class DebugMojo method applyEntities.
@Override
protected void applyEntities(Controller controller, KubernetesClient kubernetes, String namespace, String fileName, Set<HasMetadata> entities) throws Exception {
LabelSelector firstSelector = null;
for (HasMetadata entity : entities) {
String name = getName(entity);
LabelSelector selector = null;
if (entity instanceof Deployment) {
Deployment resource = (Deployment) entity;
DeploymentSpec spec = resource.getSpec();
if (spec != null) {
if (enableDebugging(entity, spec.getTemplate())) {
kubernetes.extensions().deployments().inNamespace(namespace).withName(name).replace(resource);
}
selector = getPodLabelSelector(entity);
}
} else if (entity instanceof ReplicaSet) {
ReplicaSet resource = (ReplicaSet) entity;
ReplicaSetSpec spec = resource.getSpec();
if (spec != null) {
if (enableDebugging(entity, spec.getTemplate())) {
kubernetes.extensions().replicaSets().inNamespace(namespace).withName(name).replace(resource);
}
selector = getPodLabelSelector(entity);
}
} else if (entity instanceof ReplicationController) {
ReplicationController resource = (ReplicationController) entity;
ReplicationControllerSpec spec = resource.getSpec();
if (spec != null) {
if (enableDebugging(entity, spec.getTemplate())) {
kubernetes.replicationControllers().inNamespace(namespace).withName(name).replace(resource);
}
selector = getPodLabelSelector(entity);
}
} else if (entity instanceof DeploymentConfig) {
DeploymentConfig resource = (DeploymentConfig) entity;
DeploymentConfigSpec spec = resource.getSpec();
if (spec != null) {
if (enableDebugging(entity, spec.getTemplate())) {
OpenShiftClient openshiftClient = new Controller(kubernetes).getOpenShiftClientOrNull();
if (openshiftClient == null) {
log.warn("Ignoring DeploymentConfig %s as not connected to an OpenShift cluster", name);
continue;
}
openshiftClient.deploymentConfigs().inNamespace(namespace).withName(name).replace(resource);
}
selector = getPodLabelSelector(entity);
}
}
if (selector != null) {
firstSelector = selector;
} else {
controller.apply(entity, fileName);
}
}
if (firstSelector != null) {
Map<String, String> envVars = new TreeMap<>();
envVars.put(DebugConstants.ENV_VAR_JAVA_DEBUG, "true");
envVars.put(DebugConstants.ENV_VAR_JAVA_DEBUG_SUSPEND, String.valueOf(this.debugSuspend));
if (this.debugSuspendValue != null) {
envVars.put(DebugConstants.ENV_VAR_JAVA_DEBUG_SESSION, this.debugSuspendValue);
}
String podName = waitForRunningPodWithEnvVar(kubernetes, namespace, firstSelector, envVars);
portForward(controller, podName);
}
}
Aggregations