Skip to content

Commit 83e1231

Browse files
varshavaradarajanbdpiprava
authored andcommitted
Replace pod name with pod name + uuid if necessary.
* Upgrade gocd agent image version to latest.
1 parent d5b4a89 commit 83e1231

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import java.io.IOException;
3636
import java.io.StringReader;
3737
import java.io.StringWriter;
38-
import java.net.MalformedURLException;
3938
import java.net.URL;
4039
import java.text.ParseException;
4140
import java.util.*;
@@ -233,11 +232,12 @@ private static String image(Map<String, String> properties) {
233232
private KubernetesInstance createUsingPodYaml(CreateAgentRequest request, PluginSettings settings, KubernetesClient client, PluginRequest pluginRequest) {
234233
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
235234
String podYaml = request.properties().get(POD_CONFIGURATION.getKey());
236-
String templatizedPodYaml = getTemplatizedPodYamlString(podYaml);
235+
String templatizedPodYaml = getTemplatizedPodSpec(podYaml);
237236

238237
Pod elasticAgentPod = new Pod();
239238
try {
240239
elasticAgentPod = mapper.readValue(templatizedPodYaml, Pod.class);
240+
setPodNameIfNecessary(elasticAgentPod, podYaml);
241241
} catch (IOException e) {
242242
//ignore error here, handle this inside validate profile!
243243
LOG.error(e.getMessage());
@@ -268,7 +268,9 @@ else if ("yaml".equalsIgnoreCase(fileType)) {
268268
FileUtils.copyURLToFile(new URL(fileToDownload), podSpecFile);
269269
LOG.debug(format("Finished downloading %s to %s", fileToDownload, podSpecFile));
270270
String spec = FileUtils.readFileToString(podSpecFile, UTF_8);
271-
elasticAgentPod = mapper.readValue(spec, Pod.class);
271+
String templatizedPodSpec = getTemplatizedPodSpec(spec);
272+
elasticAgentPod = mapper.readValue(templatizedPodSpec, Pod.class);
273+
setPodNameIfNecessary(elasticAgentPod, spec);
272274
FileUtils.deleteQuietly(podSpecFile);
273275
LOG.debug(format("Deleted %s", podSpecFile));
274276

@@ -280,11 +282,18 @@ else if ("yaml".equalsIgnoreCase(fileType)) {
280282
return createKubernetesPod(client, elasticAgentPod);
281283
}
282284

285+
private void setPodNameIfNecessary(Pod elasticAgentPod, String spec) {
286+
if (!spec.contains(POD_POSTFIX)) {
287+
String newPodName = elasticAgentPod.getMetadata().getName().concat(String.format("-%s", UUID.randomUUID().toString()));
288+
elasticAgentPod.getMetadata().setName(newPodName);
289+
}
290+
}
291+
283292

284-
public static String getTemplatizedPodYamlString(String podYaml) {
293+
public static String getTemplatizedPodSpec(String podSpec) {
285294
StringWriter writer = new StringWriter();
286295
MustacheFactory mf = new DefaultMustacheFactory();
287-
Mustache mustache = mf.compile(new StringReader(podYaml), "templatePod");
296+
Mustache mustache = mf.compile(new StringReader(podSpec), "templatePod");
288297
mustache.execute(writer, KubernetesInstanceFactory.getJinJavaContext());
289298
return writer.toString();
290299
}
@@ -294,7 +303,7 @@ private static Map<String, String> getJinJavaContext() {
294303
context.put(POD_POSTFIX, UUID.randomUUID().toString());
295304
context.put(CONTAINER_POSTFIX, UUID.randomUUID().toString());
296305
context.put(GOCD_AGENT_IMAGE, "gocd/gocd-agent-alpine-3.6");
297-
context.put(LATEST_VERSION, "v18.10.0");
306+
context.put(LATEST_VERSION, "v19.1.0");
298307
return context;
299308
}
300309
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ private void validatePodYaml(HashMap<String, String> properties, ArrayList<Map<S
130130

131131
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
132132
try {
133-
mapper.readValue(KubernetesInstanceFactory.getTemplatizedPodYamlString(podYaml), Pod.class);
133+
mapper.readValue(KubernetesInstanceFactory.getTemplatizedPodSpec(podYaml), Pod.class);
134134
} catch (IOException e) {
135135
addError(result, key, "Invalid Pod Yaml.");
136136
}

src/test/java/cd/go/contrib/elasticagent/KubernetesAgentInstancesIntegrationTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.util.Map;
3737

3838
import static cd.go.contrib.elasticagent.executors.GetProfileMetadataExecutor.PRIVILEGED;
39+
import static org.hamcrest.Matchers.containsString;
3940
import static org.hamcrest.Matchers.is;
4041
import static org.junit.Assert.*;
4142
import static org.mockito.ArgumentMatchers.any;
@@ -265,7 +266,7 @@ public void usingPodYamlConfigurations_shouldCreateKubernetesPodWithPodMetadata(
265266
Pod elasticAgentPod = argumentCaptor.getValue();
266267

267268
assertNotNull(elasticAgentPod.getMetadata());
268-
assertThat(elasticAgentPod.getMetadata().getName(), is("test-pod-yaml"));
269+
assertThat(elasticAgentPod.getMetadata().getName(), containsString("test-pod-yaml"));
269270

270271
assertThat(elasticAgentPod.getMetadata().getName(), is(instance.name()));
271272
}
@@ -389,7 +390,7 @@ public void usingRemoteFile_shouldCreateKubernetesPodWithPodMetadata() {
389390
Pod elasticAgentPod = argumentCaptor.getValue();
390391

391392
assertNotNull(elasticAgentPod.getMetadata());
392-
assertThat(elasticAgentPod.getMetadata().getName(), is("test-pod-json"));
393+
assertThat(elasticAgentPod.getMetadata().getName(), containsString("test-pod-json"));
393394

394395
assertThat(elasticAgentPod.getMetadata().getName(), is(instance.name()));
395396
}

0 commit comments

Comments
 (0)