Skip to content

Commit

Permalink
Fix #548: Define property for skipping cluster autodetect/offline mode
Browse files Browse the repository at this point in the history
+ Add a property `jkube.offline` which which jkube would skip
  cluster auto detection and go straight to offline mode.
+ Update integration tests to use jkube.offline flag to reduce build time

Signed-off-by: Rohan Kumar <rohaan@redhat.com>
  • Loading branch information
rohanKanojia authored and manusa committed Jun 21, 2021
1 parent 32be156 commit 0af0925
Show file tree
Hide file tree
Showing 61 changed files with 185 additions and 116 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Usage:
* Fix #714: feat: Helm support for Golang expressions
* Port fabric8io/docker-maven-plugin#1318: Update ECR autorization token URL
* Fix #710: Support DockerImage as output for Openshift builds
* Fix #548: Define property for skipping cluster autodetect/offline mode

### 1.3.0
* Fix #497: Assembly descriptor removed but still in documentation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,62 +68,33 @@ public class JKubeServiceHub implements Closeable {
private LazyBuilder<UndeployService> undeployService;
private LazyBuilder<MigrateService> migrateService;
private LazyBuilder<DebugService> debugService;
private final boolean offline;

@Builder
public JKubeServiceHub(
ClusterAccess clusterAccess, RuntimeMode platformMode, KitLogger log,
ServiceHub dockerServiceHub, JKubeConfiguration configuration,
BuildServiceConfig buildServiceConfig,
LazyBuilder<ResourceService> resourceService) {
LazyBuilder<ResourceService> resourceService, boolean offline) {
this.clusterAccess = clusterAccess;
this.platformMode = platformMode;
this.log = log;
this.dockerServiceHub = dockerServiceHub;
this.configuration = configuration;
this.buildServiceConfig = buildServiceConfig;
this.resourceService = resourceService;
this.offline = offline;
init();
}

private void init() {
Objects.requireNonNull(configuration, "JKubeKitConfiguration is required");
Objects.requireNonNull(log, "log is a required parameter");
Objects.requireNonNull(platformMode, "platformMode is a required parameter");
if (clusterAccess == null) {
clusterAccess = new ClusterAccess(log,
ClusterConfiguration.from(System.getProperties(), configuration.getProject().getProperties()).build());
}
this.client = clusterAccess.createDefaultClient();

applyService = new LazyBuilder<>(() -> new ApplyService(client, log));
buildService = new LazyBuilder<>(() -> {
BuildService ret;
if (JKubeBuildStrategy.jib == buildServiceConfig.getJKubeBuildStrategy()) {
return new JibBuildService(JKubeServiceHub.this, log);
}
// Creating platform-dependent services
if (platformMode == RuntimeMode.OPENSHIFT) {
if (!isOpenShift(client)) {
throw new IllegalStateException("OpenShift platform has been specified but OpenShift has not been detected!");
}
// OpenShift services
ret = new OpenshiftBuildService((OpenShiftClient) client, log, JKubeServiceHub.this);
} else {
// Kubernetes services
ret = new DockerBuildService(JKubeServiceHub.this);
}
return ret;
});
initClusterAccessAndLazyBuilders();
artifactResolverService = new LazyBuilder<>(() -> new JKubeArtifactResolverService(configuration.getProject()));
undeployService = new LazyBuilder<>(() -> {
if (platformMode == RuntimeMode.OPENSHIFT && isOpenShift(client)) {
return new OpenshiftUndeployService(this, log);
}
return new KubernetesUndeployService(this, log);
});
migrateService = new LazyBuilder<>(() -> new MigrateService(getConfiguration().getBasedir(), log));
portForwardService = new LazyBuilder<>(() -> new PortForwardService(client, log));
debugService = new LazyBuilder<>(() -> new DebugService(log, client, portForwardService.get(), applyService.get()));
}

@Override
Expand Down Expand Up @@ -168,4 +139,57 @@ public DebugService getDebugService() {
return debugService.get();
}

