Skip to content

Commit

Permalink
Fix: Template resolution and helm work in OpenShift-Maven-Plugin
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Nuri <[email protected]>
  • Loading branch information
manusa committed Jul 21, 2020
1 parent 1a3a2dd commit 03ed981
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 87 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ Usage:
* Fix warning message in log goal when no pod is found
* Fix #267: openshift-maven-plugin does not update Routes
* Fix #286: Refactor ImageConfiguration model
* Fix #283: Add support for WildFly Bootable JAR
* Fix #306: Template resolution and helm work in OpenShift-Maven-Plugin

### 1.0.0-alpha-4 (2020-06-08)
* Fix #173: Use OpenShift compliant git/vcs annotations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.UnknownHostException;
import java.nio.charset.Charset;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
Expand Down Expand Up @@ -87,7 +86,6 @@
import io.fabric8.openshift.api.model.DeploymentConfig;
import io.fabric8.openshift.api.model.DeploymentConfigSpec;
import io.fabric8.openshift.api.model.Template;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jkube.kit.common.KitLogger;
Expand Down Expand Up @@ -728,50 +726,6 @@ public static Pod getNewestPod(Collection<Pod> pods) {
return sortedPods.get(sortedPods.size() - 1);
}

public static void resolveTemplateVariablesIfAny(KubernetesList resources, File targetDir) throws IllegalStateException {
Template template = findTemplate(resources);
if (template != null) {
List<io.fabric8.openshift.api.model.Parameter> parameters = template.getParameters();
if (parameters == null || parameters.isEmpty()) {
return;
}
File kubernetesYaml = new File(targetDir, "kubernetes.yml");
resolveTemplateVariables(parameters, kubernetesYaml);
}
}

public static void resolveTemplateVariables(List<io.fabric8.openshift.api.model.Parameter> parameters, File kubernetesYaml) throws IllegalStateException {
String text;
try {
text = FileUtils.readFileToString(kubernetesYaml, Charset.defaultCharset());
} catch (IOException e) {
throw new IllegalStateException("Failed to load " + kubernetesYaml + " so we can replace template expressions " + e, e);
}
String original = text;
for (io.fabric8.openshift.api.model.Parameter parameter : parameters) {
String from = "${" + parameter.getName() + "}";
String to = parameter.getValue();
if (to == null) {
throw new IllegalStateException("Missing value for HELM template parameter " + from + " in " + kubernetesYaml);
}
text = text.replace(from, to);
}
if (!original.equals(text)) {
try {
FileUtils.writeStringToFile(kubernetesYaml, text, Charset.defaultCharset());
} catch (IOException e) {
throw new IllegalStateException("Failed to save " + kubernetesYaml + " after replacing template expressions " +e, e);
}
}
}

public static Template findTemplate(KubernetesList resources) {
return (Template) resources.getItems().stream()
.filter(template -> template instanceof Template)
.findFirst()
.orElse(null);
}

/**
* Convert a map of env vars to a list of K8s EnvVar objects.
* @param envVars the name-value map containing env vars
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ public class HelmMojo extends AbstractJKubeMojo {
/**
* The generated kubernetes YAML file
*/
@Parameter(property = PROPERTY_KUBERNETES_MANIFEST, defaultValue = "${basedir}/target/classes/META-INF/jkube/kubernetes.yml")
@Parameter(property = "jkube.kubernetesManifest", defaultValue = "${basedir}/target/classes/META-INF/jkube/kubernetes.yml")
File kubernetesManifest;

/**
* The generated kubernetes YAML file
*/
@Parameter(property = PROPERTY_KUBERNETES_TEMPLATE, defaultValue = "${basedir}/target/classes/META-INF/jkube/kubernetes")
@Parameter(property = "jkube.kubernetesTemplate", defaultValue = "${basedir}/target/classes/META-INF/jkube/kubernetes")
File kubernetesTemplate;

@Component
Expand All @@ -96,6 +96,14 @@ public void executeInternal() throws MojoExecutionException {
}
}

protected File getKubernetesManifest() {
return kubernetesManifest;
}

protected File getKubernetesTemplate() {
return kubernetesTemplate;
}

