Skip to content

Commit 0bf5f55

Browse files
committed
Add validation error when generateName field is specified in Elastic Agent Profile Pod Yaml
1 parent e3894e8 commit 0bf5f55

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,7 @@ public GoPluginApiResponse execute() {
8989
validationError.put("message", "Should be one of `properties`, `remote`, `yaml`.");
9090
result.add(validationError);
9191
}
92-
}
93-
else {
92+
} else {
9493

9594
boolean isSpecifiedUsingPodYaml = Boolean.valueOf(new HashMap<>(request.getProperties()).get(SPECIFIED_USING_POD_CONFIGURATION.getKey()));
9695

@@ -127,12 +126,17 @@ private void validatePodYaml(HashMap<String, String> properties, ArrayList<Map<S
127126
addNotBlankError(result, key, "Pod Configuration");
128127
return;
129128
}
130-
129+
Pod pod = new Pod();
131130
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
132131
try {
133-
mapper.readValue(KubernetesInstanceFactory.getTemplatizedPodSpec(podYaml), Pod.class);
132+
pod = mapper.readValue(KubernetesInstanceFactory.getTemplatizedPodSpec(podYaml), Pod.class);
134133
} catch (IOException e) {
135134
addError(result, key, "Invalid Pod Yaml.");
135+
return;
136+
}
137+
138+
if (StringUtils.isNotBlank(pod.getMetadata().getGenerateName())) {
139+
addError(result, key, "Invalid Pod Yaml. generateName field is not supported by GoCD. Please use {{ POD_POSTFIX }} instead.");
136140
}
137141
}
138142

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,26 @@ public void shouldAllowPodYamlConfiguration() throws Exception {
8484
JSONAssert.assertEquals("[]", json, JSONCompareMode.NON_EXTENSIBLE);
8585
}
8686

87+
@Test
88+
public void shouldNotAllowPodYamlConfigurationWhenGenerateNameIsSpecified() throws Exception {
89+
Map<String, String> properties = new HashMap<>();
90+
properties.put("PodSpecType", "yaml");
91+
String podYaml = "apiVersion: v1\n" +
92+
"kind: Pod\n" +
93+
"metadata:\n" +
94+
" generateName: pod-name\n" +
95+
" labels:\n" +
96+
" app: web\n" +
97+
"spec:\n" +
98+
" containers:\n" +
99+
" - name: gocd-agent-container\n" +
100+
" image: gocd/fancy-agent-image:latest";
101+
102+
properties.put("PodConfiguration", podYaml);
103+
ProfileValidateRequestExecutor executor = new ProfileValidateRequestExecutor(new ProfileValidateRequest(properties));
104+
String json = executor.execute().responseBody();
105+
JSONAssert.assertEquals("[{\"message\":\"Invalid Pod Yaml. generateName field is not supported by GoCD. Please use {{ POD_POSTFIX }} instead.\",\"key\":\"PodConfiguration\"}]", json, JSONCompareMode.NON_EXTENSIBLE);
106+
}
87107

88108
@Test
89109
public void shouldAllowJinjaTemplatedPodYaml() throws Exception {

0 commit comments

Comments
 (0)