private void initClusterAccessAndLazyBuilders() {
if (!offline) {
if (clusterAccess == null) {
clusterAccess = new ClusterAccess(log,
ClusterConfiguration.from(System.getProperties(), configuration.getProject().getProperties()).build());
}
this.client = clusterAccess.createDefaultClient();
}
applyService = new LazyBuilder<>(() -> {
validateIfConnectedToCluster();
return new ApplyService(client, log);
});
portForwardService = new LazyBuilder<>(() -> {
validateIfConnectedToCluster();
return new PortForwardService(client, log);
});
debugService = new LazyBuilder<>(() -> {
validateIfConnectedToCluster();
return new DebugService(log, client, portForwardService.get(), applyService.get());
});
undeployService = new LazyBuilder<>(() -> {
validateIfConnectedToCluster();
if (platformMode == RuntimeMode.OPENSHIFT && isOpenShift(client)) {
return new OpenshiftUndeployService(this, log);
}
return new KubernetesUndeployService(this, log);
});
buildService = new LazyBuilder<>(() -> {
BuildService ret;
if (JKubeBuildStrategy.jib == buildServiceConfig.getJKubeBuildStrategy()) {
return new JibBuildService(JKubeServiceHub.this, log);
}
// Creating platform-dependent services
if (platformMode == RuntimeMode.OPENSHIFT) {
validateIfConnectedToCluster();
if (!isOpenShift(client)) {
throw new IllegalStateException("OpenShift platform has been specified but OpenShift has not been detected!");
}
// OpenShift services
ret = new OpenshiftBuildService((OpenShiftClient) client, log, JKubeServiceHub.this);
} else {
// Kubernetes services
ret = new DockerBuildService(JKubeServiceHub.this);
}
return ret;
});
}

