Skip to content

Commit 246bdb8

Browse files
authored
Merge pull request #35 from bdpiparva/pending-pod-count
Refresh instance state before taking a lock.
2 parents c720475 + 9a1184e commit 246bdb8

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,16 @@ public KubernetesAgentInstances(KubernetesClientFactory factory, KubernetesInsta
5858
}
5959

6060
@Override
61-
public KubernetesInstance create(CreateAgentRequest request, PluginSettings settings, PluginRequest pluginRequest) throws Exception {
61+
public KubernetesInstance create(CreateAgentRequest request, PluginSettings settings, PluginRequest pluginRequest) {
6262
final Integer maxAllowedContainers = settings.getMaxPendingPods();
6363
synchronized (instances) {
64+
refreshAll(pluginRequest);
6465
doWithLockOnSemaphore(new SetupSemaphore(maxAllowedContainers, instances, semaphore));
6566

6667
if (semaphore.tryAcquire()) {
6768
return createKubernetesInstance(request, settings, pluginRequest);
6869
} else {
69-
LOG.warn(format("The number of pending kubernetes pods is currently at the maximum permissible limit ({0}). Total kubernetes pods ({1}). Not creating any more containers.", maxAllowedContainers, instances.size()));
70+
LOG.warn(format("Create Agent Request] The number of pending kubernetes pods is currently at the maximum permissible limit ({0}). Total kubernetes pods ({1}). Not creating any more containers.", maxAllowedContainers, instances.size()));
7071
return null;
7172
}
7273
}
@@ -107,7 +108,7 @@ private boolean isUsingPodYaml(CreateAgentRequest request) {
107108
}
108109

109110
@Override
110-
public void terminate(String agentId, PluginSettings settings) throws Exception {
111+
public void terminate(String agentId, PluginSettings settings) {
111112
KubernetesInstance instance = instances.get(agentId);
112113
if (instance != null) {
113114
KubernetesClient client = factory.client(settings);

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

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,19 @@
1818

1919
import cd.go.contrib.elasticagent.model.JobIdentifier;
2020
import cd.go.contrib.elasticagent.requests.CreateAgentRequest;
21+
import io.fabric8.kubernetes.api.model.DoneablePod;
22+
import io.fabric8.kubernetes.api.model.Pod;
23+
import io.fabric8.kubernetes.api.model.PodList;
2124
import io.fabric8.kubernetes.client.KubernetesClient;
25+
import io.fabric8.kubernetes.client.dsl.MixedOperation;
26+
import io.fabric8.kubernetes.client.dsl.PodResource;
2227
import org.joda.time.DateTime;
2328
import org.junit.Before;
2429
import org.junit.Test;
30+
import org.mockito.InOrder;
2531
import org.mockito.Mock;
2632

33+
import java.util.Collections;
2734
import java.util.HashMap;
2835

2936
import static org.junit.Assert.assertTrue;
@@ -50,10 +57,13 @@ public class KubernetesAgentInstancesTest {
5057
@Mock
5158
PluginRequest mockPluginRequest;
5259

60+
@Mock
61+
private MixedOperation<Pod, PodList, DoneablePod, PodResource<Pod, DoneablePod>> mockedOperation;
62+
5363
private HashMap<String, String> testProperties;
5464

5565
@Before
56-
public void setUp() throws Exception {
66+
public void setUp() {
5767
initMocks(this);
5868
testProperties = new HashMap<>();
5969
when(mockCreateAgentRequest.properties()).thenReturn(testProperties);
@@ -64,7 +74,7 @@ public void setUp() throws Exception {
6474
}
6575

6676
@Test
67-
public void shouldCreateKubernetesPodUsingPodYamlAndCacheCreatedInstance() throws Exception {
77+
public void shouldCreateKubernetesPodUsingPodYamlAndCacheCreatedInstance() {
6878
KubernetesInstance kubernetesInstance = new KubernetesInstance(new DateTime(), "test", "test-agent", new HashMap<>(), 100L, PodState.Running);
6979
when(mockKubernetesInstanceFactory.create(mockCreateAgentRequest, mockPluginSettings, mockKubernetesClient, mockPluginRequest, true)).
7080
thenReturn(kubernetesInstance);
@@ -77,7 +87,7 @@ public void shouldCreateKubernetesPodUsingPodYamlAndCacheCreatedInstance() throw
7787
}
7888

7989
@Test
80-
public void shouldCreateKubernetesPodAndCacheCreatedInstance() throws Exception {
90+
public void shouldCreateKubernetesPodAndCacheCreatedInstance() {
8191
KubernetesInstance kubernetesInstance = new KubernetesInstance(new DateTime(), "test", "test-agent", new HashMap<>(), 100L, PodState.Running);
8292
when(mockKubernetesInstanceFactory.create(mockCreateAgentRequest, mockPluginSettings, mockKubernetesClient, mockPluginRequest, false)).
8393
thenReturn(kubernetesInstance);
@@ -88,7 +98,7 @@ public void shouldCreateKubernetesPodAndCacheCreatedInstance() throws Exception
8898
}
8999

90100
@Test
91-
public void shouldNotCreatePodWhenOutstandingRequestsExistForJobs() throws Exception {
101+
public void shouldNotCreatePodWhenOutstandingRequestsExistForJobs() {
92102
KubernetesInstance kubernetesInstance = new KubernetesInstance(new DateTime(), "test", "test-agent", new HashMap<>(), 100L, PodState.Running);
93103
when(mockKubernetesInstanceFactory.create(mockCreateAgentRequest, mockPluginSettings, mockKubernetesClient, mockPluginRequest, false)).
94104
thenReturn(kubernetesInstance);
@@ -106,7 +116,7 @@ public void shouldNotCreatePodWhenOutstandingRequestsExistForJobs() throws Excep
106116
}
107117

108118
@Test
109-
public void shouldNotCreatePodsWhenOutstandingLimitOfPendingKubernetesPodsHasReached() throws Exception {
119+
public void shouldNotCreatePodsWhenOutstandingLimitOfPendingKubernetesPodsHasReached() {
110120
//set maximum pending pod count to 1
111121
when(mockPluginSettings.getMaxPendingPods()).thenReturn(1);
112122

@@ -128,4 +138,23 @@ public void shouldNotCreatePodsWhenOutstandingLimitOfPendingKubernetesPodsHasRea
128138
agentInstances.create(mockCreateAgentRequest, mockPluginSettings, mockPluginRequest);
129139
verify(mockKubernetesInstanceFactory, times(0)).create(any(), any(), any(), any(), any());
130140
}
141+
142+
@Test
143+
public void shouldSyncPodsStateFromClusterBeforeCreatingPod() {
144+
final PodList podList = mock(PodList.class);
145+
when(mockKubernetesClient.pods()).thenReturn(mockedOperation);
146+
when(mockPluginRequest.getPluginSettings()).thenReturn(mockPluginSettings);
147+
when(mockedOperation.list()).thenReturn(podList);
148+
when(podList.getItems()).thenReturn(Collections.emptyList());
149+
when(mockKubernetesInstanceFactory.create(mockCreateAgentRequest, mockPluginSettings, mockKubernetesClient, mockPluginRequest, false)).
150+
thenReturn(new KubernetesInstance(new DateTime(), "test", "test-agent", new HashMap<>(), 100L, PodState.Running));
151+
152+
final KubernetesAgentInstances agentInstances = new KubernetesAgentInstances(factory, mockKubernetesInstanceFactory);
153+
154+
agentInstances.create(mockCreateAgentRequest, mockPluginSettings, mockPluginRequest);
155+
156+
InOrder inOrder = inOrder(mockKubernetesInstanceFactory, mockedOperation);
157+
inOrder.verify(mockedOperation).list();
158+
inOrder.verify(mockKubernetesInstanceFactory).create(mockCreateAgentRequest, mockPluginSettings, mockKubernetesClient, mockPluginRequest, false);
159+
}
131160
}

0 commit comments

Comments
 (0)