Skip to content

Commit 313ddf0

Browse files
committed
Prevent accidental IP deallocation in statefulsets
This commit addresses a potential issue where IP allocations could be accidentally deleted when new pods with the same name and namespace are created in statefulset scenarios. The logic now correctly checks if a pod with the matching name and namespace exists and is not marked for deletion. If such a pod exists, the deallocation is skipped, preventing conflicts. The commit also includes clarified logging messages for improved debugging. Signed-off-by: Marcelo <[email protected]>
1 parent 9400392 commit 313ddf0

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

pkg/controlloop/pod.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"k8s.io/client-go/kubernetes"
1414

1515
v1 "k8s.io/api/core/v1"
16+
apierrors "k8s.io/apimachinery/pkg/api/errors"
1617
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1718
"k8s.io/apimachinery/pkg/fields"
1819
"k8s.io/apimachinery/pkg/util/wait"
@@ -222,6 +223,21 @@ func (pc *PodController) garbageCollectPodIPs(pod *v1.Pod) error {
222223
for _, pool := range pools {
223224
for allocationIndex, allocation := range pool.Spec.Allocations {
224225
if allocation.PodRef == podID(podNamespace, podName) {
226+
logging.Verbosef("Found an existing allocation: %+v", allocation)
227+
228+
// The allocation could belong to a new pod with the same name and namespace. Stateful set scenarios.
229+
// The previous pod should be gone by the time a pod deletion event is received.
230+
if newPod, err := pc.k8sClient.CoreV1().Pods(podNamespace).Get(context.TODO(), podName, metav1.GetOptions{}); err == nil {
231+
if newPod.DeletionTimestamp == nil {
232+
logging.Verbosef("A pod with the same name and namespace was found and is not marked for deletion. Skipping deallocation")
233+
continue
234+
}
235+
236+
logging.Verbosef("A pod with the same name and namespace was found and is marked for deletion. Cleaning up the stale allocation. DeletionTimestamp: %+v", newPod.DeletionTimestamp)
237+
} else if !apierrors.IsNotFound(err) {
238+
return fmt.Errorf("failed to get pod to verify allocation: %+v", err)
239+
}
240+
225241
logging.Verbosef("stale allocation to cleanup: %+v", allocation)
226242

227243
client := *wbclient.NewKubernetesClient(pc.wbClient, pc.k8sClient)

0 commit comments

Comments
 (0)