Skip to content

Commit

Permalink
fix: check agent pod deletiontimestamp before deleting
Browse files Browse the repository at this point in the history
Signed-off-by: Zespre Chang <zespre.chang@suse.com>
  • Loading branch information
starbops committed Feb 23, 2024
1 parent 1f991fc commit 4f94271
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
22 changes: 17 additions & 5 deletions pkg/controller/ippool/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,11 +291,15 @@ func (h *Handler) DeployAgent(ipPool *networkv1.IPPool, status networkv1.IPPoolS

logrus.Warningf("(ippool.DeployAgent) agent pod %s missing, redeploying", ipPool.Status.AgentPodRef.Name)
} else {
if pod.GetUID() == ipPool.Status.AgentPodRef.UID {
return status, nil
if pod.DeletionTimestamp != nil {
return status, fmt.Errorf("agent pod %s marked for deletion", ipPool.Status.AgentPodRef.Name)
}

return status, fmt.Errorf("agent pod %s uid mismatch, waiting for removal then redeploy", ipPool.Status.AgentPodRef.Name)
if pod.GetUID() != ipPool.Status.AgentPodRef.UID {
return status, fmt.Errorf("agent pod %s uid mismatch", ipPool.Status.AgentPodRef.Name)
}

return status, nil
}
}

Expand Down Expand Up @@ -403,11 +407,19 @@ func (h *Handler) MonitorAgent(ipPool *networkv1.IPPool, status networkv1.IPPool
}

if agentPod.GetUID() != ipPool.Status.AgentPodRef.UID || agentPod.Spec.Containers[0].Image != ipPool.Status.AgentPodRef.Image {
return status, h.podClient.Delete(agentPod.Namespace, agentPod.Name, &metav1.DeleteOptions{})
if agentPod.DeletionTimestamp != nil {
return status, fmt.Errorf("agent pod %s marked for deletion", agentPod.Name)
}

if err := h.podClient.Delete(agentPod.Namespace, agentPod.Name, &metav1.DeleteOptions{}); err != nil {
return status, err
}

return status, fmt.Errorf("agent pod %s obsolete and purged", agentPod.Name)
}

if !isPodReady(agentPod) {
return status, fmt.Errorf("agent %s for ippool %s/%s is not ready", agentPod.Name, ipPool.Namespace, ipPool.Name)
return status, fmt.Errorf("agent pod %s not ready", agentPod.Name)
}

return status, nil
Expand Down
7 changes: 3 additions & 4 deletions pkg/controller/ippool/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ func TestHandler_DeployAgent(t *testing.T) {
}

_, err = handler.DeployAgent(givenIPPool, givenIPPool.Status)
assert.Equal(t, fmt.Sprintf("agent pod %s uid mismatch, waiting for removal then redeploy", testPodName), err.Error())
assert.Equal(t, fmt.Sprintf("agent pod %s uid mismatch", testPodName), err.Error())
})
}

Expand Down Expand Up @@ -752,7 +752,7 @@ func TestHandler_MonitorAgent(t *testing.T) {
}

_, err = handler.MonitorAgent(givenIPPool, givenIPPool.Status)
assert.Equal(t, fmt.Sprintf("agent %s for ippool %s/%s is not ready", testPodName, testIPPoolNamespace, testIPPoolName), err.Error())
assert.Equal(t, fmt.Sprintf("agent pod %s not ready", testPodName), err.Error())
})

t.Run("agent pod ready", func(t *testing.T) {
Expand Down Expand Up @@ -821,10 +821,9 @@ func TestHandler_MonitorAgent(t *testing.T) {
}

_, err = handler.MonitorAgent(givenIPPool, givenIPPool.Status)
assert.Nil(t, err)
assert.Equal(t, fmt.Sprintf("agent pod %s obsolete and purged", testPodName), err.Error())

_, err = handler.podClient.Get(testPodNamespace, testPodName, metav1.GetOptions{})
assert.NotNil(t, err)
assert.Equal(t, fmt.Sprintf("pods \"%s\" not found", testPodName), err.Error())
})
}

0 comments on commit 4f94271

Please sign in to comment.