-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce KubeUtils and add methods for work with install plans (#44)
Signed-off-by: David Kornel <[email protected]>
- Loading branch information
Showing
2 changed files
with
228 additions
and
0 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
test-frame-common/src/main/java/io/skodjob/testframe/utils/KubeUtils.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,68 @@ | ||
/* | ||
* Copyright Skodjob authors. | ||
* License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html). | ||
*/ | ||
package io.skodjob.testframe.utils; | ||
|
||
import io.fabric8.openshift.api.model.operatorhub.v1alpha1.InstallPlan; | ||
import io.fabric8.openshift.api.model.operatorhub.v1alpha1.InstallPlanBuilder; | ||
import io.skodjob.testframe.TestFrameConstants; | ||
import io.skodjob.testframe.resources.KubeResourceManager; | ||
import io.skodjob.testframe.wait.Wait; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Utility methods for Kubernetes and Openshift clusters. | ||
*/ | ||
public class KubeUtils { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(KubeUtils.class); | ||
|
||
private KubeUtils() { | ||
} | ||
|
||
/** | ||
* Approve install plan in namespace | ||
* | ||
* @param namespaceName namespace | ||
* @param installPlanName install plan name | ||
*/ | ||
public static void approveInstallPlan(String namespaceName, String installPlanName) { | ||
LOGGER.debug("Approving InstallPlan {}", installPlanName); | ||
Wait.until("InstallPlan approval", TestFrameConstants.GLOBAL_POLL_INTERVAL_SHORT, 15_000, () -> { | ||
try { | ||
InstallPlan installPlan = | ||
new InstallPlanBuilder(KubeResourceManager.getKubeClient() | ||
.getOpenShiftClient().operatorHub().installPlans() | ||
.inNamespace(namespaceName).withName(installPlanName).get()) | ||
.editSpec() | ||
.withApproved() | ||
.endSpec() | ||
.build(); | ||
|
||
KubeResourceManager.getKubeClient().getOpenShiftClient().operatorHub().installPlans() | ||
.inNamespace(namespaceName).withName(installPlanName).patch(installPlan); | ||
return true; | ||
} catch (Exception ex) { | ||
LOGGER.error(String.valueOf(ex)); | ||
return false; | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Read not approved install-plans with prefix | ||
* | ||
* @param namespaceName namespace | ||
* @param csvPrefix prefix of install plans | ||
* @return list of not approved install-plans | ||
*/ | ||
public InstallPlan getNonApprovedInstallPlan(String namespaceName, String csvPrefix) { | ||
return KubeResourceManager.getKubeClient().getOpenShiftClient().operatorHub().installPlans() | ||
.inNamespace(namespaceName).list().getItems().stream() | ||
.filter(installPlan -> !installPlan.getSpec().getApproved() | ||
&& installPlan.getSpec().getClusterServiceVersionNames().toString().contains(csvPrefix)) | ||
.findFirst().get(); | ||
} | ||
} |
160 changes: 160 additions & 0 deletions
160
test-frame-openshift/src/main/java/io/skodjob/testframe/resources/InstallPlanResource.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,160 @@ | ||
/* | ||
* Copyright Skodjob authors. | ||
* License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html). | ||
*/ | ||
package io.skodjob.testframe.resources; | ||
|
||
import io.fabric8.kubernetes.client.dsl.MixedOperation; | ||
import io.fabric8.kubernetes.client.dsl.Resource; | ||
import io.fabric8.openshift.api.model.operatorhub.v1alpha1.InstallPlan; | ||
import io.fabric8.openshift.api.model.operatorhub.v1alpha1.InstallPlanList; | ||
import io.skodjob.testframe.interfaces.NamespacedResourceType; | ||
|
||
import java.util.function.Consumer; | ||
|
||
/** | ||
* Implementation of ResourceType for specific kubernetes resource | ||
*/ | ||
public class InstallPlanResource implements NamespacedResourceType<InstallPlan> { | ||
|
||
private final MixedOperation<InstallPlan, InstallPlanList, Resource<InstallPlan>> client; | ||
|
||
/** | ||
* Constructor | ||
*/ | ||
public InstallPlanResource() { | ||
this.client = KubeResourceManager.getKubeClient().getOpenShiftClient().operatorHub().installPlans(); | ||
} | ||
|
||
/** | ||
* Kind of api resource | ||
* @return kind name | ||
*/ | ||
@Override | ||
public String getKind() { | ||
return "OperatorGroup"; | ||
} | ||
|
||
/** | ||
* Get specific client for resoruce | ||
* @return specific client | ||
*/ | ||
@Override | ||
public MixedOperation<?, ?, ?> getClient() { | ||
return client; | ||
} | ||
|
||
/** | ||
* Creates specific {@link InstallPlan} resource | ||
* | ||
* @param resource {@link InstallPlan} resource | ||
*/ | ||
@Override | ||
public void create(InstallPlan resource) { | ||
client.resource(resource).create(); | ||
} | ||
|
||
/** | ||
* Updates specific {@link InstallPlan} resource | ||
* | ||
* @param resource {@link InstallPlan} resource that will be updated | ||
*/ | ||
@Override | ||
public void update(InstallPlan resource) { | ||
client.resource(resource).update(); | ||
} | ||
|
||
/** | ||
* Deletes {@link InstallPlan} resource from Namespace in current context | ||
* | ||
* @param resourceName name of the {@link InstallPlan} that will be deleted | ||
*/ | ||
@Override | ||
public void delete(String resourceName) { | ||
client.withName(resourceName).delete(); | ||
} | ||
|
||
/** | ||
* Replaces {@link InstallPlan} resource using {@link Consumer} | ||
* from which is the current {@link InstallPlan} resource updated | ||
* | ||
* @param resourceName name of the {@link InstallPlan} that will be replaced | ||
* @param editor {@link Consumer} containing updates to the resource | ||
*/ | ||
@Override | ||
public void replace(String resourceName, Consumer<InstallPlan> editor) { | ||
InstallPlan toBeReplaced = client.withName(resourceName).get(); | ||
editor.accept(toBeReplaced); | ||
update(toBeReplaced); | ||
} | ||
|
||
/** | ||
* Waits for {@link InstallPlan} to be ready (created/running) | ||
* | ||
* @param resource resource | ||
* @return result of the readiness check | ||
*/ | ||
@Override | ||
public boolean waitForReadiness(InstallPlan resource) { | ||
return resource != null; | ||
} | ||
|
||
/** | ||
* Waits for {@link InstallPlan} to be deleted | ||
* | ||
* @param resource resource | ||
* @return result of the deletion | ||
*/ | ||
@Override | ||
public boolean waitForDeletion(InstallPlan resource) { | ||
return resource == null; | ||
} | ||
|
||
/** | ||
* Creates specific {@link InstallPlan} resource in Namespace specified by user | ||
* | ||
* @param namespaceName Namespace, where the resource should be created | ||
* @param resource {@link InstallPlan} resource | ||
*/ | ||
@Override | ||
public void createInNamespace(String namespaceName, InstallPlan resource) { | ||
client.inNamespace(namespaceName).resource(resource).create(); | ||
} | ||
|
||
/** | ||
* Updates specific {@link InstallPlan} resource in Namespace specified by user | ||
* | ||
* @param namespaceName Namespace, where the resource should be updated | ||
* @param resource {@link InstallPlan} updated resource | ||
*/ | ||
@Override | ||
public void updateInNamespace(String namespaceName, InstallPlan resource) { | ||
client.inNamespace(namespaceName).resource(resource).update(); | ||
} | ||
|
||
/** | ||
* Deletes {@link InstallPlan} resource from Namespace specified by user | ||
* | ||
* @param namespaceName Namespace, where the resource should be deleted | ||
* @param resourceName name of the {@link InstallPlan} that will be deleted | ||
*/ | ||
@Override | ||
public void deleteFromNamespace(String namespaceName, String resourceName) { | ||
client.inNamespace(namespaceName).withName(resourceName).delete(); | ||
} | ||
|
||
/** | ||
* Replaces {@link InstallPlan} resource in Namespace specified by user, using {@link Consumer} | ||
* from which is the current {@link InstallPlan} resource updated | ||
* | ||
* @param namespaceName Namespace, where the resource should be replaced | ||
* @param resourceName name of the {@link InstallPlan} that will be replaced | ||
* @param editor {@link Consumer} containing updates to the resource | ||
*/ | ||
@Override | ||
public void replaceInNamespace(String namespaceName, String resourceName, Consumer<InstallPlan> editor) { | ||
InstallPlan toBeReplaced = client.inNamespace(namespaceName).withName(resourceName).get(); | ||
editor.accept(toBeReplaced); | ||
updateInNamespace(namespaceName, toBeReplaced); | ||
} | ||
} |