Skip to content

Commit 1baca25

Browse files
committed
Handle upgrade-check panics in a single place
1 parent 1522c57 commit 1baca25

File tree

2 files changed

+25
-50
lines changed

2 files changed

+25
-50
lines changed

internal/upgradecheck/http.go

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,6 @@ func checkForUpgrades(ctx context.Context, url, versionString string, backoff wa
8383
isOpenShift bool) (message string, header string, err error) {
8484
var headerPayloadStruct *clientUpgradeData
8585

86-
// Guard against panics within the checkForUpgrades function to allow the
87-
// checkForUpgradesScheduler to reschedule a check
88-
defer func() {
89-
if panicErr := recover(); panicErr != nil {
90-
err = fmt.Errorf("%s", panicErr)
91-
}
92-
}()
93-
9486
// Prep request
9587
req, err := http.NewRequest("GET", url, nil)
9688
if err == nil {
@@ -151,13 +143,6 @@ func CheckForUpgradesScheduler(ctx context.Context,
151143
cacheClient CacheWithWait,
152144
) {
153145
log := logging.FromContext(ctx)
154-
defer func() {
155-
if err := recover(); err != nil {
156-
log.V(1).Info("encountered panic in upgrade check",
157-
"response", err,
158-
)
159-
}
160-
}()
161146

162147
if url == "" {
163148
url = upgradeCheckURL
@@ -174,30 +159,38 @@ func CheckForUpgradesScheduler(ctx context.Context,
174159
return
175160
}
176161

177-
info, header, err := checkForUpgrades(ctx, url, versionString, backoff,
178-
crclient, cfg, isOpenShift)
179-
if err != nil {
180-
log.V(1).Info("could not complete upgrade check",
181-
"response", err.Error())
182-
} else {
183-
log.Info(info, clientHeader, header)
184-
}
162+
check(ctx, versionString, url, crclient, cfg, isOpenShift)
185163

186164
ticker := time.NewTicker(upgradeCheckPeriod)
187165
for {
188166
select {
189167
case <-ticker.C:
190-
info, header, err = checkForUpgrades(ctx, url, versionString, backoff,
191-
crclient, cfg, isOpenShift)
192-
if err != nil {
193-
log.V(1).Info("could not complete scheduled upgrade check",
194-
"response", err.Error())
195-
} else {
196-
log.Info(info, clientHeader, header)
197-
}
168+
check(ctx, versionString, url, crclient, cfg, isOpenShift)
198169
case <-ctx.Done():
199170
ticker.Stop()
200171
return
201172
}
202173
}
203174
}
175+
176+
func check(ctx context.Context,
177+
versionString, url string, crclient crclient.Client,
178+
cfg *rest.Config, isOpenShift bool,
179+
) {
180+
log := logging.FromContext(ctx)
181+
182+
defer func() {
183+
if v := recover(); v != nil {
184+
log.V(1).Info("encountered panic in upgrade check", "response", v)
185+
}
186+
}()
187+
188+
info, header, err := checkForUpgrades(ctx,
189+
url, versionString, backoff, crclient, cfg, isOpenShift)
190+
191+
if err != nil {
192+
log.V(1).Info("could not complete upgrade check", "response", err.Error())
193+
} else {
194+
log.Info(info, clientHeader, header)
195+
}
196+
}

internal/upgradecheck/http_test.go

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -113,24 +113,6 @@ func TestCheckForUpgrades(t *testing.T) {
113113
checkData(t, header)
114114
})
115115

116-
t.Run("recovers from panic", func(t *testing.T) {
117-
var counter int
118-
// A panicking call
119-
funcFoo = func() (*http.Response, error) {
120-
counter++
121-
panic(fmt.Errorf("oh no!"))
122-
}
123-
124-
res, header, err := checkForUpgrades(ctx, "", "4.7.3", backoff,
125-
fakeClient, cfg, false)
126-
// One call because of panic
127-
assert.Equal(t, counter, 1)
128-
assert.Equal(t, res, "")
129-
assert.Equal(t, err.Error(), `oh no!`)
130-
// no http response returned, so don't perform full check
131-
assert.Assert(t, header == "")
132-
})
133-
134116
t.Run("total failure, bad StatusCode", func(t *testing.T) {
135117
var counter int
136118
// A call returning bad StatusCode
@@ -213,7 +195,7 @@ func TestCheckForUpgradesScheduler(t *testing.T) {
213195
// Sleeping leads to some non-deterministic results, but we expect at least 1 execution
214196
// plus one log for the failure to apply the configmap
215197
assert.Assert(t, len(calls) >= 2)
216-
assert.Assert(t, cmp.Contains(calls[1], `could not complete upgrade check`))
198+
assert.Assert(t, cmp.Contains(calls[1], `encountered panic in upgrade check`))
217199
})
218200

219201
t.Run("cache sync fail leads to log and exit", func(t *testing.T) {

0 commit comments

Comments
 (0)