private void validateIfConnectedToCluster() {
if (client == null) {
throw new IllegalArgumentException("Connection to Cluster required. Please check if offline mode is set to false");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@
import static junit.framework.TestCase.assertTrue;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;

@SuppressWarnings({"ResultOfMethodCallIgnored", "unused"})
public class JKubeServiceHubTest {
Expand All @@ -60,25 +62,26 @@ public class JKubeServiceHubTest {

private JKubeServiceHub.JKubeServiceHubBuilder commonInit() {
return JKubeServiceHub.builder()
.configuration(configuration)
.clusterAccess(clusterAccess)
.log(logger)
.dockerServiceHub(dockerServiceHub)
.buildServiceConfig(buildServiceConfig);
.configuration(configuration)
.clusterAccess(clusterAccess)
.log(logger)
.dockerServiceHub(dockerServiceHub)
.offline(false)
.buildServiceConfig(buildServiceConfig);
}

@Test(expected = NullPointerException.class)
public void testMissingClusterAccess() {
JKubeServiceHub.builder()
.log(logger)
.build();
.log(logger)
.build();
}

@Test(expected = NullPointerException.class)
public void testMissingKitLogger() {
JKubeServiceHub.builder()
.clusterAccess(clusterAccess)
.build();
.clusterAccess(clusterAccess)
.build();
}

@SuppressWarnings("ResultOfMethodCallIgnored")
Expand All @@ -92,10 +95,10 @@ public void testBasicInit() {
// @formatter:on
// When
try (final JKubeServiceHub jKubeServiceHub = JKubeServiceHub.builder()
.platformMode(RuntimeMode.KUBERNETES)
.configuration(configuration)
.log(logger)
.build()
.platformMode(RuntimeMode.KUBERNETES)
.configuration(configuration)
.log(logger)
.build()
) {
// Then
assertThat(jKubeServiceHub, notNullValue());
Expand All @@ -108,8 +111,8 @@ public void testBasicInit() {
public void testGetBuildServiceInKubernetes() {
// Given
JKubeServiceHub hub = commonInit()
.platformMode(RuntimeMode.KUBERNETES)
.build();
.platformMode(RuntimeMode.KUBERNETES)
.build();
// When
BuildService buildService = hub.getBuildService();
// Then
Expand All @@ -121,14 +124,14 @@ public void testGetBuildServiceInKubernetes() {
public void testGetBuildServiceInOpenShift() {
// Given
// @formatter:off
new Expectations() {{
buildServiceConfig.getJKubeBuildStrategy(); result = null;
openShiftClient.isAdaptable(OpenShiftClient.class); result = true;
}};
// @formatter:on
new Expectations() {{
buildServiceConfig.getJKubeBuildStrategy(); result = null;
openShiftClient.isAdaptable(OpenShiftClient.class); result = true;
}};
// @formatter:on
JKubeServiceHub hub = commonInit()
.platformMode(RuntimeMode.OPENSHIFT)
.build();
.platformMode(RuntimeMode.OPENSHIFT)
.build();
// When
BuildService buildService = hub.getBuildService();
// Then
Expand All @@ -139,8 +142,8 @@ public void testGetBuildServiceInOpenShift() {
@Test
public void testGetArtifactResolverService() {
JKubeServiceHub hub = commonInit()
.platformMode(RuntimeMode.KUBERNETES)
.build();
.platformMode(RuntimeMode.KUBERNETES)
.build();

assertNotNull(hub.getArtifactResolverService());
}
Expand All @@ -154,8 +157,8 @@ public void testGetJibBuildServiceInKubernetes() {
}};
// @formatter:on
JKubeServiceHub hub = commonInit()
.platformMode(RuntimeMode.KUBERNETES)
.build();
.platformMode(RuntimeMode.KUBERNETES)
.build();
// When
BuildService buildService = hub.getBuildService();
// Then
Expand All @@ -167,8 +170,8 @@ public void testGetJibBuildServiceInKubernetes() {
public void testGetUndeployServiceInKubernetes() {
// Given
JKubeServiceHub hub = commonInit()
.platformMode(RuntimeMode.KUBERNETES)
.build();
.platformMode(RuntimeMode.KUBERNETES)
.build();
// When
final UndeployService result = hub.getUndeployService();
// Then
Expand All @@ -185,8 +188,8 @@ public void testGetUndeployServiceInOpenShiftWithInvalidClient() {
}};
// @formatter:on
JKubeServiceHub hub = commonInit()
.platformMode(RuntimeMode.OPENSHIFT)
.build();
.platformMode(RuntimeMode.OPENSHIFT)
.build();
// When
final UndeployService result = hub.getUndeployService();
// Then
Expand All @@ -203,8 +206,8 @@ public void testGetUndeployServiceInOpenShiftWithValidClient() {
}};
// @formatter:on
JKubeServiceHub hub = commonInit()
.platformMode(RuntimeMode.OPENSHIFT)
.build();
.platformMode(RuntimeMode.OPENSHIFT)
.build();
// When
final UndeployService result = hub.getUndeployService();
// Then
Expand Down Expand Up @@ -239,4 +242,24 @@ public void testGetDebugService() {
// Then
assertNotNull(debugService);
}
}

@Test
public void testBasicInitWithOffline() {
// Given + When
try (final JKubeServiceHub jKubeServiceHub = commonInit().platformMode(RuntimeMode.KUBERNETES).offline(true).build()) {
// Then
assertThat(jKubeServiceHub, notNullValue());
assertThat(jKubeServiceHub.getClient(), nullValue());
}
}

@Test
public void testAccessServiceWithNonInitializedClientThrowsException() {
// Given + When
try (final JKubeServiceHub jKubeServiceHub = commonInit().platformMode(RuntimeMode.KUBERNETES).offline(true).build()) {
// Then
assertThat(jKubeServiceHub, notNullValue());
assertThrows(IllegalArgumentException.class, jKubeServiceHub::getApplyService);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -975,4 +975,10 @@ endif::[]
| Number of replicas for the container.
|

| *offline*
| Whether to try detecting Kubernetes Cluster or stay offline.

Defaults to `false`.
| `jkube.offline`

|===
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
#

invoker.goals.1=k8s:resource
invoker.mavenOpts=-Djkube.verbose
invoker.mavenOpts=-Djkube.verbose -Djkube.offline=true
invoker.debug=false
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
#

invoker.goals.1=clean k8s:resource
invoker.mavenOpts=-Djkube.verbose
invoker.mavenOpts=-Djkube.verbose -Djkube.offline=true
invoker.debug=false
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
#

invoker.goals.1=clean k8s:resource
invoker.mavenOpts=-Djkube.verbose
invoker.mavenOpts=-Djkube.verbose -Djkube.offline=true
invoker.debug=false
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@

invoker.goals.1=clean install k8s:resource
invoker.debug=false
invoker.mavenOpts=-Djkube.offline=true
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
#

invoker.goals.1=clean k8s:resource
invoker.mavenOpts=-Djkube.verbose
invoker.mavenOpts=-Djkube.verbose -Djkube.offline=true
invoker.debug=false
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
#

invoker.goals.1=clean k8s:resource
invoker.mavenOpts=-Djkube.verbose
invoker.mavenOpts=-Djkube.verbose -Djkube.offline=true
invoker.debug=false
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
#

invoker.goals.1=clean k8s:resource
invoker.mavenOpts=-Djkube.verbose
invoker.mavenOpts=-Djkube.verbose -Djkube.offline=true
invoker.debug=false
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@

invoker.goals.1=clean k8s:resource
invoker.debug=false
invoker.mavenOpts=-Djkube.offline=true
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@

invoker.goals.1=clean k8s:resource k8s:helm
invoker.debug=false
invoker.mavenOpts=-Djkube.offline=true
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@

invoker.goals.1=clean k8s:resource
invoker.debug=false
invoker.mavenOpts=-Djkube.offline=true
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@

invoker.goals.1=clean package
invoker.debug=false
invoker.mavenOpts=-Djkube.offline=true
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ invoker.goals.2=clean k8s:resource -Pcreate-namespace
invoker.goals.3=clean k8s:resource -Pset-namespace
invoker.goals.4=clean k8s:resource -Pcreate-and-set-namespace
invoker.goals.5=clean k8s:resource -Pcreate-and-set-different-namespace
invoker.mavenOpts=-Djkube.offline=true
Loading

0 comments on commit 0af0925

Please sign in to comment.