Skip to content

Commit 63c64b0

Browse files
committed
Avoid already terminated pods while checking for unregistered pods.
1 parent faa7579 commit 63c64b0

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ public void terminateUnregisteredInstances(PluginSettings settings, Agents agent
127127
}
128128

129129
LOG.warn(format("Terminating instances that did not register {0}.", toTerminate.instances.keySet()));
130-
for (KubernetesInstance container : toTerminate.instances.values()) {
131-
terminate(container.name(), settings);
130+
for (String podName : toTerminate.instances.keySet()) {
131+
terminate(podName, settings);
132132
}
133133
}
134134

@@ -154,6 +154,7 @@ public void refreshAll(PluginRequest pluginRequest) {
154154
KubernetesClient client = factory.client(pluginRequest.getPluginSettings());
155155
PodList list = client.pods().list();
156156

157+
instances.clear();
157158
for (Pod pod : list.getItems()) {
158159
Map<String, String> podLabels = pod.getMetadata().getLabels();
159160
if (podLabels != null) {
@@ -162,6 +163,8 @@ public void refreshAll(PluginRequest pluginRequest) {
162163
}
163164
}
164165
}
166+
167+
LOG.info(String.format("[refresh-pod-state] Pod information successfully synced. All(Running/Pending) pod count is %d.", instances.size()));
165168
}
166169

167170
@Override
@@ -182,7 +185,13 @@ private KubernetesAgentInstances unregisteredAfterTimeout(PluginSettings setting
182185
if (knownAgents.containsAgentWithId(instanceName)) {
183186
continue;
184187
}
185-
Pod pod = client.pods().withName(instanceName).get();
188+
189+
Pod pod = getPod(client, instanceName);
190+
if (pod == null) {
191+
LOG.debug(String.format("[server-ping] Pod with name %s is already deleted.", instanceName));
192+
continue;
193+
}
194+
186195
Date createdAt = getSimpleDateFormat().parse(pod.getMetadata().getCreationTimestamp());
187196
DateTime dateTimeCreated = new DateTime(createdAt);
188197

@@ -193,6 +202,15 @@ private KubernetesAgentInstances unregisteredAfterTimeout(PluginSettings setting
193202
return unregisteredInstances;
194203
}
195204

205+
private Pod getPod(KubernetesClient client, String instanceName) {
206+
try {
207+
return client.pods().withName(instanceName).get();
208+
} catch (Exception e) {
209+
LOG.warn(String.format("[server-ping] Failed to fetch pod[%s] information:", instanceName), e);
210+
return null;
211+
}
212+
}
213+
196214
public boolean instanceExists(KubernetesInstance instance) {
197215
return instances.contains(instance);
198216
}

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import cd.go.contrib.elasticagent.model.JobIdentifier;
2020
import cd.go.contrib.elasticagent.requests.CreateAgentRequest;
2121
import io.fabric8.kubernetes.api.model.DoneablePod;
22+
import io.fabric8.kubernetes.api.model.ObjectMeta;
2223
import io.fabric8.kubernetes.api.model.Pod;
2324
import io.fabric8.kubernetes.api.model.PodList;
2425
import io.fabric8.kubernetes.client.KubernetesClient;
@@ -30,9 +31,12 @@
3031
import org.mockito.InOrder;
3132
import org.mockito.Mock;
3233

34+
import java.util.Arrays;
3335
import java.util.Collections;
3436
import java.util.HashMap;
37+
import java.util.Map;
3538

39+
import static cd.go.contrib.elasticagent.Constants.JOB_ID_LABEL_KEY;
3640
import static org.junit.Assert.assertTrue;
3741
import static org.mockito.ArgumentMatchers.any;
3842
import static org.mockito.Mockito.*;
@@ -60,6 +64,8 @@ public class KubernetesAgentInstancesTest {
6064
@Mock
6165
private MixedOperation<Pod, PodList, DoneablePod, PodResource<Pod, DoneablePod>> mockedOperation;
6266

67+
@Mock
68+
private PodList podList;
6369
private HashMap<String, String> testProperties;
6470

6571
@Before
@@ -72,7 +78,6 @@ public void setUp() {
7278
JobIdentifier jobId = new JobIdentifier("test", 1L, "Test pipeline", "test name", "1", "test job", 100L);
7379
when(mockCreateAgentRequest.jobIdentifier()).thenReturn(jobId);
7480

75-
final PodList podList = mock(PodList.class);
7681
when(mockKubernetesClient.pods()).thenReturn(mockedOperation);
7782
when(mockPluginRequest.getPluginSettings()).thenReturn(mockPluginSettings);
7883
when(mockedOperation.list()).thenReturn(podList);
@@ -117,6 +122,18 @@ public void shouldNotCreatePodWhenOutstandingRequestsExistForJobs() {
117122
verify(mockKubernetesInstanceFactory, times(1)).create(any(), any(), any(), any(), any());
118123
reset(mockKubernetesInstanceFactory);
119124

125+
final Map<String, String> labels = new HashMap<>();
126+
labels.put(JOB_ID_LABEL_KEY, jobId.getJobId().toString());
127+
labels.put(Constants.KUBERNETES_POD_KIND_LABEL_KEY, Constants.KUBERNETES_POD_KIND_LABEL_VALUE);
128+
129+
final Pod pod = mock(Pod.class);
130+
final ObjectMeta objectMeta = mock(ObjectMeta.class);
131+
when(pod.getMetadata()).thenReturn(objectMeta);
132+
when(objectMeta.getLabels()).thenReturn(labels);
133+
when(objectMeta.getName()).thenReturn("test-agent");
134+
when(podList.getItems()).thenReturn(Arrays.asList(pod));
135+
when(mockKubernetesInstanceFactory.fromKubernetesPod(pod)).thenReturn(kubernetesInstance);
136+
120137
agentInstances.create(mockCreateAgentRequest, mockPluginSettings, mockPluginRequest);
121138
verify(mockKubernetesInstanceFactory, times(0)).create(any(), any(), any(), any(), any());
122139
}
@@ -140,6 +157,18 @@ public void shouldNotCreatePodsWhenOutstandingLimitOfPendingKubernetesPodsHasRea
140157
verify(mockKubernetesInstanceFactory, times(1)).create(any(), any(), any(), any(), any());
141158
reset(mockKubernetesInstanceFactory);
142159

160+
final Map<String, String> labels = new HashMap<>();
161+
labels.put(JOB_ID_LABEL_KEY, jobId.getJobId().toString());
162+
labels.put(Constants.KUBERNETES_POD_KIND_LABEL_KEY, Constants.KUBERNETES_POD_KIND_LABEL_VALUE);
163+
164+
final Pod pod = mock(Pod.class);
165+
final ObjectMeta objectMeta = mock(ObjectMeta.class);
166+
when(pod.getMetadata()).thenReturn(objectMeta);
167+
when(objectMeta.getLabels()).thenReturn(labels);
168+
when(objectMeta.getName()).thenReturn("test-agent");
169+
when(podList.getItems()).thenReturn(Arrays.asList(pod));
170+
when(mockKubernetesInstanceFactory.fromKubernetesPod(pod)).thenReturn(kubernetesInstance);
171+
143172
//second create agent request
144173
agentInstances.create(mockCreateAgentRequest, mockPluginSettings, mockPluginRequest);
145174
verify(mockKubernetesInstanceFactory, times(0)).create(any(), any(), any(), any(), any());

0 commit comments

Comments
 (0)