Skip to content

Commit a1b66c3

Browse files
avanommuzikar
authored andcommitted
[OperatorHub] Rename WithOperator to WithOperatorHub and add missing
options
1 parent 235e022 commit a1b66c3

File tree

9 files changed

+265
-301
lines changed

9 files changed

+265
-301
lines changed

system-x/common/src/main/java/software/tnb/common/deployment/WithOperator.java

Lines changed: 0 additions & 44 deletions
This file was deleted.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package software.tnb.common.deployment;
2+
3+
import software.tnb.common.openshift.OpenshiftClient;
4+
import software.tnb.common.utils.WaitUtils;
5+
6+
import java.util.Optional;
7+
import java.util.function.BooleanSupplier;
8+
9+
public interface WithOperatorHub {
10+
String operatorName();
11+
12+
default String operatorCatalog() {
13+
return "redhat-operators";
14+
}
15+
16+
default String operatorCatalogNamespace() {
17+
return "openshift-marketplace";
18+
}
19+
20+
default String operatorChannel() {
21+
return "stable";
22+
}
23+
24+
default String subscriptionName() {
25+
return "tnb-" + getSuperClassName();
26+
}
27+
28+
default String targetNamespace() {
29+
return OpenshiftClient.get().getNamespace();
30+
}
31+
32+
default String getOperatorCatalog() {
33+
return getValue("operator.catalog", operatorCatalog());
34+
}
35+
36+
default String getOperatorChannel() {
37+
return getValue("operator.channel", operatorChannel());
38+
}
39+
40+
default String getOperatorName() {
41+
return getValue("operator.name", operatorName());
42+
}
43+
44+
default String getOperatorCatalogNamespace() {
45+
return getValue("catalog.namespace", operatorCatalogNamespace());
46+
}
47+
48+
default boolean clusterWide() {
49+
return false;
50+
}
51+
52+
default void createSubscription() {
53+
OpenshiftClient.get()
54+
.createSubscription(getOperatorChannel(), getOperatorName(), getOperatorCatalog(), subscriptionName(), getOperatorCatalogNamespace(),
55+
targetNamespace(), clusterWide());
56+
OpenshiftClient.get().waitForInstallPlanToComplete(subscriptionName(), targetNamespace());
57+
}
58+
59+
default void deleteSubscription(BooleanSupplier waitCondition) {
60+
OpenshiftClient.get().deleteSubscription(subscriptionName(), targetNamespace());
61+
WaitUtils.waitFor(waitCondition, "Waiting until the operator is undeployed");
62+
}
63+
64+
/**
65+
* this.getClass() should always be an openshift variant of a system-x service, for example OpenshiftMySQL.class
66+
* and this class should always extend the abstract system-x service class, e.g. OpenshiftMySQL extends MySQL
67+
* All properties are prefixed with the name of the parent class - for example to override the operator catalog, the resulting property name is
68+
* mysql.operator.catalog
69+
*
70+
* @return value from system property or default value
71+
*/
72+
private String getValue(final String property, final String defaultValue) {
73+
return Optional.ofNullable(System.getProperty(getSuperClassName() + "." + property)).orElse(defaultValue);
74+
}
75+
76+
private String getSuperClassName() {
77+
final Class<?> superclass = this.getClass().getSuperclass();
78+
if (Object.class.equals(superclass)) {
79+
throw new IllegalStateException("Current class " + this.getClass().getSimpleName() + " does not extend any other class"
80+
+ " and default method from WithOperatorHub was called, either override this method or "
81+
+ "check what's wrong as this shouldn't happen");
82+
}
83+
return superclass.getSimpleName().toLowerCase();
84+
}
85+
}

system-x/services/cryostat/src/main/java/software/tnb/cryostat/resource/openshift/OpenshiftCryostat.java

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package software.tnb.cryostat.resource.openshift;
22

