Skip to content

Commit f36875b

Browse files
committed
Use context to stop the upgradecheck goroutine
1 parent f474cb0 commit f36875b

File tree

3 files changed

+25
-25
lines changed

3 files changed

+25
-25
lines changed

cmd/postgres-operator/main.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,11 @@ func main() {
8585

8686
// Enable upgrade checking
8787
upgradeCheckingDisabled := strings.EqualFold(os.Getenv("CHECK_FOR_UPGRADES"), "false")
88-
done := make(chan bool, 1)
8988
if !upgradeCheckingDisabled {
9089
log.Info("upgrade checking enabled")
9190
// get the URL for the check for upgrades endpoint if set in the env
9291
upgradeCheckURL := os.Getenv("CHECK_FOR_UPGRADES_URL")
93-
go upgradecheck.CheckForUpgradesScheduler(done, versionString, upgradeCheckURL,
92+
go upgradecheck.CheckForUpgradesScheduler(ctx, versionString, upgradeCheckURL,
9493
mgr.GetClient(), mgr.GetConfig(), isOpenshift(ctx, mgr.GetConfig()),
9594
mgr.GetCache(),
9695
)
@@ -100,10 +99,6 @@ func main() {
10099

101100
assertNoError(mgr.Start(ctx))
102101
log.Info("signal received, exiting")
103-
if !upgradeCheckingDisabled {
104-
// Send true to channel to cancel ticker cleanly
105-
done <- true
106-
}
107102
}
108103

109104
// addControllersToManager adds all PostgreSQL Operator controllers to the provided controller

internal/upgradecheck/http.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,12 @@ func checkForUpgrades(ctx context.Context, versionString string, backoff wait.Ba
141141
}
142142

143143
// CheckForUpgradesScheduler invokes the check func when the operator starts
144-
// and then on the given period schedule
145-
func CheckForUpgradesScheduler(channel chan bool,
144+
// and then on the given period schedule. It stops when the context is cancelled.
145+
func CheckForUpgradesScheduler(ctx context.Context,
146146
versionString, url string, crclient crclient.Client,
147147
cfg *rest.Config, isOpenShift bool,
148148
cacheClient CacheWithWait,
149149
) {
150-
ctx := context.Background()
151150
log := logging.FromContext(ctx)
152151
defer func() {
153152
if err := recover(); err != nil {
@@ -194,7 +193,7 @@ func CheckForUpgradesScheduler(channel chan bool,
194193
} else {
195194
log.V(1).Info(info)
196195
}
197-
case <-channel:
196+
case <-ctx.Done():
198197
ticker.Stop()
199198
return
200199
}

internal/upgradecheck/http_test.go

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -171,22 +171,24 @@ func TestCheckForUpgradesScheduler(t *testing.T) {
171171
const testUpgradeCheckURL = "http://localhost:8080"
172172

173173
t.Run("panic from checkForUpgrades doesn't bubble up", func(t *testing.T) {
174-
done := make(chan bool, 1)
174+
ctx, cancel := context.WithCancel(context.Background())
175+
defer cancel()
176+
175177
// capture logs
176178
var calls []string
177-
logging.SetLogFunc(1, func(input genericr.Entry) {
179+
ctx = logging.NewContext(ctx, genericr.New(func(input genericr.Entry) {
178180
calls = append(calls, input.Message)
179-
})
181+
}))
180182

181183
// A panicking call
182184
funcFoo = func() (*http.Response, error) {
183185
panic(fmt.Errorf("oh no!"))
184186
}
185187

186-
go CheckForUpgradesScheduler(done, "4.7.3", testUpgradeCheckURL, fakeClient, cfg, false,
188+
go CheckForUpgradesScheduler(ctx, "4.7.3", testUpgradeCheckURL, fakeClient, cfg, false,
187189
&MockCacheClient{works: true})
188190
time.Sleep(1 * time.Second)
189-
done <- true
191+
cancel()
190192

191193
// Sleeping leads to some non-deterministic results, but we expect at least 1 execution
192194
// plus one log for the failure to apply the configmap
@@ -195,32 +197,36 @@ func TestCheckForUpgradesScheduler(t *testing.T) {
195197
})
196198

197199
t.Run("cache sync fail leads to log and exit", func(t *testing.T) {
198-
done := make(chan bool, 1)
200+
ctx, cancel := context.WithCancel(context.Background())
201+
defer cancel()
202+
199203
// capture logs
200204
var calls []string
201-
logging.SetLogFunc(1, func(input genericr.Entry) {
205+
ctx = logging.NewContext(ctx, genericr.New(func(input genericr.Entry) {
202206
calls = append(calls, input.Message)
203-
})
207+
}))
204208

205209
// Set loop time to 1s and sleep for 2s before sending the done signal -- though the cache sync
206210
// failure will exit the func before the sleep ends
207211
upgradeCheckPeriod = 1 * time.Second
208-
go CheckForUpgradesScheduler(done, "4.7.3", testUpgradeCheckURL, fakeClient, cfg, false,
212+
go CheckForUpgradesScheduler(ctx, "4.7.3", testUpgradeCheckURL, fakeClient, cfg, false,
209213
&MockCacheClient{works: false})
210214
time.Sleep(2 * time.Second)
211-
done <- true
215+
cancel()
212216

213217
assert.Assert(t, len(calls) == 1)
214218
assert.Equal(t, calls[0], `unable to sync cache for upgrade check`)
215219
})
216220

217221
t.Run("successful log each loop, ticker works", func(t *testing.T) {
218-
done := make(chan bool, 1)
222+
ctx, cancel := context.WithCancel(context.Background())
223+
defer cancel()
224+
219225
// capture logs
220226
var calls []string
221-
logging.SetLogFunc(1, func(input genericr.Entry) {
227+
ctx = logging.NewContext(ctx, genericr.New(func(input genericr.Entry) {
222228
calls = append(calls, input.Message)
223-
})
229+
}))
224230

225231
// A successful call
226232
funcFoo = func() (*http.Response, error) {
@@ -233,10 +239,10 @@ func TestCheckForUpgradesScheduler(t *testing.T) {
233239

234240
// Set loop time to 1s and sleep for 2s before sending the done signal
235241
upgradeCheckPeriod = 1 * time.Second
236-
go CheckForUpgradesScheduler(done, "4.7.3", testUpgradeCheckURL, fakeClient, cfg, false,
242+
go CheckForUpgradesScheduler(ctx, "4.7.3", testUpgradeCheckURL, fakeClient, cfg, false,
237243
&MockCacheClient{works: true})
238244
time.Sleep(2 * time.Second)
239-
done <- true
245+
cancel()
240246

241247
// Sleeping leads to some non-deterministic results, but we expect at least 2 executions
242248
// plus one log for the failure to apply the configmap

0 commit comments

Comments
 (0)