forked from fabric8io/fabric8-maven-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor fabric8io#802: Remove resource object post processing
- Loading branch information
1 parent
6b5512b
commit 220f001
Showing
68 changed files
with
716 additions
and
314 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
core/src/main/java/io/fabric8/maven/core/config/RuntimeMode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
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, "")); | ||
} | ||
} |
174 changes: 174 additions & 0 deletions
174
core/src/main/java/io/fabric8/maven/core/handler/DeploymentConfigHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
/** | ||
* 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.handler; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
import io.fabric8.kubernetes.api.model.Container; | ||
import io.fabric8.kubernetes.api.model.HasMetadata; | ||
import io.fabric8.kubernetes.api.model.LabelSelector; | ||
import io.fabric8.kubernetes.api.model.ObjectMeta; | ||
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; | ||
import io.fabric8.kubernetes.api.model.PodSpec; | ||
import io.fabric8.kubernetes.api.model.PodTemplateSpec; | ||
import io.fabric8.kubernetes.api.model.apps.Deployment; | ||
import io.fabric8.kubernetes.api.model.apps.DeploymentBuilder; | ||
import io.fabric8.kubernetes.api.model.apps.DeploymentSpec; | ||
import io.fabric8.kubernetes.api.model.apps.DeploymentSpecBuilder; | ||
import io.fabric8.kubernetes.api.model.apps.DeploymentStrategy; | ||
import io.fabric8.maven.core.config.ResourceConfig; | ||
import io.fabric8.maven.core.config.RuntimeMode; | ||
import io.fabric8.maven.core.util.kubernetes.KubernetesHelper; | ||
import io.fabric8.maven.docker.config.ImageConfiguration; | ||
import io.fabric8.maven.docker.util.ImageName; | ||
import io.fabric8.openshift.api.model.DeploymentConfig; | ||
import io.fabric8.openshift.api.model.DeploymentConfigBuilder; | ||
import io.fabric8.openshift.api.model.DeploymentConfigFluent; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
/** | ||
*/ | ||
public class DeploymentConfigHandler { | ||
private final PodTemplateHandler podTemplateHandler; | ||
Long openshiftDeployTimeoutSeconds; // TODO: Make this configura | ||
|
||
DeploymentConfigHandler(PodTemplateHandler podTemplateHandler) { | ||
this.podTemplateHandler = podTemplateHandler; | ||
} | ||
|
||
public DeploymentConfig getDeploymentConfig(RuntimeMode runtimeMode, ResourceConfig config, | ||
List<ImageConfiguration> images, Boolean enableAutomaticTrigger) { | ||
Deployment deployment = new DeploymentBuilder() | ||
.withMetadata(createDeploymentConfigMetaData(config)) | ||
.withSpec(createDeploymentSpec(config, images)) | ||
.build(); | ||
|
||
return convert(runtimeMode, deployment, enableAutomaticTrigger); | ||
} | ||
|
||
// =========================================================== | ||
|
||
private ObjectMeta createDeploymentConfigMetaData(ResourceConfig config) { | ||
return new ObjectMetaBuilder() | ||
.withName(KubernetesHelper.validateKubernetesId(config.getControllerName(), "controller name")) | ||
.build(); | ||
} | ||
|
||
private DeploymentSpec createDeploymentSpec(ResourceConfig config, List<ImageConfiguration> images) { | ||
return new DeploymentSpecBuilder() | ||
.withReplicas(config.getReplicas()) | ||
.withTemplate(podTemplateHandler.getPodTemplate(config,images)) | ||
.build(); | ||
} | ||
|
||
public DeploymentConfig convert(RuntimeMode runtimeMode, HasMetadata item, Boolean enableAutomaticTrigger) { | ||
Deployment resource = (Deployment) item; | ||
DeploymentConfigBuilder builder = new DeploymentConfigBuilder(); | ||
builder.withMetadata(resource.getMetadata()); | ||
DeploymentSpec spec = resource.getSpec(); | ||
if (spec != null) { | ||
DeploymentConfigFluent.SpecNested<DeploymentConfigBuilder> specBuilder = builder.withNewSpec(); | ||
Integer replicas = spec.getReplicas(); | ||
if (replicas != null) { | ||
specBuilder.withReplicas(replicas); | ||
} | ||
Integer revisionHistoryLimit = spec.getRevisionHistoryLimit(); | ||
if (revisionHistoryLimit != null) { | ||
specBuilder.withRevisionHistoryLimit(revisionHistoryLimit); | ||
} | ||
|
||
LabelSelector selector = spec.getSelector(); | ||
if (selector != null) { | ||
Map<String, String> matchLabels = selector.getMatchLabels(); | ||
if (matchLabels != null && !matchLabels.isEmpty()) { | ||
specBuilder.withSelector(matchLabels); | ||
} | ||
} | ||
Map<String, String> containerToImageMap = new HashMap<>(); | ||
PodTemplateSpec template = spec.getTemplate(); | ||
if (template != null) { | ||
specBuilder.withTemplate(template); | ||
PodSpec podSpec = template.getSpec(); | ||
Objects.requireNonNull(podSpec, "No PodSpec for PodTemplate:" + template); | ||
List<Container> containers = podSpec.getContainers(); | ||
Objects.requireNonNull(podSpec, "No containers for PodTemplate.spec: " + template); | ||
for (Container container : containers) { | ||
validateContainer(container); | ||
containerToImageMap.put(container.getName(), container.getImage()); | ||
} | ||
} | ||
DeploymentStrategy strategy = spec.getStrategy(); | ||
String strategyType = null; | ||
if (strategy != null) { | ||
strategyType = strategy.getType(); | ||
} | ||
if (openshiftDeployTimeoutSeconds != null && openshiftDeployTimeoutSeconds > 0) { | ||
if (StringUtils.isBlank(strategyType) || "Rolling".equals(strategyType)) { | ||
specBuilder.withNewStrategy().withType("Rolling"). | ||
withNewRollingParams().withTimeoutSeconds(openshiftDeployTimeoutSeconds).endRollingParams().endStrategy(); | ||
} else if ("Recreate".equals(strategyType)) { | ||
specBuilder.withNewStrategy().withType("Recreate"). | ||
withNewRecreateParams().withTimeoutSeconds(openshiftDeployTimeoutSeconds).endRecreateParams().endStrategy(); | ||
} else { | ||
specBuilder.withNewStrategy().withType(strategyType).endStrategy(); | ||
} | ||
} else if (StringUtils.isNotBlank(strategyType)) { | ||
// TODO is there any values we can copy across? | ||
specBuilder.withNewStrategy().withType(strategyType).endStrategy(); | ||
} | ||
|
||
if(enableAutomaticTrigger) { | ||
specBuilder.addNewTrigger().withType("ConfigChange").endTrigger(); | ||
} | ||
|
||
// add a new image change trigger for the build stream | ||
if (containerToImageMap.size() != 0) { | ||
if(runtimeMode.equals(RuntimeMode.openshift)) { | ||
for (Map.Entry<String, String> entry : containerToImageMap.entrySet()) { | ||
String containerName = entry.getKey(); | ||
ImageName image = new ImageName(entry.getValue()); | ||
String tag = image.getTag() != null ? image.getTag() : "latest"; | ||
specBuilder.addNewTrigger() | ||
.withType("ImageChange") | ||
.withNewImageChangeParams() | ||
.withAutomatic(true) | ||
.withNewFrom() | ||
.withKind("ImageStreamTag") | ||
.withName(image.getSimpleName() + ":" + tag) | ||
.withNamespace(image.getUser()) | ||
.endFrom() | ||
.withContainerNames(containerName) | ||
.endImageChangeParams() | ||
.endTrigger(); | ||
} | ||
} | ||
} | ||
|
||
specBuilder.endSpec(); | ||
} | ||
return builder.build(); | ||
} | ||
|
||
private void validateContainer(Container container) { | ||
if (container.getImage() == null) { | ||
throw new IllegalArgumentException("Container " + container.getName() + " has no Docker image configured. " + | ||
"Please check your Docker image configuration (including the generators which are supposed to run)"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.