diff --git a/src/main/java/cz/xtf/openshift/OpenShiftUtil.java b/src/main/java/cz/xtf/openshift/OpenShiftUtil.java index 1c754406..407a387e 100644 --- a/src/main/java/cz/xtf/openshift/OpenShiftUtil.java +++ b/src/main/java/cz/xtf/openshift/OpenShiftUtil.java @@ -284,6 +284,11 @@ public List 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 getUserSecrets() { return getSecrets().stream().filter(s -> !s.getType().startsWith("kubernetes.io/")).collect(Collectors.toList()); } @@ -343,6 +348,15 @@ public boolean deleteRoute(Route route) { return client.routes().delete(route); } + // ReplicationControllers - Only for internal usage with clean + private List 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); @@ -502,8 +516,19 @@ public List getServiceAccounts() { return client.serviceAccounts().list().getItems(); } + /** + * Retrieves service accounts that aren't considered default. + * Service accounts that are left out from list: + *
    + *
  • builder
  • + *
  • default
  • + *
  • deployer
  • + *
+ * + * @return List of service accounts that aren't considered default. + */ public List 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) { @@ -523,6 +548,22 @@ public List getRoleBindings() { return client.roleBindings().list().getItems(); } + /** + * Retrieves role bindings that aren't considered default. + * Role bindings that are left out from list: + *
    + *
  • admin
  • + *
  • system:deployers
  • + *
  • system:image-builders
  • + *
  • system:image-pullers
  • + *
+ * + * @return List of role bindings that aren't considered default. + */ + public List 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); } @@ -734,7 +775,14 @@ public List getEvents() { // Clean up function /** - * Deletes all resources in namespace. Waits till all are deleted. + * Deletes all* resources in namespace. Waits till all are deleted.
+ *
+ * + * * 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. */ @@ -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.
+ *
+ * + * * 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. */ @@ -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.
+ *
+ * + * * 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(); @@ -773,6 +835,7 @@ public void clean() { client.configMaps().delete(); getUserSecrets().forEach(this::deleteSecret); getUserServiceAccounts().forEach(this::deleteServiceAccount); + getUserRoleBindings().forEach(this::deleteRoleBinding); } @Override diff --git a/src/main/java/cz/xtf/openshift/OpenShiftWaiters.java b/src/main/java/cz/xtf/openshift/OpenShiftWaiters.java index ba62a272..3fea3ef0 100644 --- a/src/main/java/cz/xtf/openshift/OpenShiftWaiters.java +++ b/src/main/java/cz/xtf/openshift/OpenShiftWaiters.java @@ -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); }; @@ -84,7 +85,7 @@ public Waiter isDcReady(String dcName) { */ public Waiter isDcReady(String dcName, int restartTolerance) { Supplier> 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); } diff --git a/src/main/java/cz/xtf/wait/Waiter.java b/src/main/java/cz/xtf/wait/Waiter.java index fe8d8762..39709358 100644 --- a/src/main/java/cz/xtf/wait/Waiter.java +++ b/src/main/java/cz/xtf/wait/Waiter.java @@ -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; @@ -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); } /**