Skip to content

Commit

Permalink
Add logs for OLM resources
Browse files Browse the repository at this point in the history
  • Loading branch information
tommaso-borgato committed Mar 23, 2021
1 parent 613f988 commit bbe3e25
Show file tree
Hide file tree
Showing 4 changed files with 210 additions and 2 deletions.
4 changes: 2 additions & 2 deletions junit5/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
Expand Down Expand Up @@ -44,5 +45,4 @@
</dependency>
</dependencies>


</project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import cz.xtf.core.openshift.OpenShifts;
import cz.xtf.junit5.annotations.OpenShiftRecorder;
import cz.xtf.junit5.config.JUnitConfig;
import cz.xtf.junit5.extensions.helpers.CustomResourceHelper;
import cz.xtf.junit5.extensions.helpers.EventsFilterBuilder;
import cz.xtf.junit5.extensions.helpers.ResourcesFilterBuilder;
import cz.xtf.junit5.extensions.helpers.ResourcesPrinterHelper;
Expand All @@ -32,6 +33,10 @@
import io.fabric8.openshift.api.model.DeploymentConfig;
import io.fabric8.openshift.api.model.ImageStream;
import io.fabric8.openshift.api.model.Route;
import io.fabric8.openshift.api.model.operatorhub.v1.OperatorGroup;
import io.fabric8.openshift.api.model.operatorhub.v1alpha1.ClusterServiceVersion;
import io.fabric8.openshift.api.model.operatorhub.v1alpha1.InstallPlan;
import io.fabric8.openshift.api.model.operatorhub.v1alpha1.Subscription;

