Skip to content

Commit c5e45aa

Browse files
author
Marcelo Guerrero Viveros
authored
Merge pull request #480 from mlguerrero12/removeparenttimeout
Align api calls timeouts cronjob ip reconciler
2 parents 4a6fc53 + d394ff2 commit c5e45aa

File tree

8 files changed

+81
-97
lines changed

8 files changed

+81
-97
lines changed

cmd/whereabouts_test.go

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ func AllocateAndReleaseAddressesTest(ipRange string, gw string, kubeconfigPath s
5757
wbClient := *kubernetes.NewKubernetesClient(
5858
fake.NewSimpleClientset(
5959
ipPool(conf.IPRanges[0].Range, podNamespace, ipamNetworkName)),
60-
fakek8sclient.NewSimpleClientset(),
61-
0)
60+
fakek8sclient.NewSimpleClientset())
6261

6362
for i := 0; i < len(expectedAddresses); i++ {
6463
name := fmt.Sprintf("%s-%d", podName, i)
@@ -164,8 +163,7 @@ var _ = Describe("Whereabouts operations", func() {
164163
fake.NewSimpleClientset(
165164
ipPool(ipamConf.IPRanges[0].Range, podNamespace, ipamNetworkName, []whereaboutstypes.IPReservation{
166165
{PodRef: ipamConf.GetPodRef(), IfName: ifname, IP: net.ParseIP(expectedAddress)}, {PodRef: "test"}}...)),
167-
fakek8sclient.NewSimpleClientset(),
168-
0)
166+
fakek8sclient.NewSimpleClientset())
169167

170168
cniConf, err := newCNINetConf(cniVersion, ipamConf)
171169
Expect(err).NotTo(HaveOccurred())
@@ -928,8 +926,7 @@ var _ = Describe("Whereabouts operations", func() {
928926
wbClient := *kubernetes.NewKubernetesClient(
929927
fake.NewSimpleClientset(
930928
ipPool(ipamConf.IPRanges[0].Range, podNamespace, ipamConf.NetworkName)),
931-
fakek8sclient.NewSimpleClientset(),
932-
0)
929+
fakek8sclient.NewSimpleClientset())
933930

934931
// allocate 8 IPs (192.168.1.5 - 192.168.1.12); the entirety of the pool defined above
935932
for i := 0; i < 8; i++ {
@@ -1000,8 +997,7 @@ var _ = Describe("Whereabouts operations", func() {
1000997
wbClient := *kubernetes.NewKubernetesClient(
1001998
fake.NewSimpleClientset(
1002999
ipPool(firstRange, podNamespace, ""), ipPool(secondRange, podNamespace, "")),
1003-
fakek8sclient.NewSimpleClientset(),
1004-
0)
1000+
fakek8sclient.NewSimpleClientset())
10051001

10061002
// ----------------------------- range 1
10071003

@@ -1124,8 +1120,7 @@ var _ = Describe("Whereabouts operations", func() {
11241120
wbClient := *kubernetes.NewKubernetesClient(
11251121
fake.NewSimpleClientset(
11261122
ipPool(firstRange, podNamespace, ""), ipPool(secondRange, podNamespace, "")),
1127-
fakek8sclient.NewSimpleClientset(),
1128-
0)
1123+
fakek8sclient.NewSimpleClientset())
11291124

11301125
// ----------------------------- range 1
11311126

@@ -1248,8 +1243,7 @@ var _ = Describe("Whereabouts operations", func() {
12481243
wbClient := *kubernetes.NewKubernetesClient(
12491244
fake.NewSimpleClientset(
12501245
ipPool(firstRange, podNamespace, ""), ipPool(secondRange, podNamespace, "")),
1251-
fakek8sclient.NewSimpleClientset(),
1252-
0)
1246+
fakek8sclient.NewSimpleClientset())
12531247

12541248
// ----------------------------- range 1
12551249

@@ -1373,7 +1367,7 @@ func newK8sIPAM(containerID, ifName string, ipamConf *whereaboutstypes.IPAMConfi
13731367
if err != nil {
13741368
return nil
13751369
}
1376-
k8sIPAM.Client = *kubernetes.NewKubernetesClient(wbClient, k8sCoreClient, 0)
1370+
k8sIPAM.Client = *kubernetes.NewKubernetesClient(wbClient, k8sCoreClient)
13771371
return k8sIPAM
13781372
}
13791373

e2e/e2e_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"net"
77
"os"
8+
"sort"
89
"strings"
910
"testing"
1011
"time"
@@ -598,6 +599,10 @@ var _ = Describe("Whereabouts functionality", func() {
598599
It("can reclaim the previously allocated IPs", func() {
599600
By("checking that the IP allocation is removed when the pod is deleted")
600601
Expect(clientInfo.ScaleStatefulSet(serviceName, namespace, -1)).To(Succeed())
602+
603+
const podDeleteTimeout = 20 * time.Second
604+
err := wbtestclient.WaitForPodToDisappear(context.Background(), clientInfo.Client, namespace, podName, podDeleteTimeout)
605+
Expect(err).NotTo(HaveOccurred())
601606
verifyNoAllocationsForPodRef(clientInfo, rangeWithTwoIPs, namespace, podName, secondaryIPs)
602607

603608
By("adding previous allocations")
@@ -895,6 +900,11 @@ func allocationForPodRef(podRef string, ipPool v1alpha1.IPPool) []v1alpha1.IPAll
895900
allocations = append(allocations, allocation)
896901
}
897902
}
903+
904+
sort.Slice(allocations, func(i, j int) bool {
905+
return allocations[i].IfName < allocations[j].IfName
906+
})
907+
898908
return allocations
899909
}
900910

pkg/controlloop/pod.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ func (pc *PodController) garbageCollectPodIPs(pod *v1.Pod) error {
225225
if allocation.PodRef == podID(podNamespace, podName) {
226226
logging.Verbosef("stale allocation to cleanup: %+v", allocation)
227227

228-
client := *wbclient.NewKubernetesClient(nil, pc.k8sClient, 0)
228+
client := *wbclient.NewKubernetesClient(nil, pc.k8sClient)
229229
wbClient := &wbclient.KubernetesIPAM{
230230
Client: client,
231231
Config: *ipamConfig,

pkg/reconciler/ip.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,20 @@
11
package reconciler
22

33
import (
4-
"context"
5-
"time"
6-
74
"github.com/k8snetworkplumbingwg/whereabouts/pkg/logging"
85
)
96

10-
const (
11-
defaultReconcilerTimeout = 30
12-
)
13-
147
func ReconcileIPs(errorChan chan error) {
158
logging.Verbosef("starting reconciler run")
16-
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(defaultReconcilerTimeout*time.Second))
17-
defer cancel()
189

19-
ipReconcileLoop, err := NewReconcileLooper(ctx, defaultReconcilerTimeout)
10+
ipReconcileLoop, err := NewReconcileLooper()
2011
if err != nil {
2112
_ = logging.Errorf("failed to create the reconcile looper: %v", err)
2213
errorChan <- err
2314
return
2415
}
2516

26-
cleanedUpIps, err := ipReconcileLoop.ReconcileIPPools(ctx)
17+
cleanedUpIps, err := ipReconcileLoop.ReconcileIPPools()
2718
if err != nil {
2819
_ = logging.Errorf("failed to clean up IP for allocations: %v", err)
2920
errorChan <- err
@@ -36,7 +27,7 @@ func ReconcileIPs(errorChan chan error) {
3627
logging.Debugf("no IP addresses to cleanup")
3728
}
3829

39-
if err := ipReconcileLoop.ReconcileOverlappingIPAddresses(ctx); err != nil {
30+
if err := ipReconcileLoop.ReconcileOverlappingIPAddresses(); err != nil {
4031
errorChan <- err
4132
return
4233
}

pkg/reconciler/ip_test.go

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ var _ = Describe("Whereabouts IP reconciler", func() {
3838
namespace = "default"
3939
networkName = "net1"
4040
podName = "pod1"
41-
timeout = 10
4241
)
4342

4443
var (
@@ -75,16 +74,16 @@ var _ = Describe("Whereabouts IP reconciler", func() {
7574
Context("reconciling the IPPool", func() {
7675
BeforeEach(func() {
7776
var err error
78-
reconcileLooper, err = NewReconcileLooperWithClient(context.TODO(), kubernetes.NewKubernetesClient(wbClient, k8sClientSet, timeout), timeout)
77+
reconcileLooper, err = NewReconcileLooperWithClient(kubernetes.NewKubernetesClient(wbClient, k8sClientSet))
7978
Expect(err).NotTo(HaveOccurred())
8079
})
8180

8281
It("should report the deleted IP reservation", func() {
83-
Expect(reconcileLooper.ReconcileIPPools(context.TODO())).To(Equal([]net.IP{net.ParseIP("10.10.10.1")}))
82+
Expect(reconcileLooper.ReconcileIPPools()).To(Equal([]net.IP{net.ParseIP("10.10.10.1")}))
8483
})
8584

8685
It("the pool's orphaned IP should be deleted after the reconcile loop", func() {
87-
_, err := reconcileLooper.ReconcileIPPools(context.TODO())
86+
_, err := reconcileLooper.ReconcileIPPools()
8887
Expect(err).NotTo(HaveOccurred())
8988
poolAfterCleanup, err := wbClient.WhereaboutsV1alpha1().IPPools(namespace).Get(context.TODO(), pool.GetName(), metav1.GetOptions{})
9089
Expect(err).NotTo(HaveOccurred())
@@ -139,18 +138,18 @@ var _ = Describe("Whereabouts IP reconciler", func() {
139138
Context("reconciling the IPPool", func() {
140139
BeforeEach(func() {
141140
var err error
142-
reconcileLooper, err = NewReconcileLooperWithClient(context.TODO(), kubernetes.NewKubernetesClient(wbClient, k8sClientSet, timeout), timeout)
141+
reconcileLooper, err = NewReconcileLooperWithClient(kubernetes.NewKubernetesClient(wbClient, k8sClientSet))
143142
Expect(err).NotTo(HaveOccurred())
144143
})
145144

146145
It("should report the dead pod's IP address as deleted", func() {
147-
deletedIPAddrs, err := reconcileLooper.ReconcileIPPools(context.TODO())
146+
deletedIPAddrs, err := reconcileLooper.ReconcileIPPools()
148147
Expect(err).NotTo(HaveOccurred())
149148
Expect(deletedIPAddrs).To(Equal([]net.IP{net.ParseIP("10.10.10.1")}))
150149
})
151150

152151
It("the IPPool should have only the IP reservation of the live pod", func() {
153-
deletedIPAddrs, err := reconcileLooper.ReconcileIPPools(context.TODO())
152+
deletedIPAddrs, err := reconcileLooper.ReconcileIPPools()
154153
Expect(err).NotTo(HaveOccurred())
155154
Expect(deletedIPAddrs).NotTo(BeEmpty())
156155

@@ -190,11 +189,11 @@ var _ = Describe("Whereabouts IP reconciler", func() {
190189

191190
By("initializing the reconciler")
192191
var err error
193-
reconcileLooper, err = NewReconcileLooperWithClient(context.TODO(), kubernetes.NewKubernetesClient(wbClient, k8sClientSet, timeout), timeout)
192+
reconcileLooper, err = NewReconcileLooperWithClient(kubernetes.NewKubernetesClient(wbClient, k8sClientSet))
194193
Expect(err).NotTo(HaveOccurred())
195194

196195
By("reconciling and checking that the correct entry is deleted")
197-
deletedIPAddrs, err := reconcileLooper.ReconcileIPPools(context.TODO())
196+
deletedIPAddrs, err := reconcileLooper.ReconcileIPPools()
198197
Expect(err).NotTo(HaveOccurred())
199198
Expect(deletedIPAddrs).To(Equal([]net.IP{net.ParseIP("10.10.10.2")}))
200199

@@ -272,9 +271,9 @@ var _ = Describe("Whereabouts IP reconciler", func() {
272271

273272
It("will delete an orphaned IP address", func() {
274273
Expect(k8sClientSet.CoreV1().Pods(namespace).Delete(context.TODO(), pods[podIndexToRemove].Name, metav1.DeleteOptions{})).NotTo(HaveOccurred())
275-
newReconciler, err := NewReconcileLooperWithClient(context.TODO(), kubernetes.NewKubernetesClient(wbClient, k8sClientSet, timeout), timeout)
274+
newReconciler, err := NewReconcileLooperWithClient(kubernetes.NewKubernetesClient(wbClient, k8sClientSet))
276275
Expect(err).NotTo(HaveOccurred())
277-
Expect(newReconciler.ReconcileOverlappingIPAddresses(context.TODO())).To(Succeed())
276+
Expect(newReconciler.ReconcileOverlappingIPAddresses()).To(Succeed())
278277

279278
expectedClusterWideIPs := 2
280279
clusterWideIPAllocations, err := wbClient.WhereaboutsV1alpha1().OverlappingRangeIPReservations(namespace).List(context.TODO(), metav1.ListOptions{})
@@ -338,9 +337,9 @@ var _ = Describe("Whereabouts IP reconciler", func() {
338337
})
339338

340339
It("will not delete an IP address that isn't orphaned after running reconciler", func() {
341-
newReconciler, err := NewReconcileLooperWithClient(context.TODO(), kubernetes.NewKubernetesClient(wbClient, k8sClientSet, timeout), timeout)
340+
newReconciler, err := NewReconcileLooperWithClient(kubernetes.NewKubernetesClient(wbClient, k8sClientSet))
342341
Expect(err).NotTo(HaveOccurred())
343-
Expect(newReconciler.ReconcileOverlappingIPAddresses(context.TODO())).To(Succeed())
342+
Expect(newReconciler.ReconcileOverlappingIPAddresses()).To(Succeed())
344343

345344
expectedClusterWideIPs := 1
346345
clusterWideIPAllocations, err := wbClient.WhereaboutsV1alpha1().OverlappingRangeIPReservations(namespace).List(context.TODO(), metav1.ListOptions{})
@@ -369,12 +368,12 @@ var _ = Describe("Whereabouts IP reconciler", func() {
369368

370369
pool = generateIPPoolSpec(ipRange, namespace, poolName, pod.Name)
371370
wbClient = fakewbclient.NewSimpleClientset(pool)
372-
reconcileLooper, err = NewReconcileLooperWithClient(context.TODO(), kubernetes.NewKubernetesClient(wbClient, k8sClientSet, timeout), timeout)
371+
reconcileLooper, err = NewReconcileLooperWithClient(kubernetes.NewKubernetesClient(wbClient, k8sClientSet))
373372
Expect(err).NotTo(HaveOccurred())
374373
})
375374

376375
It("can be reconciled", func() {
377-
Expect(reconcileLooper.ReconcileIPPools(context.TODO())).NotTo(BeEmpty())
376+
Expect(reconcileLooper.ReconcileIPPools()).NotTo(BeEmpty())
378377
})
379378
})
380379
})
@@ -410,7 +409,7 @@ var _ = Describe("IPReconciler", func() {
410409
})
411410

412411
It("does not delete anything", func() {
413-
reconciledIPs, err := ipReconciler.ReconcileIPPools(context.TODO())
412+
reconciledIPs, err := ipReconciler.ReconcileIPPools()
414413
Expect(err).NotTo(HaveOccurred())
415414
Expect(reconciledIPs).To(BeEmpty())
416415
})
@@ -438,7 +437,7 @@ var _ = Describe("IPReconciler", func() {
438437
})
439438

440439
It("does delete the orphaned IP address", func() {
441-
reconciledIPs, err := ipReconciler.ReconcileIPPools(context.TODO())
440+
reconciledIPs, err := ipReconciler.ReconcileIPPools()
442441
Expect(err).NotTo(HaveOccurred())
443442
Expect(reconciledIPs).To(Equal([]net.IP{net.ParseIP(firstIPInRange)}))
444443
})
@@ -458,7 +457,7 @@ var _ = Describe("IPReconciler", func() {
458457
})
459458

460459
It("does delete *only the orphaned* the IP address", func() {
461-
reconciledIPs, err := ipReconciler.ReconcileIPPools(context.TODO())
460+
reconciledIPs, err := ipReconciler.ReconcileIPPools()
462461
Expect(err).NotTo(HaveOccurred())
463462
Expect(reconciledIPs).To(ConsistOf([]net.IP{net.ParseIP("192.168.14.2")}))
464463
})

pkg/reconciler/iploop.go

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,39 +21,29 @@ type ReconcileLooper struct {
2121
liveWhereaboutsPods map[string]podWrapper
2222
orphanedIPs []OrphanedIPReservations
2323
orphanedClusterWideIPs []whereaboutsv1alpha1.OverlappingRangeIPReservation
24-
requestTimeout int
2524
}
2625

2726
type OrphanedIPReservations struct {
2827
Pool storage.IPPool
2928
Allocations []types.IPReservation
3029
}
3130

32-
func NewReconcileLooperWithKubeconfig(ctx context.Context, kubeconfigPath string, timeout int) (*ReconcileLooper, error) {
33-
logging.Debugf("NewReconcileLooper - Kubernetes config file located at: %s", kubeconfigPath)
34-
k8sClient, err := kubernetes.NewClientViaKubeconfig(kubeconfigPath, time.Duration(timeout)*time.Second)
35-
if err != nil {
36-
return nil, logging.Errorf("failed to instantiate the Kubernetes client: %+v", err)
37-
}
38-
return NewReconcileLooperWithClient(ctx, k8sClient, timeout)
39-
}
40-
41-
func NewReconcileLooper(ctx context.Context, timeout int) (*ReconcileLooper, error) {
31+
func NewReconcileLooper() (*ReconcileLooper, error) {
4232
logging.Debugf("NewReconcileLooper - inferred connection data")
43-
k8sClient, err := kubernetes.NewClient(time.Duration(timeout) * time.Second)
33+
k8sClient, err := kubernetes.NewClient()
4434
if err != nil {
4535
return nil, logging.Errorf("failed to instantiate the Kubernetes client: %+v", err)
4636
}
47-
return NewReconcileLooperWithClient(ctx, k8sClient, timeout)
37+
return NewReconcileLooperWithClient(k8sClient)
4838
}
4939

50-
func NewReconcileLooperWithClient(ctx context.Context, k8sClient *kubernetes.Client, timeout int) (*ReconcileLooper, error) {
51-
ipPools, err := k8sClient.ListIPPools(ctx)
40+
func NewReconcileLooperWithClient(k8sClient *kubernetes.Client) (*ReconcileLooper, error) {
41+
ipPools, err := k8sClient.ListIPPools()
5242
if err != nil {
5343
return nil, logging.Errorf("failed to retrieve all IP pools: %v", err)
5444
}
5545

56-
pods, err := k8sClient.ListPods(ctx)
46+
pods, err := k8sClient.ListPods()
5747
if err != nil {
5848
return nil, err
5949
}
@@ -62,14 +52,13 @@ func NewReconcileLooperWithClient(ctx context.Context, k8sClient *kubernetes.Cli
6252
looper := &ReconcileLooper{
6353
k8sClient: *k8sClient,
6454
liveWhereaboutsPods: indexPods(pods, whereaboutsPodRefs),
65-
requestTimeout: timeout,
6655
}
6756

6857
if err := looper.findOrphanedIPsPerPool(ipPools); err != nil {
6958
return nil, err
7059
}
7160

72-
if err := looper.findClusterWideIPReservations(ctx); err != nil {
61+
if err := looper.findClusterWideIPReservations(); err != nil {
7362
return nil, err
7463
}
7564
return looper, nil
@@ -173,7 +162,7 @@ func composePodRef(pod v1.Pod) string {
173162
return fmt.Sprintf("%s/%s", pod.GetNamespace(), pod.GetName())
174163
}
175164

176-
func (rl ReconcileLooper) ReconcileIPPools(ctx context.Context) ([]net.IP, error) {
165+
func (rl ReconcileLooper) ReconcileIPPools() ([]net.IP, error) {
177166
findAllocationIndex := func(reservation types.IPReservation, reservations []types.IPReservation) int {
178167
for idx, r := range reservations {
179168
if r.PodRef == reservation.PodRef && r.IP.Equal(reservation.IP) {
@@ -206,21 +195,23 @@ func (rl ReconcileLooper) ReconcileIPPools(ctx context.Context) ([]net.IP, error
206195

207196
if len(cleanedUpIpsPerPool) != 0 {
208197
logging.Debugf("Going to update the reserve list to: %+v", currentIPReservations)
198+
199+
ctx, cancel := context.WithTimeout(context.Background(), storage.RequestTimeout)
209200
if err := orphanedIP.Pool.Update(ctx, currentIPReservations); err != nil {
201+
cancel()
210202
return nil, logging.Errorf("failed to update the reservation list: %v", err)
211203
}
204+
205+
cancel()
212206
totalCleanedUpIps = append(totalCleanedUpIps, cleanedUpIpsPerPool...)
213207
}
214208
}
215209

216210
return totalCleanedUpIps, nil
217211
}
218212

219-
func (rl *ReconcileLooper) findClusterWideIPReservations(ctx context.Context) error {
220-
ctxWithTimeout, cancel := context.WithTimeout(ctx, time.Duration(rl.requestTimeout)*time.Second)
221-
defer cancel()
222-
223-
clusterWideIPReservations, err := rl.k8sClient.ListOverlappingIPs(ctxWithTimeout)
213+
func (rl *ReconcileLooper) findClusterWideIPReservations() error {
214+
clusterWideIPReservations, err := rl.k8sClient.ListOverlappingIPs()
224215
if err != nil {
225216
return logging.Errorf("failed to list all OverLappingIPs: %v", err)
226217
}
@@ -243,14 +234,11 @@ func (rl *ReconcileLooper) findClusterWideIPReservations(ctx context.Context) er
243234
return nil
244235
}
245236

246-
func (rl ReconcileLooper) ReconcileOverlappingIPAddresses(ctx context.Context) error {
237+
func (rl ReconcileLooper) ReconcileOverlappingIPAddresses() error {
247238
var failedReconciledClusterWideIPs []string
248239

249-
ctxWithTimeout, cancel := context.WithTimeout(ctx, time.Duration(rl.requestTimeout)*time.Second)
250-
defer cancel()
251-
252240
for _, overlappingIPStruct := range rl.orphanedClusterWideIPs {
253-
if err := rl.k8sClient.DeleteOverlappingIP(ctxWithTimeout, &overlappingIPStruct); err != nil {
241+
if err := rl.k8sClient.DeleteOverlappingIP(&overlappingIPStruct); err != nil {
254242
logging.Errorf("failed to remove cluster wide IP: %s", overlappingIPStruct.GetName())
255243
failedReconciledClusterWideIPs = append(failedReconciledClusterWideIPs, overlappingIPStruct.GetName())
256244
continue

0 commit comments

Comments
 (0)