Skip to content

Commit c77d4f1

Browse files
committed
Remove use of io/ioutil package because its deprecated
Signed-off-by: Alvin Lin <[email protected]>
1 parent 5d0df05 commit c77d4f1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+158
-178
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ dist/
2424
Makefile.local
2525

2626
.idea
27+
.vscode
2728
compose
2829
compose-simple

.golangci.yml

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ linters-settings:
1313
staticcheck:
1414
checks:
1515
- all
16-
- "-SA1019" # Disable because too many file uses io/ioutil now, will fix later
1716
errcheck:
1817
# path to a file containing a list of functions to exclude from checking
1918
# see https://github.com/kisielk/errcheck#excluding-functions for details

cmd/cortex/main.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"crypto/sha256"
66
"flag"
77
"fmt"
8-
"io/ioutil"
8+
"io"
99
"math/rand"
1010
"os"
1111
"runtime"
@@ -204,7 +204,7 @@ func main() {
204204
func parseConfigFileParameter(args []string) (configFile string, expandEnv bool) {
205205
// ignore errors and any output here. Any flag errors will be reported by main flag.Parse() call.
206206
fs := flag.NewFlagSet("", flag.ContinueOnError)
207-
fs.SetOutput(ioutil.Discard)
207+
fs.SetOutput(io.Discard)
208208

209209
// usage not used in these functions.
210210
fs.StringVar(&configFile, configFileOption, "", "")
@@ -223,7 +223,7 @@ func parseConfigFileParameter(args []string) (configFile string, expandEnv bool)
223223

224224
// LoadConfig read YAML-formatted config from filename into cfg.
225225
func LoadConfig(filename string, expandENV bool, cfg *cortex.Config) error {
226-
buf, err := ioutil.ReadFile(filename)
226+
buf, err := os.ReadFile(filename)
227227
if err != nil {
228228
return errors.Wrap(err, "Error reading config file")
229229
}

integration/api_endpoints_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package integration
55

66
import (
77
"fmt"
8-
"io/ioutil"
8+
"io"
99
"net/http"
1010
"testing"
1111

@@ -55,7 +55,7 @@ func TestConfigAPIEndpoint(t *testing.T) {
5555
require.NoError(t, err)
5656

5757
defer runutil.ExhaustCloseWithErrCapture(&err, res.Body, "config API response")
58-
body, err := ioutil.ReadAll(res.Body)
58+
body, err := io.ReadAll(res.Body)
5959
require.NoError(t, err)
6060
require.Equal(t, http.StatusOK, res.StatusCode)
6161

integration/e2e/scenario_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ package e2e_test
66
import (
77
"bytes"
88
"context"
9-
"io/ioutil"
9+
"io"
1010
"testing"
1111
"time"
1212

@@ -65,13 +65,13 @@ func testMinioWorking(t *testing.T, m *e2e.HTTPService) {
6565

6666
r, err := bkt.Get(ctx, "recipe")
6767
require.NoError(t, err)
68-
b, err = ioutil.ReadAll(r)
68+
b, err = io.ReadAll(r)
6969
require.NoError(t, err)
7070
require.Equal(t, "Just go to Pastry Shop and buy.", string(b))
7171

7272
r, err = bkt.Get(ctx, "mom/recipe")
7373
require.NoError(t, err)
74-
b, err = ioutil.ReadAll(r)
74+
b, err = io.ReadAll(r)
7575
require.NoError(t, err)
7676
require.Equal(t, "https://www.bbcgoodfood.com/recipes/strawberry-cheesecake-4-easy-steps", string(b))
7777
}

integration/e2e/service.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7-
"io/ioutil"
7+
"io"
88
"net"
99
"os/exec"
1010
"regexp"
@@ -418,7 +418,7 @@ func (p *HTTPReadinessProbe) Ready(service *ConcreteService) (err error) {
418418
}
419419

420420
defer runutil.ExhaustCloseWithErrCapture(&err, res.Body, "response readiness")
421-
body, _ := ioutil.ReadAll(res.Body)
421+
body, _ := io.ReadAll(res.Body)
422422

423423
if res.StatusCode < p.expectedStatusRangeStart || res.StatusCode > p.expectedStatusRangeEnd {
424424
return fmt.Errorf("expected code in range: [%v, %v], got status code: %v and body: %v", p.expectedStatusRangeStart, p.expectedStatusRangeEnd, res.StatusCode, string(body))
@@ -534,7 +534,7 @@ func (s *HTTPService) Metrics() (_ string, err error) {
534534
}
535535

536536
defer runutil.ExhaustCloseWithErrCapture(&err, res.Body, "metrics response")
537-
body, err := ioutil.ReadAll(res.Body)
537+
body, err := io.ReadAll(res.Body)
538538

539539
return string(body), err
540540
}

integration/e2e/util.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package e2e
22

33
import (
44
"context"
5-
"io/ioutil"
65
"math"
76
"math/rand"
87
"net/http"
@@ -157,7 +156,7 @@ func GetTempDirectory() (string, error) {
157156
}
158157
}
159158

160-
tmpDir, err := ioutil.TempDir(dir, "e2e_integration_test")
159+
tmpDir, err := os.MkdirTemp(dir, "e2e_integration_test")
161160
if err != nil {
162161
return "", err
163162
}

integration/querier_remote_read_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ package integration
66
import (
77
"bytes"
88
"context"
9-
"io/ioutil"
9+
"io"
1010
"net/http"
1111
"testing"
1212
"time"
@@ -102,7 +102,7 @@ func TestQuerierRemoteRead(t *testing.T) {
102102
require.NoError(t, err)
103103
require.Equal(t, http.StatusOK, httpResp.StatusCode)
104104

105-
compressed, err = ioutil.ReadAll(httpResp.Body)
105+
compressed, err = io.ReadAll(httpResp.Body)
106106
require.NoError(t, err)
107107

108108
uncompressed, err := snappy.Decode(nil, compressed)

integration/util.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package integration
55

66
import (
77
"bytes"
8-
"io/ioutil"
98
"os"
109
"os/exec"
1110
"path/filepath"
@@ -44,14 +43,14 @@ func writeFileToSharedDir(s *e2e.Scenario, dst string, content []byte) error {
4443
return err
4544
}
4645

47-
return ioutil.WriteFile(
46+
return os.WriteFile(
4847
dst,
4948
content,
5049
os.ModePerm)
5150
}
5251

5352
func copyFileToSharedDir(s *e2e.Scenario, src, dst string) error {
54-
content, err := ioutil.ReadFile(filepath.Join(getCortexProjectDir(), src))
53+
content, err := os.ReadFile(filepath.Join(getCortexProjectDir(), src))
5554
if err != nil {
5655
return errors.Wrapf(err, "unable to read local file %s", src)
5756
}

pkg/alertmanager/alertmanager_http_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package alertmanager
22

33
import (
44
"context"
5-
"io/ioutil"
5+
"io"
66
"net/http/httptest"
77
"testing"
88
"time"
@@ -77,7 +77,7 @@ func TestMultitenantAlertmanager_GetStatusHandler(t *testing.T) {
7777

7878
resp := w.Result()
7979
require.Equal(t, 200, w.Code)
80-
body, _ := ioutil.ReadAll(resp.Body)
80+
body, _ := io.ReadAll(resp.Body)
8181
content := string(body)
8282
require.Contains(t, content, tt.content)
8383
require.NotContains(t, content, tt.nocontent)

pkg/alertmanager/alertstore/bucketclient/bucket_client.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package bucketclient
33
import (
44
"bytes"
55
"context"
6-
"io/ioutil"
6+
"io"
77
"strings"
88
"sync"
99

@@ -186,7 +186,7 @@ func (s *BucketAlertStore) get(ctx context.Context, bkt objstore.Bucket, name st
186186

187187
defer runutil.CloseWithLogOnErr(s.logger, readCloser, "close bucket reader")
188188

189-
buf, err := ioutil.ReadAll(readCloser)
189+
buf, err := io.ReadAll(readCloser)
190190
if err != nil {
191191
return errors.Wrapf(err, "failed to read alertmanager config for user %s", name)
192192
}

pkg/alertmanager/alertstore/local/store.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package local
33
import (
44
"context"
55
"flag"
6-
"io/ioutil"
76
"os"
87
"path/filepath"
98
"strings"
@@ -141,7 +140,7 @@ func (f *Store) reloadConfigs() (map[string]alertspb.AlertConfigDesc, error) {
141140
}
142141

143142
// Load the file to be returned by the store.
144-
content, err := ioutil.ReadFile(path)
143+
content, err := os.ReadFile(path)
145144
if err != nil {
146145
return errors.Wrapf(err, "unable to read alertmanager config %s", path)
147146
}

pkg/alertmanager/alertstore/local/store_test.go

+7-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package local
33
import (
44
"context"
55
"fmt"
6-
"io/ioutil"
76
"os"
87
"path/filepath"
98
"testing"
@@ -29,11 +28,11 @@ func TestStore_ListAllUsers(t *testing.T) {
2928
{
3029
user1Cfg := prepareAlertmanagerConfig("user-1")
3130
user2Cfg := prepareAlertmanagerConfig("user-2")
32-
require.NoError(t, ioutil.WriteFile(filepath.Join(storeDir, "user-1.yaml"), []byte(user1Cfg), os.ModePerm))
33-
require.NoError(t, ioutil.WriteFile(filepath.Join(storeDir, "user-2.yaml"), []byte(user2Cfg), os.ModePerm))
31+
require.NoError(t, os.WriteFile(filepath.Join(storeDir, "user-1.yaml"), []byte(user1Cfg), os.ModePerm))
32+
require.NoError(t, os.WriteFile(filepath.Join(storeDir, "user-2.yaml"), []byte(user2Cfg), os.ModePerm))
3433

3534
// The following file is expected to be skipped.
36-
require.NoError(t, ioutil.WriteFile(filepath.Join(storeDir, "user-3.unsupported-extension"), []byte{}, os.ModePerm))
35+
require.NoError(t, os.WriteFile(filepath.Join(storeDir, "user-3.unsupported-extension"), []byte{}, os.ModePerm))
3736

3837
users, err := store.ListAllUsers(ctx)
3938
require.NoError(t, err)
@@ -55,8 +54,8 @@ func TestStore_GetAlertConfig(t *testing.T) {
5554
{
5655
user1Cfg := prepareAlertmanagerConfig("user-1")
5756
user2Cfg := prepareAlertmanagerConfig("user-2")
58-
require.NoError(t, ioutil.WriteFile(filepath.Join(storeDir, "user-1.yaml"), []byte(user1Cfg), os.ModePerm))
59-
require.NoError(t, ioutil.WriteFile(filepath.Join(storeDir, "user-2.yaml"), []byte(user2Cfg), os.ModePerm))
57+
require.NoError(t, os.WriteFile(filepath.Join(storeDir, "user-1.yaml"), []byte(user1Cfg), os.ModePerm))
58+
require.NoError(t, os.WriteFile(filepath.Join(storeDir, "user-2.yaml"), []byte(user2Cfg), os.ModePerm))
6059

6160
config, err := store.GetAlertConfig(ctx, "user-1")
6261
require.NoError(t, err)
@@ -82,7 +81,7 @@ func TestStore_GetAlertConfigs(t *testing.T) {
8281
// The storage contains some configs.
8382
{
8483
user1Cfg := prepareAlertmanagerConfig("user-1")
85-
require.NoError(t, ioutil.WriteFile(filepath.Join(storeDir, "user-1.yaml"), []byte(user1Cfg), os.ModePerm))
84+
require.NoError(t, os.WriteFile(filepath.Join(storeDir, "user-1.yaml"), []byte(user1Cfg), os.ModePerm))
8685

8786
configs, err := store.GetAlertConfigs(ctx, []string{"user-1", "user-2"})
8887
require.NoError(t, err)
@@ -92,7 +91,7 @@ func TestStore_GetAlertConfigs(t *testing.T) {
9291

9392
// Add another user config.
9493
user2Cfg := prepareAlertmanagerConfig("user-2")
95-
require.NoError(t, ioutil.WriteFile(filepath.Join(storeDir, "user-2.yaml"), []byte(user2Cfg), os.ModePerm))
94+
require.NoError(t, os.WriteFile(filepath.Join(storeDir, "user-2.yaml"), []byte(user2Cfg), os.ModePerm))
9695

9796
configs, err = store.GetAlertConfigs(ctx, []string{"user-1", "user-2"})
9897
require.NoError(t, err)

pkg/alertmanager/api.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"fmt"
66
"io"
7-
"io/ioutil"
87
"net/http"
98
"os"
109
"path/filepath"
@@ -111,7 +110,7 @@ func (am *MultitenantAlertmanager) SetUserConfig(w http.ResponseWriter, r *http.
111110
input = r.Body
112111
}
113112

114-
payload, err := ioutil.ReadAll(input)
113+
payload, err := io.ReadAll(input)
115114
if err != nil {
116115
level.Error(logger).Log("msg", errReadingConfiguration, "err", err.Error())
117116
http.Error(w, fmt.Sprintf("%s: %s", errReadingConfiguration, err.Error()), http.StatusBadRequest)
@@ -223,7 +222,7 @@ func validateUserConfig(logger log.Logger, cfg alertspb.AlertConfigDesc, limits
223222
// not to configured data dir, and on the flipside, it'll fail if we can't write
224223
// to tmpDir. Ignoring both cases for now as they're ultra rare but will revisit if
225224
// we see this in the wild.
226-
userTempDir, err := ioutil.TempDir("", "validate-config-"+cfg.User)
225+
userTempDir, err := os.MkdirTemp("", "validate-config-"+cfg.User)
227226
if err != nil {
228227
return err
229228
}

pkg/alertmanager/api_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7-
"io/ioutil"
7+
"io"
88
"net/http"
99
"net/http/httptest"
1010
"testing"
@@ -553,7 +553,7 @@ template_files:
553553
am.SetUserConfig(w, req.WithContext(ctx))
554554
resp := w.Result()
555555

556-
body, err := ioutil.ReadAll(resp.Body)
556+
body, err := io.ReadAll(resp.Body)
557557
require.NoError(t, err)
558558

559559
if tc.err == nil {
@@ -688,7 +688,7 @@ receivers:
688688
resp := w.Result()
689689
require.Equal(t, http.StatusOK, resp.StatusCode)
690690
require.Equal(t, "application/yaml", resp.Header.Get("Content-Type"))
691-
body, err := ioutil.ReadAll(resp.Body)
691+
body, err := io.ReadAll(resp.Body)
692692
require.NoError(t, err)
693693
old, err := yaml.Marshal(testCases)
694694
require.NoError(t, err)

pkg/alertmanager/distributor.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package alertmanager
33
import (
44
"context"
55
"hash/fnv"
6-
"io/ioutil"
6+
"io"
77
"math/rand"
88
"net/http"
99
"path"
@@ -164,7 +164,7 @@ func (d *Distributor) doQuorum(userID string, w http.ResponseWriter, r *http.Req
164164
var body []byte
165165
var err error
166166
if r.Body != nil {
167-
body, err = ioutil.ReadAll(http.MaxBytesReader(w, r.Body, d.maxRecvMsgSize))
167+
body, err = io.ReadAll(http.MaxBytesReader(w, r.Body, d.maxRecvMsgSize))
168168
if err != nil {
169169
if util.IsRequestBodyTooLarge(err) {
170170
http.Error(w, "Request body too large", http.StatusRequestEntityTooLarge)
@@ -233,7 +233,7 @@ func (d *Distributor) doUnary(userID string, w http.ResponseWriter, r *http.Requ
233233
return
234234
}
235235

236-
body, err := ioutil.ReadAll(http.MaxBytesReader(w, r.Body, d.maxRecvMsgSize))
236+
body, err := io.ReadAll(http.MaxBytesReader(w, r.Body, d.maxRecvMsgSize))
237237
if err != nil {
238238
if util.IsRequestBodyTooLarge(err) {
239239
http.Error(w, "Request body too large", http.StatusRequestEntityTooLarge)

0 commit comments

Comments
 (0)