Skip to content

Commit

Permalink
Add support for CK global operator
Browse files Browse the repository at this point in the history
  • Loading branch information
bouskaJ committed May 24, 2023
1 parent 68e5906 commit 64a7aed
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Optional;
import java.util.Properties;
import java.util.function.BooleanSupplier;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -143,6 +144,25 @@ private static String getTokenByUsernameAndPassword(String username, String pass
}
}

/**
* Executes function in given namespace.
*
* @param ns namespace to use
* @param function a function to execute
* @return result of the function
*/
public synchronized <T> T inNamespace(String ns, Function<OpenshiftClient, T> function) {
String currentNs = get().config.getNamespace();
get().config.setNamespace(ns);
T result;
try {
result = function.apply(get());
} finally {
get().config.setNamespace(currentNs);
}
return result;
}

/**
* Creates the operatorgroup and subscription.
*
Expand Down
71 changes: 47 additions & 24 deletions fuse-products/src/main/java/software/tnb/product/ck/CamelK.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ public class CamelK extends OpenshiftProduct implements KameletOps, BeforeEachCa

protected CamelKClient camelKClient;

private boolean useGlobalInstallation() {
return CamelKConfiguration.getConfiguration().useGlobalInstallation();
}

private String operatorNamespace() {
return useGlobalInstallation() ? "openshift-operators" : OpenshiftClient.get().getNamespace();
}

@Override
public void setupProduct() {
// Avoid creating static clients in case the camel-k should not be running (all product instances are created in ProductFactory.create()
Expand All @@ -87,26 +95,31 @@ public void setupProduct() {
CamelKConfiguration config = CamelKConfiguration.getConfiguration();

if (!isReady()) {
LOG.info("Deploying Camel-K");
if (CamelKConfiguration.forceUpstream()) {
LOG.warn(
"You are going to deploy upstream version of Camel-K. "
+ "Be aware that upstream Camel-K APIs does not have to be compatible with the PROD ones and this installation can break the "
+ "cluster for other tests."
);
}
if (OpenshiftClient.get().operatorHub().catalogSources().inNamespace(config.subscriptionSourceNamespace())
.withName(config.subscriptionSource()).get() == null) {
LOG.error("Operator Hub catalog source {} not found! Set {} property to an existing catalog source or create a new catalog source"
+ " with {} name in {} namespace. Be careful, as if someone else uses the same cluster with a different version,"
+ " the deployments may fail due changes in CRDs between versions", config.subscriptionSource(),
CamelKConfiguration.SUBSCRIPTION_SOURCE, config.subscriptionSource(), config.subscriptionSourceNamespace());
throw new RuntimeException("Operator Hub catalog source " + config.subscriptionSource() + " not found!");
if (!useGlobalInstallation()) {
LOG.info("Deploying Camel-K");
if (CamelKConfiguration.forceUpstream()) {
LOG.warn(
"You are going to deploy upstream version of Camel-K. "
+ "Be aware that upstream Camel-K APIs does not have to be compatible with the PROD ones and this installation can "
+ "break the "
+ "cluster for other tests."
);
}
if (OpenshiftClient.get().operatorHub().catalogSources().inNamespace(config.subscriptionSourceNamespace())
.withName(config.subscriptionSource()).get() == null) {
LOG.error("Operator Hub catalog source {} not found! Set {} property to an existing catalog source or create a new catalog source"
+ " with {} name in {} namespace. Be careful, as if someone else uses the same cluster with a different version,"
+ " the deployments may fail due changes in CRDs between versions", config.subscriptionSource(),
CamelKConfiguration.SUBSCRIPTION_SOURCE, config.subscriptionSource(), config.subscriptionSourceNamespace());
throw new RuntimeException("Operator Hub catalog source " + config.subscriptionSource() + " not found!");
}

OpenshiftClient.get().createSubscription(config.subscriptionChannel(), config.subscriptionOperatorName(), config.subscriptionSource(),
config.subscriptionName(), config.subscriptionSourceNamespace(), OpenshiftClient.get().getNamespace(), false);
OpenshiftClient.get().waitForInstallPlanToComplete(config.subscriptionName());
} else {
LOG.info("Reusing global Camel-K installation.");
}

OpenshiftClient.get().createSubscription(config.subscriptionChannel(), config.subscriptionOperatorName(), config.subscriptionSource(),
config.subscriptionName(), config.subscriptionSourceNamespace(), OpenshiftClient.get().getNamespace(), false);
OpenshiftClient.get().waitForInstallPlanToComplete(config.subscriptionName());
}

// @formatter:off
Expand Down Expand Up @@ -174,8 +187,8 @@ private void setupLogger() {
private boolean kameletsDeployed() {
if (operatorKameletCount == -1) {
final PodShellOutput shellOutput =
OpenshiftClient.get().podShell(OpenshiftClient.get().getLabeledPods("name", "camel-k-operator").get(0))
.executeWithBash("ls /kamelets/* | wc -l");
OpenshiftClient.get().inNamespace(operatorNamespace(), c -> c.podShell(c.getLabeledPods("name", "camel-k-operator").get(0))
.executeWithBash("ls /kamelets/* | wc -l"));
if (!shellOutput.getError().isEmpty()) {
LOG.error("Unable to list all kamelets: {}", shellOutput.getError());
return false;
Expand All @@ -202,8 +215,17 @@ private boolean kameletsReady() {
});
}

private boolean platformReady() {
List<IntegrationPlatform> ip = camelKClient.v1().integrationPlatforms().list().getItems();
return ip.size() == 1 && ip.get(0).getStatus() != null && ip.get(0).getStatus().getPhase().equals("Ready");
}

@Override
public void teardownProduct() {
if (useGlobalInstallation()) {
LOG.debug("Skipping product teardown for global operator.");
return;
}
OpenshiftClient.get().deleteSubscription(CamelKConfiguration.getConfiguration().subscriptionName());
removeKamelets();
}
Expand Down Expand Up @@ -266,8 +288,9 @@ private App createApp(Object integrationSource) {

@Override
public boolean isReady() {
return ResourceFunctions.areExactlyNPodsReady(1).apply(OpenshiftClient.get().getLabeledPods("name", "camel-k-operator"))
&& kameletsDeployed() && kameletsReady();
return ResourceFunctions.areExactlyNPodsReady(1).apply(
OpenshiftClient.get().inNamespace(operatorNamespace(), c -> c.getLabeledPods("name", "camel-k-operator")))
&& platformReady() && kameletsDeployed() && kameletsReady();
}

@Override
Expand Down Expand Up @@ -366,7 +389,7 @@ public void removeIntegrations() {

@Override
public void beforeEach(ExtensionContext extensionContext) throws Exception {
Pod operator = OpenshiftClient.get().getLabeledPods("name", "camel-k-operator").get(0);
Pod operator = OpenshiftClient.get(operatorNamespace()).getLabeledPods("name", "camel-k-operator").get(0);
operatorLogOutput = TestConfiguration.appLocation()
.resolve(String.format("camel-k-operator-%s.log", extensionContext.getParent().get().getDisplayName()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

public abstract class CamelKConfiguration extends CamelConfiguration {
public static final String FORCE_UPSTREAM = "force.upstream";
public static final String USE_GLOBAL_INSTALLATION = "camelk.operator.global";

public static final String SUBSCRIPTION_CHANNEL = "camelk.subscription.channel";
public static final String SUBSCRIPTION_OPERATOR_NAME = "camelk.subscription.operatorName";
Expand Down Expand Up @@ -55,6 +56,10 @@ public static boolean forceUpstream() {
return getBoolean(FORCE_UPSTREAM, false);
}

public static boolean useGlobalInstallation() {
return getBoolean(USE_GLOBAL_INSTALLATION, false);
}

public static CamelKConfiguration getConfiguration() {
if (forceUpstream()) {
return new CamelKUpstreamConfiguration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

import software.tnb.product.openshift.OpenshiftTestParent;
import software.tnb.product.parent.TestParent;
import software.tnb.util.ck.TestCamelK;
import software.tnb.util.openshift.TestOpenshiftClient;
import software.tnb.common.config.TestConfiguration;
import software.tnb.common.openshift.OpenshiftClient;
import software.tnb.common.utils.IOUtils;
Expand All @@ -17,6 +13,10 @@
import software.tnb.product.ck.configuration.CamelKConfiguration;
import software.tnb.product.ck.configuration.CamelKProdConfiguration;
import software.tnb.product.ck.configuration.CamelKUpstreamConfiguration;
import software.tnb.product.openshift.OpenshiftTestParent;
import software.tnb.product.parent.TestParent;
import software.tnb.util.ck.TestCamelK;
import software.tnb.util.openshift.TestOpenshiftClient;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -182,6 +182,7 @@ public void isReadyShouldReturnFalseWhenKameletsAreNotDeployedTest() {
expectOperatorGet();
expectKameletLs();
expectKamelets(null);
expectIntegrationPlatform();

camelK.setClient(OpenshiftClient.get().adapt(CamelKClient.class));
assertThat(camelK.isReady()).isFalse();
Expand All @@ -194,6 +195,7 @@ public void kameletsReadyShouldReturnFalseWhenKameletIsntReadyTest() {
expectOperatorGet();
expectKameletLs();
expectKamelets(false);
expectIntegrationPlatform();

camelK.setClient(OpenshiftClient.get().adapt(CamelKClient.class));
assertThat(camelK.isReady()).isFalse();
Expand All @@ -206,6 +208,7 @@ public void shouldBeReadyTest() {
expectOperatorGet();
expectKameletLs();
expectKamelets(true);
expectIntegrationPlatform();

camelK.setClient(OpenshiftClient.get().adapt(CamelKClient.class));
assertThat(camelK.isReady()).isTrue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import io.fabric8.camelk.v1.IntegrationPlatformBuilder;
import io.fabric8.camelk.v1.IntegrationPlatformListBuilder;
import io.fabric8.camelk.v1.IntegrationPlatformStatusBuilder;
import io.fabric8.camelk.v1alpha1.KameletBinding;
import io.fabric8.camelk.v1alpha1.KameletBindingList;
import io.fabric8.kubernetes.api.model.ContainerBuilder;
Expand Down Expand Up @@ -82,4 +85,15 @@ protected void expectOperatorGet() {
.endItem().build()
).always();
}

protected void expectIntegrationPlatform() {
expectServer.expect().get().withPath("/apis/camel.apache.org/v1/namespaces/test/integrationplatforms")
.andReturn(200,
new IntegrationPlatformListBuilder()
.addNewItemLike(new IntegrationPlatformBuilder()
.withStatus(new IntegrationPlatformStatusBuilder().withPhase("Ready").build())
.build())
.endItem().build()
).always();
}
}

0 comments on commit 64a7aed

Please sign in to comment.