Skip to content

Commit b921952

Browse files
authored
Merge pull request #1846 from atlassian-forks/master
✨ [WIP] Add BaseContext to manager Options for use with Runnables
2 parents c46b410 + 7f57edc commit b921952

File tree

3 files changed

+38
-18
lines changed

3 files changed

+38
-18
lines changed

pkg/manager/manager.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,11 @@ type Options struct {
239239
// use the cache for reads and the client for writes.
240240
NewClient cluster.NewClientFunc
241241

242+
// BaseContext is the function that provides Context values to Runnables
243+
// managed by the Manager. If a BaseContext function isn't provided, Runnables
244+
// will receive a new Background Context instead.
245+
BaseContext BaseContextFunc
246+
242247
// ClientDisableCacheFor tells the client that, if any cache is used, to bypass it
243248
// for the given objects.
244249
ClientDisableCacheFor []client.Object
@@ -278,6 +283,10 @@ type Options struct {
278283
newHealthProbeListener func(addr string) (net.Listener, error)
279284
}
280285

286+
// BaseContextFunc is a function used to provide a base Context to Runnables
287+
// managed by a Manager.
288+
type BaseContextFunc func() context.Context
289+
281290
// Runnable allows a component to be started.
282291
// It's very important that Start blocks until
283292
// it's done running.
@@ -377,7 +386,7 @@ func New(config *rest.Config, options Options) (Manager, error) {
377386
}
378387

379388
errChan := make(chan error)
380-
runnables := newRunnables(errChan)
389+
runnables := newRunnables(options.BaseContext, errChan)
381390

382391
return &controllerManager{
383392
stopProcedureEngaged: pointer.Int64(0),
@@ -529,6 +538,12 @@ func defaultHealthProbeListener(addr string) (net.Listener, error) {
529538
return ln, nil
530539
}
531540

541+
// defaultBaseContext is used as the BaseContext value in Options if one
542+
// has not already been set.
543+
func defaultBaseContext() context.Context {
544+
return context.Background()
545+
}
546+
532547
// setOptionsDefaults set default values for Options fields.
533548
func setOptionsDefaults(options Options) Options {
534549
// Allow newResourceLock to be mocked
@@ -592,5 +607,9 @@ func setOptionsDefaults(options Options) Options {
592607
options.Logger = log.Log
593608
}
594609

610+
if options.BaseContext == nil {
611+
options.BaseContext = defaultBaseContext
612+
}
613+
595614
return options
596615
}

pkg/manager/runnable_group.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ type runnables struct {
3535
}
3636

3737
// newRunnables creates a new runnables object.
38-
func newRunnables(errChan chan error) *runnables {
38+
func newRunnables(baseContext BaseContextFunc, errChan chan error) *runnables {
3939
return &runnables{
40-
Webhooks: newRunnableGroup(errChan),
41-
Caches: newRunnableGroup(errChan),
42-
LeaderElection: newRunnableGroup(errChan),
43-
Others: newRunnableGroup(errChan),
40+
Webhooks: newRunnableGroup(baseContext, errChan),
41+
Caches: newRunnableGroup(baseContext, errChan),
42+
LeaderElection: newRunnableGroup(baseContext, errChan),
43+
Others: newRunnableGroup(baseContext, errChan),
4444
}
4545
}
4646

@@ -100,14 +100,15 @@ type runnableGroup struct {
100100
wg *sync.WaitGroup
101101
}
102102

103-
func newRunnableGroup(errChan chan error) *runnableGroup {
103+
func newRunnableGroup(baseContext BaseContextFunc, errChan chan error) *runnableGroup {
104104
r := &runnableGroup{
105105
startReadyCh: make(chan *readyRunnable),
106106
errChan: errChan,
107107
ch: make(chan *readyRunnable),
108108
wg: new(sync.WaitGroup),
109109
}
110-
r.ctx, r.cancel = context.WithCancel(context.Background())
110+
111+
r.ctx, r.cancel = context.WithCancel(baseContext())
111112
return r
112113
}
113114

pkg/manager/runnable_group_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@ var _ = Describe("runnables", func() {
1818
errCh := make(chan error)
1919

2020
It("should be able to create a new runnables object", func() {
21-
Expect(newRunnables(errCh)).ToNot(BeNil())
21+
Expect(newRunnables(defaultBaseContext, errCh)).ToNot(BeNil())
2222
})
2323

2424
It("should add caches to the appropriate group", func() {
2525
cache := &cacheProvider{cache: &informertest.FakeInformers{Error: fmt.Errorf("expected error")}}
26-
r := newRunnables(errCh)
26+
r := newRunnables(defaultBaseContext, errCh)
2727
Expect(r.Add(cache)).To(Succeed())
2828
Expect(r.Caches.startQueue).To(HaveLen(1))
2929
})
3030

3131
It("should add webhooks to the appropriate group", func() {
3232
webhook := &webhook.Server{}
33-
r := newRunnables(errCh)
33+
r := newRunnables(defaultBaseContext, errCh)
3434
Expect(r.Add(webhook)).To(Succeed())
3535
Expect(r.Webhooks.startQueue).To(HaveLen(1))
3636
})
@@ -41,7 +41,7 @@ var _ = Describe("runnables", func() {
4141
return err
4242
})
4343

44-
r := newRunnables(errCh)
44+
r := newRunnables(defaultBaseContext, errCh)
4545
Expect(r.Add(runnable)).To(Succeed())
4646
Expect(r.LeaderElection.startQueue).To(HaveLen(1))
4747
})
@@ -53,7 +53,7 @@ var _ = Describe("runnableGroup", func() {
5353
It("should be able to add new runnables before it starts", func() {
5454
ctx, cancel := context.WithCancel(context.Background())
5555
defer cancel()
56-
rg := newRunnableGroup(errCh)
56+
rg := newRunnableGroup(defaultBaseContext, errCh)
5757
Expect(rg.Add(RunnableFunc(func(c context.Context) error {
5858
<-ctx.Done()
5959
return nil
@@ -65,7 +65,7 @@ var _ = Describe("runnableGroup", func() {
6565
It("should be able to add new runnables before and after start", func() {
6666
ctx, cancel := context.WithCancel(context.Background())
6767
defer cancel()
68-
rg := newRunnableGroup(errCh)
68+
rg := newRunnableGroup(defaultBaseContext, errCh)
6969
Expect(rg.Add(RunnableFunc(func(c context.Context) error {
7070
<-ctx.Done()
7171
return nil
@@ -81,7 +81,7 @@ var _ = Describe("runnableGroup", func() {
8181
It("should be able to add new runnables before and after start concurrently", func() {
8282
ctx, cancel := context.WithCancel(context.Background())
8383
defer cancel()
84-
rg := newRunnableGroup(errCh)
84+
rg := newRunnableGroup(defaultBaseContext, errCh)
8585

8686
go func() {
8787
defer GinkgoRecover()
@@ -106,7 +106,7 @@ var _ = Describe("runnableGroup", func() {
106106
ctx, cancel := context.WithCancel(context.Background())
107107

108108
exited := pointer.Int64(0)
109-
rg := newRunnableGroup(errCh)
109+
rg := newRunnableGroup(defaultBaseContext, errCh)
110110
for i := 0; i < 10; i++ {
111111
Expect(rg.Add(RunnableFunc(func(c context.Context) error {
112112
defer atomic.AddInt64(exited, 1)
@@ -131,7 +131,7 @@ var _ = Describe("runnableGroup", func() {
131131
It("should be able to wait for all runnables to be ready at different intervals", func() {
132132
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
133133
defer cancel()
134-
rg := newRunnableGroup(errCh)
134+
rg := newRunnableGroup(defaultBaseContext, errCh)
135135

136136
go func() {
137137
defer GinkgoRecover()
@@ -157,7 +157,7 @@ var _ = Describe("runnableGroup", func() {
157157
It("should not turn ready if some readiness check fail", func() {
158158
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
159159
defer cancel()
160-
rg := newRunnableGroup(errCh)
160+
rg := newRunnableGroup(defaultBaseContext, errCh)
161161

162162
go func() {
163163
defer GinkgoRecover()

0 commit comments

Comments
 (0)