-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CHE-220: Improving 'removeContainer' API. Refactoring #5766
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
...ent/src/main/java/org/eclipse/che/plugin/openshift/client/OpenShiftDeploymentCleaner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2012-2017 Red Hat, Inc. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.che.plugin.openshift.client; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import javax.inject.Singleton; | ||
|
||
import org.eclipse.che.plugin.openshift.client.exception.OpenShiftException; | ||
import org.eclipse.che.plugin.openshift.client.kubernetes.KubernetesResourceUtil; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import io.fabric8.kubernetes.api.model.Pod; | ||
import io.fabric8.kubernetes.api.model.PodList; | ||
import io.fabric8.kubernetes.api.model.Service; | ||
import io.fabric8.kubernetes.api.model.extensions.Deployment; | ||
import io.fabric8.kubernetes.api.model.extensions.DoneableDeployment; | ||
import io.fabric8.kubernetes.api.model.extensions.ReplicaSet; | ||
import io.fabric8.kubernetes.client.KubernetesClientException; | ||
import io.fabric8.kubernetes.client.Watch; | ||
import io.fabric8.kubernetes.client.Watcher; | ||
import io.fabric8.kubernetes.client.dsl.FilterWatchListDeletable; | ||
import io.fabric8.kubernetes.client.dsl.ScalableResource; | ||
import io.fabric8.openshift.api.model.Route; | ||
import io.fabric8.openshift.client.DefaultOpenShiftClient; | ||
import io.fabric8.openshift.client.OpenShiftClient; | ||
|
||
@Singleton | ||
public class OpenShiftDeploymentCleaner { | ||
private static final Logger LOG = LoggerFactory.getLogger(OpenShiftDeploymentCleaner.class); | ||
private static final int OPENSHIFT_POD_DELETION_TIMEOUT = 120; | ||
|
||
public void cleanDeploymentResources(final String deploymentName, final String namespace) throws IOException { | ||
scaleDownDeployment(deploymentName, namespace); | ||
cleanUpWorkspaceResources(deploymentName, namespace); | ||
waitUntilWorkspacePodIsDeleted(deploymentName, namespace); | ||
} | ||
|
||
private void scaleDownDeployment(String deploymentName, final String namespace) throws OpenShiftException { | ||
try (OpenShiftClient openShiftClient = new DefaultOpenShiftClient()) { | ||
ScalableResource<Deployment, DoneableDeployment> deployment = openShiftClient.extensions() | ||
.deployments() | ||
.inNamespace(namespace) | ||
.withName(deploymentName); | ||
|
||
if (deployment != null) { | ||
deployment.scale(0, true); | ||
} | ||
} | ||
} | ||
|
||
private void cleanUpWorkspaceResources(final String deploymentName, final String namespace) throws IOException { | ||
Deployment deployment = KubernetesResourceUtil.getDeploymentByName(deploymentName, namespace); | ||
Service service = KubernetesResourceUtil.getServiceBySelector(OpenShiftConnector.OPENSHIFT_DEPLOYMENT_LABEL, deploymentName, namespace); | ||
List<Route> routes = KubernetesResourceUtil.getRoutesByLabel(OpenShiftConnector.OPENSHIFT_DEPLOYMENT_LABEL, deploymentName, namespace); | ||
List<ReplicaSet> replicaSets = KubernetesResourceUtil.getReplicaSetByLabel(OpenShiftConnector.OPENSHIFT_DEPLOYMENT_LABEL, deploymentName, namespace); | ||
|
||
try (OpenShiftClient openShiftClient = new DefaultOpenShiftClient()) { | ||
|
||
if (deployment != null) { | ||
LOG.info("Removing OpenShift Deployment {}", deployment.getMetadata().getName()); | ||
openShiftClient.resource(deployment).delete(); | ||
} | ||
|
||
if (replicaSets != null && replicaSets.size() > 0) { | ||
LOG.info("Removing OpenShift ReplicaSets for deployment {}", deploymentName); | ||
replicaSets.forEach(rs -> openShiftClient.resource(rs).delete()); | ||
} | ||
|
||
if (routes != null) { | ||
for (Route route: routes) { | ||
LOG.info("Removing OpenShift Route {}", route.getMetadata().getName()); | ||
openShiftClient.resource(route).delete(); | ||
} | ||
} | ||
|
||
if (service != null) { | ||
LOG.info("Removing OpenShift Service {}", service.getMetadata().getName()); | ||
openShiftClient.resource(service).delete(); | ||
} | ||
} | ||
} | ||
|
||
private void waitUntilWorkspacePodIsDeleted(final String deploymentName, final String namespace) throws OpenShiftException { | ||
try (OpenShiftClient client = new DefaultOpenShiftClient()) { | ||
FilterWatchListDeletable<Pod, PodList, Boolean, Watch, Watcher<Pod>> pods = client.pods() | ||
.inNamespace(namespace) | ||
.withLabel(OpenShiftConnector.OPENSHIFT_DEPLOYMENT_LABEL, deploymentName); | ||
|
||
int numberOfPodsToStop = pods.list().getItems().size(); | ||
LOG.info("Number of workspace pods to stop {}", numberOfPodsToStop); | ||
if (numberOfPodsToStop > 0) { | ||
final CountDownLatch podCount = new CountDownLatch(numberOfPodsToStop); | ||
pods.watch(new Watcher<Pod>() { | ||
@Override | ||
public void eventReceived(Action action, Pod pod) { | ||
try { | ||
switch (action) { | ||
case ADDED: | ||
case MODIFIED: | ||
case ERROR: | ||
break; | ||
case DELETED: | ||
LOG.info("Pod {} deleted", pod.getMetadata().getName()); | ||
podCount.countDown(); | ||
break; | ||
} | ||
} catch (Exception e) { | ||
LOG.error("Failed to process {} on Pod {}. Error: ", action, pod, e); | ||
} | ||
} | ||
|
||
@Override | ||
public void onClose(KubernetesClientException ex) { | ||
} | ||
}); | ||
|
||
try { | ||
LOG.info("Waiting for all pods to be deleted for deployment '{}'", deploymentName); | ||
podCount.await(OPENSHIFT_POD_DELETION_TIMEOUT, TimeUnit.SECONDS); | ||
} catch (InterruptedException e) { | ||
LOG.error("Exception while waiting for pods to be deleted", e); | ||
throw new OpenShiftException("Timeout while waiting for pods to terminate", e); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does waiting for the scale down take significantly longer than the previous way, or does the scale down take grace termination period into account? I ask because we had the issue on OpenShift where stopping and restarting a pod quickly could case PV issues.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there should not be PV issues since this change was applied in
openshift-connector
rebased, but grace termination period is taken into accountThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@amisevsk btw in master I do not see that we use "terminationGracePeriodSeconds", So while testing it on minishift scale down does not happen fast and require some time - this might be the main reason of quota limit issue that was reported today
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this PR is not applied to master #5007
But this is a separate conversation, I will provide another PR for that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good - thanks @ibuziuk !