Skip to content

Commit 76a7db0

Browse files
authored
Allow specifying templated values in elastic agent pod yaml (#17)
1 parent fbf6bd7 commit 76a7db0

File tree

4 files changed

+65
-10
lines changed

4 files changed

+65
-10
lines changed

src/main/java/cd/go/contrib/elasticagent/KubernetesInstanceFactory.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,7 @@ private static String image(Map<String, String> properties) {
201201
private KubernetesInstance createUsingPodYaml(CreateAgentRequest request, PluginSettings settings, KubernetesClient client, PluginRequest pluginRequest) {
202202
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
203203
String podYaml = request.properties().get(POD_CONFIGURATION.getKey());
204-
205-
StringWriter writer = new StringWriter();
206-
MustacheFactory mf = new DefaultMustacheFactory();
207-
Mustache mustache = mf.compile(new StringReader(podYaml), "templatePod");
208-
mustache.execute(writer, KubernetesInstanceFactory.getJinJavaContext());
209-
String templatizedPodYaml = writer.toString();
204+
String templatizedPodYaml = getTemplatizedPodYamlString(podYaml);
210205

211206
Pod elasticAgentPod = new Pod();
212207
try {
@@ -220,6 +215,14 @@ private KubernetesInstance createUsingPodYaml(CreateAgentRequest request, Plugin
220215
return createKubernetesPod(client, elasticAgentPod);
221216
}
222217

218+
public static String getTemplatizedPodYamlString(String podYaml) {
219+
StringWriter writer = new StringWriter();
220+
MustacheFactory mf = new DefaultMustacheFactory();
221+
Mustache mustache = mf.compile(new StringReader(podYaml), "templatePod");
222+
mustache.execute(writer, KubernetesInstanceFactory.getJinJavaContext());
223+
return writer.toString();
224+
}
225+
223226
public static Map<String, String> getJinJavaContext() {
224227
HashMap<String, String> context = new HashMap<>();
225228
context.put(Constants.POD_POSTFIX, UUID.randomUUID().toString());

src/main/java/cd/go/contrib/elasticagent/executors/ProfileValidateRequestExecutor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package cd.go.contrib.elasticagent.executors;
1818

19+
import cd.go.contrib.elasticagent.KubernetesInstanceFactory;
1920
import cd.go.contrib.elasticagent.RequestExecutor;
2021
import cd.go.contrib.elasticagent.model.Metadata;
2122
import cd.go.contrib.elasticagent.requests.ProfileValidateRequest;
@@ -96,7 +97,7 @@ private void validatePodYaml(HashMap<String, String> properties, ArrayList<Map<S
9697

9798
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
9899
try {
99-
mapper.readValue(podYaml, Pod.class);
100+
mapper.readValue(KubernetesInstanceFactory.getTemplatizedPodYamlString(podYaml), Pod.class);
100101
} catch (IOException e) {
101102
addError(result, key, "Invalid Pod Yaml.");
102103
}

src/main/java/cd/go/contrib/elasticagent/executors/ValidateConfigurationExecutor.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import cd.go.contrib.elasticagent.model.Field;
2323
import cd.go.contrib.elasticagent.model.ServerInfo;
2424
import cd.go.contrib.elasticagent.requests.ValidatePluginSettings;
25-
import com.google.gson.Gson;
2625
import com.thoughtworks.go.plugin.api.response.DefaultGoPluginApiResponse;
2726
import com.thoughtworks.go.plugin.api.response.GoPluginApiResponse;
2827
import org.apache.commons.lang3.StringUtils;

src/test/java/cd/go/contrib/elasticagent/executors/ProfileValidateRequestExecutorTest.java

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.skyscreamer.jsonassert.JSONAssert;
2222
import org.skyscreamer.jsonassert.JSONCompareMode;
2323

24-
import java.util.Collections;
2524
import java.util.HashMap;
2625
import java.util.Map;
2726

@@ -54,6 +53,59 @@ public void shouldValidateMandatoryKeysForPodConfiguration() throws Exception {
5453
JSONAssert.assertEquals("[{\"message\":\"Pod Configuration must not be blank.\",\"key\":\"PodConfiguration\"}]", json, JSONCompareMode.NON_EXTENSIBLE);
5554
}
5655

56+
@Test
57+
public void shouldValidatePodConfigurationWhenSpecifiedAsYaml() throws Exception {
58+
Map<String, String> properties = new HashMap<>();
59+
properties.put("SpecifiedUsingPodConfiguration", "true");
60+
properties.put("PodConfiguration", "this is my invalid fancy pod yaml!!");
61+
ProfileValidateRequestExecutor executor = new ProfileValidateRequestExecutor(new ProfileValidateRequest(properties));
62+
String json = executor.execute().responseBody();
63+
JSONAssert.assertEquals("[{\"message\":\"Invalid Pod Yaml.\",\"key\":\"PodConfiguration\"}]", json, JSONCompareMode.NON_EXTENSIBLE);
64+
}
65+
66+
@Test
67+
public void shouldAllowPodYamlConfiguration() throws Exception {
68+
Map<String, String> properties = new HashMap<>();
69+
properties.put("SpecifiedUsingPodConfiguration", "true");
70+
String podYaml = "apiVersion: v1\n" +
71+
"kind: Pod\n" +
72+
"metadata:\n" +
73+
" name: pod-name\n" +
74+
" labels:\n" +
75+
" app: web\n" +
76+
"spec:\n" +
77+
" containers:\n" +
78+
" - name: gocd-agent-container\n" +
79+
" image: gocd/fancy-agent-image:latest";
80+
81+
properties.put("PodConfiguration", podYaml);
82+
ProfileValidateRequestExecutor executor = new ProfileValidateRequestExecutor(new ProfileValidateRequest(properties));
83+
String json = executor.execute().responseBody();
84+
JSONAssert.assertEquals("[]", json, JSONCompareMode.NON_EXTENSIBLE);
85+
}
86+
87+
88+
@Test
89+
public void shouldAllowGinjaTemplatedPodYaml() throws Exception {
90+
Map<String, String> properties = new HashMap<>();
91+
properties.put("SpecifiedUsingPodConfiguration", "true");
92+
String podYaml = "apiVersion: v1\n" +
93+
"kind: Pod\n" +
94+
"metadata:\n" +
95+
" name: pod-name-prefix-{{ POD_POSTFIX }}\n" +
96+
" labels:\n" +
97+
" app: web\n" +
98+
"spec:\n" +
99+
" containers:\n" +
100+
" - name: gocd-agent-container-{{ CONTAINER_POSTFIX }}\n" +
101+
" image: {{ GOCD_AGENT_IMAGE }}:{{ LATEST_VERSION }}";
102+
103+
properties.put("PodConfiguration", podYaml);
104+
ProfileValidateRequestExecutor executor = new ProfileValidateRequestExecutor(new ProfileValidateRequest(properties));
105+
String json = executor.execute().responseBody();
106+
JSONAssert.assertEquals("[]", json, JSONCompareMode.NON_EXTENSIBLE);
107+
}
108+
57109
@Test
58110
public void shouldValidatePodConfiguration() throws Exception {
59111
Map<String, String> properties = new HashMap<>();
@@ -63,4 +115,4 @@ public void shouldValidatePodConfiguration() throws Exception {
63115
String json = executor.execute().responseBody();
64116
JSONAssert.assertEquals("[{\"message\":\"Invalid Pod Yaml.\",\"key\":\"PodConfiguration\"}]", json, JSONCompareMode.NON_EXTENSIBLE);
65117
}
66-
}
118+
}

0 commit comments

Comments
 (0)