Skip to content

Commit 26a80cf

Browse files
committed
Tidy up usage of channels for signaling
This started as a small effort to simplify the usage of "ready" channel in nfd-master. It extended into a wider simplification/unification of the channel usage.
1 parent 275e625 commit 26a80cf

File tree

6 files changed

+18
-25
lines changed

6 files changed

+18
-25
lines changed

pkg/nfd-gc/nfd-gc_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func TestNRTGC(t *testing.T) {
4040
Convey("When theres is old NRT ", t, func() {
4141
gc := newMockGC(nil, []string{"node1"})
4242

43-
errChan := make(chan error, 1)
43+
errChan := make(chan error)
4444
go func() { errChan <- gc.Run() }()
4545

4646
So(waitForNRT(gc.topoClient), ShouldBeTrue)
@@ -51,7 +51,7 @@ func TestNRTGC(t *testing.T) {
5151
Convey("When theres is one old NRT and one up to date", t, func() {
5252
gc := newMockGC([]string{"node1"}, []string{"node1", "node2"})
5353

54-
errChan := make(chan error, 1)
54+
errChan := make(chan error)
5555
go func() { errChan <- gc.Run() }()
5656

5757
So(waitForNRT(gc.topoClient, "node1"), ShouldBeTrue)
@@ -62,7 +62,7 @@ func TestNRTGC(t *testing.T) {
6262
Convey("Should react to delete event", t, func() {
6363
gc := newMockGC([]string{"node1", "node2"}, []string{"node1", "node2"})
6464

65-
errChan := make(chan error, 1)
65+
errChan := make(chan error)
6666
go func() { errChan <- gc.Run() }()
6767

6868
err := gc.k8sClient.CoreV1().Nodes().Delete(context.TODO(), "node1", metav1.DeleteOptions{})
@@ -81,7 +81,7 @@ func TestNRTGC(t *testing.T) {
8181
},
8282
}
8383

84-
errChan := make(chan error, 1)
84+
errChan := make(chan error)
8585
go func() { errChan <- gc.Run() }()
8686

8787
_, err := gc.topoClient.TopologyV1alpha2().NodeResourceTopologies().Create(context.TODO(), &nrt, metav1.CreateOptions{})
@@ -98,7 +98,7 @@ func newMockGC(nodes, nrts []string) *mockGC {
9898
factory: informers.NewSharedInformerFactory(k8sClient, 5*time.Minute),
9999
nfdClient: fakenfdclientset.NewSimpleClientset(),
100100
topoClient: faketopologyv1alpha2.NewSimpleClientset(createFakeNRTs(nrts...)...),
101-
stopChan: make(chan struct{}, 1),
101+
stopChan: make(chan struct{}),
102102
args: &Args{
103103
GCPeriod: 10 * time.Minute,
104104
},

pkg/nfd-master/nfd-api-controller.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func init() {
5555

5656
func newNfdController(config *restclient.Config, nfdApiControllerOptions nfdApiControllerOptions) (*nfdController, error) {
5757
c := &nfdController{
58-
stopChan: make(chan struct{}, 1),
58+
stopChan: make(chan struct{}),
5959
updateAllNodesChan: make(chan struct{}, 1),
6060
updateOneNodeChan: make(chan string),
6161
}

pkg/nfd-master/nfd-master-internal_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func newTestNode() *corev1.Node {
6868

6969
func newFakeNfdAPIController(client *fakenfdclient.Clientset) *nfdController {
7070
c := &nfdController{
71-
stopChan: make(chan struct{}, 1),
71+
stopChan: make(chan struct{}),
7272
updateAllNodesChan: make(chan struct{}, 1),
7373
updateOneNodeChan: make(chan string),
7474
}

pkg/nfd-master/nfd-master.go

+7-13
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ type nfdMaster struct {
149149
server *grpc.Server
150150
healthServer *grpc.Server
151151
stop chan struct{}
152-
ready chan bool
152+
ready chan struct{}
153153
k8sClient k8sclient.Interface
154154
nodeUpdaterPool *nodeUpdaterPool
155155
deniedNs
@@ -161,8 +161,8 @@ func NewNfdMaster(args *Args) (NfdMaster, error) {
161161
nfd := &nfdMaster{args: *args,
162162
nodeName: utils.NodeName(),
163163
namespace: utils.GetKubernetesNamespace(),
164-
ready: make(chan bool, 1),
165-
stop: make(chan struct{}, 1),
164+
ready: make(chan struct{}),
165+
stop: make(chan struct{}),
166166
}
167167

168168
if args.Instance != "" {
@@ -272,7 +272,7 @@ func (m *nfdMaster) Run() error {
272272
}
273273

274274
// Run gRPC server
275-
grpcErr := make(chan error, 1)
275+
grpcErr := make(chan error)
276276
// If the NodeFeature API is enabled, don'tregister the labeler API
277277
// server. Otherwise, register the labeler server.
278278
if !features.NFDFeatureGate.Enabled(features.NodeFeatureAPI) {
@@ -296,7 +296,6 @@ func (m *nfdMaster) Run() error {
296296
}
297297

298298
// Notify that we're ready to accept connections
299-
m.ready <- true
300299
close(m.ready)
301300

302301
// NFD-Master main event loop
@@ -397,7 +396,7 @@ func (m *nfdMaster) runGrpcServer(errChan chan<- error) {
397396
klog.InfoS("gRPC server serving", "port", m.args.Port)
398397

399398
// Run gRPC server
400-
grpcErr := make(chan error, 1)
399+
grpcErr := make(chan error)
401400
go func() {
402401
defer lis.Close()
403402
grpcErr <- m.server.Serve(lis)
@@ -475,15 +474,10 @@ func (m *nfdMaster) Stop() {
475474
// Wait until NfdMaster is able able to accept connections.
476475
func (m *nfdMaster) WaitForReady(timeout time.Duration) bool {
477476
select {
478-
case ready, ok := <-m.ready:
479-
// Ready if the flag is true or the channel has been closed
480-
if ready || !ok {
481-
return true
482-
}
477+
case <-m.ready:
478+
return true
483479
case <-time.After(timeout):
484-
return false
485480
}
486-
// We should never end-up here
487481
return false
488482
}
489483

pkg/nfd-topology-updater/nfd-topology-updater.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func NewTopologyUpdater(args Args, resourcemonitorArgs resourcemonitor.Args) (Nf
111111
nfd := &nfdTopologyUpdater{
112112
args: args,
113113
resourcemonitorArgs: resourcemonitorArgs,
114-
stop: make(chan struct{}, 1),
114+
stop: make(chan struct{}),
115115
nodeName: utils.NodeName(),
116116
eventSource: eventSource,
117117
config: &NFDConfig{},
@@ -207,7 +207,6 @@ func (w *nfdTopologyUpdater) Run() error {
207207
// CAUTION: these resources are expected to change rarely - if ever.
208208
// So we are intentionally do this once during the process lifecycle.
209209
// TODO: Obtain node resources dynamically from the podresource API
210-
// zonesChannel := make(chan v1alpha1.ZoneList)
211210
var zones v1alpha2.ZoneList
212211

213212
excludeList := resourcemonitor.NewExcludeResourceList(w.config.ExcludeList, w.nodeName)
@@ -216,7 +215,7 @@ func (w *nfdTopologyUpdater) Run() error {
216215
return fmt.Errorf("failed to obtain node resource information: %w", err)
217216
}
218217

219-
grpcErr := make(chan error, 1)
218+
grpcErr := make(chan error)
220219

221220
// Start gRPC server for liveness probe (at this point we're "live")
222221
if w.args.GrpcHealthPort != 0 {

pkg/nfd-worker/nfd-worker.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func NewNfdWorker(args *Args) (NfdWorker, error) {
146146
args: *args,
147147
config: &NFDConfig{},
148148
kubernetesNamespace: utils.GetKubernetesNamespace(),
149-
stop: make(chan struct{}, 1),
149+
stop: make(chan struct{}),
150150
}
151151

152152
// Check TLS related args
@@ -290,7 +290,7 @@ func (w *nfdWorker) Run() error {
290290
return nil
291291
}
292292

293-
grpcErr := make(chan error, 1)
293+
grpcErr := make(chan error)
294294

295295
// Start gRPC server for liveness probe (at this point we're "live")
296296
if w.args.GrpcHealthPort != 0 {

0 commit comments

Comments
 (0)