35
35
import java .io .IOException ;
36
36
import java .io .StringReader ;
37
37
import java .io .StringWriter ;
38
- import java .net .MalformedURLException ;
39
38
import java .net .URL ;
40
39
import java .text .ParseException ;
41
40
import java .util .*;
@@ -233,11 +232,12 @@ private static String image(Map<String, String> properties) {
233
232
private KubernetesInstance createUsingPodYaml (CreateAgentRequest request , PluginSettings settings , KubernetesClient client , PluginRequest pluginRequest ) {
234
233
ObjectMapper mapper = new ObjectMapper (new YAMLFactory ());
235
234
String podYaml = request .properties ().get (POD_CONFIGURATION .getKey ());
236
- String templatizedPodYaml = getTemplatizedPodYamlString (podYaml );
235
+ String templatizedPodYaml = getTemplatizedPodSpec (podYaml );
237
236
238
237
Pod elasticAgentPod = new Pod ();
239
238
try {
240
239
elasticAgentPod = mapper .readValue (templatizedPodYaml , Pod .class );
240
+ setPodNameIfNecessary (elasticAgentPod , podYaml );
241
241
} catch (IOException e ) {
242
242
//ignore error here, handle this inside validate profile!
243
243
LOG .error (e .getMessage ());
@@ -268,7 +268,9 @@ else if ("yaml".equalsIgnoreCase(fileType)) {
268
268
FileUtils .copyURLToFile (new URL (fileToDownload ), podSpecFile );
269
269
LOG .debug (format ("Finished downloading %s to %s" , fileToDownload , podSpecFile ));
270
270
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 );
272
274
FileUtils .deleteQuietly (podSpecFile );
273
275
LOG .debug (format ("Deleted %s" , podSpecFile ));
274
276
@@ -280,11 +282,18 @@ else if ("yaml".equalsIgnoreCase(fileType)) {
280
282
return createKubernetesPod (client , elasticAgentPod );
281
283
}
282
284
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
+
283
292
284
- public static String getTemplatizedPodYamlString (String podYaml ) {
293
+ public static String getTemplatizedPodSpec (String podSpec ) {
285
294
StringWriter writer = new StringWriter ();
286
295
MustacheFactory mf = new DefaultMustacheFactory ();
287
- Mustache mustache = mf .compile (new StringReader (podYaml ), "templatePod" );
296
+ Mustache mustache = mf .compile (new StringReader (podSpec ), "templatePod" );
288
297
mustache .execute (writer , KubernetesInstanceFactory .getJinJavaContext ());
289
298
return writer .toString ();
290
299
}
@@ -294,7 +303,7 @@ private static Map<String, String> getJinJavaContext() {
294
303
context .put (POD_POSTFIX , UUID .randomUUID ().toString ());
295
304
context .put (CONTAINER_POSTFIX , UUID .randomUUID ().toString ());
296
305
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" );
298
307
return context ;
299
308
}
300
309
}
0 commit comments