From f5a64e64693c7bbd7617282f92c8633d43325b17 Mon Sep 17 00:00:00 2001 From: Arjun Baindur Date: Wed, 19 Jan 2022 19:30:54 -0800 Subject: [PATCH] ip-reconciler: Use ContainerID instead of PodRef --- cmd/reconciler/ip_test.go | 5 ++++- pkg/reconciler/iploop.go | 18 +++++++++--------- pkg/reconciler/iploop_test.go | 25 +++++++++++++++---------- 3 files changed, 28 insertions(+), 20 deletions(-) diff --git a/cmd/reconciler/ip_test.go b/cmd/reconciler/ip_test.go index 17630ebf4..abafc02e8 100644 --- a/cmd/reconciler/ip_test.go +++ b/cmd/reconciler/ip_test.go @@ -4,7 +4,9 @@ import ( "context" "encoding/json" "fmt" + "math/rand" "net" + "strconv" "strings" . "github.com/onsi/ginkgo" @@ -290,7 +292,8 @@ func generateIPPoolSpec(ipRange string, namespace string, poolName string, podNa allocations := map[string]v1alpha1.IPAllocation{} for i, podName := range podNames { allocations[fmt.Sprintf("%d", i+1)] = v1alpha1.IPAllocation{ - PodRef: fmt.Sprintf("%s/%s", namespace, podName), + PodRef: fmt.Sprintf("%s/%s", namespace, podName), + ContainerID: strconv.Itoa(rand.Intn(1000)), } } return &v1alpha1.IPPool{ diff --git a/pkg/reconciler/iploop.go b/pkg/reconciler/iploop.go index 7f070226b..11f5782e0 100644 --- a/pkg/reconciler/iploop.go +++ b/pkg/reconciler/iploop.go @@ -120,10 +120,10 @@ func composePodRef(pod v1.Pod) string { } func (rl ReconcileLooper) ReconcileIPPools(ctx context.Context) ([]net.IP, error) { - matchByPodRef := func(reservations []types.IPReservation, podRef string) int { + matchByContainerID := func(reservations []types.IPReservation, containerID string) int { foundidx := -1 for idx, v := range reservations { - if v.PodRef == podRef { + if v.ContainerID == containerID { return idx } } @@ -134,10 +134,10 @@ func (rl ReconcileLooper) ReconcileIPPools(ctx context.Context) ([]net.IP, error var totalCleanedUpIps []net.IP for _, orphanedIP := range rl.orphanedIPs { currentIPReservations := orphanedIP.Pool.Allocations() - podRefsToDeallocate := findOutPodRefsToDeallocateIPsFrom(orphanedIP) + containerIDsToDeallocate := findOutContainerIDsToDeallocateIPsFrom(orphanedIP) var deallocatedIP net.IP - for _, podRef := range podRefsToDeallocate { - currentIPReservations, deallocatedIP, err = allocate.IterateForDeallocation(currentIPReservations, podRef, matchByPodRef) + for _, containerID := range containerIDsToDeallocate { + currentIPReservations, deallocatedIP, err = allocate.IterateForDeallocation(currentIPReservations, containerID, matchByContainerID) if err != nil { return nil, err } @@ -196,10 +196,10 @@ func (rl ReconcileLooper) ReconcileOverlappingIPAddresses(ctx context.Context) e return nil } -func findOutPodRefsToDeallocateIPsFrom(orphanedIP OrphanedIPReservations) []string { - var podRefsToDeallocate []string +func findOutContainerIDsToDeallocateIPsFrom(orphanedIP OrphanedIPReservations) []string { + var containerIDsToDeallocate []string for _, orphanedAllocation := range orphanedIP.Allocations { - podRefsToDeallocate = append(podRefsToDeallocate, orphanedAllocation.PodRef) + containerIDsToDeallocate = append(containerIDsToDeallocate, orphanedAllocation.ContainerID) } - return podRefsToDeallocate + return containerIDsToDeallocate } diff --git a/pkg/reconciler/iploop_test.go b/pkg/reconciler/iploop_test.go index 20115f8a0..cf5cbf526 100644 --- a/pkg/reconciler/iploop_test.go +++ b/pkg/reconciler/iploop_test.go @@ -65,7 +65,8 @@ var _ = Describe("IPReconciler", func() { BeforeEach(func() { podRef := "default/pod1" - reservations := generateIPReservation(firstIPInRange, podRef) + containerID := "1234567890" + reservations := generateIPReservation(firstIPInRange, podRef, containerID) pool := generateIPPool(ipCIDR, podRef) orphanedIPAddr := OrphanedIPReservations{ @@ -85,7 +86,8 @@ var _ = Describe("IPReconciler", func() { Context("and they are actually multiple IPs", func() { BeforeEach(func() { podRef := "default/pod2" - reservations := generateIPReservation("192.168.14.2", podRef) + containerID := "1234567890" + reservations := generateIPReservation("192.168.14.2", podRef, containerID) pool := generateIPPool(ipCIDR, podRef, "default/pod2", "default/pod3") orphanedIPAddr := OrphanedIPReservations{ @@ -104,12 +106,14 @@ var _ = Describe("IPReconciler", func() { }) Context("but the IP reservation owner does not match", func() { - var reservationPodRef string + var reservationPodRef, reservationContainerID string BeforeEach(func() { - reservationPodRef = "default/pod2" + reservationPodRef = "default/pod1" + reservationContainerID = "1234567890" podRef := "default/pod1" - reservations := generateIPReservation(firstIPInRange, podRef) - erroredReservations := generateIPReservation(firstIPInRange, reservationPodRef) + podContainerID := "0987654321" + reservations := generateIPReservation(firstIPInRange, podRef, podContainerID) + erroredReservations := generateIPReservation(firstIPInRange, reservationPodRef, reservationContainerID) pool := generateIPPool(ipCIDR, podRef) orphanedIPAddr := OrphanedIPReservations{ @@ -122,7 +126,7 @@ var _ = Describe("IPReconciler", func() { It("errors when attempting to clean up the IP address", func() { reconciledIPs, err := ipReconciler.ReconcileIPPools(context.TODO()) - Expect(err).To(MatchError(fmt.Sprintf("did not find reserved IP for container %s", reservationPodRef))) + Expect(err).To(MatchError(fmt.Sprintf("did not find reserved IP for container %s", reservationContainerID))) Expect(reconciledIPs).To(BeEmpty()) }) }) @@ -143,11 +147,12 @@ func generateIPPool(cidr string, podRefs ...string) whereaboutsv1alpha1.IPPool { } } -func generateIPReservation(ip string, podRef string) []types.IPReservation { +func generateIPReservation(ip string, podRef, containerID string) []types.IPReservation { return []types.IPReservation{ { - IP: net.ParseIP(ip), - PodRef: podRef, + IP: net.ParseIP(ip), + PodRef: podRef, + ContainerID: containerID, }, } }