Skip to content

Commit

Permalink
Fix eclipse-jkube#218: Remove RuntimeMode mode parametere from Plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
rohanKanojia committed Jun 16, 2020
1 parent 02155e6 commit a4f0176
Show file tree
Hide file tree
Showing 125 changed files with 176 additions and 3,531 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Usage:
### 1.0.0-SNAPSHOT
* Fix #187: Provided Dockerfile is always skipped in simple Dockerfile mode
* Fix #237: Remove deprecated fields and method calls
* Fix #218: Remove build mode from mojos
* Fix #192: Removed `@Deprecated` fields from ClusterAccess

### 1.0.0-alpha-4 (2020-06-08)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import io.fabric8.openshift.client.OpenShiftClient;
import org.eclipse.jkube.kit.common.KitLogger;
import org.eclipse.jkube.kit.common.util.OpenshiftHelper;
import org.eclipse.jkube.kit.config.resource.RuntimeMode;

import java.net.UnknownHostException;

Expand Down Expand Up @@ -90,17 +89,5 @@ public boolean isOpenShift() {
return false;
}

public RuntimeMode resolveRuntimeMode(RuntimeMode mode) {
RuntimeMode resolvedMode;
if (mode == null) {
mode = RuntimeMode.DEFAULT;
}
if (mode.isAuto()) {
resolvedMode = isOpenShiftImageStream() ? RuntimeMode.openshift : RuntimeMode.kubernetes;
} else {
resolvedMode = mode;
}
return resolvedMode;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -33,36 +33,23 @@ public enum RuntimeMode {
* onto cluster. It can be used both on vanilla Kubernetes and
* OpenShift.
*/
kubernetes(false, "Kubernetes"),
KUBERNETES("Kubernetes"),

/**
* Use special OpenShift features like BuildConfigs, DeploymentConfigs
* ImageStreams and S2I builds while deploying onto cluster. It can be
* used only when on OpenShift.
*/
openshift(false, "OpenShift"),
OPENSHIFT("OpenShift");

/**
* Detect automatically whether running cluster is OpenShift or Kuberentes.
* This is done by contacting cluster API server.
*/
auto(true, "Auto");

public static final RuntimeMode DEFAULT = RuntimeMode.auto;
public static final String FABRIC8_EFFECTIVE_PLATFORM_MODE = "jkube.internal.effective.platform.mode";

private boolean autoFlag;
private String label;

RuntimeMode(boolean autoFlag, String label) {
this.autoFlag = autoFlag;
RuntimeMode(String label) {
this.label = label;
}

public boolean isAuto() {
return autoFlag;
}

public String getLabel() {
return label;
}
Expand All @@ -71,7 +58,7 @@ public String getLabel() {
* Returns true if the given maven properties indicate running in OpenShift platform mode
*/
public static boolean isOpenShiftMode(Properties properties) {
return properties != null && Objects.equals(openshift.toString(), properties.getProperty(FABRIC8_EFFECTIVE_PLATFORM_MODE, ""));
return properties != null && Objects.equals(OPENSHIFT.toString(), properties.getProperty(FABRIC8_EFFECTIVE_PLATFORM_MODE, ""));
}
}

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

import io.fabric8.kubernetes.client.KubernetesClient;
import org.eclipse.jkube.kit.common.KitLogger;
import org.eclipse.jkube.kit.config.resource.RuntimeMode;

import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientException;
Expand All @@ -28,7 +27,6 @@
import mockit.Verifications;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -115,35 +113,4 @@ public void createDefaultClientInOpenShiftShouldReturnOpenShiftClient() {
assertNotNull(result);
assertTrue(result instanceof OpenShiftClient);
}

@Test
public void resolveRuntimeModeWithAutoInKubernetesShouldReturnKubernetes() {
// When
final RuntimeMode result = new ClusterAccess(logger, null).resolveRuntimeMode(null);
// Then
assertEquals(RuntimeMode.kubernetes, result);
}

@Test
public void resolveRuntimeModeWithAutoInOpenShiftShouldReturnOpenShift() {
// Given
// @formatter:off
new Expectations() {{
defaultKubernetesClient.isAdaptable(OpenShiftClient.class); result = true;
defaultOpenShiftClient.supportsOpenShiftAPIGroup("image.openshift.io"); result = true;
}};
// @formatter:on
// When
final RuntimeMode result = new ClusterAccess(logger, null).resolveRuntimeMode(null);
// Then
assertEquals(RuntimeMode.openshift, result);
}

@Test
public void resolveRuntimeModeWithSpecificShouldReturnSpecific() {
// When
final RuntimeMode result = new ClusterAccess(logger, null).resolveRuntimeMode(RuntimeMode.openshift);
// Then
assertEquals(RuntimeMode.openshift, result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public class JKubeServiceHub implements Closeable {
private ServiceHub dockerServiceHub;
@Getter
private BuildServiceConfig buildServiceConfig;
private RuntimeMode resolvedMode;
@Getter
private KubernetesClient client;
private LazyBuilder<ArtifactResolverService> artifactResolverService;
Expand All @@ -71,16 +70,9 @@ public JKubeServiceHub(
private void init() {
Objects.requireNonNull(configuration, "JKubeKitConfiguration is required");
Objects.requireNonNull(log, "log is a required parameter");
if (platformMode == null) {
platformMode = RuntimeMode.DEFAULT;
}
if (clusterAccess == null) {
clusterAccess = new ClusterAccess(log, ClusterConfiguration.from(System.getProperties()).build());
}
this.resolvedMode = clusterAccess.resolveRuntimeMode(platformMode);
if (resolvedMode != RuntimeMode.kubernetes && resolvedMode != RuntimeMode.openshift) {
throw new IllegalArgumentException("Unknown platform mode " + platformMode + " resolved as "+ resolvedMode);
}
this.client = clusterAccess.createDefaultClient();

// Lazily building services
Expand All @@ -96,7 +88,7 @@ protected ApplyService build() {
protected BuildService build() {
BuildService ret;
// Creating platform-dependent services
if (resolvedMode == RuntimeMode.openshift) {
if (platformMode == RuntimeMode.OPENSHIFT) {
if (!(client instanceof OpenShiftClient)) {
throw new IllegalStateException("OpenShift platform has been specified but OpenShift has not been detected!");
}
Expand All @@ -123,7 +115,7 @@ public void close() {
}

public RuntimeMode getRuntimeMode() {
return resolvedMode;
return platformMode;
}

public ArtifactResolverService getArtifactResolverService() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
import org.eclipse.jkube.kit.config.resource.RuntimeMode;
import org.eclipse.jkube.kit.config.service.kubernetes.DockerBuildService;
import org.eclipse.jkube.kit.config.service.openshift.OpenshiftBuildService;
import mockit.Expectations;
import mockit.Mocked;
import org.junit.Before;
import org.junit.Test;

import static junit.framework.TestCase.assertTrue;
Expand Down Expand Up @@ -52,25 +50,6 @@ public class JKubeServiceHubTest {
@Mocked
private BuildServiceConfig buildServiceConfig;

@Before
public void init() {
// @formatter:off
new Expectations() {{
clusterAccess.resolveRuntimeMode(RuntimeMode.kubernetes);
result = RuntimeMode.kubernetes;
minTimes = 0;

clusterAccess.resolveRuntimeMode(RuntimeMode.openshift);
result = RuntimeMode.openshift;
minTimes = 0;

clusterAccess.resolveRuntimeMode(RuntimeMode.auto);
result = RuntimeMode.kubernetes;
minTimes = 0;
}};
// @formatter:on
}

@Test(expected = NullPointerException.class)
public void testMissingClusterAccess() {
JKubeServiceHub.builder()
Expand All @@ -89,14 +68,15 @@ public void testMissingKitLogger() {
public void testBasicInit() {
// When
try (final JKubeServiceHub jKubeServiceHub = JKubeServiceHub.builder()
.platformMode(RuntimeMode.KUBERNETES)
.configuration(configuration)
.log(logger)
.build()
) {
// Then
assertThat(jKubeServiceHub, notNullValue());
assertThat(jKubeServiceHub.getClient(), notNullValue());
assertThat(jKubeServiceHub.getRuntimeMode(), is(RuntimeMode.kubernetes));
assertThat(jKubeServiceHub.getRuntimeMode(), is(RuntimeMode.KUBERNETES));
}
}

Expand All @@ -106,7 +86,7 @@ public void testObtainBuildService() {
.configuration(configuration)
.clusterAccess(clusterAccess)
.log(logger)
.platformMode(RuntimeMode.kubernetes)
.platformMode(RuntimeMode.KUBERNETES)
.dockerServiceHub(dockerServiceHub)
.buildServiceConfig(buildServiceConfig)
.build();
Expand All @@ -123,7 +103,7 @@ public void testObtainOpenshiftBuildService() {
.configuration(configuration)
.clusterAccess(clusterAccess)
.log(logger)
.platformMode(RuntimeMode.openshift)
.platformMode(RuntimeMode.OPENSHIFT)
.dockerServiceHub(dockerServiceHub)
.buildServiceConfig(buildServiceConfig)
.build();
Expand All @@ -140,7 +120,7 @@ public void testObtainArtifactResolverService() {
.configuration(configuration)
.clusterAccess(clusterAccess)
.log(logger)
.platformMode(RuntimeMode.kubernetes)
.platformMode(RuntimeMode.KUBERNETES)
.dockerServiceHub(dockerServiceHub)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private SecretNameTestConfig(PlatformMode mode, String tlsSecretNameConfig, Stri
}

private static final class AdaptTestConfig {
private final PlatformMode mode;
private final RuntimeMode mode;
private final String initContainerNameConfig;
private final String initContainerName;
private final String initContainerImageConfig;
Expand All @@ -73,7 +73,7 @@ private static final class AdaptTestConfig {
private final String jksVolumeNameConfig;
private final String jksVolumeName;

private AdaptTestConfig(PlatformMode mode, String initContainerNameConfig, String initContainerName,
private AdaptTestConfig(RuntimeMode mode, String initContainerNameConfig, String initContainerName,
String initContainerImageConfig, String initContainerImage, String tlsSecretVolumeNameConfig,
String tlsSecretVolumeName, String jksVolumeNameConfig, String jksVolumeName) {
this.mode = mode;
Expand All @@ -91,18 +91,18 @@ private AdaptTestConfig(PlatformMode mode, String initContainerNameConfig, Strin
@Test
public void testAdapt() throws Exception {
final AdaptTestConfig[] data = new AdaptTestConfig[] {
new AdaptTestConfig(PlatformMode.kubernetes, null, null, null, null, null, null, null, null),
new AdaptTestConfig(PlatformMode.openshift, null, "tls-jks-converter", null,
new AdaptTestConfig(RuntimeMode.KUBERNETES, null, null, null, null, null, null, null, null),
new AdaptTestConfig(RuntimeMode.OPENSHIFT, null, "tls-jks-converter", null,
"jimmidyson/pemtokeystore:v0.1.0", null, "tls-pem", null, "tls-jks"),
new AdaptTestConfig(PlatformMode.openshift, null, "tls-jks-converter", null,
new AdaptTestConfig(RuntimeMode.OPENSHIFT, null, "tls-jks-converter", null,
"jimmidyson/pemtokeystore:v0.1.0", "tls-a", "tls-a", null, "tls-jks"),
new AdaptTestConfig(PlatformMode.openshift, null, "tls-jks-converter", null,
new AdaptTestConfig(RuntimeMode.OPENSHIFT, null, "tls-jks-converter", null,
"jimmidyson/pemtokeystore:v0.1.0", null, "tls-pem", "jks-b", "jks-b"),
new AdaptTestConfig(PlatformMode.openshift, "test-container-name", "test-container-name", "image/123",
new AdaptTestConfig(RuntimeMode.OPENSHIFT, "test-container-name", "test-container-name", "image/123",
"image/123", "tls-a", "tls-a", "jks-b", "jks-b") };

for (final AdaptTestConfig tc : data) {
TreeMap configMap = new TreeMap() {
TreeMap<String, String> configMap = new TreeMap<String, String>() {
{
put(AutoTLSEnricher.Config.pemToJKSInitContainerName.name(), tc.initContainerNameConfig);
put(AutoTLSEnricher.Config.pemToJKSInitContainerImage.name(), tc.initContainerImageConfig);
Expand Down Expand Up @@ -138,9 +138,9 @@ public void testAdapt() throws Exception {
PodTemplate pt = (PodTemplate) klb.buildItems().get(0);

List<Container> initContainers = pt.getTemplate().getSpec().getInitContainers();
assertEquals(tc.mode == PlatformMode.openshift, !initContainers.isEmpty());
assertEquals(tc.mode == RuntimeMode.OPENSHIFT, !initContainers.isEmpty());

if (tc.mode == PlatformMode.kubernetes) {
if (tc.mode == RuntimeMode.KUBERNETES) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public FromSelector(GeneratorContext context) {
public String getFrom() {
RuntimeMode mode = context.getRuntimeMode();
OpenShiftBuildStrategy strategy = context.getStrategy();
if (mode == RuntimeMode.openshift && strategy == OpenShiftBuildStrategy.s2i) {
if (mode == RuntimeMode.OPENSHIFT && strategy == OpenShiftBuildStrategy.s2i) {
return getS2iBuildFrom();
} else {
return getDockerBuildFrom();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ protected void addFrom(BuildConfiguration.BuildConfigurationBuilder builder) {
* @return Docker image name which is never null
*/
protected String getImageName() {
if (getContext().getRuntimeMode() == RuntimeMode.openshift) {
if (getContext().getRuntimeMode() == RuntimeMode.OPENSHIFT) {
return getConfigWithFallback(Config.name, "jkube.generator.name", "%a:%l");
} else {
return getConfigWithFallback(Config.name, "jkube.generator.name", "%g/%a:%l");
Expand All @@ -180,7 +180,7 @@ protected String getImageName() {
* @return The docker registry if configured
*/
protected String getRegistry() {
if (getContext().getRuntimeMode() == RuntimeMode.openshift &&
if (getContext().getRuntimeMode() == RuntimeMode.OPENSHIFT &&
getContext().getStrategy() == OpenShiftBuildStrategy.s2i) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

import static org.eclipse.jkube.kit.config.image.build.OpenShiftBuildStrategy.s2i;
import static org.eclipse.jkube.kit.config.image.build.OpenShiftBuildStrategy.docker;
import static org.eclipse.jkube.kit.config.resource.RuntimeMode.openshift;
import static org.eclipse.jkube.kit.config.resource.RuntimeMode.OPENSHIFT;
import static org.junit.Assert.assertEquals;

public class FromSelectorTest {
Expand All @@ -43,12 +43,12 @@ public class FromSelectorTest {
@Test
public void simple() {
final TestCase[] testCases = new TestCase[]{
new TestCase(openshift, s2i, "s2i-prop", "istag-prop"),
new TestCase(openshift, docker, "docker-prop", "istag-prop"),
new TestCase(OPENSHIFT, s2i, "s2i-prop", "istag-prop"),
new TestCase(OPENSHIFT, docker, "docker-prop", "istag-prop"),
new TestCase(null, s2i, "docker-prop", "istag-prop"),
new TestCase(null, docker, "docker-prop", "istag-prop"),
new TestCase(openshift, null, "docker-prop", "istag-prop"),
new TestCase(openshift, null, "docker-prop", "istag-prop"),
new TestCase(OPENSHIFT, null, "docker-prop", "istag-prop"),
new TestCase(OPENSHIFT, null, "docker-prop", "istag-prop"),
new TestCase(null, null, "docker-prop", "istag-prop"),
new TestCase(null, null, "docker-prop", "istag-prop"),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ public void setupContextKubernetes(final Properties projectProps, final String c
ctx.getConfig(); result = config;
config.getConfig("test-generator", "from"); result = configFrom; minTimes = 0;
config.getConfig("test-generator", "fromMode"); result = configFromMode; minTimes = 0;
ctx.getRuntimeMode();result = RuntimeMode.kubernetes;minTimes = 0;
ctx.getRuntimeMode();result = RuntimeMode.KUBERNETES;minTimes = 0;
ctx.getStrategy(); result = null; minTimes = 0;
}};
}
Expand All @@ -319,7 +319,7 @@ public void setupContextOpenShift(final Properties projectProps, final String co
ctx.getConfig(); result = config;
config.getConfig("test-generator", "from"); result = configFrom; minTimes = 0;
config.getConfig("test-generator", "fromMode"); result = configFromMode; minTimes = 0;
ctx.getRuntimeMode();result = RuntimeMode.openshift;minTimes = 0;
ctx.getRuntimeMode();result = RuntimeMode.OPENSHIFT;minTimes = 0;
ctx.getStrategy(); result = OpenShiftBuildStrategy.s2i; minTimes = 0;
}};
}
Expand Down

0 comments on commit a4f0176

Please sign in to comment.