Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
Refactor #802: Remove resource object post processing
Browse files Browse the repository at this point in the history
  • Loading branch information
rohanKanojia committed Feb 26, 2019
1 parent 19c3689 commit c184b6f
Show file tree
Hide file tree
Showing 75 changed files with 764 additions and 332 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.maven.core.config.PlatformMode;
import io.fabric8.maven.core.config.RuntimeMode;
import io.fabric8.maven.core.util.kubernetes.OpenshiftHelper;
import io.fabric8.maven.docker.util.Logger;
import io.fabric8.openshift.client.DefaultOpenShiftClient;
Expand Down Expand Up @@ -122,13 +123,13 @@ public boolean isOpenShift(Logger log) {
return false;
}

public PlatformMode resolvePlatformMode(PlatformMode mode, Logger log) {
PlatformMode resolvedMode;
public RuntimeMode resolveRuntimeMode(RuntimeMode mode, Logger log) {
RuntimeMode resolvedMode;
if (mode == null) {
mode = PlatformMode.DEFAULT;
mode = RuntimeMode.DEFAULT;
}
if (mode.isAuto()) {
resolvedMode = isOpenShiftImageStream(log) ? PlatformMode.openshift : PlatformMode.kubernetes;
resolvedMode = isOpenShiftImageStream(log) ? RuntimeMode.openshift : RuntimeMode.kubernetes;
} else {
resolvedMode = mode;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.google.common.base.Objects;

/**
* Mode how to create resouce descriptors
* Mode how to create resource descriptors
*
* @author roland
* @since 25/05/16
Expand Down
71 changes: 71 additions & 0 deletions core/src/main/java/io/fabric8/maven/core/config/RuntimeMode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* Copyright 2016 Red Hat, Inc.
*
* Red Hat licenses this file to you under the Apache License, version
* 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package io.fabric8.maven.core.config;

import com.google.common.base.Objects;

import java.util.Properties;

/**
* Mode how to interact with Kubernetes/Openshift cluster.
*
* @author roland
* @since 25/05/16
*/
public enum RuntimeMode {

/**
* Create resources descriptors for vanilla Kubernetes
*/
kubernetes(false, "Kubernetes"),

/**
* Use special OpenShift features like BuildConfigs
*/
openshift(false, "OpenShift"),

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

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

private boolean autoFlag;
private String label;

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

public boolean isAuto() {
return autoFlag;
}

public String getLabel() {
return label;
}

/**
* Returns true if the given maven properties indicate running in OpenShift platform mode
*/
public static boolean isOpenShiftMode(Properties properties) {
return properties == null ? false : Objects.equal(openshift.toString(), properties.getProperty(FABRIC8_EFFECTIVE_PLATFORM_MODE, ""));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@

import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.maven.core.access.ClusterAccess;
import io.fabric8.maven.core.config.PlatformMode;
import io.fabric8.maven.core.config.RuntimeMode;
import io.fabric8.maven.core.config.RuntimeMode;
import io.fabric8.maven.core.service.kubernetes.DockerBuildService;
import io.fabric8.maven.core.service.openshift.OpenshiftBuildService;
import io.fabric8.maven.core.util.LazyBuilder;
Expand All @@ -41,7 +42,7 @@ public class Fabric8ServiceHub {
*/
private ClusterAccess clusterAccess;

private PlatformMode platformMode;
private RuntimeMode platformMode;

private Logger log;

Expand All @@ -58,7 +59,7 @@ public class Fabric8ServiceHub {
* Computed resources
*/

private PlatformMode resolvedMode;
private RuntimeMode resolvedMode;

private KubernetesClient client;

Expand All @@ -71,8 +72,8 @@ private void init() {
Objects.requireNonNull(clusterAccess, "clusterAccess");
Objects.requireNonNull(log, "log");

this.resolvedMode = clusterAccess.resolvePlatformMode(platformMode, log);
if (resolvedMode != PlatformMode.kubernetes && resolvedMode != PlatformMode.openshift) {
this.resolvedMode = clusterAccess.resolveRuntimeMode(platformMode, log);
if (resolvedMode != RuntimeMode.kubernetes && resolvedMode != RuntimeMode.openshift) {
throw new IllegalArgumentException("Unknown platform mode " + platformMode + " resolved as "+ resolvedMode);
}
this.client = clusterAccess.createDefaultClient(log);
Expand All @@ -90,7 +91,7 @@ protected ApplyService build() {
protected BuildService build() {
BuildService buildService;
// Creating platform-dependent services
if (resolvedMode == PlatformMode.openshift) {
if (resolvedMode == RuntimeMode.openshift) {
if (!(client instanceof OpenShiftClient)) {
throw new IllegalStateException("Openshift platform has been specified but Openshift has not been detected!");
}
Expand Down Expand Up @@ -135,7 +136,7 @@ public Builder clusterAccess(ClusterAccess clusterAccess) {
return this;
}

public Builder platformMode(PlatformMode platformMode) {
public Builder platformMode(RuntimeMode platformMode) {
hub.platformMode = platformMode;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@ public class ResourceVersioning {

private String jobVersion;

private String openshiftV1version;

public ResourceVersioning() {
}

public ResourceVersioning(String coreVersion, String extensionsVersion, String appsVersion, String jobVersion) {
public ResourceVersioning(String coreVersion, String extensionsVersion, String appsVersion, String jobVersion, String openshiftV1version) {
this.coreVersion = coreVersion;
this.extensionsVersion = extensionsVersion;
this.appsVersion = appsVersion;
this.jobVersion = jobVersion;
this.openshiftV1version = openshiftV1version;
}

public String getCoreVersion() {
Expand All @@ -63,6 +66,14 @@ public void setAppsVersion(String appsVersion) {
this.appsVersion = appsVersion;
}

public void setOpenshiftV1Version(String openshiftV1Version) {
this.openshiftV1version = openshiftV1Version;
}

public String getOpenshiftV1version() {
return openshiftV1version;
}

public String getJobVersion() {
return jobVersion;
}
Expand All @@ -89,6 +100,12 @@ public ResourceVersioning withAppsVersion(String appsVersion) {
return c;
}

public ResourceVersioning withOpenshiftV1Version(String version) {
ResourceVersioning c = copy();
c.setOpenshiftV1Version(version);
return c;
}

public ResourceVersioning withJobVersion(String jobVersion) {
ResourceVersioning c = copy();
c.setJobVersion(jobVersion);
Expand All @@ -102,11 +119,12 @@ public String toString() {
sb.append(", extensionsVersion='").append(extensionsVersion).append('\'');
sb.append(", appsVersion='").append(appsVersion).append('\'');
sb.append(", jobVersion='").append(jobVersion).append('\'');
sb.append(", openshiftV1Version='").append(openshiftV1version).append('\'');
sb.append('}');
return sb.toString();
}

protected ResourceVersioning copy() {
return new ResourceVersioning(coreVersion, extensionsVersion, appsVersion, jobVersion);
return new ResourceVersioning(coreVersion, extensionsVersion, appsVersion, jobVersion, openshiftV1version);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.fabric8.maven.core.util.kubernetes;

import io.fabric8.maven.core.config.PlatformMode;
import io.fabric8.maven.core.model.GroupArtifactVersion;
import java.io.File;
import java.io.FilenameFilter;
Expand Down Expand Up @@ -106,10 +107,12 @@ public class KubernetesResourceUtil {
public static final String API_EXTENSIONS_VERSION = "extensions/v1beta1";
public static final String API_APPS_VERSION = "apps/v1beta1";
public static final String JOB_VERSION = "batch/v1";
public static final String OPENSHIFT_V1_VERSION = "apps.openshift.io/v1";
public static final ResourceVersioning DEFAULT_RESOURCE_VERSIONING = new ResourceVersioning()
.withCoreVersion(API_VERSION)
.withExtensionsVersion(API_EXTENSIONS_VERSION)
.withAppsVersion(API_APPS_VERSION)
.withOpenshiftV1Version(OPENSHIFT_V1_VERSION)
.withJobVersion(JOB_VERSION);

public static final HashSet<Class<?>> SIMPLE_FIELD_TYPES = new HashSet<>();
Expand All @@ -129,13 +132,13 @@ public class KubernetesResourceUtil {
* @return the list builder
* @throws IOException
*/
public static KubernetesListBuilder readResourceFragmentsFrom(ResourceVersioning apiVersions,
public static KubernetesListBuilder readResourceFragmentsFrom(PlatformMode platformMode, ResourceVersioning apiVersions,
String defaultName,
File[] resourceFiles) throws IOException {
KubernetesListBuilder builder = new KubernetesListBuilder();
if (resourceFiles != null) {
for (File file : resourceFiles) {
HasMetadata resource = getResource(apiVersions, file, defaultName);
HasMetadata resource = getResource(platformMode, apiVersions, file, defaultName);
builder.addToItems(resource);
}
}
Expand All @@ -157,9 +160,9 @@ public static KubernetesListBuilder readResourceFragmentsFrom(ResourceVersioning
* @param file file to read, whose name must match {@link #FILENAME_PATTERN}. @return map holding the fragment
* @param appName resource name specifying resources belonging to this application
*/
public static HasMetadata getResource(ResourceVersioning apiVersions,
public static HasMetadata getResource(PlatformMode platformMode, ResourceVersioning apiVersions,
File file, String appName) throws IOException {
Map<String,Object> fragment = readAndEnrichFragment(apiVersions, file, appName);
Map<String,Object> fragment = readAndEnrichFragment(platformMode, apiVersions, file, appName);
ObjectMapper mapper = new ObjectMapper();
try {
return mapper.convertValue(fragment, HasMetadata.class);
Expand All @@ -171,12 +174,7 @@ public static HasMetadata getResource(ResourceVersioning apiVersions,
public static File[] listResourceFragments(File resourceDir) {
final Pattern filenamePattern = Pattern.compile(FILENAME_PATTERN);
final Pattern exludePattern = Pattern.compile(PROFILES_PATTERN);
return resourceDir.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return filenamePattern.matcher(name).matches() && !exludePattern.matcher(name).matches();
}
});
return resourceDir.listFiles((File dir, String name) -> filenamePattern.matcher(name).matches() && !exludePattern.matcher(name).matches());
}


Expand Down Expand Up @@ -222,7 +220,7 @@ public final static void updateKindFilenameMapper(final Map<String, List<String>
private static final String PROFILES_PATTERN = "^profiles?\\.ya?ml$";

// Read fragment and add default values
private static Map<String, Object> readAndEnrichFragment(ResourceVersioning apiVersions,
private static Map<String, Object> readAndEnrichFragment(PlatformMode platformMode, ResourceVersioning apiVersions,
File file, String appName) throws IOException {
Pattern pattern = Pattern.compile(FILENAME_PATTERN, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(file.getName());
Expand All @@ -239,6 +237,7 @@ private static Map<String, Object> readAndEnrichFragment(ResourceVersioning apiV

if (type != null) {
kind = getAndValidateKindFromType(file, type);

} else {
// Try name as type
kind = FILENAME_TO_KIND_MAPPER.get(name.toLowerCase());
Expand All @@ -248,6 +247,9 @@ private static Map<String, Object> readAndEnrichFragment(ResourceVersioning apiV
}
}

if(kind != null && kind.equals("Deployment") && platformMode.equals(PlatformMode.openshift)) {
kind = "DeploymentConfig";
}
addKind(fragment, kind, file.getName());

String apiVersion = apiVersions.getCoreVersion();
Expand All @@ -257,6 +259,8 @@ private static Map<String, Object> readAndEnrichFragment(ResourceVersioning apiV
apiVersion = apiVersions.getAppsVersion();
} else if (Objects.equals(kind, "Job")) {
apiVersion = apiVersions.getJobVersion();
} else if(Objects.equals(kind, "DeploymentConfig") && platformMode == PlatformMode.openshift) {
apiVersion = apiVersions.getOpenshiftV1version();
}
addIfNotExistent(fragment, "apiVersion", apiVersion);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import io.fabric8.kubernetes.api.model.RootPaths;
import io.fabric8.kubernetes.client.Client;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.maven.core.config.PlatformMode;
import io.fabric8.maven.core.config.RuntimeMode;
import io.fabric8.maven.docker.util.Logger;
import io.fabric8.openshift.client.OpenShiftClient;
import io.fabric8.openshift.client.server.mock.OpenShiftMockServer;
Expand All @@ -38,15 +38,15 @@ public class ClusterAccessTest {
@Mocked
private Logger logger;

private PlatformMode mode;
private RuntimeMode mode;

private List<String> paths = new ArrayList<String>() ;

OpenShiftMockServer mockServer = new OpenShiftMockServer(false);
OpenShiftClient client = mockServer.createOpenShiftClient();

@Test
public void openshiftPlatformModeTest() throws Exception {
public void openshiftRuntimeModeTest() throws Exception {

paths.add("/oapi");
paths.add("/oapi/v1");
Expand All @@ -59,18 +59,18 @@ public void openshiftPlatformModeTest() throws Exception {

ClusterAccess clusterAccess = new ClusterAccess(null, client);

mode = clusterAccess.resolvePlatformMode(PlatformMode.openshift, logger);
assertEquals(PlatformMode.openshift, mode);
mode = clusterAccess.resolveRuntimeMode(RuntimeMode.openshift, logger);
assertEquals(RuntimeMode.openshift, mode);

mode = clusterAccess.resolvePlatformMode(PlatformMode.DEFAULT, logger);
assertEquals(PlatformMode.openshift, mode);
mode = clusterAccess.resolveRuntimeMode(RuntimeMode.DEFAULT, logger);
assertEquals(RuntimeMode.openshift, mode);

mode = clusterAccess.resolvePlatformMode(null, logger);
assertEquals(PlatformMode.openshift, mode);
mode = clusterAccess.resolveRuntimeMode(null, logger);
assertEquals(RuntimeMode.openshift, mode);
}

@Test
public void kubernetesPlatformModeTest() throws Exception {
public void kubernetesRuntimeModeTest() throws Exception {

RootPaths rootpaths = new RootPaths();

Expand All @@ -80,14 +80,14 @@ public void kubernetesPlatformModeTest() throws Exception {

ClusterAccess clusterAccess = new ClusterAccess(null, client);

mode = clusterAccess.resolvePlatformMode(PlatformMode.kubernetes, logger);
assertEquals(PlatformMode.kubernetes, mode);
mode = clusterAccess.resolveRuntimeMode(RuntimeMode.kubernetes, logger);
assertEquals(RuntimeMode.kubernetes, mode);

mode = clusterAccess.resolvePlatformMode(PlatformMode.DEFAULT, logger);
assertEquals(PlatformMode.kubernetes, mode);
mode = clusterAccess.resolveRuntimeMode(RuntimeMode.DEFAULT, logger);
assertEquals(RuntimeMode.kubernetes, mode);

mode = clusterAccess.resolvePlatformMode(null, logger);
assertEquals(PlatformMode.kubernetes, mode);
mode = clusterAccess.resolveRuntimeMode(null, logger);
assertEquals(RuntimeMode.kubernetes, mode);
}

@Test
Expand Down
Loading

0 comments on commit c184b6f

Please sign in to comment.