Skip to content

Remove use of io/ioutil package because it's deprecated #4851

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ dist/
Makefile.local

.idea
.vscode
compose
compose-simple
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ linters-settings:
staticcheck:
checks:
- all
- "-SA1019" # Disable because too many file uses io/ioutil now, will fix later
errcheck:
# path to a file containing a list of functions to exclude from checking
# see https://github.com/kisielk/errcheck#excluding-functions for details
Expand Down
6 changes: 3 additions & 3 deletions cmd/cortex/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"crypto/sha256"
"flag"
"fmt"
"io/ioutil"
"io"
"math/rand"
"os"
"runtime"
Expand Down Expand Up @@ -204,7 +204,7 @@ func main() {
func parseConfigFileParameter(args []string) (configFile string, expandEnv bool) {
// ignore errors and any output here. Any flag errors will be reported by main flag.Parse() call.
fs := flag.NewFlagSet("", flag.ContinueOnError)
fs.SetOutput(ioutil.Discard)
fs.SetOutput(io.Discard)

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

// LoadConfig read YAML-formatted config from filename into cfg.
func LoadConfig(filename string, expandENV bool, cfg *cortex.Config) error {
buf, err := ioutil.ReadFile(filename)
buf, err := os.ReadFile(filename)
if err != nil {
return errors.Wrap(err, "Error reading config file")
}
Expand Down
4 changes: 2 additions & 2 deletions integration/api_endpoints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package integration

import (
"fmt"
"io/ioutil"
"io"
"net/http"
"testing"

Expand Down Expand Up @@ -55,7 +55,7 @@ func TestConfigAPIEndpoint(t *testing.T) {
require.NoError(t, err)

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

Expand Down
6 changes: 3 additions & 3 deletions integration/e2e/scenario_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package e2e_test
import (
"bytes"
"context"
"io/ioutil"
"io"
"testing"
"time"

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

r, err := bkt.Get(ctx, "recipe")
require.NoError(t, err)
b, err = ioutil.ReadAll(r)
b, err = io.ReadAll(r)
require.NoError(t, err)
require.Equal(t, "Just go to Pastry Shop and buy.", string(b))

r, err = bkt.Get(ctx, "mom/recipe")
require.NoError(t, err)
b, err = ioutil.ReadAll(r)
b, err = io.ReadAll(r)
require.NoError(t, err)
require.Equal(t, "https://www.bbcgoodfood.com/recipes/strawberry-cheesecake-4-easy-steps", string(b))
}
Expand Down
6 changes: 3 additions & 3 deletions integration/e2e/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"io"
"net"
"os/exec"
"regexp"
Expand Down Expand Up @@ -418,7 +418,7 @@ func (p *HTTPReadinessProbe) Ready(service *ConcreteService) (err error) {
}

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

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

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

return string(body), err
}
Expand Down
3 changes: 1 addition & 2 deletions integration/e2e/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package e2e

import (
"context"
"io/ioutil"
"math"
"math/rand"
"net/http"
Expand Down Expand Up @@ -157,7 +156,7 @@ func GetTempDirectory() (string, error) {
}
}

tmpDir, err := ioutil.TempDir(dir, "e2e_integration_test")
tmpDir, err := os.MkdirTemp(dir, "e2e_integration_test")
if err != nil {
return "", err
}
Expand Down
4 changes: 2 additions & 2 deletions integration/querier_remote_read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package integration
import (
"bytes"
"context"
"io/ioutil"
"io"
"net/http"
"testing"
"time"
Expand Down Expand Up @@ -102,7 +102,7 @@ func TestQuerierRemoteRead(t *testing.T) {
require.NoError(t, err)
require.Equal(t, http.StatusOK, httpResp.StatusCode)

compressed, err = ioutil.ReadAll(httpResp.Body)
compressed, err = io.ReadAll(httpResp.Body)
require.NoError(t, err)

uncompressed, err := snappy.Decode(nil, compressed)
Expand Down
5 changes: 2 additions & 3 deletions integration/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package integration

import (
"bytes"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -44,14 +43,14 @@ func writeFileToSharedDir(s *e2e.Scenario, dst string, content []byte) error {
return err
}

return ioutil.WriteFile(
return os.WriteFile(
dst,
content,
os.ModePerm)
}

func copyFileToSharedDir(s *e2e.Scenario, src, dst string) error {
content, err := ioutil.ReadFile(filepath.Join(getCortexProjectDir(), src))
content, err := os.ReadFile(filepath.Join(getCortexProjectDir(), src))
if err != nil {
return errors.Wrapf(err, "unable to read local file %s", src)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/alertmanager/alertmanager_http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package alertmanager

import (
"context"
"io/ioutil"
"io"
"net/http/httptest"
"testing"
"time"
Expand Down Expand Up @@ -77,7 +77,7 @@ func TestMultitenantAlertmanager_GetStatusHandler(t *testing.T) {

resp := w.Result()
require.Equal(t, 200, w.Code)
body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)
content := string(body)
require.Contains(t, content, tt.content)
require.NotContains(t, content, tt.nocontent)
Expand Down
4 changes: 2 additions & 2 deletions pkg/alertmanager/alertstore/bucketclient/bucket_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package bucketclient
import (
"bytes"
"context"
"io/ioutil"
"io"
"strings"
"sync"

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

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

buf, err := ioutil.ReadAll(readCloser)
buf, err := io.ReadAll(readCloser)
if err != nil {
return errors.Wrapf(err, "failed to read alertmanager config for user %s", name)
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/alertmanager/alertstore/local/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package local
import (
"context"
"flag"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -141,7 +140,7 @@ func (f *Store) reloadConfigs() (map[string]alertspb.AlertConfigDesc, error) {
}

// Load the file to be returned by the store.
content, err := ioutil.ReadFile(path)
content, err := os.ReadFile(path)
if err != nil {
return errors.Wrapf(err, "unable to read alertmanager config %s", path)
}
Expand Down
15 changes: 7 additions & 8 deletions pkg/alertmanager/alertstore/local/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package local
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand All @@ -29,11 +28,11 @@ func TestStore_ListAllUsers(t *testing.T) {
{
user1Cfg := prepareAlertmanagerConfig("user-1")
user2Cfg := prepareAlertmanagerConfig("user-2")
require.NoError(t, ioutil.WriteFile(filepath.Join(storeDir, "user-1.yaml"), []byte(user1Cfg), os.ModePerm))
require.NoError(t, ioutil.WriteFile(filepath.Join(storeDir, "user-2.yaml"), []byte(user2Cfg), os.ModePerm))
require.NoError(t, os.WriteFile(filepath.Join(storeDir, "user-1.yaml"), []byte(user1Cfg), os.ModePerm))
require.NoError(t, os.WriteFile(filepath.Join(storeDir, "user-2.yaml"), []byte(user2Cfg), os.ModePerm))

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

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

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

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

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

configs, err = store.GetAlertConfigs(ctx, []string{"user-1", "user-2"})
require.NoError(t, err)
Expand Down
5 changes: 2 additions & 3 deletions pkg/alertmanager/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -111,7 +110,7 @@ func (am *MultitenantAlertmanager) SetUserConfig(w http.ResponseWriter, r *http.
input = r.Body
}

payload, err := ioutil.ReadAll(input)
payload, err := io.ReadAll(input)
if err != nil {
level.Error(logger).Log("msg", errReadingConfiguration, "err", err.Error())
http.Error(w, fmt.Sprintf("%s: %s", errReadingConfiguration, err.Error()), http.StatusBadRequest)
Expand Down Expand Up @@ -223,7 +222,7 @@ func validateUserConfig(logger log.Logger, cfg alertspb.AlertConfigDesc, limits
// not to configured data dir, and on the flipside, it'll fail if we can't write
// to tmpDir. Ignoring both cases for now as they're ultra rare but will revisit if
// we see this in the wild.
userTempDir, err := ioutil.TempDir("", "validate-config-"+cfg.User)
userTempDir, err := os.MkdirTemp("", "validate-config-"+cfg.User)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/alertmanager/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -553,7 +553,7 @@ template_files:
am.SetUserConfig(w, req.WithContext(ctx))
resp := w.Result()

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)

if tc.err == nil {
Expand Down Expand Up @@ -688,7 +688,7 @@ receivers:
resp := w.Result()
require.Equal(t, http.StatusOK, resp.StatusCode)
require.Equal(t, "application/yaml", resp.Header.Get("Content-Type"))
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
old, err := yaml.Marshal(testCases)
require.NoError(t, err)
Expand Down
6 changes: 3 additions & 3 deletions pkg/alertmanager/distributor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package alertmanager
import (
"context"
"hash/fnv"
"io/ioutil"
"io"
"math/rand"
"net/http"
"path"
Expand Down Expand Up @@ -164,7 +164,7 @@ func (d *Distributor) doQuorum(userID string, w http.ResponseWriter, r *http.Req
var body []byte
var err error
if r.Body != nil {
body, err = ioutil.ReadAll(http.MaxBytesReader(w, r.Body, d.maxRecvMsgSize))
body, err = io.ReadAll(http.MaxBytesReader(w, r.Body, d.maxRecvMsgSize))
if err != nil {
if util.IsRequestBodyTooLarge(err) {
http.Error(w, "Request body too large", http.StatusRequestEntityTooLarge)
Expand Down Expand Up @@ -233,7 +233,7 @@ func (d *Distributor) doUnary(userID string, w http.ResponseWriter, r *http.Requ
return
}

body, err := ioutil.ReadAll(http.MaxBytesReader(w, r.Body, d.maxRecvMsgSize))
body, err := io.ReadAll(http.MaxBytesReader(w, r.Body, d.maxRecvMsgSize))
if err != nil {
if util.IsRequestBodyTooLarge(err) {
http.Error(w, "Request body too large", http.StatusRequestEntityTooLarge)
Expand Down
Loading