Skip to content

Commit

Permalink
ip-reconciler: Use ContainerID instead of PodRef
Browse files Browse the repository at this point in the history
  • Loading branch information
xagent003 committed Mar 30, 2022
1 parent 02e9af6 commit f5a64e6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 20 deletions.
5 changes: 4 additions & 1 deletion cmd/reconciler/ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"context"
"encoding/json"
"fmt"
"math/rand"
"net"
"strconv"
"strings"

. "github.com/onsi/ginkgo"
Expand Down Expand Up @@ -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{
Expand Down
18 changes: 9 additions & 9 deletions pkg/reconciler/iploop.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
25 changes: 15 additions & 10 deletions pkg/reconciler/iploop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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())
})
})
Expand All @@ -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,
},
}
}

0 comments on commit f5a64e6

Please sign in to comment.