35
35
import java .text .ParseException ;
36
36
import java .util .*;
37
37
38
+ import static cd .go .contrib .elasticagent .Constants .*;
38
39
import static cd .go .contrib .elasticagent .KubernetesPlugin .LOG ;
39
40
import static cd .go .contrib .elasticagent .executors .GetProfileMetadataExecutor .POD_CONFIGURATION ;
40
41
import static cd .go .contrib .elasticagent .executors .GetProfileMetadataExecutor .PRIVILEGED ;
41
42
import static cd .go .contrib .elasticagent .utils .Util .GSON ;
42
43
import static cd .go .contrib .elasticagent .utils .Util .getSimpleDateFormat ;
44
+ import static java .lang .String .format ;
45
+ import static java .lang .String .valueOf ;
43
46
import static org .apache .commons .lang3 .StringUtils .isBlank ;
44
47
45
48
public class KubernetesInstanceFactory {
@@ -52,7 +55,7 @@ public KubernetesInstance create(CreateAgentRequest request, PluginSettings sett
52
55
}
53
56
54
57
private KubernetesInstance create (CreateAgentRequest request , PluginSettings settings , KubernetesClient client , PluginRequest pluginRequest ) {
55
- String containerName = Constants . KUBERNETES_POD_NAME + UUID .randomUUID ().toString ();
58
+ String containerName = format ( "%s-%s" , KUBERNETES_POD_NAME_PREFIX , UUID .randomUUID ().toString () );
56
59
57
60
Container container = new Container ();
58
61
container .setName (containerName );
@@ -98,13 +101,13 @@ private ResourceRequirements getPodResources(CreateAgentRequest request) {
98
101
String maxMemory = request .properties ().get ("MaxMemory" );
99
102
if (StringUtils .isNotBlank (maxMemory )) {
100
103
Size mem = Size .parse (maxMemory );
101
- LOG .debug (String . format ("[Create Agent] Setting memory resource limit on k8s pod:%s" , new Quantity (String . valueOf (mem .toMegabytes ()), "M" )));
102
- limits .put ("memory" , new Quantity (String . valueOf (mem .toBytes ())));
104
+ LOG .debug (format ("[Create Agent] Setting memory resource limit on k8s pod:%s" , new Quantity (valueOf (mem .toMegabytes ()), "M" )));
105
+ limits .put ("memory" , new Quantity (valueOf (mem .toBytes ())));
103
106
}
104
107
105
108
String maxCPU = request .properties ().get ("MaxCPU" );
106
109
if (StringUtils .isNotBlank (maxCPU )) {
107
- LOG .debug (String . format ("[Create Agent] Setting cpu resource limit on k8s pod:%s" , new Quantity (maxCPU )));
110
+ LOG .debug (format ("[Create Agent] Setting cpu resource limit on k8s pod:%s" , new Quantity (maxCPU )));
108
111
limits .put ("cpu" , new Quantity (maxCPU ));
109
112
}
110
113
@@ -122,13 +125,13 @@ private static void setLabels(Pod pod, CreateAgentRequest request) {
122
125
private static void setAnnotations (Pod pod , CreateAgentRequest request ) {
123
126
Map <String , String > existingAnnotations = (pod .getMetadata ().getAnnotations () != null ) ? pod .getMetadata ().getAnnotations () : new HashMap <>();
124
127
existingAnnotations .putAll (request .properties ());
125
- existingAnnotations .put (Constants . JOB_IDENTIFIER_LABEL_KEY , GSON .toJson (request .jobIdentifier ()));
128
+ existingAnnotations .put (JOB_IDENTIFIER_LABEL_KEY , GSON .toJson (request .jobIdentifier ()));
126
129
pod .getMetadata ().setAnnotations (existingAnnotations );
127
130
}
128
131
129
132
private KubernetesInstance createKubernetesPod (KubernetesClient client , Pod elasticAgentPod ) {
130
- LOG .info (String . format ("[Create Agent] Creating K8s pod with spec:%s" , elasticAgentPod .toString ()));
131
- Pod pod = client .pods ().inNamespace (Constants . KUBERNETES_NAMESPACE ).create (elasticAgentPod );
133
+ LOG .info (format ("[Create Agent] Creating K8s pod with spec:%s" , elasticAgentPod .toString ()));
134
+ Pod pod = client .pods ().inNamespace (KUBERNETES_NAMESPACE ).create (elasticAgentPod );
132
135
return fromKubernetesPod (pod );
133
136
}
134
137
@@ -140,8 +143,8 @@ public KubernetesInstance fromKubernetesPod(Pod elasticAgentPod) {
140
143
if (StringUtils .isNotBlank (metadata .getCreationTimestamp ())) {
141
144
createdAt = new DateTime (getSimpleDateFormat ().parse (metadata .getCreationTimestamp ())).withZone (DateTimeZone .UTC );
142
145
}
143
- String environment = metadata .getLabels ().get (Constants . ENVIRONMENT_LABEL_KEY );
144
- Long jobId = Long .valueOf (metadata .getLabels ().get (Constants . JOB_ID_LABEL_KEY ));
146
+ String environment = metadata .getLabels ().get (ENVIRONMENT_LABEL_KEY );
147
+ Long jobId = Long .valueOf (metadata .getLabels ().get (JOB_ID_LABEL_KEY ));
145
148
kubernetesInstance = new KubernetesInstance (createdAt , environment , metadata .getName (), metadata .getAnnotations (), jobId , PodState .fromPod (elasticAgentPod ));
146
149
} catch (ParseException e ) {
147
150
throw new RuntimeException (e );
@@ -183,14 +186,14 @@ private static Collection<? extends EnvVar> parseEnvironments(String environment
183
186
private static HashMap <String , String > labelsFrom (CreateAgentRequest request ) {
184
187
HashMap <String , String > labels = new HashMap <>();
185
188
186
- labels .put (Constants . CREATED_BY_LABEL_KEY , Constants . PLUGIN_ID );
187
- labels .put (Constants . JOB_ID_LABEL_KEY , String . valueOf (request .jobIdentifier ().getJobId ()));
189
+ labels .put (CREATED_BY_LABEL_KEY , PLUGIN_ID );
190
+ labels .put (JOB_ID_LABEL_KEY , valueOf (request .jobIdentifier ().getJobId ()));
188
191
189
192
if (StringUtils .isNotBlank (request .environment ())) {
190
- labels .put (Constants . ENVIRONMENT_LABEL_KEY , request .environment ());
193
+ labels .put (ENVIRONMENT_LABEL_KEY , request .environment ());
191
194
}
192
195
193
- labels .put (Constants . KUBERNETES_POD_KIND_LABEL_KEY , Constants . KUBERNETES_POD_KIND_LABEL_VALUE );
196
+ labels .put (KUBERNETES_POD_KIND_LABEL_KEY , KUBERNETES_POD_KIND_LABEL_VALUE );
194
197
195
198
return labels ;
196
199
}
@@ -235,10 +238,10 @@ public static String getTemplatizedPodYamlString(String podYaml) {
235
238
236
239
public static Map <String , String > getJinJavaContext () {
237
240
HashMap <String , String > context = new HashMap <>();
238
- context .put (Constants . POD_POSTFIX , UUID .randomUUID ().toString ());
239
- context .put (Constants . CONTAINER_POSTFIX , UUID .randomUUID ().toString ());
240
- context .put (Constants . GOCD_AGENT_IMAGE , "gocd/gocd-agent-alpine-3.5" );
241
- context .put (Constants . LATEST_VERSION , "v17.10.0" );
241
+ context .put (POD_POSTFIX , UUID .randomUUID ().toString ());
242
+ context .put (CONTAINER_POSTFIX , UUID .randomUUID ().toString ());
243
+ context .put (GOCD_AGENT_IMAGE , "gocd/gocd-agent-alpine-3.5" );
244
+ context .put (LATEST_VERSION , "v17.10.0" );
242
245
return context ;
243
246
}
244
247
}
0 commit comments