1
1
package ipam
2
2
3
3
import (
4
+ "context"
4
5
"net"
5
6
6
7
whereaboutsallocate "github.com/k8snetworkplumbingwg/whereabouts/pkg/allocate"
7
8
whereaboutslogging "github.com/k8snetworkplumbingwg/whereabouts/pkg/logging"
8
9
whereaboutstypes "github.com/k8snetworkplumbingwg/whereabouts/pkg/types"
10
+ "sigs.k8s.io/controller-runtime/pkg/log"
9
11
10
12
"k8s.io/apimachinery/pkg/types"
11
13
)
@@ -16,20 +18,21 @@ type ipamAction = func(
16
18
) (net.IPNet , []whereaboutstypes.IPReservation , error )
17
19
18
20
// makeAcquireAction creates a callback which changes IPPool state to include a new IP reservation.
19
- func makeAcquireAction (vmName types.NamespacedName ) ipamAction {
21
+ func makeAcquireAction (ctx context. Context , vmName types.NamespacedName ) ipamAction {
20
22
return func (ipRange RangeConfiguration , reservation []whereaboutstypes.IPReservation ) (net.IPNet , []whereaboutstypes.IPReservation , error ) {
21
- return doAcquire (ipRange , reservation , vmName )
23
+ return doAcquire (ctx , ipRange , reservation , vmName )
22
24
}
23
25
}
24
26
25
27
// makeReleaseAction creates a callback which changes IPPool state to deallocate an IP reservation.
26
- func makeReleaseAction (vmName types.NamespacedName ) ipamAction {
28
+ func makeReleaseAction (ctx context. Context , vmName types.NamespacedName ) ipamAction {
27
29
return func (ipRange RangeConfiguration , reservation []whereaboutstypes.IPReservation ) (net.IPNet , []whereaboutstypes.IPReservation , error ) {
28
- return doRelease (ipRange , reservation , vmName )
30
+ return doRelease (ctx , ipRange , reservation , vmName )
29
31
}
30
32
}
31
33
32
34
func doAcquire (
35
+ _ context.Context ,
33
36
ipRange RangeConfiguration ,
34
37
reservation []whereaboutstypes.IPReservation ,
35
38
vmName types.NamespacedName ,
@@ -57,19 +60,27 @@ func doAcquire(
57
60
}
58
61
59
62
func doRelease (
63
+ ctx context.Context ,
60
64
ipRange RangeConfiguration ,
61
65
reservation []whereaboutstypes.IPReservation ,
62
66
vmName types.NamespacedName ,
63
67
) (net.IPNet , []whereaboutstypes.IPReservation , error ) {
64
68
// reduce whereabouts logging
65
69
whereaboutslogging .SetLogLevel ("error" )
66
70
71
+ log := log .FromContext (ctx )
72
+
67
73
_ , ipnet , _ := net .ParseCIDR (ipRange .Range )
68
74
69
75
// try to release IP for given VM
70
76
newReservation , ip , err := whereaboutsallocate .IterateForDeallocation (reservation , vmName .String (), getMatchingIPReservationIndex )
71
77
if err != nil {
72
- return net.IPNet {}, nil , err
78
+ // The only reason to get an error here is if we are trying
79
+ // to deallocate the same IP twice.
80
+ log .Info ("Failed to deallocate IP" , "error" , err )
81
+
82
+ // Ignore the error.
83
+ return net.IPNet {IP : ip , Mask : ipnet .Mask }, newReservation , nil
73
84
}
74
85
75
86
return net.IPNet {IP : ip , Mask : ipnet .Mask }, newReservation , nil
0 commit comments