33
import software.tnb.common.deployment.ReusableOpenshiftDeployable;
4-
import software.tnb.common.deployment.WithOperator;
4+
import software.tnb.common.deployment.WithOperatorHub;
55
import software.tnb.common.openshift.OpenshiftClient;
66
import software.tnb.common.utils.HTTPUtils;
77
import software.tnb.common.utils.WaitUtils;
@@ -26,16 +26,9 @@
2626
import okhttp3.Request;
2727

2828
@AutoService(Cryostat.class)
29-
public class OpenshiftCryostat extends Cryostat implements ReusableOpenshiftDeployable, WithOperator {
29+
public class OpenshiftCryostat extends Cryostat implements ReusableOpenshiftDeployable, WithOperatorHub {
3030

3131
private static final Logger LOG = LoggerFactory.getLogger(OpenshiftCryostat.class);
32-
33-
private static final String DEFAULT_CHANNEL = "stable";
34-
private static final String OPERATOR_NAME = "cryostat-operator";
35-
private static final String DEFAULT_SOURCE = "redhat-operators";
36-
private static final String SUBSCRIPTION_NAME = "tnb-cryostat";
37-
private static final String SUBSCRIPTION_NAMESPACE = "openshift-marketplace";
38-
3932
private static final String APP_NAME = "cryostat-" + OpenshiftClient.get().getNamespace();
4033

4134
private static final CustomResourceDefinitionContext CRYOSTAT_CTX = new CustomResourceDefinitionContext.Builder()
@@ -52,7 +45,8 @@ public void undeploy() {
5245
OpenshiftClient.get().customResource(CRYOSTAT_CTX).delete(OpenshiftClient.get().getNamespace(), APP_NAME, true);
5346
WaitUtils.waitFor(() -> OpenshiftClient.get().getLabeledPods(Map.of("kind", "cryostat", "app", APP_NAME))
5447
.isEmpty(), "Waiting until Cryostat pods are deleted");
55-
OpenshiftClient.get().deleteSubscription(SUBSCRIPTION_NAME);
48+
deleteSubscription(() -> OpenshiftClient.get().getLabeledPods("control-plane", "controller-manager")
49+
.stream().noneMatch(p -> p.getMetadata().getName().contains("cryostat")));
5650
} catch (IOException e) {
5751
LOG.error("Error on Cryostat deletetion", e);
5852
throw new RuntimeException(e);
@@ -79,8 +73,7 @@ public void closeResources() {
7973
public void create() {
8074
LOG.debug("Creating Cryostat instance");
8175
// Create subscription
82-
OpenshiftClient.get().createSubscription(operatorChannel(), OPERATOR_NAME, operatorCatalog(), SUBSCRIPTION_NAME, SUBSCRIPTION_NAMESPACE);
83-
OpenshiftClient.get().waitForInstallPlanToComplete(SUBSCRIPTION_NAME);
76+
createSubscription();
8477

8578
try {
8679
OpenshiftClient.get().customResource(CRYOSTAT_CTX).createOrReplace(getCryostatDefinition());
@@ -144,12 +137,7 @@ public void cleanup() {
144137
}
145138

146139
@Override
147-
public String defaultOperatorCatalog() {
148-
return DEFAULT_SOURCE;
149-
}
150-
151-
@Override
152-
public String defaultOperatorChannel() {
153-
return DEFAULT_CHANNEL;
140+
public String operatorName() {
141+
return "cryostat-operator";
154142
}
155143
}

system-x/services/elasticsearch/src/main/java/software/tnb/elasticsearch/resource/openshift/OpenshiftElasticsearch.java

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import software.tnb.common.deployment.ReusableOpenshiftDeployable;
88
import software.tnb.common.deployment.WithExternalHostname;
99
import software.tnb.common.deployment.WithInClusterHostname;
10-
import software.tnb.common.deployment.WithOperator;
10+
import software.tnb.common.deployment.WithOperatorHub;
1111
import software.tnb.common.openshift.OpenshiftClient;
1212
import software.tnb.elasticsearch.account.ElasticsearchAccount;
1313
import software.tnb.elasticsearch.service.Elasticsearch;
@@ -31,11 +31,7 @@
3131

3232
@AutoService(Elasticsearch.class)
3333
public class OpenshiftElasticsearch extends Elasticsearch implements ReusableOpenshiftDeployable, WithInClusterHostname, WithExternalHostname
34-
, WithOperator {
35-
private static final String DEFAULT_CHANNEL = "stable";
36-
private static final String OPERATOR_NAME = "elasticsearch-eck-operator-certified";
37-
private static final String DEFAULT_CATALOGSOURCE_NAME = "certified-operators";
38-
private static final String SUBSCRIPTION_NAME = "tnb-elasticsearch";
34+
, WithOperatorHub {
3935

4036
private static final CustomResourceDefinitionContext ELASTICSEARCH_CTX = new CustomResourceDefinitionContext.Builder()
4137
.withGroup("elasticsearch.k8s.elastic.co")
@@ -50,10 +46,7 @@ public class OpenshiftElasticsearch extends Elasticsearch implements ReusableOpe
5046

5147
@Override
5248
public void create() {
53-
OpenshiftClient.get().createSubscription(operatorChannel(), OPERATOR_NAME, operatorCatalog(), SUBSCRIPTION_NAME);
54-
55-
OpenShiftWaiters.get(OpenshiftClient.get(), () -> false).areExactlyNPodsReady(1, "control-plane", "elastic-operator")
56-
.timeout(300_000).waitFor();
49+
createSubscription();
5750

5851
try (InputStream is = this.getClass().getResourceAsStream("/cr.yaml")) {
5952
String content = IOUtils.toString(is, StandardCharsets.UTF_8)
@@ -85,13 +78,11 @@ public void create() {
8578
@Override
8679
public void undeploy() {
8780
OpenshiftClient.get().routes().withName(routeName).delete();
88-
OpenshiftClient.get().customResource(ELASTICSEARCH_CTX).delete(OpenshiftConfiguration.openshiftNamespace());
81+
OpenshiftClient.get().customResource(ELASTICSEARCH_CTX).inNamespace(OpenshiftConfiguration.openshiftNamespace()).delete();
8982
OpenShiftWaiters.get(OpenshiftClient.get(), () -> false)
9083
.areExactlyNPodsRunning(0, "elasticsearch.k8s.elastic.co/cluster-name", clusterName())
9184
.timeout(120_000).waitFor();
92-
OpenshiftClient.get().deleteSubscription(SUBSCRIPTION_NAME);
93-
OpenShiftWaiters.get(OpenshiftClient.get(), () -> false).areExactlyNPodsRunning(0, "control-plane", "elastic-operator")
94-
.timeout(120_000).waitFor();
85+
deleteSubscription(() -> OpenshiftClient.get().getLabeledPods("control-plane", "elastic-operator").isEmpty());
9586
}
9687

9788
@Override
@@ -146,12 +137,12 @@ public String inClusterHostname() {
146137
}
147138

148139
@Override
149-
public String defaultOperatorCatalog() {
150-
return DEFAULT_CATALOGSOURCE_NAME;
140+
public String operatorCatalog() {
141+
return "certified-operators";
151142
}
152143

153144
@Override
154-
public String defaultOperatorChannel() {
155-
return DEFAULT_CHANNEL;
145+
public String operatorName() {
146+
return "elasticsearch-eck-operator-certified";
156147
}
157148
}

system-x/services/hyperfoil/src/main/java/software/tnb/hyperfoil/resource/openshift/OpenshiftHyperfoil.java

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import software.tnb.common.deployment.ReusableOpenshiftDeployable;
44
import software.tnb.common.deployment.WithExternalHostname;
5-
import software.tnb.common.deployment.WithOperator;
5+
import software.tnb.common.deployment.WithOperatorHub;
66
import software.tnb.common.openshift.OpenshiftClient;
77
import software.tnb.common.utils.WaitUtils;
88
import software.tnb.hyperfoil.service.Hyperfoil;
@@ -25,16 +25,10 @@
2525
import io.fabric8.openshift.client.internal.readiness.OpenShiftReadiness;
2626

2727
@AutoService(Hyperfoil.class)
28-
public class OpenshiftHyperfoil extends Hyperfoil implements ReusableOpenshiftDeployable, WithExternalHostname, WithOperator {
28+
public class OpenshiftHyperfoil extends Hyperfoil implements ReusableOpenshiftDeployable, WithExternalHostname, WithOperatorHub {
2929

3030
private static final Logger LOG = LoggerFactory.getLogger(OpenshiftHyperfoil.class);
3131

32-
private static final String DEFAULT_CHANNEL = "alpha";
33-
private static final String OPERATOR_NAME = "hyperfoil-bundle";
34-
private static final String DEFAULT_SOURCE = "community-operators";
35-
private static final String SUBSCRIPTION_NAME = "tnb-hyperfoil";
36-
private static final String SUBSCRIPTION_NAMESPACE = "openshift-marketplace";
37-
3832
private static final String APP_NAME = "hyperfoil-" + OpenshiftClient.get().getNamespace();
3933

4034
private static final CustomResourceDefinitionContext HYPERFOIL_CTX = new CustomResourceDefinitionContext.Builder()
@@ -52,7 +46,8 @@ public void undeploy() {
5246
OpenshiftClient.get().customResource(HYPERFOIL_CTX).delete(OpenshiftClient.get().getNamespace(), APP_NAME, true);
5347
WaitUtils.waitFor(() -> OpenshiftClient.get().getLabeledPods(Map.of("kind", HYPERFOIL_CTX.getName(), "app", APP_NAME))
5448
.isEmpty(), "Waiting until hyperfoil pods are deleted");
55-
OpenshiftClient.get().deleteSubscription(SUBSCRIPTION_NAME);
49+
deleteSubscription(() -> OpenshiftClient.get().getLabeledPods("control-plane", "controller-manager")
50+
.stream().noneMatch(p -> p.getMetadata().getName().contains("hyperfoil")));
5651
} catch (IOException e) {
5752
LOG.error("Error on Hyperfoil deletetion", e);
5853
throw new RuntimeException(e);
@@ -78,9 +73,7 @@ public void closeResources() {
7873
@Override
7974
public void create() {
8075
LOG.debug("Creating Hyperfoil instance");
81-
// Create subscription
82-
OpenshiftClient.get().createSubscription(operatorChannel(), OPERATOR_NAME, operatorCatalog(), SUBSCRIPTION_NAME, SUBSCRIPTION_NAMESPACE);
83-
OpenshiftClient.get().waitForInstallPlanToComplete(SUBSCRIPTION_NAME);
76+
createSubscription();
8477

8578
try {
8679
OpenshiftClient.get().customResource(HYPERFOIL_CTX).createOrReplace(getHyperfoilDefinition());
@@ -143,12 +136,17 @@ public String externalHostname() {
143136
}
144137

145138
@Override
146-
public String defaultOperatorCatalog() {
147-
return DEFAULT_SOURCE;
139+
public String operatorCatalog() {
140+
return "community-operators";
141+
}
142+
143+
@Override
144+
public String operatorChannel() {
145+
return "alpha";
148146
}
149147

150148
@Override
151-
public String defaultOperatorChannel() {
152-
return DEFAULT_CHANNEL;
149+
public String operatorName() {
150+
return "hyperfoil-bundle";
153151
}
154152
}

system-x/services/jms/amq/src/main/java/software/tnb/jms/amq/resource/openshift/OpenshiftAMQBroker.java

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import software.tnb.common.deployment.OpenshiftDeployable;
66
import software.tnb.common.deployment.WithExternalHostname;
77
import software.tnb.common.deployment.WithInClusterHostname;
8-
import software.tnb.common.deployment.WithOperator;
8+
import software.tnb.common.deployment.WithOperatorHub;
99
import software.tnb.common.openshift.OpenshiftClient;
1010
import software.tnb.jms.amq.resource.openshift.generated.Acceptor;
1111
import software.tnb.jms.amq.resource.openshift.generated.ActiveMQArtemis;
@@ -47,18 +47,12 @@
4747
import io.fabric8.openshift.api.model.Route;
4848

4949
@AutoService(AMQBroker.class)
50-
public class OpenshiftAMQBroker extends AMQBroker implements OpenshiftDeployable, WithInClusterHostname, WithExternalHostname, WithOperator {
50+
public class OpenshiftAMQBroker extends AMQBroker implements OpenshiftDeployable, WithInClusterHostname, WithExternalHostname, WithOperatorHub {
5151
private static final Logger LOG = LoggerFactory.getLogger(OpenshiftAMQBroker.class);
5252

5353
public static final String BROKER_NAME = "tnb-amq-broker";
5454
private static final String SSL_SECRET_NAME = "tnb-ssl-secret";
5555

56-
private static final String DEFAULT_CHANNEL = "7.10.x";
57-
private static final String OPERATOR_NAME = "amq-broker-rhel8";
58-
private static final String DEFAULT_SOURCE = "redhat-operators";
59-
private static final String SUBSCRIPTION_NAME = "tnb-amq-broker";
60-
private static final String SUBSCRIPTION_NAMESPACE = "openshift-marketplace";
61-
6256
private static final CustomResourceDefinitionContext ARTEMIS_CTX = new CustomResourceDefinitionContext.Builder()
6357
.withName("ActiveMQArtemis")
6458
.withGroup("broker.amq.io")
@@ -71,9 +65,7 @@ public class OpenshiftAMQBroker extends AMQBroker implements OpenshiftDeployable
7165
public void create() {
7266
LOG.debug("Creating AMQ broker");
7367
// Create subscription for amq broker operator
74-
OpenshiftClient.get().createSubscription(operatorChannel(), OPERATOR_NAME, operatorCatalog(), SUBSCRIPTION_NAME, SUBSCRIPTION_NAMESPACE,
75-
OpenshiftClient.get().getNamespace(), false);
76-
OpenshiftClient.get().waitForInstallPlanToComplete(SUBSCRIPTION_NAME);
68+
createSubscription();
7769
// Create amq-broker custom resource
7870
amqBrokerCli().createOrReplace(createBrokerCR());
7971
}
@@ -114,9 +106,7 @@ public void undeploy() {
114106
OpenShiftWaiters.get(OpenshiftClient.get(), () -> false).areExactlyNPodsRunning(0, "ActiveMQArtemis", BROKER_NAME)
115107
.timeout(120_000).waitFor();
116108
OpenshiftClient.get().deleteSecret(SSL_SECRET_NAME);
117-
OpenshiftClient.get().deleteSubscription(SUBSCRIPTION_NAME);
118-
OpenShiftWaiters.get(OpenshiftClient.get(), () -> false).areExactlyNPodsRunning(0, "name", "amq-broker-operator")
119-
.timeout(120_000).waitFor();
109+
deleteSubscription(() -> OpenshiftClient.get().getLabeledPods("name", "amq-broker-operator").isEmpty());
120110
}
121111

122112
@Override
@@ -268,12 +258,12 @@ private NonNamespaceOperation<ActiveMQArtemis, ActiveMQArtemisList, Resource<Act
268258
}
269259

270260
@Override
271-
public String defaultOperatorCatalog() {
272-
return DEFAULT_SOURCE;
261+
public String operatorChannel() {
262+
return "7.10.x";
273263
}
274264

275265
@Override
276-
public String defaultOperatorChannel() {
277-
return DEFAULT_CHANNEL;
266+
public String operatorName() {
267+
return "amq-broker-rhel8";
278268
}
279269
}

0 commit comments

Comments
 (0)