protected HelmConfig.HelmType getDefaultHelmType() {
return HelmConfig.HelmType.KUBERNETES;
}
Expand Down Expand Up @@ -152,12 +160,12 @@ private static List<Maintainer> maintainersFromProject(MavenProject mavenProject

private String findIconURL() throws MojoExecutionException {
String answer = null;
if (kubernetesManifest != null && kubernetesManifest.isFile()) {
if (getKubernetesManifest() != null && getKubernetesManifest().isFile()) {
KubernetesResource dto;
try {
dto = ResourceUtil.load(kubernetesManifest, KubernetesResource.class);
dto = ResourceUtil.load(getKubernetesManifest(), KubernetesResource.class);
} catch (IOException e) {
throw new MojoExecutionException("Failed to load kubernetes YAML " + kubernetesManifest + ". " + e, e);
throw new MojoExecutionException("Failed to load kubernetes YAML " + getKubernetesManifest() + ". " + e, e);
}
if (dto instanceof HasMetadata) {
answer = KubernetesHelper.getOrCreateAnnotations((HasMetadata) dto).get("jkube.io/iconUrl");
Expand All @@ -175,7 +183,7 @@ private String findIconURL() throws MojoExecutionException {
}
}
} else {
getLog().warn("No kubernetes manifest file has been generated yet by the kubernetes:resource goal at: " + kubernetesManifest);
getLog().warn("No kubernetes manifest file has been generated yet by the kubernetes:resource goal at: " + getKubernetesManifest());
}
return answer;
}
Expand All @@ -201,10 +209,10 @@ private Optional<File> firstProjectFile(String fileName) {
private List<Template> findTemplates() throws IOException {
final List<Template> ret = new ArrayList<>();
final File[] sourceFiles;
if (kubernetesTemplate != null && kubernetesTemplate.isDirectory()) {
sourceFiles = kubernetesTemplate.listFiles((dir, filename) -> filename.endsWith("-template.yml"));
} else if (kubernetesTemplate != null) {
sourceFiles = new File[] { kubernetesTemplate };
if (getKubernetesTemplate() != null && getKubernetesTemplate().isDirectory()) {
sourceFiles = getKubernetesTemplate().listFiles((dir, filename) -> filename.endsWith("-template.yml"));
} else if (getKubernetesTemplate() != null) {
sourceFiles = new File[] { getKubernetesTemplate() };
} else {
sourceFiles = new File[0];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -573,26 +573,20 @@ protected void writeResources(KubernetesList resources, ResourceClassifier class
File file =
writeResourcesIndividualAndComposite(resources, resourceFileBase, this.resourceFileType, log);
// Resolve template placeholders
if (classifier == ResourceClassifier.KUBERNETES) {
resolveTemplateVariablesIfAny(resources);
}

KubernetesHelper.resolveTemplateVariablesIfAny(resources, this.targetDir);
resolveTemplateVariablesIfAny(resources, resourceFileBase, file);

// Attach it to the Maven reactor so that it will also get deployed
projectHelper.attachArtifact(project, this.resourceFileType.getArtifactType(), classifier.getValue(), file);
}

private void resolveTemplateVariablesIfAny(KubernetesList resources) throws MojoExecutionException {
private void resolveTemplateVariablesIfAny(KubernetesList resources, File kubernetesResourceDir, File resourceYaml) throws MojoExecutionException {
Template template = findTemplate(resources);
if (template != null) {
List<io.fabric8.openshift.api.model.Parameter> parameters = template.getParameters();
if (parameters == null || parameters.isEmpty()) {
return;
}
File kubernetesYaml = new File(this.targetDir, "kubernetes.yml");
resolveTemplateVariables(parameters, kubernetesYaml);
File kubernetesResourceDir = new File(this.targetDir, "kubernetes");
resolveTemplateVariables(parameters, resourceYaml);
File[] kubernetesResources = kubernetesResourceDir.listFiles((dir, filename) -> filename.endsWith(".yml"));
if (kubernetesResources != null) {
for (File kubernetesResource : kubernetesResources) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,24 @@ public class OpenshiftHelmMojo extends HelmMojo {
/**
* The generated kubernetes YAML file
*/
@Parameter(property = PROPERTY_KUBERNETES_MANIFEST, defaultValue = "${basedir}/target/classes/META-INF/jkube/openshift.yml")
File kubernetesManifest; // NOSONAR (Override Mojo Property en HelmMojo)
@Parameter(property = "jkube.kubernetesManifest", defaultValue = "${basedir}/target/classes/META-INF/jkube/openshift.yml")
private File openShiftManifest;

/**
* The generated kubernetes YAML file
*/
@Parameter(property = PROPERTY_KUBERNETES_TEMPLATE, defaultValue = "${basedir}/target/classes/META-INF/jkube/openshift")
File kubernetesTemplate; // NOSONAR (Override Mojo Property en HelmMojo)
@Parameter(property = "jkube.kubernetesManifest", defaultValue = "${basedir}/target/classes/META-INF/jkube/openshift")
private File openShiftTemplate;

@Override
protected File getKubernetesManifest() {
return openShiftManifest;
}

@Override
protected File getKubernetesTemplate() {
return openShiftTemplate;
}

@Override
protected HelmConfig.HelmType getDefaultHelmType() {
Expand Down
27 changes: 23 additions & 4 deletions quickstarts/maven/spring-boot-helm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,30 @@ Helm chart can also be customized through `pom.xml` plugin configuration:

## How to test

### Docker build strategy (default)
With Minikube running, perform the following commands:
```shell script
eval $(minikube docker-env)
mvn clean package k8s:build k8s:resource k8s:helm
helm install spring-boot-yaml target/jkube/helm/spring-boot-yaml/kubernetes/
minikube service spring-boot-yaml
$ eval $(minikube docker-env)
$ mvn -Pkubernetes clean package
$ helm install spring-boot-helm target/jkube/helm/spring-boot-helm/kubernetes/
$ minikube service spring-boot-helm
```

### JIB build strategy
With Minikube running, perform the following commands:
```shell script
$ mvn -Pkubernetes clean package -Djkube.build.strategy=jib
$ eval $(minikube docker-env)
$ docker load -i target/docker/maven/spring-boot-helm/latest/tmp/docker-build.tar
$ helm install spring-boot-helm target/jkube/helm/spring-boot-helm/kubernetes/
$ minikube service spring-boot-helm
```

### OpenShift (S2I build)
With a valid OpenShift cluster, perform the following commands:
```shell script
$ mvn -Popenshift clean package
$ helm install spring-boot-helm target/jkube/helm/spring-boot-helm/openshift/
$ oc3 expose svc/spring-boot-helm
$ curl ${route-url}
```
19 changes: 5 additions & 14 deletions quickstarts/maven/spring-boot-helm/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
<version>2.2.7.RELEASE</version>
</parent>

<groupId>org.eclipse.jkube.quickstarts.maven</groupId>
<artifactId>spring-boot-helm</artifactId>
<version>1.0.0-alpha-4</version>
<version>1.0.0-SNAPSHOT</version>
<name>Eclipse JKube :: Quickstarts :: Maven :: Spring Boot - Helm</name>
<packaging>jar</packaging>

Expand All @@ -45,11 +45,6 @@
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>

</dependencies>

<build>
Expand All @@ -60,7 +55,6 @@
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>


</plugins>
</build>

Expand Down Expand Up @@ -107,6 +101,7 @@

<executions>
<execution>
<phase>package</phase>
<goals>
<goal>resource</goal>
<goal>build</goal>
Expand Down Expand Up @@ -142,19 +137,15 @@
</spring-boot>Open Shift Maven Plugin
</config>
</generator>
<enricher>
</enricher>
<!-- <helm>-->
<!-- <chart>This is a Test</chart>-->
<!-- <type>openshift</type>-->
<!-- </helm>-->
</configuration>

<executions>
<execution>
<phase>package</phase>
<goals>
<goal>resource</goal>
<goal>build</goal>
<goal>helm</goal>
</goals>
</execution>
</executions>
Expand Down

0 comments on commit 03ed981

Please sign in to comment.