/**
* Record OpenShift isolated state relative to a test.
Expand Down Expand Up @@ -88,6 +93,15 @@ public class OpenShiftRecorderService {
private static final String IS_FILTER_BUILDS = "IS_FILTER_BUILDS";
private static final String EVENT_FILTER_BUILDS = "EVENT_FILTER_BUILDS";

private CustomResourceHelper customResourceHelper;

private CustomResourceHelper getCustomResourceHelper() {
if (customResourceHelper == null) {
customResourceHelper = new CustomResourceHelper();
}
return customResourceHelper;
}

/**
* Initialize filters by collecting information OCP resources which are relevant for the current test execution
* context (e.g.: called by a {@link org.junit.jupiter.api.extension.BeforeAllCallback#beforeAll(ExtensionContext)}
Expand Down Expand Up @@ -213,6 +227,10 @@ public void recordState(ExtensionContext context) throws IOException {
!isMasterAndBuildNamespaceSame() ? getFilter(context, BUILD_FILTER_MASTER) : null);
saveEvents(context, getFilter(context, EVENT_FILTER_MASTER),
!isMasterAndBuildNamespaceSame() ? getFilter(context, EVENT_FILTER_BUILDS) : null);
saveClusterServiceVersions(context);
saveInstallPlans(context);
saveOperatorGroups(context);
saveSubscriptions(context);
}

private boolean isFilterInitializationComplete(ExtensionContext context) {
Expand Down Expand Up @@ -521,6 +539,39 @@ protected void saveBuildLogs(ExtensionContext context, ResourcesFilterBuilder<Bu
}
}

protected void saveClusterServiceVersions(ExtensionContext context) throws IOException {
final Path logPath = Paths.get(attachmentsDir(), dirNameForTest(context), "clusterServiceVersions.log");
try (final ResourcesPrinterHelper<ClusterServiceVersion> printer = ResourcesPrinterHelper
.forClusterServiceVersion(logPath)) {
getCustomResourceHelper().getClusterServiceVersionClient().list().getItems().stream()
.forEach(printer::row);
}
}

protected void saveInstallPlans(ExtensionContext context) throws IOException {
final Path logPath = Paths.get(attachmentsDir(), dirNameForTest(context), "installPlans.log");
try (final ResourcesPrinterHelper<InstallPlan> printer = ResourcesPrinterHelper.forInstallPlan(logPath)) {
getCustomResourceHelper().getInstallPlanClient().list().getItems().stream()
.forEach(printer::row);
}
}

protected void saveOperatorGroups(ExtensionContext context) throws IOException {
final Path logPath = Paths.get(attachmentsDir(), dirNameForTest(context), "operatorGroups.log");
try (final ResourcesPrinterHelper<OperatorGroup> printer = ResourcesPrinterHelper.forOperatorGroup(logPath)) {
getCustomResourceHelper().getOperatorGroupClient().list().getItems().stream()
.forEach(printer::row);
}
}

protected void saveSubscriptions(ExtensionContext context) throws IOException {
final Path logPath = Paths.get(attachmentsDir(), dirNameForTest(context), "subscriptions.log");
try (final ResourcesPrinterHelper<Subscription> printer = ResourcesPrinterHelper.forSubscription(logPath)) {
getCustomResourceHelper().getSubscriptionClient().list().getItems().stream()
.forEach(printer::row);
}
}

private String attachmentsDir() {
return JUnitConfig.recordDir() != null ? JUnitConfig.recordDir() : System.getProperty("user.dir");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package cz.xtf.junit5.extensions.helpers;

import cz.xtf.core.openshift.OpenShifts;
import io.fabric8.kubernetes.client.dsl.MixedOperation;
import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext;
import io.fabric8.openshift.api.model.operatorhub.v1.DoneableOperatorGroup;
import io.fabric8.openshift.api.model.operatorhub.v1.OperatorGroup;
import io.fabric8.openshift.api.model.operatorhub.v1.OperatorGroupList;
import io.fabric8.openshift.api.model.operatorhub.v1alpha1.ClusterServiceVersion;
import io.fabric8.openshift.api.model.operatorhub.v1alpha1.ClusterServiceVersionList;
import io.fabric8.openshift.api.model.operatorhub.v1alpha1.DoneableClusterServiceVersion;
import io.fabric8.openshift.api.model.operatorhub.v1alpha1.DoneableInstallPlan;
import io.fabric8.openshift.api.model.operatorhub.v1alpha1.DoneableSubscription;
import io.fabric8.openshift.api.model.operatorhub.v1alpha1.InstallPlan;
import io.fabric8.openshift.api.model.operatorhub.v1alpha1.InstallPlanList;
import io.fabric8.openshift.api.model.operatorhub.v1alpha1.Subscription;
import io.fabric8.openshift.api.model.operatorhub.v1alpha1.SubscriptionList;

/**
* <p>This class provides the clients used to interact with some of the APIs of the Operator Lifecycle Manager (OLM)</p>
* <p>For example it provides the client to ineract with <code>ClusterServiceVersion</code></p>
*/
public class CustomResourceHelper {

public static final String OPERATORS_COREOS_COM = "operators.coreos.com";

public NonNamespaceOperation<ClusterServiceVersion, ClusterServiceVersionList, DoneableClusterServiceVersion, Resource<ClusterServiceVersion, DoneableClusterServiceVersion>> getClusterServiceVersionClient() {
CustomResourceDefinitionContext clusterserviceversionsContext = new CustomResourceDefinitionContext.Builder()
.withGroup(OPERATORS_COREOS_COM).withVersion("v1alpha1").withScope("Namespaced")
.withName("clusterserviceversions.operators.coreos.com").withPlural("clusterserviceversions")
.withKind("ClusterServiceVersion").build();

NonNamespaceOperation<ClusterServiceVersion, ClusterServiceVersionList, DoneableClusterServiceVersion, Resource<ClusterServiceVersion, DoneableClusterServiceVersion>> client = OpenShifts
.admin().customResources(clusterserviceversionsContext, ClusterServiceVersion.class,
ClusterServiceVersionList.class, DoneableClusterServiceVersion.class);
client = ((MixedOperation<ClusterServiceVersion, ClusterServiceVersionList, DoneableClusterServiceVersion, Resource<ClusterServiceVersion, DoneableClusterServiceVersion>>) client)
.inNamespace(OpenShifts.master().getNamespace());
return client;
}

public NonNamespaceOperation<InstallPlan, InstallPlanList, DoneableInstallPlan, Resource<InstallPlan, DoneableInstallPlan>> getInstallPlanClient() {
CustomResourceDefinitionContext installplansContext = new CustomResourceDefinitionContext.Builder()
.withGroup(OPERATORS_COREOS_COM).withVersion("v1alpha1").withScope("Namespaced")
.withName("installplans.operators.coreos.com").withPlural("installplans").withKind("InstallPlan")
.build();

NonNamespaceOperation<InstallPlan, InstallPlanList, DoneableInstallPlan, Resource<InstallPlan, DoneableInstallPlan>> client = OpenShifts
.admin().customResources(installplansContext, InstallPlan.class, InstallPlanList.class,
DoneableInstallPlan.class);
client = ((MixedOperation<InstallPlan, InstallPlanList, DoneableInstallPlan, Resource<InstallPlan, DoneableInstallPlan>>) client)
.inNamespace(OpenShifts.master().getNamespace());

return client;
}

public NonNamespaceOperation<OperatorGroup, OperatorGroupList, DoneableOperatorGroup, Resource<OperatorGroup, DoneableOperatorGroup>> getOperatorGroupClient() {
CustomResourceDefinitionContext operatorgroupsContext = new CustomResourceDefinitionContext.Builder()
.withGroup(OPERATORS_COREOS_COM).withVersion("v1").withScope("Namespaced")
.withName("operatorgroups.operators.coreos.com").withPlural("operatorgroups").withKind("OperatorGroup")
.build();

NonNamespaceOperation<OperatorGroup, OperatorGroupList, DoneableOperatorGroup, Resource<OperatorGroup, DoneableOperatorGroup>> client = OpenShifts
.admin().customResources(operatorgroupsContext, OperatorGroup.class, OperatorGroupList.class,
DoneableOperatorGroup.class);
client = ((MixedOperation<OperatorGroup, OperatorGroupList, DoneableOperatorGroup, Resource<OperatorGroup, DoneableOperatorGroup>>) client)
.inNamespace(OpenShifts.master().getNamespace());

return client;
}

public NonNamespaceOperation<Subscription, SubscriptionList, DoneableSubscription, Resource<Subscription, DoneableSubscription>> getSubscriptionClient() {
CustomResourceDefinitionContext subscriptionsContext = new CustomResourceDefinitionContext.Builder()
.withGroup(OPERATORS_COREOS_COM).withVersion("v1alpha1").withScope("Namespaced")
.withName("subscriptions.operators.coreos.com").withPlural("subscriptions").withKind("Subscription")
.build();

NonNamespaceOperation<Subscription, SubscriptionList, DoneableSubscription, Resource<Subscription, DoneableSubscription>> client = OpenShifts
.admin().customResources(subscriptionsContext, Subscription.class, SubscriptionList.class,
DoneableSubscription.class);
client = ((MixedOperation<Subscription, SubscriptionList, DoneableSubscription, Resource<Subscription, DoneableSubscription>>) client)
.inNamespace(OpenShifts.master().getNamespace());
return client;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

import io.fabric8.kubernetes.api.model.ConfigMap;
import io.fabric8.kubernetes.api.model.ContainerStatus;
Expand All @@ -23,6 +24,10 @@
import io.fabric8.openshift.api.model.DeploymentConfig;
import io.fabric8.openshift.api.model.ImageStream;
import io.fabric8.openshift.api.model.Route;
import io.fabric8.openshift.api.model.operatorhub.v1.OperatorGroup;
import io.fabric8.openshift.api.model.operatorhub.v1alpha1.ClusterServiceVersion;
import io.fabric8.openshift.api.model.operatorhub.v1alpha1.InstallPlan;
import io.fabric8.openshift.api.model.operatorhub.v1alpha1.Subscription;

public class ResourcesPrinterHelper<X> implements AutoCloseable {
private final Path file;
Expand Down Expand Up @@ -189,6 +194,72 @@ private static LinkedHashMap<String, String> getServicesCols(Service service) {
return map;
}

public static ResourcesPrinterHelper<ClusterServiceVersion> forClusterServiceVersion(Path filePath) {
return new ResourcesPrinterHelper<>(filePath,
ResourcesPrinterHelper::getClusterServiceVersionCols);
}

private static LinkedHashMap<String, String> getClusterServiceVersionCols(ClusterServiceVersion csv) {
LinkedHashMap<String, String> map = new LinkedHashMap<>(5);
map.put("NAME", csv.getMetadata().getName());
map.put("CREATED", csv.getMetadata().getCreationTimestamp());
map.put("PHASE", csv.getStatus().getPhase());
map.put("REASON", csv.getStatus().getReason());
map.put("MESSAGE", csv.getStatus().getMessage());
return map;
}

public static ResourcesPrinterHelper<InstallPlan> forInstallPlan(Path filePath) {
return new ResourcesPrinterHelper<>(filePath,
ResourcesPrinterHelper::getInstallPlanCols);
}

private static LinkedHashMap<String, String> getInstallPlanCols(InstallPlan ip) {
LinkedHashMap<String, String> map = new LinkedHashMap<>(6);
map.put("NAME", ip.getMetadata().getName());
map.put("CREATED", ip.getMetadata().getCreationTimestamp());
map.put("PHASE", ip.getStatus().getPhase());
map.put("CATALOG SOURCES", ip.getStatus().getCatalogSources().toString());
map.put("CSVS", ip.getSpec().getClusterServiceVersionNames().toString());
map.put("CONDITIONS", ip.getStatus().getConditions()
.stream()
.filter(cond -> "True".equalsIgnoreCase(cond.getStatus()))
.map(cond -> cond.getType())
.collect(Collectors.joining(",", "[", "]")));
return map;
}

public static ResourcesPrinterHelper<OperatorGroup> forOperatorGroup(Path filePath) {
return new ResourcesPrinterHelper<>(filePath,
ResourcesPrinterHelper::getOperatorGroupCols);
}

private static LinkedHashMap<String, String> getOperatorGroupCols(OperatorGroup og) {
LinkedHashMap<String, String> map = new LinkedHashMap<>(3);
map.put("NAME", og.getMetadata().getName());
map.put("TARGET NAMESPACE", og.getSpec().getTargetNamespaces().toString());
map.put("NAMESPACES", og.getStatus().getNamespaces().toString());
return map;
}

public static ResourcesPrinterHelper<Subscription> forSubscription(Path filePath) {
return new ResourcesPrinterHelper<>(filePath,
ResourcesPrinterHelper::getSubscriptionCols);
}

private static LinkedHashMap<String, String> getSubscriptionCols(Subscription subscription) {
LinkedHashMap<String, String> map = new LinkedHashMap<>(8);
map.put("NAME", subscription.getMetadata().getName());
map.put("SOURCE", subscription.getSpec().getSource());
map.put("CHANNEL", subscription.getSpec().getChannel());
map.put("STARTING CSV", subscription.getSpec().getStartingCSV());
map.put("INSTALLED CSV", subscription.getStatus().getInstalledCSV());
map.put("CURRENT CSV", subscription.getStatus().getCurrentCSV());
map.put("STATE", subscription.getStatus().getState());
map.put("REASON", subscription.getStatus().getReason());
return map;
}

public void row(X resource) {
row(resourceToCols.apply(resource));
}
Expand Down

0 comments on commit bbe3e25

Please sign in to comment.