Skip to content

Commit b450b79

Browse files
authored
Merge pull request #547 from rohitagarwal003/context
Don't store Context inside a struct, pass it explicitly to each function that needs it.
2 parents f155aaf + 9a50bc7 commit b450b79

File tree

1 file changed

+12
-17
lines changed

1 file changed

+12
-17
lines changed

pkg/kstatus/polling/engine/engine.go

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ func (s *PollerEngine) Poll(ctx context.Context, identifiers object.ObjMetadataS
6161
}
6262

6363
runner := &statusPollerRunner{
64-
ctx: ctx,
6564
clusterReader: clusterReader,
6665
statusReaders: s.StatusReaders,
6766
defaultStatusReader: s.DefaultStatusReader,
@@ -70,7 +69,7 @@ func (s *PollerEngine) Poll(ctx context.Context, identifiers object.ObjMetadataS
7069
eventChannel: eventChannel,
7170
pollingInterval: options.PollInterval,
7271
}
73-
runner.Run()
72+
runner.Run(ctx)
7473
}()
7574

7675
return eventChannel
@@ -123,10 +122,6 @@ type Options struct {
123122
// with LIST calls before each polling loop, or the normal ClusterReader that just forwards each call
124123
// to the client.Reader from controller-runtime.
125124
type statusPollerRunner struct {
126-
// ctx is the context for the runner. It will be used by the caller of Poll to cancel
127-
// polling resources.
128-
ctx context.Context
129-
130125
// clusterReader is the interface for fetching and listing resources from the cluster. It can be implemented
131126
// to make call directly to the cluster or use caching to reduce the number of calls to the cluster.
132127
clusterReader ClusterReader
@@ -159,26 +154,26 @@ type statusPollerRunner struct {
159154
}
160155

161156
// Run starts the polling loop of the statusReaders.
162-
func (r *statusPollerRunner) Run() {
157+
func (r *statusPollerRunner) Run(ctx context.Context) {
163158
// Sets up ticker that will trigger the regular polling loop at a regular interval.
164159
ticker := time.NewTicker(r.pollingInterval)
165160
defer func() {
166161
ticker.Stop()
167162
}()
168163

169-
err := r.syncAndPoll()
164+
err := r.syncAndPoll(ctx)
170165
if err != nil {
171166
r.handleSyncAndPollErr(err)
172167
return
173168
}
174169

175170
for {
176171
select {
177-
case <-r.ctx.Done():
172+
case <-ctx.Done():
178173
return
179174
case <-ticker.C:
180175
// First sync and then compute status for all resources.
181-
err := r.syncAndPoll()
176+
err := r.syncAndPoll(ctx)
182177
if err != nil {
183178
r.handleSyncAndPollErr(err)
184179
return
@@ -202,34 +197,34 @@ func (r *statusPollerRunner) handleSyncAndPollErr(err error) {
202197
}
203198
}
204199

205-
func (r *statusPollerRunner) syncAndPoll() error {
200+
func (r *statusPollerRunner) syncAndPoll(ctx context.Context) error {
206201
// First trigger a sync of the ClusterReader. This may or may not actually
207202
// result in calls to the cluster, depending on the implementation.
208203
// If this call fails, there is no clean way to recover, so we just return an ErrorEvent
209204
// and shut down.
210-
err := r.clusterReader.Sync(r.ctx)
205+
err := r.clusterReader.Sync(ctx)
211206
if err != nil {
212207
return err
213208
}
214209
// Poll all resources and compute status. If the polling of resources has completed (based
215210
// on information from the StatusAggregator and the value of pollUntilCancelled), we send
216211
// a CompletedEvent and return.
217-
return r.pollStatusForAllResources()
212+
return r.pollStatusForAllResources(ctx)
218213
}
219214

220215
// pollStatusForAllResources iterates over all the resources in the set and delegates
221216
// to the appropriate engine to compute the status.
222-
func (r *statusPollerRunner) pollStatusForAllResources() error {
217+
func (r *statusPollerRunner) pollStatusForAllResources(ctx context.Context) error {
223218
for _, id := range r.identifiers {
224219
// Check if the context has been cancelled on every iteration.
225220
select {
226-
case <-r.ctx.Done():
227-
return r.ctx.Err()
221+
case <-ctx.Done():
222+
return ctx.Err()
228223
default:
229224
}
230225
gk := id.GroupKind
231226
statusReader := r.statusReaderForGroupKind(gk)
232-
resourceStatus, err := statusReader.ReadStatus(r.ctx, r.clusterReader, id)
227+
resourceStatus, err := statusReader.ReadStatus(ctx, r.clusterReader, id)
233228
if err != nil {
234229
return err
235230
}

0 commit comments

Comments
 (0)