Skip to content

Commit 0bcb0d0

Browse files
authored
chore: migrate from exit/err channels to context (#900)
* chore: migrate from exit/err channels to context Signed-off-by: Evan Baker <[email protected]> * pass context instead of storing * rename controller packages * comment init
1 parent 6dfaa60 commit 0bcb0d0

24 files changed

+486
-253
lines changed

build/tools/go.sum

Lines changed: 256 additions & 0 deletions
Large diffs are not rendered by default.

cns/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const (
3636
// HTTPService describes the min API interface that every service should have.
3737
type HTTPService interface {
3838
common.ServiceAPI
39-
SendNCSnapShotPeriodically(int, chan bool)
39+
SendNCSnapShotPeriodically(context.Context, int)
4040
SetNodeOrchestrator(*SetOrchestratorTypeRequest)
4141
SyncNodeStatus(string, string, string, json.RawMessage) (int, string)
4242
GetPendingProgramIPConfigs() []IPConfigurationStatus

cns/common/service.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ package common
55

66
import (
77
"errors"
8-
"github.com/Azure/azure-container-networking/server/tls"
98

109
"github.com/Azure/azure-container-networking/cns/logger"
1110
acn "github.com/Azure/azure-container-networking/common"
11+
"github.com/Azure/azure-container-networking/server/tls"
1212
"github.com/Azure/azure-container-networking/store"
1313
)
1414

@@ -17,7 +17,7 @@ type Service struct {
1717
Name string
1818
Version string
1919
Options map[string]interface{}
20-
ErrChan chan error
20+
ErrChan chan<- error
2121
Store store.KeyValueStore
2222
ChannelMode string
2323
}
@@ -36,7 +36,7 @@ type ServiceConfig struct {
3636
Name string
3737
Version string
3838
Listener *acn.Listener
39-
ErrChan chan error
39+
ErrChan chan<- error
4040
Store store.KeyValueStore
4141
ChannelMode string
4242
TlsSettings tls.TlsSettings

cns/fakes/cnsfake.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,6 @@ func (ipm *IPStateManager) AddIPConfigs(ipconfigs []cns.IPConfigurationStatus) {
9696
ipm.PendingReleaseIPConfigState[ipconfigs[i].ID] = ipconfigs[i]
9797
}
9898
}
99-
100-
return
10199
}
102100

103101
func (ipm *IPStateManager) RemovePendingReleaseIPConfigs(ipconfigNames []string) {
@@ -107,8 +105,6 @@ func (ipm *IPStateManager) RemovePendingReleaseIPConfigs(ipconfigNames []string)
107105
for i := 0; i < len(ipconfigNames); i++ {
108106
delete(ipm.PendingReleaseIPConfigState, ipconfigNames[i])
109107
}
110-
111-
return
112108
}
113109

114110
func (ipm *IPStateManager) ReserveIPConfig() (cns.IPConfigurationStatus, error) {
@@ -176,6 +172,8 @@ func (ipm *IPStateManager) MarkIPAsPendingRelease(numberOfIPsToMark int) (map[st
176172
return pendingRelease, nil
177173
}
178174

175+
var _ cns.HTTPService = (*HTTPServiceFake)(nil)
176+
179177
type HTTPServiceFake struct {
180178
IPStateManager IPStateManager
181179
PoolMonitor cns.IPAMPoolMonitor
@@ -220,22 +218,16 @@ func (fake *HTTPServiceFake) SetNumberOfAllocatedIPs(desiredAllocatedIPCount int
220218
return nil
221219
}
222220

223-
func (fake *HTTPServiceFake) SendNCSnapShotPeriodically(int, chan bool) {
224-
225-
}
226-
227-
func (fake *HTTPServiceFake) SetNodeOrchestrator(*cns.SetOrchestratorTypeRequest) {
221+
func (fake *HTTPServiceFake) SendNCSnapShotPeriodically(context.Context, int) {}
228222

229-
}
223+
func (fake *HTTPServiceFake) SetNodeOrchestrator(*cns.SetOrchestratorTypeRequest) {}
230224

231225
func (fake *HTTPServiceFake) SyncNodeStatus(string, string, string, json.RawMessage) (int, string) {
232226
return 0, ""
233227
}
234228

235229
// SyncHostNCVersion will update HostVersion in containerstatus.
236-
func (fake *HTTPServiceFake) SyncHostNCVersion(context.Context, string, time.Duration) {
237-
return
238-
}
230+
func (fake *HTTPServiceFake) SyncHostNCVersion(context.Context, string, time.Duration) {}
239231

240232
func (fake *HTTPServiceFake) GetPendingProgramIPConfigs() []cns.IPConfigurationStatus {
241233
ipconfigs := []cns.IPConfigurationStatus{}

cns/fakes/requestcontrollerfake.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ import (
55
"net"
66

77
"github.com/Azure/azure-container-networking/cns"
8+
"github.com/Azure/azure-container-networking/cns/singletenantcontroller"
89
nnc "github.com/Azure/azure-container-networking/nodenetworkconfig/api/v1alpha"
910
"github.com/google/uuid"
1011
)
1112

13+
var _ singletenantcontroller.RequestController = (*RequestControllerFake)(nil)
14+
1215
type RequestControllerFake struct {
1316
fakecns *HTTPServiceFake
1417
cachedCRD nnc.NodeNetworkConfig
@@ -22,9 +25,11 @@ func NewRequestControllerFake(cnsService *HTTPServiceFake, scalar nnc.Scaler, su
2225
Spec: nnc.NodeNetworkConfigSpec{},
2326
Status: nnc.NodeNetworkConfigStatus{
2427
Scaler: scalar,
25-
NetworkContainers: []nnc.NetworkContainer{nnc.NetworkContainer{
26-
SubnetAddressSpace: subnetAddressSpace,
27-
}},
28+
NetworkContainers: []nnc.NetworkContainer{
29+
{
30+
SubnetAddressSpace: subnetAddressSpace,
31+
},
32+
},
2833
},
2934
},
3035
}
@@ -62,21 +67,20 @@ func (rc *RequestControllerFake) CarveIPConfigsAndAddToStatusAndCNS(numberOfIPCo
6267
return cnsIPConfigs
6368
}
6469

65-
func (rc *RequestControllerFake) InitRequestController() error {
70+
func (rc *RequestControllerFake) Init(context.Context) error {
6671
return nil
6772
}
6873

69-
func (rc *RequestControllerFake) StartRequestController(exitChan <-chan struct{}) error {
74+
func (rc *RequestControllerFake) Start(context.Context) error {
7075
return nil
7176
}
7277

7378
func (rc *RequestControllerFake) IsStarted() bool {
7479
return true
7580
}
7681

77-
func (rc *RequestControllerFake) UpdateCRDSpec(cntxt context.Context, desiredSpec nnc.NodeNetworkConfigSpec) error {
82+
func (rc *RequestControllerFake) UpdateCRDSpec(_ context.Context, desiredSpec nnc.NodeNetworkConfigSpec) error {
7883
rc.cachedCRD.Spec = desiredSpec
79-
8084
return nil
8185
}
8286

cns/ipampoolmonitor/ipampoolmonitor.go

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
"github.com/Azure/azure-container-networking/cns"
1010
"github.com/Azure/azure-container-networking/cns/logger"
11-
"github.com/Azure/azure-container-networking/cns/requestcontroller"
11+
"github.com/Azure/azure-container-networking/cns/singletenantcontroller"
1212
nnc "github.com/Azure/azure-container-networking/nodenetworkconfig/api/v1alpha"
1313
)
1414

@@ -17,21 +17,18 @@ const (
1717
)
1818

1919
type CNSIPAMPoolMonitor struct {
20-
pendingRelease bool
21-
20+
MaximumFreeIps int64
21+
MinimumFreeIps int64
2222
cachedNNC nnc.NodeNetworkConfig
23-
updatingIpsNotInUseCount int
23+
httpService cns.HTTPService
24+
mu sync.RWMutex
25+
pendingRelease bool
26+
rc singletenantcontroller.RequestController
2427
scalarUnits nnc.Scaler
25-
26-
httpService cns.HTTPService
27-
rc requestcontroller.RequestController
28-
MinimumFreeIps int64
29-
MaximumFreeIps int64
30-
31-
mu sync.RWMutex
28+
updatingIpsNotInUseCount int
3229
}
3330

34-
func NewCNSIPAMPoolMonitor(httpService cns.HTTPService, rc requestcontroller.RequestController) *CNSIPAMPoolMonitor {
31+
func NewCNSIPAMPoolMonitor(httpService cns.HTTPService, rc singletenantcontroller.RequestController) *CNSIPAMPoolMonitor {
3532
logger.Printf("NewCNSIPAMPoolMonitor: Create IPAM Pool Monitor")
3633
return &CNSIPAMPoolMonitor{
3734
pendingRelease: false,
@@ -40,16 +37,6 @@ func NewCNSIPAMPoolMonitor(httpService cns.HTTPService, rc requestcontroller.Req
4037
}
4138
}
4239

43-
func stopReconcile(ch <-chan struct{}) bool {
44-
select {
45-
case <-ch:
46-
return true
47-
default:
48-
}
49-
50-
return false
51-
}
52-
5340
func (pm *CNSIPAMPoolMonitor) Start(ctx context.Context, poolMonitorRefreshMilliseconds int) error {
5441
logger.Printf("[ipam-pool-monitor] Starting CNS IPAM Pool Monitor")
5542

@@ -60,15 +47,15 @@ func (pm *CNSIPAMPoolMonitor) Start(ctx context.Context, poolMonitorRefreshMilli
6047
case <-ctx.Done():
6148
return fmt.Errorf("[ipam-pool-monitor] CNS IPAM Pool Monitor received cancellation signal")
6249
case <-ticker.C:
63-
err := pm.Reconcile()
50+
err := pm.Reconcile(ctx)
6451
if err != nil {
6552
logger.Printf("[ipam-pool-monitor] Reconcile failed with err %v", err)
6653
}
6754
}
6855
}
6956
}
7057

71-
func (pm *CNSIPAMPoolMonitor) Reconcile() error {
58+
func (pm *CNSIPAMPoolMonitor) Reconcile(ctx context.Context) error {
7259
cnsPodIPConfigCount := len(pm.httpService.GetPodIPConfigState())
7360
pendingProgramCount := len(pm.httpService.GetPendingProgramIPConfigs()) // TODO: add pending program count to real cns
7461
allocatedPodIPCount := len(pm.httpService.GetAllocatedIPConfigs())
@@ -90,18 +77,18 @@ func (pm *CNSIPAMPoolMonitor) Reconcile() error {
9077
}
9178

9279
logger.Printf("[ipam-pool-monitor] Increasing pool size...%s ", msg)
93-
return pm.increasePoolSize()
80+
return pm.increasePoolSize(ctx)
9481

9582
// pod count is decreasing
9683
case freeIPConfigCount > pm.MaximumFreeIps:
9784
logger.Printf("[ipam-pool-monitor] Decreasing pool size...%s ", msg)
98-
return pm.decreasePoolSize(pendingReleaseIPCount)
85+
return pm.decreasePoolSize(ctx, pendingReleaseIPCount)
9986

10087
// CRD has reconciled CNS state, and target spec is now the same size as the state
10188
// free to remove the IP's from the CRD
10289
case pm.pendingRelease && int(pm.cachedNNC.Spec.RequestedIPCount) == cnsPodIPConfigCount:
10390
logger.Printf("[ipam-pool-monitor] Removing Pending Release IP's from CRD...%s ", msg)
104-
return pm.cleanPendingRelease()
91+
return pm.cleanPendingRelease(ctx)
10592

10693
// no pods scheduled
10794
case allocatedPodIPCount == 0:
@@ -112,7 +99,7 @@ func (pm *CNSIPAMPoolMonitor) Reconcile() error {
11299
return nil
113100
}
114101

115-
func (pm *CNSIPAMPoolMonitor) increasePoolSize() error {
102+
func (pm *CNSIPAMPoolMonitor) increasePoolSize(ctx context.Context) error {
116103
defer pm.mu.Unlock()
117104
pm.mu.Lock()
118105

@@ -143,7 +130,7 @@ func (pm *CNSIPAMPoolMonitor) increasePoolSize() error {
143130

144131
logger.Printf("[ipam-pool-monitor] Increasing pool size, Current Pool Size: %v, Updated Requested IP Count: %v, Pods with IP's:%v, ToBeDeleted Count: %v", len(pm.httpService.GetPodIPConfigState()), tempNNCSpec.RequestedIPCount, len(pm.httpService.GetAllocatedIPConfigs()), len(tempNNCSpec.IPsNotInUse))
145132

146-
err = pm.rc.UpdateCRDSpec(context.Background(), tempNNCSpec)
133+
err = pm.rc.UpdateCRDSpec(ctx, tempNNCSpec)
147134
if err != nil {
148135
// caller will retry to update the CRD again
149136
return err
@@ -155,7 +142,7 @@ func (pm *CNSIPAMPoolMonitor) increasePoolSize() error {
155142
return nil
156143
}
157144

158-
func (pm *CNSIPAMPoolMonitor) decreasePoolSize(existingPendingReleaseIPCount int) error {
145+
func (pm *CNSIPAMPoolMonitor) decreasePoolSize(ctx context.Context, existingPendingReleaseIPCount int) error {
159146
defer pm.mu.Unlock()
160147
pm.mu.Lock()
161148

@@ -215,7 +202,7 @@ func (pm *CNSIPAMPoolMonitor) decreasePoolSize(existingPendingReleaseIPCount int
215202
tempNNCSpec.RequestedIPCount -= int64(len(pendingIpAddresses))
216203
logger.Printf("[ipam-pool-monitor] Decreasing pool size, Current Pool Size: %v, Requested IP Count: %v, Pods with IP's: %v, ToBeDeleted Count: %v", len(pm.httpService.GetPodIPConfigState()), tempNNCSpec.RequestedIPCount, len(pm.httpService.GetAllocatedIPConfigs()), len(tempNNCSpec.IPsNotInUse))
217204

218-
err = pm.rc.UpdateCRDSpec(context.Background(), tempNNCSpec)
205+
err = pm.rc.UpdateCRDSpec(ctx, tempNNCSpec)
219206
if err != nil {
220207
// caller will retry to update the CRD again
221208
return err
@@ -236,7 +223,7 @@ func (pm *CNSIPAMPoolMonitor) decreasePoolSize(existingPendingReleaseIPCount int
236223

237224
// if cns pending ip release map is empty, request controller has already reconciled the CNS state,
238225
// so we can remove it from our cache and remove the IP's from the CRD
239-
func (pm *CNSIPAMPoolMonitor) cleanPendingRelease() error {
226+
func (pm *CNSIPAMPoolMonitor) cleanPendingRelease(ctx context.Context) error {
240227
defer pm.mu.Unlock()
241228
pm.mu.Lock()
242229

@@ -247,7 +234,7 @@ func (pm *CNSIPAMPoolMonitor) cleanPendingRelease() error {
247234
return err
248235
}
249236

250-
err = pm.rc.UpdateCRDSpec(context.Background(), tempNNCSpec)
237+
err = pm.rc.UpdateCRDSpec(ctx, tempNNCSpec)
251238
if err != nil {
252239
// caller will retry to update the CRD again
253240
return err

0 commit comments

Comments
 (0)