Skip to content

Commit c239f33

Browse files
committed
Add JobIdentifier information as part of pod annotations
1 parent 474af8d commit c239f33

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ public interface Constants {
4545
// internal use only
4646
String CREATED_BY_LABEL_KEY = "Elastic-Agent-Created-By";
4747
String ENVIRONMENT_LABEL_KEY = "Elastic-Agent-Environment-Name";
48-
String JOB_ID_LABEL_KEY = "Elastic-Agent-Job-Identifier";
48+
String JOB_ID_LABEL_KEY = "Elastic-Agent-Job-Id";
49+
String JOB_IDENTIFIER_LABEL_KEY = "Elastic-Agent-Job-Identifier";
4950

5051
String KUBERNETES_NAMESPACE_KEY = "default";
5152
String KUBERNETES_POD_KIND_LABEL_KEY = "kind";

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
import static cd.go.contrib.elasticagent.KubernetesPlugin.LOG;
3939
import static cd.go.contrib.elasticagent.executors.GetProfileMetadataExecutor.POD_CONFIGURATION;
40+
import static cd.go.contrib.elasticagent.utils.Util.GSON;
4041
import static cd.go.contrib.elasticagent.utils.Util.getSimpleDateFormat;
4142
import static org.apache.commons.lang3.StringUtils.isBlank;
4243

@@ -99,6 +100,7 @@ private static void setLabels(Pod pod, CreateAgentRequest request) {
99100
private static void setAnnotations(Pod pod, CreateAgentRequest request) {
100101
Map<String, String> existingAnnotations = (pod.getMetadata().getAnnotations() != null) ? pod.getMetadata().getAnnotations() : new HashMap<>();
101102
existingAnnotations.putAll(request.properties());
103+
existingAnnotations.put(Constants.JOB_IDENTIFIER_LABEL_KEY, GSON.toJson(request.jobIdentifier()));
102104
pod.getMetadata().setAnnotations(existingAnnotations);
103105
}
104106

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package cd.go.contrib.elasticagent;
1818

1919
import cd.go.contrib.elasticagent.requests.CreateAgentRequest;
20+
import com.google.gson.Gson;
2021
import io.fabric8.kubernetes.api.model.Container;
2122
import io.fabric8.kubernetes.api.model.EnvVar;
2223
import io.fabric8.kubernetes.api.model.Pod;
@@ -32,6 +33,7 @@
3233
import java.util.ArrayList;
3334
import java.util.HashMap;
3435
import java.util.List;
36+
import java.util.Map;
3537

3638
import static org.hamcrest.Matchers.is;
3739
import static org.junit.Assert.*;
@@ -145,7 +147,10 @@ public void shouldCreateKubernetesPodWithPodAnnotations() throws Exception {
145147

146148
assertNotNull(elasticAgentPod.getMetadata());
147149

148-
assertThat(elasticAgentPod.getMetadata().getAnnotations(), is(createAgentRequest.properties()));
150+
Map<String, String> expectedAnnotations = new HashMap<>();
151+
expectedAnnotations.putAll(createAgentRequest.properties());
152+
expectedAnnotations.put(Constants.JOB_IDENTIFIER_LABEL_KEY, new Gson().toJson(createAgentRequest.jobIdentifier()));
153+
assertThat(elasticAgentPod.getMetadata().getAnnotations(), is(expectedAnnotations));
149154
}
150155

151156
@Test
@@ -249,6 +254,7 @@ public void usingPodYamlConfigurations_shouldCreateKubernetesPodWithPodAnnotatio
249254
HashMap<String, String> expectedAnnotations = new HashMap<>();
250255
expectedAnnotations.putAll(createAgentRequest.properties());
251256
expectedAnnotations.put("annotation-key", "my-fancy-annotation-value");
257+
expectedAnnotations.put(Constants.JOB_IDENTIFIER_LABEL_KEY, new Gson().toJson(createAgentRequest.jobIdentifier()));
252258

253259
assertThat(elasticAgentPod.getMetadata().getAnnotations(), is(expectedAnnotations));
254260
}

0 commit comments

Comments
 (0)