Skip to content

Commit

Permalink
+ Resolving template placeholders only in kubernetes.yaml but not ind…
Browse files Browse the repository at this point in the history
…ividual

  template files (which will be resolved by helm mojo)
+ HELM mode does not support parameters without value

Ported PR fabric8io/fabric8-maven-plugin#1713
Ported PR fabric8io/fabric8-maven-plugin#1733
  • Loading branch information
rohanKanojia committed Nov 7, 2019
1 parent 141f267 commit f7d1d68
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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 @@ -80,7 +81,9 @@
import io.fabric8.openshift.api.model.Template;
import io.jkube.kit.common.KitLogger;
import io.jkube.kit.common.ResourceFileType;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.plugin.MojoExecutionException;

/**
* @author roland
Expand Down Expand Up @@ -712,5 +715,49 @@ 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);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,8 @@ protected void writeResources(KubernetesList resources, ResourceClassifier class
File file =
writeResourcesIndividualAndComposite(resources, resourceFileBase, this.resourceFileType, log, generateRoute);

KubernetesHelper.resolveTemplateVariablesIfAny(resources, this.targetDir);

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

0 comments on commit f7d1d68

Please sign in to comment.