Skip to content

Commit ba6333f

Browse files
authored
Specify kubernetes memory limits in bytes when memory is configured on elastic profile (#12)
1 parent be03843 commit ba6333f

File tree

3 files changed

+45
-24
lines changed

3 files changed

+45
-24
lines changed

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

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,23 +58,7 @@ private KubernetesInstance create(CreateAgentRequest request, PluginSettings set
5858
container.setImage(image(request.properties()));
5959
container.setImagePullPolicy("IfNotPresent");
6060

61-
ResourceRequirements resources = new ResourceRequirements();
62-
resources.setLimits(new HashMap<String, Quantity>() {{
63-
String maxMemory = request.properties().get("MaxMemory");
64-
if (StringUtils.isNotBlank(maxMemory)) {
65-
LOG.debug(String.format("[Create Agent] Setting memory resource limit on k8s pod:%s", maxMemory));
66-
Size mem = Size.parse(maxMemory);
67-
put("memory", new Quantity(String.valueOf(mem.toMegabytes()), "Mi"));
68-
}
69-
70-
String maxCPU = request.properties().get("MaxCPU");
71-
if (StringUtils.isNotBlank(maxCPU)) {
72-
LOG.debug(String.format("[Create Agent] Setting cpu resource limit on k8s pod:%s", maxCPU));
73-
put("cpu", new Quantity(maxCPU));
74-
}
75-
}});
76-
77-
container.setResources(resources);
61+
container.setResources(getPodResources(request));
7862

7963
ObjectMeta podMetadata = new ObjectMeta();
8064
podMetadata.setName(containerName);
@@ -91,6 +75,28 @@ private KubernetesInstance create(CreateAgentRequest request, PluginSettings set
9175
return createKubernetesPod(client, elasticAgentPod);
9276
}
9377

78+
private ResourceRequirements getPodResources(CreateAgentRequest request) {
79+
ResourceRequirements resources = new ResourceRequirements();
80+
HashMap<String, Quantity> limits = new HashMap<>();
81+
82+
String maxMemory = request.properties().get("MaxMemory");
83+
if (StringUtils.isNotBlank(maxMemory)) {
84+
Size mem = Size.parse(maxMemory);
85+
LOG.debug(String.format("[Create Agent] Setting memory resource limit on k8s pod:%s", new Quantity(String.valueOf(mem.toMegabytes()), "M")));
86+
limits.put("memory", new Quantity(String.valueOf(mem.toBytes())));
87+
}
88+
89+
String maxCPU = request.properties().get("MaxCPU");
90+
if (StringUtils.isNotBlank(maxCPU)) {
91+
LOG.debug(String.format("[Create Agent] Setting cpu resource limit on k8s pod:%s", new Quantity(maxCPU)));
92+
limits.put("cpu", new Quantity(maxCPU));
93+
}
94+
95+
resources.setLimits(limits);
96+
97+
return resources;
98+
}
99+
94100
private static void setLabels(Pod pod, CreateAgentRequest request) {
95101
Map<String, String> existingLabels = (pod.getMetadata().getLabels() != null) ? pod.getMetadata().getLabels() : new HashMap<>();
96102
existingLabels.putAll(labelsFrom(request));

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ public static CreateAgentRequest defaultCreateAgentRequest() {
2727
String autoRegisterKey = UUID.randomUUID().toString();
2828
HashMap<String, String> properties = new HashMap<>();
2929
properties.put("Image", "gocd/custom-gocd-agent-alpine");
30-
properties.put("MaxMemory", "1024m");
31-
properties.put("MaxCPU", "1");
30+
properties.put("MaxMemory", "1024M");
31+
properties.put("MaxCPU", "2");
3232
properties.put("Environment", "ENV1=VALUE1\n" +
3333
"ENV2=VALUE2");
3434
properties.put("PodConfiguration", "");

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@
1818

1919
import cd.go.contrib.elasticagent.requests.CreateAgentRequest;
2020
import com.google.gson.Gson;
21-
import io.fabric8.kubernetes.api.model.Container;
22-
import io.fabric8.kubernetes.api.model.EnvVar;
23-
import io.fabric8.kubernetes.api.model.Pod;
24-
import io.fabric8.kubernetes.api.model.PodList;
21+
import io.fabric8.kubernetes.api.model.*;
2522
import io.fabric8.kubernetes.client.KubernetesClient;
2623
import io.fabric8.kubernetes.client.dsl.internal.NodeOperationsImpl;
2724
import io.fabric8.kubernetes.client.dsl.internal.PodOperationsImpl;
@@ -102,6 +99,24 @@ public void shouldCreateKubernetesPodWithContainerSpecification() throws Excepti
10299
assertThat(gocdAgentContainer.getImagePullPolicy(), is("IfNotPresent"));
103100
}
104101

102+
@Test
103+
public void shouldCreateKubernetesPodWithResourcesLimitSpecificationOnGoCDAgentContainer() throws Exception {
104+
ArgumentCaptor<Pod> argumentCaptor = ArgumentCaptor.forClass(Pod.class);
105+
kubernetesAgentInstances.create(createAgentRequest, settings, mockedPluginRequest);
106+
verify(pods).create(argumentCaptor.capture());
107+
Pod elasticAgentPod = argumentCaptor.getValue();
108+
109+
List<Container> containers = elasticAgentPod.getSpec().getContainers();
110+
assertThat(containers.size(), is(1));
111+
112+
Container gocdAgentContainer = containers.get(0);
113+
114+
ResourceRequirements resources = gocdAgentContainer.getResources();
115+
116+
assertThat(resources.getLimits().get("memory").getAmount(), is(String.valueOf(1024 * 1024 * 1024)));
117+
assertThat(resources.getLimits().get("cpu").getAmount(), is("2"));
118+
}
119+
105120
@Test
106121
public void shouldCreateKubernetesPodWithPodMetadata() throws Exception {
107122
ArgumentCaptor<Pod> argumentCaptor = ArgumentCaptor.forClass(Pod.class);
@@ -281,4 +296,4 @@ public void usingPodYamlConfigurations_shouldCreateKubernetesPodWithPodLabels()
281296
assertThat(elasticAgentPod.getMetadata().getLabels(), is(labels));
282297
}
283298

284-
}
299+
}

0 commit comments

Comments
 (0)