Skip to content

Commit

Permalink
Merge pull request #46 from Crumby/waiting-refactorization
Browse files Browse the repository at this point in the history
Logging and clean up fixes
  • Loading branch information
Crumby authored Jan 30, 2018
2 parents 4ad3a42 + bf6d14f commit 5c4c215
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 7 deletions.
73 changes: 68 additions & 5 deletions src/main/java/cz/xtf/openshift/OpenShiftUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,11 @@ public List<Secret> getSecrets() {
return client.secrets().list().getItems();
}

/**
* Retrieves secrets that aren't considered default. Secrets that are left out contain type starting with 'kubernetes.io/'.
*
* @return List of secrets that aren't considered default.
*/
public List<Secret> getUserSecrets() {
return getSecrets().stream().filter(s -> !s.getType().startsWith("kubernetes.io/")).collect(Collectors.toList());
}
Expand Down Expand Up @@ -343,6 +348,15 @@ public boolean deleteRoute(Route route) {
return client.routes().delete(route);
}

// ReplicationControllers - Only for internal usage with clean
private List<ReplicationController> getReplicationControllers() {
return client.replicationControllers().list().getItems();
}

private boolean deleteReplicationController(ReplicationController replicationController) {
return client.replicationControllers().withName(replicationController.getMetadata().getName()).cascading(false).delete();
}

// DeploymentConfigs
public DeploymentConfig createDeploymentConfig(DeploymentConfig deploymentConfig) {
return client.deploymentConfigs().create(deploymentConfig);
Expand Down Expand Up @@ -502,8 +516,19 @@ public List<ServiceAccount> getServiceAccounts() {
return client.serviceAccounts().list().getItems();
}

/**
* Retrieves service accounts that aren't considered default.
* Service accounts that are left out from list:
* <ul>
* <li>builder</li>
* <li>default</li>
* <li>deployer</li>
* </ul>
*
* @return List of service accounts that aren't considered default.
*/
public List<ServiceAccount> getUserServiceAccounts() {
return getServiceAccounts().stream().filter(sa -> !sa.getMetadata().getName().matches(".*(builder|default|deployer).*")).collect(Collectors.toList());
return getServiceAccounts().stream().filter(sa -> !sa.getMetadata().getName().matches("builder|default|deployer")).collect(Collectors.toList());
}

public boolean deleteServiceAccount(ServiceAccount serviceAccount) {
Expand All @@ -523,6 +548,22 @@ public List<RoleBinding> getRoleBindings() {
return client.roleBindings().list().getItems();
}

/**
* Retrieves role bindings that aren't considered default.
* Role bindings that are left out from list:
* <ul>
* <li>admin</li>
* <li>system:deployers</li>
* <li>system:image-builders</li>
* <li>system:image-pullers</li>
* </ul>
*
* @return List of role bindings that aren't considered default.
*/
public List<RoleBinding> getUserRoleBindings() {
return getRoleBindings().stream().filter(rb -> !rb.getMetadata().getName().matches("admin|system:deployers|system:image-builders|system:image-pullers")).collect(Collectors.toList());
}

public boolean deleteRoleBinding(RoleBinding roleBinding) {
return client.roleBindings().delete(roleBinding);
}
Expand Down Expand Up @@ -734,7 +775,14 @@ public List<Event> getEvents() {

// Clean up function
/**
* Deletes all resources in namespace. Waits till all are deleted.
* Deletes all* resources in namespace. Waits till all are deleted. <br/>
* <br/>
*
* * Only user created secrets, service accounts and role bindings are deleted. Default will remain.
*
* @see #getUserSecrets()
* @see #getUserServiceAccounts()
* @see #getUserRoleBindings()
*
* @throws TimeoutException in case that some user resources will remain even after timeout.
*/
Expand All @@ -744,7 +792,14 @@ public void cleanAndWait() throws TimeoutException {
}

/**
* Deletes all resources in namespace. Waits till all are deleted.
* Deletes all* resources in namespace. Waits till all are deleted. <br/>
* <br/>
*
* * Only user created secrets, service accounts and role bindings are deleted. Default will remain.
*
* @see #getUserSecrets()
* @see #getUserServiceAccounts()
* @see #getUserRoleBindings()
*
* @throws AssertionError in case that some user resources will remain even after timeout.
*/
Expand All @@ -755,12 +810,19 @@ public void cleanAndAssert() {


/**
* Deletes all resources in namespace. Doesn't wait till all are deleted.
* Deletes all* resources in namespace. Doesn't wait till all are deleted. <br/>
* <br/>
*
* * Only user created secrets, service accounts and role bindings are deleted. Default will remain.
*
* @see #getUserSecrets()
* @see #getUserServiceAccounts()
* @see #getUserRoleBindings()
*/
public void clean() {
// keep the order for deletion to prevent K8s creating resources again
getDeploymentConfigs().forEach(this::deleteDeploymentConfig);
client.replicationControllers().delete();
getReplicationControllers().forEach(this::deleteReplicationController);
client.buildConfigs().delete();
client.imageStreams().delete();
client.endpoints().delete();
Expand All @@ -773,6 +835,7 @@ public void clean() {
client.configMaps().delete();
getUserSecrets().forEach(this::deleteSecret);
getUserServiceAccounts().forEach(this::deleteServiceAccount);
getUserRoleBindings().forEach(this::deleteRoleBinding);
}

@Override
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/cz/xtf/openshift/OpenShiftWaiters.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public Waiter isProjectClean() {
cleanedResources.add(openShiftUtil.getConfigMaps().isEmpty());
cleanedResources.add(openShiftUtil.getUserSecrets().isEmpty());
cleanedResources.add(openShiftUtil.getUserServiceAccounts().isEmpty());
cleanedResources.add(openShiftUtil.getUserRoleBindings().isEmpty());

return !cleanedResources.contains(false);
};
Expand Down Expand Up @@ -84,7 +85,7 @@ public Waiter isDcReady(String dcName) {
*/
public Waiter isDcReady(String dcName, int restartTolerance) {
Supplier<List<Pod>> ps = () -> openShiftUtil.getPods(dcName);
String reason = "Waiting till all pods created by " + dcName + "deployment config are ready";
String reason = "Waiting till all pods created by " + dcName + " deployment config are ready";

return isDeploymentReady(dcName, ps, restartTolerance).reason(reason);
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/cz/xtf/wait/Waiter.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package cz.xtf.wait;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DurationFormatUtils;

import java.time.Duration;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

Expand Down Expand Up @@ -115,7 +117,7 @@ enum LogPoint {
* @param millis waiting timeout on condition
*/
public void logStart(String reason, long millis) {
if(this.equals(START) || this.equals(BOTH)) log.info("Waiting up to {}ms. Reason: {}", millis, reason);
if(this.equals(START) || this.equals(BOTH)) log.info("Waiting up to {}. Reason: {}", DurationFormatUtils.formatDurationWords(millis, true, true), reason);
}

/**
Expand Down

0 comments on commit 5c4c215

Please sign in to comment.