From 91c481002d4175bfdf120365068cd98220b755bf Mon Sep 17 00:00:00 2001 From: Charlie Le Date: Sat, 20 Apr 2024 18:12:06 -0700 Subject: [PATCH] Deprecate use of ioutil package Signed-off-by: Charlie Le --- pkg/alerting/runner.go | 6 +++--- pkg/bench/write_client.go | 3 +-- pkg/chunk/cassandra/storage_client.go | 4 ++-- pkg/chunk/gcp/gcs_scanner.go | 7 +++---- pkg/client/alerts.go | 5 ++--- pkg/client/rules.go | 6 +++--- pkg/commands/alerts.go | 8 ++++---- pkg/commands/analyse_grafana.go | 3 +-- pkg/commands/analyse_prometheus.go | 7 +++---- pkg/commands/analyse_ruler.go | 3 +-- pkg/commands/block_gen.go | 3 +-- pkg/commands/bucket_validation.go | 3 +-- pkg/commands/chunks.go | 3 +-- pkg/commands/overrides_exporter.go | 5 ++--- pkg/commands/remote_read.go | 3 +-- pkg/commands/rules.go | 18 ++++++++++-------- 16 files changed, 39 insertions(+), 48 deletions(-) diff --git a/pkg/alerting/runner.go b/pkg/alerting/runner.go index 3586894b2..28c0e6dae 100644 --- a/pkg/alerting/runner.go +++ b/pkg/alerting/runner.go @@ -5,7 +5,7 @@ import ( "context" "flag" "fmt" - "io/ioutil" + "os" "sync" "time" @@ -111,7 +111,7 @@ func NewRunner(cfg RunnerConfig, logger log.Logger) (*Runner, error) { var amConfig []byte if cfg.AlertmanagerConfigFile != "" { - amConfig, err = ioutil.ReadFile(cfg.AlertmanagerConfigFile) + amConfig, err = os.ReadFile(cfg.AlertmanagerConfigFile) if err != nil { return nil, fmt.Errorf("unable to read Alertmanager configuration file %q: %s", cfg.AlertmanagerConfigFile, err) } @@ -120,7 +120,7 @@ func NewRunner(cfg RunnerConfig, logger log.Logger) (*Runner, error) { var rulerConfig rwrulefmt.RuleGroup if cfg.RulesConfigFile != "" { - b, err := ioutil.ReadFile(cfg.RulesConfigFile) + b, err := os.ReadFile(cfg.RulesConfigFile) if err != nil { return nil, fmt.Errorf("unable to read Rules configuration file %q: %s", cfg.RulesConfigFile, err) } diff --git a/pkg/bench/write_client.go b/pkg/bench/write_client.go index 770e67ad7..43227e9af 100644 --- a/pkg/bench/write_client.go +++ b/pkg/bench/write_client.go @@ -6,7 +6,6 @@ import ( "context" "fmt" "io" - "io/ioutil" "net/http" "strconv" "time" @@ -95,7 +94,7 @@ func (c *writeClient) Store(ctx context.Context, req []byte) error { c.requestDuration.WithLabelValues(strconv.Itoa(httpResp.StatusCode)).Observe(time.Since(start).Seconds()) defer func() { - _, err := io.Copy(ioutil.Discard, httpResp.Body) + _, err := io.Copy(io.Discard, httpResp.Body) if err != nil { level.Error(c.logger).Log("msg", "unable to discard write request body", "err", err) } diff --git a/pkg/chunk/cassandra/storage_client.go b/pkg/chunk/cassandra/storage_client.go index 2b770443a..6e2b69866 100644 --- a/pkg/chunk/cassandra/storage_client.go +++ b/pkg/chunk/cassandra/storage_client.go @@ -3,7 +3,7 @@ package cassandra import ( "bytes" "crypto/tls" - "io/ioutil" + "os" "strings" "github.com/cortexproject/cortex/pkg/chunk" @@ -84,7 +84,7 @@ func setClusterConfig(cfg cassandra.Config, cluster *gocql.ClusterConfig) error if cfg.Auth { password := cfg.Password.Value if cfg.PasswordFile != "" { - passwordBytes, err := ioutil.ReadFile(cfg.PasswordFile) + passwordBytes, err := os.ReadFile(cfg.PasswordFile) if err != nil { return errors.Errorf("Could not read Cassandra password file: %v", err) } diff --git a/pkg/chunk/gcp/gcs_scanner.go b/pkg/chunk/gcp/gcs_scanner.go index 1c934238b..cbb685fc1 100644 --- a/pkg/chunk/gcp/gcs_scanner.go +++ b/pkg/chunk/gcp/gcs_scanner.go @@ -1,16 +1,15 @@ package gcp import ( + "cloud.google.com/go/storage" "context" "fmt" - "io/ioutil" - - "cloud.google.com/go/storage" "github.com/cortexproject/cortex/pkg/chunk" "github.com/cortexproject/cortex/pkg/chunk/gcp" "github.com/pkg/errors" "github.com/sirupsen/logrus" "google.golang.org/api/iterator" + "io" chunkTool "github.com/cortexproject/cortex-tools/pkg/chunk" ) @@ -72,7 +71,7 @@ func (s *gcsScanner) Scan(ctx context.Context, req chunkTool.ScanRequest, filter return errors.WithStack(err) } - buf, err := ioutil.ReadAll(reader) + buf, err := io.ReadAll(reader) reader.Close() if err != nil { diff --git a/pkg/client/alerts.go b/pkg/client/alerts.go index c9cc2f868..4da7352c4 100644 --- a/pkg/client/alerts.go +++ b/pkg/client/alerts.go @@ -2,11 +2,10 @@ package client import ( "context" - "io/ioutil" - "github.com/pkg/errors" log "github.com/sirupsen/logrus" "gopkg.in/yaml.v3" + "io" ) const alertmanagerAPIPath = "/api/v1/alerts" @@ -57,7 +56,7 @@ func (r *CortexClient) GetAlertmanagerConfig(_ context.Context) (string, map[str } defer res.Body.Close() - body, err := ioutil.ReadAll(res.Body) + body, err := io.ReadAll(res.Body) if err != nil { return "", nil, err } diff --git a/pkg/client/rules.go b/pkg/client/rules.go index a41775f01..77ec0d2f3 100644 --- a/pkg/client/rules.go +++ b/pkg/client/rules.go @@ -3,7 +3,7 @@ package client import ( "context" "fmt" - "io/ioutil" + "io" "net/url" "github.com/pkg/errors" @@ -77,7 +77,7 @@ func (r *CortexClient) GetRuleGroup(_ context.Context, namespace, groupName stri } defer res.Body.Close() - body, err := ioutil.ReadAll(res.Body) + body, err := io.ReadAll(res.Body) if err != nil { return nil, err @@ -109,7 +109,7 @@ func (r *CortexClient) ListRules(_ context.Context, namespace string) (map[strin } defer res.Body.Close() - body, err := ioutil.ReadAll(res.Body) + body, err := io.ReadAll(res.Body) if err != nil { return nil, err diff --git a/pkg/commands/alerts.go b/pkg/commands/alerts.go index 6c69c0dbe..61d3b2845 100644 --- a/pkg/commands/alerts.go +++ b/pkg/commands/alerts.go @@ -4,13 +4,13 @@ import ( "context" "encoding/json" "fmt" + "io" "net/http" "os" "os/signal" "strings" "time" - "io/ioutil" "net/url" "github.com/pkg/errors" @@ -109,7 +109,7 @@ func (a *AlertmanagerCommand) getConfig(_ *kingpin.ParseContext) error { } func (a *AlertmanagerCommand) loadConfig(_ *kingpin.ParseContext) error { - content, err := ioutil.ReadFile(a.AlertmanagerConfigFile) + content, err := os.ReadFile(a.AlertmanagerConfigFile) if err != nil { return errors.Wrap(err, "unable to load config file: "+a.AlertmanagerConfigFile) } @@ -122,7 +122,7 @@ func (a *AlertmanagerCommand) loadConfig(_ *kingpin.ParseContext) error { templates := map[string]string{} for _, f := range a.TemplateFiles { - tmpl, err := ioutil.ReadFile(f) + tmpl, err := os.ReadFile(f) if err != nil { return errors.Wrap(err, "unable to load template file: "+f) } @@ -258,7 +258,7 @@ func (a *AlertCommand) runVerifyQuery(ctx context.Context, query string) (int, e return 0, err } - body, err := ioutil.ReadAll(res.Body) + body, err := io.ReadAll(res.Body) if err != nil { return 0, err } diff --git a/pkg/commands/analyse_grafana.go b/pkg/commands/analyse_grafana.go index 26d2b7332..a5a904a8e 100644 --- a/pkg/commands/analyse_grafana.go +++ b/pkg/commands/analyse_grafana.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" "os" "sort" "time" @@ -70,5 +69,5 @@ func writeOut(mig *analyse.MetricsInGrafana, outputFile string) error { return err } - return ioutil.WriteFile(outputFile, out, os.FileMode(int(0666))) + return os.WriteFile(outputFile, out, os.FileMode(int(0666))) } diff --git a/pkg/commands/analyse_prometheus.go b/pkg/commands/analyse_prometheus.go index b5fa42ca6..ec42d5835 100644 --- a/pkg/commands/analyse_prometheus.go +++ b/pkg/commands/analyse_prometheus.go @@ -3,7 +3,6 @@ package commands import ( "context" "encoding/json" - "io/ioutil" "os" "sort" "time" @@ -41,7 +40,7 @@ func (cmd *PrometheusAnalyseCommand) run(_ *kingpin.ParseContext) error { if _, err := os.Stat(cmd.grafanaMetricsFile); err == nil { hasGrafanaMetrics = true - byt, err := ioutil.ReadFile(cmd.grafanaMetricsFile) + byt, err := os.ReadFile(cmd.grafanaMetricsFile) if err != nil { return err } @@ -53,7 +52,7 @@ func (cmd *PrometheusAnalyseCommand) run(_ *kingpin.ParseContext) error { if _, err := os.Stat(cmd.rulerMetricsFile); err == nil { hasRulerMetrics = true - byt, err := ioutil.ReadFile(cmd.rulerMetricsFile) + byt, err := os.ReadFile(cmd.rulerMetricsFile) if err != nil { return err } @@ -228,5 +227,5 @@ func (cmd *PrometheusAnalyseCommand) run(_ *kingpin.ParseContext) error { return err } - return ioutil.WriteFile(cmd.outputFile, out, os.FileMode(int(0666))) + return os.WriteFile(cmd.outputFile, out, os.FileMode(int(0666))) } diff --git a/pkg/commands/analyse_ruler.go b/pkg/commands/analyse_ruler.go index 59d7a171a..1c428c0c4 100644 --- a/pkg/commands/analyse_ruler.go +++ b/pkg/commands/analyse_ruler.go @@ -3,7 +3,6 @@ package commands import ( "context" "encoding/json" - "io/ioutil" "os" "sort" @@ -65,5 +64,5 @@ func writeOutRuleMetrics(mir *analyse.MetricsInRuler, outputFile string) error { return err } - return ioutil.WriteFile(outputFile, out, os.FileMode(int(0666))) + return os.WriteFile(outputFile, out, os.FileMode(int(0666))) } diff --git a/pkg/commands/block_gen.go b/pkg/commands/block_gen.go index 613dd27ea..45878af62 100644 --- a/pkg/commands/block_gen.go +++ b/pkg/commands/block_gen.go @@ -2,7 +2,6 @@ package commands import ( "context" - "io/ioutil" "os" "sort" "time" @@ -57,7 +56,7 @@ func (f *BlockGenCommand) run(_ *kingpin.ParseContext) error { if f.Cfg.BlockDir == "" { var err error - f.Cfg.BlockDir, err = ioutil.TempDir("", "mockdata") + f.Cfg.BlockDir, err = os.MkdirTemp("", "mockdata") if err != nil { return errors.Wrap(err, "failed to create tmp dir") } diff --git a/pkg/commands/bucket_validation.go b/pkg/commands/bucket_validation.go index 2d37fb7d5..b7cca01b2 100644 --- a/pkg/commands/bucket_validation.go +++ b/pkg/commands/bucket_validation.go @@ -5,7 +5,6 @@ import ( "flag" "fmt" "io" - "io/ioutil" "os" "strings" @@ -245,7 +244,7 @@ func (b *BucketValidationCommand) validateTestObjects(ctx context.Context) error return errors.Wrapf(err, "failed to get object (%s)", objectPath) } - content, err := ioutil.ReadAll(reader) + content, err := io.ReadAll(reader) if err != nil { return errors.Wrapf(err, "failed to read object (%s)", objectPath) } diff --git a/pkg/commands/chunks.go b/pkg/commands/chunks.go index 8da6aa30e..b09e7d8b1 100644 --- a/pkg/commands/chunks.go +++ b/pkg/commands/chunks.go @@ -5,7 +5,6 @@ import ( "context" "encoding/hex" "fmt" - "io/ioutil" "os" "strings" "sync" @@ -309,7 +308,7 @@ func writeBatch(ctx context.Context, client *cassandra.StorageClient, batch chun // 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") } diff --git a/pkg/commands/overrides_exporter.go b/pkg/commands/overrides_exporter.go index 8e52b0c13..e4ba22fdd 100644 --- a/pkg/commands/overrides_exporter.go +++ b/pkg/commands/overrides_exporter.go @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - "io/ioutil" "net/http" "os" "os/signal" @@ -70,7 +69,7 @@ func (o *OverridesExporterCommand) updateOverridesMetrics() error { overrides := &struct { TenantLimits map[string]*validation.Limits `yaml:"overrides"` }{} - bytes, err := ioutil.ReadFile(o.overridesFilePath) + bytes, err := os.ReadFile(o.overridesFilePath) if err != nil { return fmt.Errorf("failed to update overrides, err: %w", err) } @@ -92,7 +91,7 @@ func (o *OverridesExporterCommand) updatePresetsMetrics() error { presets := &struct { Presets map[string]*validation.Limits `yaml:"presets"` }{} - bytes, err := ioutil.ReadFile(o.presetsFilePath) + bytes, err := os.ReadFile(o.presetsFilePath) if err != nil { return fmt.Errorf("failed to update presets, error reading file: %w", err) } diff --git a/pkg/commands/remote_read.go b/pkg/commands/remote_read.go index 5d0828f40..e1df21a18 100644 --- a/pkg/commands/remote_read.go +++ b/pkg/commands/remote_read.go @@ -6,7 +6,6 @@ import ( "context" "fmt" "io" - "io/ioutil" "math" "net/http" "net/url" @@ -385,7 +384,7 @@ func (c *RemoteReadCommand) export(_ *kingpin.ParseContext) error { } if c.tsdbPath == "" { - c.tsdbPath, err = ioutil.TempDir("", "cortextool-tsdb") + c.tsdbPath, err = os.MkdirTemp("", "cortextool-tsdb") if err != nil { return err } diff --git a/pkg/commands/rules.go b/pkg/commands/rules.go index 6c3111afa..21fc0db1a 100644 --- a/pkg/commands/rules.go +++ b/pkg/commands/rules.go @@ -3,7 +3,6 @@ package commands import ( "context" "fmt" - "io/ioutil" "os" "path/filepath" "reflect" @@ -334,8 +333,11 @@ func (r *RuleCommand) setupFiles() error { func (r *RuleCommand) listRules(_ *kingpin.ParseContext) error { rules, err := r.cli.ListRules(context.Background(), "") if err != nil { + if errors.Is(err, client.ErrResourceNotFound) { + log.Infof("no rule groups currently exist for this user") + return nil + } log.Fatalf("unable to read rules from cortex, %v", err) - } p := printer.New(r.DisableColor) @@ -345,7 +347,7 @@ func (r *RuleCommand) listRules(_ *kingpin.ParseContext) error { func (r *RuleCommand) printRules(_ *kingpin.ParseContext) error { rules, err := r.cli.ListRules(context.Background(), "") if err != nil { - if err == client.ErrResourceNotFound { + if errors.Is(err, client.ErrResourceNotFound) { log.Infof("no rule groups currently exist for this user") return nil } @@ -359,7 +361,7 @@ func (r *RuleCommand) printRules(_ *kingpin.ParseContext) error { func (r *RuleCommand) getRuleGroup(_ *kingpin.ParseContext) error { group, err := r.cli.GetRuleGroup(context.Background(), r.Namespace, r.RuleGroup) if err != nil { - if err == client.ErrResourceNotFound { + if errors.Is(err, client.ErrResourceNotFound) { log.Infof("this rule group does not currently exist") return nil } @@ -372,7 +374,7 @@ func (r *RuleCommand) getRuleGroup(_ *kingpin.ParseContext) error { func (r *RuleCommand) deleteRuleGroup(_ *kingpin.ParseContext) error { err := r.cli.DeleteRuleGroup(context.Background(), r.Namespace, r.RuleGroup) - if err != nil && err != client.ErrResourceNotFound { + if err != nil && !errors.Is(err, client.ErrResourceNotFound) { log.Fatalf("unable to delete rule group from cortex, %v", err) } return nil @@ -380,7 +382,7 @@ func (r *RuleCommand) deleteRuleGroup(_ *kingpin.ParseContext) error { func (r *RuleCommand) deleteRuleNamespace(_ *kingpin.ParseContext) error { err := r.cli.DeleteRuleNamespace(context.Background(), r.Namespace) - if err != nil && err != client.ErrResourceNotFound { + if err != nil && !errors.Is(err, client.ErrResourceNotFound) { log.Fatalf("unable to delete namespace from cortex, %v", err) } return nil @@ -397,7 +399,7 @@ func (r *RuleCommand) loadRules(_ *kingpin.ParseContext) error { for _, group := range ns.Groups { fmt.Printf("group: '%v', ns: '%v'\n", group.Name, ns.Namespace) curGroup, err := r.cli.GetRuleGroup(context.Background(), ns.Namespace, group.Name) - if err != nil && err != client.ErrResourceNotFound { + if err != nil && !errors.Is(err, client.ErrResourceNotFound) { return errors.Wrap(err, "load operation unsuccessful, unable to contact cortex api") } if curGroup != nil { @@ -784,7 +786,7 @@ func save(nss map[string]rules.RuleNamespace, i bool) error { filepath = filepath + ".result" } - if err := ioutil.WriteFile(filepath, payload, 0644); err != nil { + if err := os.WriteFile(filepath, payload, 0644); err != nil { return err } }