Skip to content

Commit 1522c57

Browse files
committed
Pass the upgrade-check URL as an argument
The global value is now a constant and somewhat easier to reason about.
1 parent 7d754bd commit 1522c57

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

internal/upgradecheck/http.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
package upgradecheck
2-
31
/*
42
Copyright 2017 - 2022 Crunchy Data Solutions, Inc.
53
Licensed under the Apache License, Version 2.0 (the "License");
@@ -15,6 +13,8 @@ package upgradecheck
1513
limitations under the License.
1614
*/
1715

16+
package upgradecheck
17+
1818
import (
1919
"context"
2020
"fmt"
@@ -44,9 +44,14 @@ var (
4444
Factor: float64(2),
4545
Steps: 4,
4646
}
47+
)
4748

49+
const (
4850
// upgradeCheckURL can be set using the CHECK_FOR_UPGRADES_URL env var
49-
upgradeCheckURL = "https://operator-maestro.crunchydata.com/pgo-versions"
51+
upgradeCheckURL = "https://operator-maestro.crunchydata.com/pgo-versions"
52+
)
53+
54+
var (
5055
upgradeCheckPeriod = 24 * time.Hour
5156
)
5257

@@ -73,7 +78,7 @@ func init() {
7378
}
7479
}
7580

76-
func checkForUpgrades(ctx context.Context, versionString string, backoff wait.Backoff,
81+
func checkForUpgrades(ctx context.Context, url, versionString string, backoff wait.Backoff,
7782
crclient crclient.Client, cfg *rest.Config,
7883
isOpenShift bool) (message string, header string, err error) {
7984
var headerPayloadStruct *clientUpgradeData
@@ -87,9 +92,7 @@ func checkForUpgrades(ctx context.Context, versionString string, backoff wait.Ba
8792
}()
8893

8994
// Prep request
90-
req, err := http.NewRequest("GET",
91-
upgradeCheckURL,
92-
nil)
95+
req, err := http.NewRequest("GET", url, nil)
9396
if err == nil {
9497
// generateHeader always returns some sort of struct, using defaults/nil values
9598
// in case some of the checks return errors
@@ -156,9 +159,8 @@ func CheckForUpgradesScheduler(ctx context.Context,
156159
}
157160
}()
158161

159-
// set the URL for the check for upgrades endpoint if provided
160-
if url != "" {
161-
upgradeCheckURL = url
162+
if url == "" {
163+
url = upgradeCheckURL
162164
}
163165

164166
// Since we pass the client to this function before we start the manager
@@ -172,7 +174,7 @@ func CheckForUpgradesScheduler(ctx context.Context,
172174
return
173175
}
174176

175-
info, header, err := checkForUpgrades(ctx, versionString, backoff,
177+
info, header, err := checkForUpgrades(ctx, url, versionString, backoff,
176178
crclient, cfg, isOpenShift)
177179
if err != nil {
178180
log.V(1).Info("could not complete upgrade check",
@@ -185,7 +187,7 @@ func CheckForUpgradesScheduler(ctx context.Context,
185187
for {
186188
select {
187189
case <-ticker.C:
188-
info, header, err = checkForUpgrades(ctx, versionString, backoff,
190+
info, header, err = checkForUpgrades(ctx, url, versionString, backoff,
189191
crclient, cfg, isOpenShift)
190192
if err != nil {
191193
log.V(1).Info("could not complete scheduled upgrade check",

internal/upgradecheck/http_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
package upgradecheck
2-
31
/*
42
Copyright 2021 - 2022 Crunchy Data Solutions, Inc.
53
Licensed under the Apache License, Version 2.0 (the "License");
@@ -15,6 +13,8 @@ package upgradecheck
1513
limitations under the License.
1614
*/
1715

16+
package upgradecheck
17+
1818
import (
1919
"context"
2020
"encoding/json"
@@ -89,7 +89,7 @@ func TestCheckForUpgrades(t *testing.T) {
8989
}, nil
9090
}
9191

92-
res, header, err := checkForUpgrades(ctx, "4.7.3", backoff,
92+
res, header, err := checkForUpgrades(ctx, "", "4.7.3", backoff,
9393
fakeClient, cfg, false)
9494
assert.NilError(t, err)
9595
assert.Equal(t, res, `{"pgo_versions":[{"tag":"v5.0.4"},{"tag":"v5.0.3"},{"tag":"v5.0.2"},{"tag":"v5.0.1"},{"tag":"v5.0.0"}]}`)
@@ -104,7 +104,7 @@ func TestCheckForUpgrades(t *testing.T) {
104104
return &http.Response{}, errors.New("whoops")
105105
}
106106

107-
res, header, err := checkForUpgrades(ctx, "4.7.3", backoff,
107+
res, header, err := checkForUpgrades(ctx, "", "4.7.3", backoff,
108108
fakeClient, cfg, false)
109109
// Two failed calls because of env var
110110
assert.Equal(t, counter, 2)
@@ -121,7 +121,7 @@ func TestCheckForUpgrades(t *testing.T) {
121121
panic(fmt.Errorf("oh no!"))
122122
}
123123

124-
res, header, err := checkForUpgrades(ctx, "4.7.3", backoff,
124+
res, header, err := checkForUpgrades(ctx, "", "4.7.3", backoff,
125125
fakeClient, cfg, false)
126126
// One call because of panic
127127
assert.Equal(t, counter, 1)
@@ -142,7 +142,7 @@ func TestCheckForUpgrades(t *testing.T) {
142142
}, nil
143143
}
144144

145-
res, header, err := checkForUpgrades(ctx, "4.7.3", backoff,
145+
res, header, err := checkForUpgrades(ctx, "", "4.7.3", backoff,
146146
fakeClient, cfg, false)
147147
assert.Equal(t, res, "")
148148
// Two failed calls because of env var
@@ -171,7 +171,7 @@ func TestCheckForUpgrades(t *testing.T) {
171171
}, nil
172172
}
173173

174-
res, header, err := checkForUpgrades(ctx, "4.7.3", backoff,
174+
res, header, err := checkForUpgrades(ctx, "", "4.7.3", backoff,
175175
fakeClient, cfg, false)
176176
assert.Equal(t, counter, 2)
177177
assert.NilError(t, err)

0 commit comments

Comments
 (0)