Skip to content

Commit

Permalink
[issues-k8s-support] - Add a k8s client, and perform refactoring on t…
Browse files Browse the repository at this point in the history
…he core and provisioers modules in order to introduce Olm based provisioning, and generic operator provisioners. Also fixing the testsuite modules accordingly
  • Loading branch information
fabiobrz committed Nov 4, 2024
1 parent 8a836e2 commit 09681ca
Show file tree
Hide file tree
Showing 121 changed files with 4,879 additions and 1,553 deletions.
5 changes: 5 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@
<artifactId>http-client</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jboss.intersmash</groupId>
<artifactId>intersmash-kubernetes-client</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
13 changes: 9 additions & 4 deletions core/src/main/java/org/jboss/intersmash/IntersmashConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,15 @@ public class IntersmashConfig {
private static final String DEPLOYMENTS_REPOSITORY_REF = "intersmash.deployments.repository.ref";

// Default Catalog for Operators
private static final String DEFAULT_OPERATOR_CATALOG_SOURCE_NAMESPACE = "openshift-marketplace";
private static final String KUBERNETES_OPERATOR_CATALOG_SOURCE_NAMESPACE = "olm";
private static final String OPENSHIFT_OPERATOR_CATALOG_SOURCE_NAMESPACE = "openshift-marketplace";
private static final String DEFAULT_OPERATOR_CATALOG_SOURCE_NAMESPACE = KUBERNETES_OPERATOR_CATALOG_SOURCE_NAMESPACE;
private static final String REDHAT_OPERATOR_CATALOG_SOURCE_NAME = "redhat-operators";
private static final String COMMUNITY_OPERATOR_CATALOG_SOURCE_NAME = "community-operators";
private static final String OPERATORHUB_IO_OPERATOR_CATALOG_SOURCE_NAME = "operatorhubio-catalog";
private static final String DEFAULT_OPERATOR_CATALOG_SOURCE_NAME = COMMUNITY_OPERATOR_CATALOG_SOURCE_NAME;
private static final String OLM_OPERATOR_CATALOG_SOURCE_NAME = "intersmash.olm.operators.catalog_source";
private static final String OLM_OPERATOR_CATALOG_SOURCE_NAMESPACE = "intersmash.olm.operators.namespace";

// Custom Catalogs for operators
private static final String INFINISPAN_OPERATOR_CATALOG_SOURCE_NAME = "intersmash.infinispan.operators.catalog_source";
Expand Down Expand Up @@ -133,11 +138,11 @@ public static String[] getKnownCatalogSources() {
}

public static String defaultOperatorCatalogSourceName() {
return DEFAULT_OPERATOR_CATALOG_SOURCE_NAME;
return XTFConfig.get(OLM_OPERATOR_CATALOG_SOURCE_NAME, DEFAULT_OPERATOR_CATALOG_SOURCE_NAME);
}

public static String defaultOperatorCatalogSourceNamespace() {
return DEFAULT_OPERATOR_CATALOG_SOURCE_NAMESPACE;
return XTFConfig.get(OLM_OPERATOR_CATALOG_SOURCE_NAMESPACE, DEFAULT_OPERATOR_CATALOG_SOURCE_NAMESPACE);
}

public static String infinispanOperatorCatalogSource() {
Expand Down Expand Up @@ -221,7 +226,7 @@ public static String activeMQOperatorPackageManifest() {
}

public static String hyperfoilOperatorCatalogSource() {
return XTFConfig.get(HYPERFOIL_OPERATOR_CATALOG_SOURCE_NAME, COMMUNITY_OPERATOR_CATALOG_SOURCE_NAME);
return XTFConfig.get(HYPERFOIL_OPERATOR_CATALOG_SOURCE_NAME, defaultOperatorCatalogSourceName());
}

public static String hyperfoilOperatorIndexImage() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.intersmash.application.openshift;
package org.jboss.intersmash.application.k8s;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.intersmash.provision.openshift;
package org.jboss.intersmash.application.k8s;

import java.util.List;

Expand All @@ -25,8 +25,8 @@
public interface HasPods {

/**
* Return pods of the application.
* @return a list of related {@link Pod} instances
* Get the provisioned application service related Pods
* @return A list of related {@link Pod} instances
*/
List<Pod> getPods();
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.intersmash.application.openshift;
package org.jboss.intersmash.application.k8s;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,23 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.intersmash.application.openshift;
package org.jboss.intersmash.application.operator;

import java.util.Collections;
import java.util.List;

import org.jboss.intersmash.application.Application;
import org.jboss.intersmash.application.k8s.HasConfigMaps;
import org.jboss.intersmash.application.k8s.HasSecrets;

import io.fabric8.kubernetes.api.model.ConfigMap;
import io.fabric8.kubernetes.api.model.Secret;

/**
* This interface is not supposed to be implemented by user Applications. See the "Mapping of implemented provisioners"
* section of Intersmash README.md file for the up-to-date list of supported end users Applications.
*/
public interface OperatorApplication extends OpenShiftApplication, HasSecrets, HasConfigMaps {
public interface OperatorApplication extends Application, HasSecrets, HasConfigMaps {

@Override
default List<Secret> getSecrets() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.jboss.intersmash.IntersmashConfig;
import org.jboss.intersmash.annotations.Intersmash;
import org.jboss.intersmash.annotations.Service;
import org.jboss.intersmash.application.openshift.OperatorApplication;
import org.jboss.intersmash.application.operator.OperatorApplication;
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.jupiter.api.extension.ExtensionContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.jboss.intersmash.junit5;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
Expand All @@ -29,9 +30,11 @@
import org.jboss.intersmash.annotations.ServiceProvisioner;
import org.jboss.intersmash.annotations.ServiceUrl;
import org.jboss.intersmash.application.Application;
import org.jboss.intersmash.k8s.KubernetesConfig;
import org.jboss.intersmash.k8s.client.Kuberneteses;
import org.jboss.intersmash.provision.Provisioner;
import org.jboss.intersmash.provision.ProvisionerManager;
import org.jboss.intersmash.provision.openshift.operator.resources.OperatorGroup;
import org.jboss.intersmash.provision.olm.OperatorGroup;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
Expand All @@ -42,6 +45,7 @@
import org.opentest4j.AssertionFailedError;
import org.opentest4j.TestAbortedException;

import cz.xtf.core.config.OpenShiftConfig;
import cz.xtf.core.openshift.OpenShifts;
import lombok.extern.slf4j.Slf4j;

Expand Down Expand Up @@ -69,15 +73,20 @@ public void beforeAll(ExtensionContext extensionContext) throws Exception {

// we don't want to touch anything if the deployment phase is skipped
if (!IntersmashConfig.skipDeploy()) {
if (IntersmashExtensionHelper.isIntersmashTargetingOpenShift(extensionContext)) {
if (!IntersmashConfig.isOcp3x(OpenShifts.admin())) {
operatorCleanup();
log.debug("Deploy operatorgroup [{}] to enable operators subscription into tested namespace",
OperatorGroup.SINGLE_NAMESPACE.getMetadata().getName());
OpenShifts.adminBinary().execute("apply", "-f",
OperatorGroup.SINGLE_NAMESPACE.save().getAbsolutePath());
if (IntersmashExtensionHelper.isIntersmashTargetingOperator(extensionContext)) {
final boolean cleanupKubernetes = IntersmashExtensionHelper
.isIntersmashTargetingKubernetes(extensionContext) && !IntersmashConfig.isOcp3x(OpenShifts.admin()),
cleanupOpenShift = IntersmashExtensionHelper.isIntersmashTargetingOpenShift(extensionContext)
&& !IntersmashConfig.isOcp3x(OpenShifts.admin());
operatorCleanup(cleanupKubernetes, cleanupOpenShift);
deployOperatorGroup(extensionContext);

if (IntersmashExtensionHelper.isIntersmashTargetingOpenShift(extensionContext)) {
OpenShifts.master().clean().waitFor();
}
if (IntersmashExtensionHelper.isIntersmashTargetingKubernetes(extensionContext)) {
Kuberneteses.master().clean().waitFor();
}
OpenShifts.master().clean().waitFor();
}
}

Expand Down Expand Up @@ -145,28 +154,26 @@ public void afterAll(ExtensionContext extensionContext) {
}
// operator group is not bound to a specific product
// no Operator support on OCP3 clusters, OLM doesn't run there
if (IntersmashExtensionHelper.isIntersmashTargetingOpenShift(extensionContext)
&& !IntersmashConfig.isOcp3x(OpenShifts.admin())) {
operatorCleanup();
if (IntersmashExtensionHelper.isIntersmashTargetingOperator(extensionContext)) {
final boolean cleanupKubernetes = IntersmashExtensionHelper.isIntersmashTargetingKubernetes(extensionContext)
&& !IntersmashConfig.isOcp3x(OpenShifts.admin()),
cleanupOpenShift = IntersmashExtensionHelper.isIntersmashTargetingOpenShift(extensionContext)
&& !IntersmashConfig.isOcp3x(OpenShifts.admin());
operatorCleanup(cleanupKubernetes, cleanupOpenShift);
}
// let's cleanup once we're done
safetyCleanup();
safetyCleanup(extensionContext);
}
}

private static void safetyCleanup() {
private static void safetyCleanup(ExtensionContext extensionContext) {
log.info("Cleaning up the remaining resources on the cluster.");
OpenShifts.master().clean().waitFor();
}

/**
* Clean all OLM related objects.
* <p>
*/
public static void operatorCleanup() {
OpenShifts.adminBinary().execute("delete", "subscription", "--all");
OpenShifts.adminBinary().execute("delete", "csvs", "--all");
OpenShifts.adminBinary().execute("delete", "operatorgroup", "--all");
if (IntersmashExtensionHelper.isIntersmashTargetingOpenShift(extensionContext)) {
OpenShifts.master().clean().waitFor();
}
if (IntersmashExtensionHelper.isIntersmashTargetingKubernetes(extensionContext)) {
Kuberneteses.master().clean().waitFor();
}
}

@Override
Expand Down Expand Up @@ -223,4 +230,37 @@ private void injectServiceProvisioner(Object o, ExtensionContext extensionContex
}
}
}

private static void deployOperatorGroup(ExtensionContext extensionContext) throws IOException {
if (IntersmashExtensionHelper.isIntersmashTargetingKubernetes(extensionContext)) {
log.debug("Deploy operatorgroup [{}] to enable operators subscription into tested namespace",
new OperatorGroup(KubernetesConfig.namespace()).getMetadata().getName());
OpenShifts.adminBinary().execute("apply", "-f",
new OperatorGroup(KubernetesConfig.namespace()).save().getAbsolutePath());
}
if (IntersmashExtensionHelper.isIntersmashTargetingOpenShift(extensionContext)
&& !IntersmashConfig.isOcp3x(OpenShifts.admin())) {
log.debug("Deploy operatorgroup [{}] to enable operators subscription into tested namespace",
new OperatorGroup(OpenShiftConfig.namespace()).getMetadata().getName());
OpenShifts.adminBinary().execute("apply", "-f",
new OperatorGroup(OpenShiftConfig.namespace()).save().getAbsolutePath());
}
}

/**
* Clean all OLM related objects.
* <p>
*/
public static void operatorCleanup(final boolean cleanupKubernetes, final boolean cleanupOpenShift) {
if (cleanupKubernetes) {
Kuberneteses.adminBinary().execute("delete", "subscription", "--all");
Kuberneteses.adminBinary().execute("delete", "csvs", "--all");
Kuberneteses.adminBinary().execute("delete", "operatorgroup", "--all");
}
if (cleanupOpenShift) {
OpenShifts.adminBinary().execute("delete", "subscription", "--all");
OpenShifts.adminBinary().execute("delete", "csvs", "--all");
OpenShifts.adminBinary().execute("delete", "operatorgroup", "--all");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.jboss.intersmash.annotations.Intersmash;
import org.jboss.intersmash.application.k8s.KubernetesApplication;
import org.jboss.intersmash.application.openshift.OpenShiftApplication;
import org.jboss.intersmash.application.operator.OperatorApplication;
import org.jboss.intersmash.provision.Provisioner;
import org.junit.jupiter.api.extension.ExtensionContext;

Expand Down Expand Up @@ -50,7 +51,7 @@ public static Intersmash getIntersmash(ExtensionContext extensionContext) {
return result;
} else {
Intersmash[] intersmashes = extensionContext.getRequiredTestClass().getAnnotationsByType(Intersmash.class);
Intersmash intersmash;

if (intersmashes.length > 0) {
store.put(INTERSMASH, intersmashes[0]);
return (Intersmash) store.get(INTERSMASH);
Expand All @@ -59,6 +60,11 @@ public static Intersmash getIntersmash(ExtensionContext extensionContext) {
}
}

public static Boolean isIntersmashTargetingOperator(ExtensionContext extensionContext) {
return Arrays.stream(getIntersmash(extensionContext).value())
.anyMatch(app -> OperatorApplication.class.isAssignableFrom(app.value()));
}

public static Boolean isIntersmashTargetingOpenShift(ExtensionContext extensionContext) {
return Arrays.stream(getIntersmash(extensionContext).value())
.anyMatch(app -> OpenShiftApplication.class.isAssignableFrom(app.value()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,34 @@

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import org.jboss.intersmash.application.k8s.KubernetesApplication;
import org.jboss.intersmash.application.openshift.HasConfigMaps;
import org.jboss.intersmash.application.openshift.HasSecrets;
import org.jboss.intersmash.application.Application;
import org.jboss.intersmash.application.k8s.HasConfigMaps;
import org.jboss.intersmash.application.k8s.HasSecrets;
import org.jboss.intersmash.k8s.client.Kubernetes;
import org.jboss.intersmash.k8s.client.Kuberneteses;
import org.jboss.intersmash.provision.Provisioner;
import org.jboss.intersmash.provision.openshift.HasPods;
import org.jboss.intersmash.application.k8s.HasPods;
import org.jboss.intersmash.provision.openshift.Scalable;

import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.ConfigBuilder;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.client.NamespacedKubernetesClient;
import io.fabric8.kubernetes.client.NamespacedKubernetesClientAdapter;

/**
* Provisioner that is supposed to deploy an application on Kubernetes.
*/
public interface KubernetesProvisioner<T extends KubernetesApplication> extends Provisioner<T>, Scalable, HasPods {
public interface KubernetesProvisioner<T extends Application> extends Provisioner<T>, Scalable, HasPods {

// TODO - check for aq new class of statics like XTF OpenShifts?
KubernetesClient kubernetes = newKubernetesClient();
Kubernetes kubernetes = Kuberneteses.master();

static KubernetesClient newKubernetesClient() {
Config config = new ConfigBuilder()
.build();
return new DefaultKubernetesClient(config);
default NamespacedKubernetesClientAdapter<NamespacedKubernetesClient> client() {
return kubernetes;
}

default String execute(String... args) {
return Kuberneteses.adminBinary().execute(args);
}

@Override
Expand Down Expand Up @@ -84,4 +87,10 @@ default URL getURL() {
String.format("Failed to get an URL for the \"%s\" route", this.getClass().getSimpleName()), ex);
}
}

@Override
default List<Pod> getPods() {
return KubernetesProvisioner.kubernetes.pods().inNamespace(KubernetesProvisioner.kubernetes.getNamespace()).list()
.getItems();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.intersmash.provision.openshift.operator.resources;
package org.jboss.intersmash.provision.olm;

import io.fabric8.openshift.api.model.operatorhub.v1alpha1.CatalogSourceBuilder;

/**
* Wrapper for creating and using {@link io.fabric8.openshift.api.model.operatorhub.v1alpha1.CatalogSource}
*/
public class CatalogSource extends io.fabric8.openshift.api.model.operatorhub.v1alpha1.CatalogSource
implements OpenShiftResource<CatalogSource> {
implements SerializationCapableResource<CatalogSource> {

public CatalogSource() {
super();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.intersmash.provision.openshift.operator.resources;
package org.jboss.intersmash.provision.olm;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import cz.xtf.core.config.OpenShiftConfig;
import io.fabric8.kubernetes.client.CustomResource;
import io.fabric8.kubernetes.model.annotation.Group;
import io.fabric8.kubernetes.model.annotation.Version;
Expand All @@ -34,8 +33,8 @@
*/
@Group("operators.coreos.com")
@Version("v1")
public class OperatorGroup extends CustomResource implements OpenShiftResource<OperatorGroup> {
public static final OperatorGroup SINGLE_NAMESPACE = new OperatorGroup(OpenShiftConfig.namespace());
public class OperatorGroup extends CustomResource implements SerializationCapableResource<OperatorGroup> {

private Map<String, List<String>> spec = new HashMap<>();

public OperatorGroup() {
Expand Down
Loading

0 comments on commit 09681ca

Please sign in to comment.