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 Jan 21, 2022
1 parent abd29e8 commit a48b24c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 24 deletions.
4 changes: 3 additions & 1 deletion cmd/reconciler/ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"math/rand"
"net"
"strings"

Expand Down Expand Up @@ -289,7 +290,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: rand.Intn(1000),
}
}
return &v1alpha1.IPPool{
Expand Down
3 changes: 0 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ github.com/evanphx/json-patch v4.1.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLi
github.com/evanphx/json-patch v4.5.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/evanphx/json-patch v4.9.0+incompatible h1:kLcOMZeuLAJvL2BPWLMIj5oaZQobrkAqrL+WFZwQses=
github.com/evanphx/json-patch v4.9.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/form3tech-oss/jwt-go v3.2.2+incompatible/go.mod h1:pbq4aXjuKjdthFRnoDwaVPLA+WlJuPGy+QneDUgJi2k=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
Expand Down Expand Up @@ -249,7 +248,6 @@ github.com/imdario/mergo v0.0.0-20171009183408-7fe0c75c13ab/go.mod h1:2EnlNZ0dea
github.com/imdario/mergo v0.3.5/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/imdario/mergo v0.3.10 h1:6q5mVkdH/vYmqngx7kZQTjJ5HRsx+ImorDIEQ+beJgc=
github.com/imdario/mergo v0.3.10/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/j-keck/arping v0.0.0-20160618110441-2cf9dc699c56/go.mod h1:ymszkNOg6tORTn+6F6j+Jc8TOr5osrynvN6ivFWZ2GA=
github.com/jonboulle/clockwork v0.1.0 h1:VKV+ZcuP6l3yW9doeqz6ziZGgcynBVQO+obU0+0hcPo=
Expand Down Expand Up @@ -387,7 +385,6 @@ github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B
github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk=
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
github.com/spf13/cobra v1.1.1 h1:KfztREH0tPxJJ+geloSLaAkaPkr4ki2Er5quFV1TDo4=
github.com/spf13/cobra v1.1.1/go.mod h1:WnodtKOvamDL/PwE2M4iKs8aMDBZ5Q5klgD3qfVJQMI=
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
github.com/spf13/pflag v0.0.0-20170130214245-9ff6c6923cff/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
Expand Down
18 changes: 9 additions & 9 deletions pkg/reconciler/iploop.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ func composePodRef(pod v1.Pod) string {
}

func (rl ReconcileLooper) ReconcileIPPools() ([]net.IP, error) {
matchByPodRef := func(reservations []types.IPReservation, podRef string) int {
matchByContainerID := func(reservations []types.IPReservation, cid string) int {
foundidx := -1
for idx, v := range reservations {
if v.PodRef == podRef {
if v.ContainerID == cid {
return idx
}
}
Expand All @@ -133,10 +133,10 @@ func (rl ReconcileLooper) ReconcileIPPools() ([]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 _, cid := range containerIDsToDeallocate {
currentIPReservations, deallocatedIP, err = allocate.IterateForDeallocation(currentIPReservations, cid, matchByContainerID)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -188,10 +188,10 @@ func (rl ReconcileLooper) ReconcileOverlappingIPAddresses() error {
return nil
}

func findOutPodRefsToDeallocateIPsFrom(orphanedIP OrphanedIPReservations) []string {
var podRefsToDeallocate []string
func findOutContainerIDsToDeallocateIPsFrom(orphanedIP OrphanedIPReservations) []string {
var cidsToDeallocate []string
for _, orphanedAllocation := range orphanedIP.Allocations {
podRefsToDeallocate = append(podRefsToDeallocate, orphanedAllocation.PodRef)
cidsToDeallocate = append(cidsToDeallocate, orphanedAllocation.ContainerID)
}
return podRefsToDeallocate
return cidsToDeallocate
}
25 changes: 15 additions & 10 deletions pkg/reconciler/iploop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,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 @@ -86,7 +87,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 @@ -105,12 +107,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 @@ -123,7 +127,7 @@ var _ = Describe("IPReconciler", func() {

It("errors when attempting to clean up the IP address", func() {
reconciledIPs, err := ipReconciler.ReconcileIPPools()
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 @@ -144,11 +148,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,
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -448,9 +448,9 @@ sigs.k8s.io/controller-runtime/pkg/internal/testing/integration/internal
sigs.k8s.io/controller-runtime/pkg/log
sigs.k8s.io/controller-runtime/pkg/log/zap
sigs.k8s.io/controller-runtime/pkg/scheme
# sigs.k8s.io/structured-merge-diff/v4 v4.0.2
# sigs.k8s.io/controller-tools v0.4.1
## explicit
# sigs.k8s.io/structured-merge-diff/v4 v4.0.2
sigs.k8s.io/structured-merge-diff/v4/value
# sigs.k8s.io/yaml v1.2.0
sigs.k8s.io/yaml
Expand Down

0 comments on commit a48b24c

Please sign in to comment.