Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rebases #180: ip-reconciler: Use ContainerID instead of PodRef #399

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions pkg/reconciler/ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
"strings"
"testing"

"math/rand"
"strconv"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

Expand Down Expand Up @@ -146,9 +149,11 @@ var _ = Describe("Whereabouts IP reconciler", func() {
It("should report the dead pod's IP address as deleted", func() {
deletedIPAddrs, err := reconcileLooper.ReconcileIPPools(context.TODO())
Expect(err).NotTo(HaveOccurred())
// !bang this looks like a problem area...
Expect(deletedIPAddrs).To(Equal([]net.IP{net.ParseIP("10.10.10.1")}))
})

// !bang this can also be a problem area...
It("the IPPool should have only the IP reservation of the live pod", func() {
deletedIPAddrs, err := reconcileLooper.ReconcileIPPools(context.TODO())
Expect(err).NotTo(HaveOccurred())
Expand All @@ -162,7 +167,10 @@ var _ = Describe("Whereabouts IP reconciler", func() {
PodRef: fmt.Sprintf("%s/%s", namespace, pods[livePodIndex].Name),
},
}
Expect(poolAfterCleanup.Spec.Allocations).To(Equal(remainingAllocation))

for key, val := range poolAfterCleanup.Spec.Allocations {
Expect(val.PodRef).To(Equal(remainingAllocation[key].PodRef), "Mismatch at key: "+key)
}
})
})
})
Expand Down Expand Up @@ -384,7 +392,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 @@ -404,7 +413,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 @@ -423,12 +433,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 @@ -441,7 +453,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 @@ -452,7 +464,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 Expand Up @@ -547,11 +560,12 @@ func generateIPPool(cidr string, podRefs ...string) v1alpha1.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,
},
}
}
20 changes: 11 additions & 9 deletions pkg/reconciler/iploop.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,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 @@ -188,11 +188,13 @@ func (rl ReconcileLooper) ReconcileIPPools(ctx context.Context) ([]net.IP, error
var err error
var totalCleanedUpIps []net.IP
for _, orphanedIP := range rl.orphanedIPs {
// !bang: Reportedly: This the order returned from the below line is not fixed.
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 {
// !bang: This means it may cause the in-use pod ip to be wrongly claimed
currentIPReservations, deallocatedIP, err = allocate.IterateForDeallocation(currentIPReservations, containerID, matchByContainerID)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -256,10 +258,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
}
Loading