From 9f94c295ab4b87696c8574488213cb1a340ef32d Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Sun, 21 Aug 2022 23:51:13 +0300 Subject: [PATCH] all: use os.{Read|Write}File instead of ioutil.{Read|Write}File The ioutil.{Read|Write}File is deprecated since Go1.16 - see https://tip.golang.org/doc/go1.16#ioutil VictoriaMetrics needs at least Go1.18, so it is safe to remove ioutil usage from source code. This is a follow-up for 02ca2342ab01627eb5fdbb3174792b86fc1de7a9 --- app/vmalert/config/config.go | 4 ++-- app/vmalert/main_test.go | 2 +- app/vmalert/notifier/config.go | 4 ++-- app/vmalert/notifier/config_watcher_test.go | 2 +- app/vmalert/utils/tls.go | 4 ++-- app/vmselect/promql/rollup_result_cache.go | 4 ++-- lib/awsapi/config.go | 2 +- lib/backup/fsremote/fsremote.go | 3 +-- lib/cgroup/cpu.go | 3 +-- lib/cgroup/util.go | 8 ++++---- lib/fs/fs.go | 2 +- lib/fs/reader_at_test.go | 4 ++-- lib/fs/reader_at_timing_test.go | 4 ++-- lib/mergeset/part_header.go | 4 ++-- lib/mergeset/table.go | 3 +-- lib/persistentqueue/persistentqueue.go | 4 ++-- lib/persistentqueue/persistentqueue_test.go | 3 +-- lib/promscrape/discovery/azure/api.go | 3 +-- lib/promscrape/discovery/consul/api.go | 3 +-- lib/promscrape/discovery/kubernetes/api_watcher.go | 3 ++- lib/storage/part_header.go | 3 +-- lib/storage/partition.go | 3 +-- lib/storage/storage.go | 11 +++++------ 23 files changed, 39 insertions(+), 47 deletions(-) diff --git a/app/vmalert/config/config.go b/app/vmalert/config/config.go index c97dbd99a..b0847448a 100644 --- a/app/vmalert/config/config.go +++ b/app/vmalert/config/config.go @@ -4,8 +4,8 @@ import ( "crypto/md5" "fmt" "hash/fnv" - "io/ioutil" "net/url" + "os" "path/filepath" "sort" "strings" @@ -214,7 +214,7 @@ func Parse(pathPatterns []string, validateTplFn ValidateTplFn, validateExpressio } func parseFile(path string) ([]Group, error) { - data, err := ioutil.ReadFile(path) + data, err := os.ReadFile(path) if err != nil { return nil, fmt.Errorf("error reading alert rule file: %w", err) } diff --git a/app/vmalert/main_test.go b/app/vmalert/main_test.go index 87a98edfc..cff743752 100644 --- a/app/vmalert/main_test.go +++ b/app/vmalert/main_test.go @@ -154,7 +154,7 @@ groups: func writeToFile(t *testing.T, file, b string) { t.Helper() - err := ioutil.WriteFile(file, []byte(b), 0644) + err := os.WriteFile(file, []byte(b), 0644) if err != nil { t.Fatal(err) } diff --git a/app/vmalert/notifier/config.go b/app/vmalert/notifier/config.go index f7adf4147..1fb41ef17 100644 --- a/app/vmalert/notifier/config.go +++ b/app/vmalert/notifier/config.go @@ -4,8 +4,8 @@ import ( "crypto/md5" "fmt" "gopkg.in/yaml.v2" - "io/ioutil" "net/url" + "os" "path" "path/filepath" "strings" @@ -104,7 +104,7 @@ func (cfg *Config) UnmarshalYAML(unmarshal func(interface{}) error) error { } func parseConfig(path string) (*Config, error) { - data, err := ioutil.ReadFile(path) + data, err := os.ReadFile(path) if err != nil { return nil, fmt.Errorf("error reading config file: %w", err) } diff --git a/app/vmalert/notifier/config_watcher_test.go b/app/vmalert/notifier/config_watcher_test.go index 4120e75b2..d57f8200a 100644 --- a/app/vmalert/notifier/config_watcher_test.go +++ b/app/vmalert/notifier/config_watcher_test.go @@ -175,7 +175,7 @@ consul_sd_configs: func writeToFile(t *testing.T, file, b string) { t.Helper() - checkErr(t, ioutil.WriteFile(file, []byte(b), 0644)) + checkErr(t, os.WriteFile(file, []byte(b), 0644)) } func checkErr(t *testing.T, err error) { diff --git a/app/vmalert/utils/tls.go b/app/vmalert/utils/tls.go index 4155f16c2..02a25c49d 100644 --- a/app/vmalert/utils/tls.go +++ b/app/vmalert/utils/tls.go @@ -4,8 +4,8 @@ import ( "crypto/tls" "crypto/x509" "fmt" - "io/ioutil" "net/http" + "os" "strings" ) @@ -38,7 +38,7 @@ func TLSConfig(certFile, keyFile, CAFile, serverName string, insecureSkipVerify var rootCAs *x509.CertPool if CAFile != "" { - pem, err := ioutil.ReadFile(CAFile) + pem, err := os.ReadFile(CAFile) if err != nil { return nil, fmt.Errorf("cannot read `ca_file` %q: %w", CAFile, err) } diff --git a/app/vmselect/promql/rollup_result_cache.go b/app/vmselect/promql/rollup_result_cache.go index fd81c4716..22778f45a 100644 --- a/app/vmselect/promql/rollup_result_cache.go +++ b/app/vmselect/promql/rollup_result_cache.go @@ -4,7 +4,7 @@ import ( "crypto/rand" "flag" "fmt" - "io/ioutil" + "os" "sync" "sync/atomic" "time" @@ -413,7 +413,7 @@ func mustLoadRollupResultCacheKeyPrefix(path string) { rollupResultCacheKeyPrefix = newRollupResultCacheKeyPrefix() return } - data, err := ioutil.ReadFile(path) + data, err := os.ReadFile(path) if err != nil { logger.Errorf("cannot load %s: %s; reset rollupResult cache", path, err) rollupResultCacheKeyPrefix = newRollupResultCacheKeyPrefix() diff --git a/lib/awsapi/config.go b/lib/awsapi/config.go index e10d84736..a86ff7292 100644 --- a/lib/awsapi/config.go +++ b/lib/awsapi/config.go @@ -196,7 +196,7 @@ func (cfg *Config) getAPICredentials() (*credentials, error) { SecretAccessKey: cfg.defaultSecretKey, } if len(cfg.webTokenPath) > 0 { - token, err := ioutil.ReadFile(cfg.webTokenPath) + token, err := os.ReadFile(cfg.webTokenPath) if err != nil { return nil, fmt.Errorf("cannot read webToken from path: %q, err: %w", cfg.webTokenPath, err) } diff --git a/lib/backup/fsremote/fsremote.go b/lib/backup/fsremote/fsremote.go index 3e7c65b12..4e4939f91 100644 --- a/lib/backup/fsremote/fsremote.go +++ b/lib/backup/fsremote/fsremote.go @@ -3,7 +3,6 @@ package fsremote import ( "fmt" "io" - "io/ioutil" "os" "path/filepath" "strings" @@ -218,7 +217,7 @@ func (fs *FS) CreateFile(filePath string, data []byte) error { if err := fs.mkdirAll(path); err != nil { return err } - if err := ioutil.WriteFile(path, data, 0600); err != nil { + if err := os.WriteFile(path, data, 0600); err != nil { return fmt.Errorf("cannot write %d bytes to %q: %w", len(data), path, err) } return nil diff --git a/lib/cgroup/cpu.go b/lib/cgroup/cpu.go index 218764804..15a007adf 100644 --- a/lib/cgroup/cpu.go +++ b/lib/cgroup/cpu.go @@ -2,7 +2,6 @@ package cgroup import ( "fmt" - "io/ioutil" "os" "runtime" "strconv" @@ -80,7 +79,7 @@ func getCPUStat(statName string) (int64, error) { func getOnlineCPUCount() float64 { // See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/685#issuecomment-674423728 - data, err := ioutil.ReadFile("/sys/devices/system/cpu/online") + data, err := os.ReadFile("/sys/devices/system/cpu/online") if err != nil { return -1 } diff --git a/lib/cgroup/util.go b/lib/cgroup/util.go index 422204bd2..aa1c80ae0 100644 --- a/lib/cgroup/util.go +++ b/lib/cgroup/util.go @@ -2,7 +2,7 @@ package cgroup import ( "fmt" - "io/ioutil" + "os" "path" "strconv" "strings" @@ -23,11 +23,11 @@ func getStatGeneric(statName, sysfsPrefix, cgroupPath, cgroupGrepLine string) (i func getFileContents(statName, sysfsPrefix, cgroupPath, cgroupGrepLine string) (string, error) { filepath := path.Join(sysfsPrefix, statName) - data, err := ioutil.ReadFile(filepath) + data, err := os.ReadFile(filepath) if err == nil { return string(data), nil } - cgroupData, err := ioutil.ReadFile(cgroupPath) + cgroupData, err := os.ReadFile(cgroupPath) if err != nil { return "", err } @@ -36,7 +36,7 @@ func getFileContents(statName, sysfsPrefix, cgroupPath, cgroupGrepLine string) ( return "", fmt.Errorf("cannot find cgroup path for %q in %q: %w", cgroupGrepLine, cgroupPath, err) } filepath = path.Join(sysfsPrefix, subPath, statName) - data, err = ioutil.ReadFile(filepath) + data, err = os.ReadFile(filepath) if err != nil { return "", err } diff --git a/lib/fs/fs.go b/lib/fs/fs.go index c4d23254a..8dd6d008b 100644 --- a/lib/fs/fs.go +++ b/lib/fs/fs.go @@ -390,7 +390,7 @@ func ReadFileOrHTTP(path string) ([]byte, error) { } return data, nil } - data, err := ioutil.ReadFile(path) + data, err := os.ReadFile(path) if err != nil { return nil, fmt.Errorf("cannot read %q: %w", path, err) } diff --git a/lib/fs/reader_at_test.go b/lib/fs/reader_at_test.go index 4747030e7..5c279a5ff 100644 --- a/lib/fs/reader_at_test.go +++ b/lib/fs/reader_at_test.go @@ -2,7 +2,7 @@ package fs import ( "fmt" - "io/ioutil" + "os" "testing" ) @@ -18,7 +18,7 @@ func testReaderAt(t *testing.T, bufSize int) { path := "TestReaderAt" const fileSize = 8 * 1024 * 1024 data := make([]byte, fileSize) - if err := ioutil.WriteFile(path, data, 0600); err != nil { + if err := os.WriteFile(path, data, 0600); err != nil { t.Fatalf("cannot create %q: %s", path, err) } defer MustRemoveAll(path) diff --git a/lib/fs/reader_at_timing_test.go b/lib/fs/reader_at_timing_test.go index 87b012e97..1fd6216fc 100644 --- a/lib/fs/reader_at_timing_test.go +++ b/lib/fs/reader_at_timing_test.go @@ -2,7 +2,7 @@ package fs import ( "fmt" - "io/ioutil" + "os" "testing" ) @@ -25,7 +25,7 @@ func benchmarkReaderAtMustReadAt(b *testing.B, isMmap bool) { path := "BenchmarkReaderAtMustReadAt" const fileSize = 8 * 1024 * 1024 data := make([]byte, fileSize) - if err := ioutil.WriteFile(path, data, 0600); err != nil { + if err := os.WriteFile(path, data, 0600); err != nil { b.Fatalf("cannot create %q: %s", path, err) } defer MustRemoveAll(path) diff --git a/lib/mergeset/part_header.go b/lib/mergeset/part_header.go index a4d7007f7..785f54ba8 100644 --- a/lib/mergeset/part_header.go +++ b/lib/mergeset/part_header.go @@ -4,7 +4,7 @@ import ( "encoding/hex" "encoding/json" "fmt" - "io/ioutil" + "os" "path/filepath" "strconv" "strings" @@ -124,7 +124,7 @@ func (ph *partHeader) ParseFromPath(partPath string) error { // Read other ph fields from metadata. metadataPath := partPath + "/metadata.json" - metadata, err := ioutil.ReadFile(metadataPath) + metadata, err := os.ReadFile(metadataPath) if err != nil { return fmt.Errorf("cannot read %q: %w", metadataPath, err) } diff --git a/lib/mergeset/table.go b/lib/mergeset/table.go index 29448de6c..e614b7ad2 100644 --- a/lib/mergeset/table.go +++ b/lib/mergeset/table.go @@ -3,7 +3,6 @@ package mergeset import ( "errors" "fmt" - "io/ioutil" "os" "path/filepath" "sort" @@ -1259,7 +1258,7 @@ func runTransaction(txnLock *sync.RWMutex, pathPrefix, txnPath string) error { txnLock.RLock() defer txnLock.RUnlock() - data, err := ioutil.ReadFile(txnPath) + data, err := os.ReadFile(txnPath) if err != nil { return fmt.Errorf("cannot read transaction file: %w", err) } diff --git a/lib/persistentqueue/persistentqueue.go b/lib/persistentqueue/persistentqueue.go index ea412177f..6351b49aa 100644 --- a/lib/persistentqueue/persistentqueue.go +++ b/lib/persistentqueue/persistentqueue.go @@ -671,7 +671,7 @@ func (mi *metainfo) WriteToFile(path string) error { if err != nil { return fmt.Errorf("cannot marshal persistent queue metainfo %#v: %w", mi, err) } - if err := ioutil.WriteFile(path, data, 0600); err != nil { + if err := os.WriteFile(path, data, 0600); err != nil { return fmt.Errorf("cannot write persistent queue metainfo to %q: %w", path, err) } fs.MustSyncPath(path) @@ -680,7 +680,7 @@ func (mi *metainfo) WriteToFile(path string) error { func (mi *metainfo) ReadFromFile(path string) error { mi.Reset() - data, err := ioutil.ReadFile(path) + data, err := os.ReadFile(path) if err != nil { if os.IsNotExist(err) { return err diff --git a/lib/persistentqueue/persistentqueue_test.go b/lib/persistentqueue/persistentqueue_test.go index e135eb415..4aa3daf1c 100644 --- a/lib/persistentqueue/persistentqueue_test.go +++ b/lib/persistentqueue/persistentqueue_test.go @@ -2,7 +2,6 @@ package persistentqueue import ( "fmt" - "io/ioutil" "os" "strconv" "testing" @@ -371,7 +370,7 @@ func TestQueueLimitedSize(t *testing.T) { } func mustCreateFile(path, contents string) { - if err := ioutil.WriteFile(path, []byte(contents), 0600); err != nil { + if err := os.WriteFile(path, []byte(contents), 0600); err != nil { panic(fmt.Errorf("cannot create file %q with %d bytes contents: %w", path, len(contents), err)) } } diff --git a/lib/promscrape/discovery/azure/api.go b/lib/promscrape/discovery/azure/api.go index d328706c8..00791d009 100644 --- a/lib/promscrape/discovery/azure/api.go +++ b/lib/promscrape/discovery/azure/api.go @@ -3,7 +3,6 @@ package azure import ( "encoding/json" "fmt" - "io/ioutil" "net/url" "os" "strconv" @@ -145,7 +144,7 @@ func getCloudEnvByName(name string) (*cloudEnvironmentEndpoints, error) { } func readCloudEndpointsFromFile(filePath string) (*cloudEnvironmentEndpoints, error) { - data, err := ioutil.ReadFile(filePath) + data, err := os.ReadFile(filePath) if err != nil { return nil, fmt.Errorf("cannot file %q: %w", filePath, err) } diff --git a/lib/promscrape/discovery/consul/api.go b/lib/promscrape/discovery/consul/api.go index 479654e77..249a536ae 100644 --- a/lib/promscrape/discovery/consul/api.go +++ b/lib/promscrape/discovery/consul/api.go @@ -3,7 +3,6 @@ package consul import ( "flag" "fmt" - "io/ioutil" "os" "strconv" "strings" @@ -109,7 +108,7 @@ func getToken(token *promauth.Secret) (string, error) { return token.String(), nil } if tokenFile := os.Getenv("CONSUL_HTTP_TOKEN_FILE"); tokenFile != "" { - data, err := ioutil.ReadFile(tokenFile) + data, err := os.ReadFile(tokenFile) if err != nil { return "", fmt.Errorf("cannot read consul token file %q; probably, `token` arg is missing in `consul_sd_config`? error: %w", tokenFile, err) } diff --git a/lib/promscrape/discovery/kubernetes/api_watcher.go b/lib/promscrape/discovery/kubernetes/api_watcher.go index a4afcd200..f0689d2e3 100644 --- a/lib/promscrape/discovery/kubernetes/api_watcher.go +++ b/lib/promscrape/discovery/kubernetes/api_watcher.go @@ -9,6 +9,7 @@ import ( "io/ioutil" "net/http" "net/url" + "os" "reflect" "strconv" "strings" @@ -66,7 +67,7 @@ func newAPIWatcher(apiServer string, ac *promauth.Config, sdc *SDConfig, swcFunc namespaces := sdc.Namespaces.Names if len(namespaces) == 0 { if sdc.Namespaces.OwnNamespace { - namespace, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace") + namespace, err := os.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace") if err != nil { logger.Fatalf("cannot determine namespace for the current pod according to `own_namespace: true` option in kubernetes_sd_config: %s", err) } diff --git a/lib/storage/part_header.go b/lib/storage/part_header.go index af62a9438..1e21e2823 100644 --- a/lib/storage/part_header.go +++ b/lib/storage/part_header.go @@ -3,7 +3,6 @@ package storage import ( "errors" "fmt" - "io/ioutil" "os" "path/filepath" "strconv" @@ -131,7 +130,7 @@ func (ph *partHeader) Reset() { func (ph *partHeader) readMinDedupInterval(partPath string) error { filePath := partPath + "/min_dedup_interval" - data, err := ioutil.ReadFile(filePath) + data, err := os.ReadFile(filePath) if err != nil { if errors.Is(err, os.ErrNotExist) { // The minimum dedup interval may not exist for old parts. diff --git a/lib/storage/partition.go b/lib/storage/partition.go index 768947c6f..082bc4f15 100644 --- a/lib/storage/partition.go +++ b/lib/storage/partition.go @@ -3,7 +3,6 @@ package storage import ( "errors" "fmt" - "io/ioutil" "os" "path/filepath" "sort" @@ -1743,7 +1742,7 @@ func runTransaction(txnLock *sync.RWMutex, pathPrefix1, pathPrefix2, txnPath str txnLock.RLock() defer txnLock.RUnlock() - data, err := ioutil.ReadFile(txnPath) + data, err := os.ReadFile(txnPath) if err != nil { return fmt.Errorf("cannot read transaction file: %w", err) } diff --git a/lib/storage/storage.go b/lib/storage/storage.go index 8b68395aa..24970e987 100644 --- a/lib/storage/storage.go +++ b/lib/storage/storage.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "math" "os" "path/filepath" @@ -845,7 +844,7 @@ func (s *Storage) mustLoadNextDayMetricIDs(date uint64) *byDateMetricIDEntry { logger.Infof("nothing to load from %q", path) return e } - src, err := ioutil.ReadFile(path) + src, err := os.ReadFile(path) if err != nil { logger.Panicf("FATAL: cannot read %s: %s", path, err) } @@ -889,7 +888,7 @@ func (s *Storage) mustLoadHourMetricIDs(hour uint64, name string) *hourMetricIDs logger.Infof("nothing to load from %q", path) return hm } - src, err := ioutil.ReadFile(path) + src, err := os.ReadFile(path) if err != nil { logger.Panicf("FATAL: cannot read %s: %s", path, err) } @@ -938,7 +937,7 @@ func (s *Storage) mustSaveNextDayMetricIDs(e *byDateMetricIDEntry) { // Marshal e.v dst = marshalUint64Set(dst, &e.v) - if err := ioutil.WriteFile(path, dst, 0644); err != nil { + if err := os.WriteFile(path, dst, 0644); err != nil { logger.Panicf("FATAL: cannot write %d bytes to %q: %s", len(dst), path, err) } logger.Infof("saved %s to %q in %.3f seconds; entriesCount: %d; sizeBytes: %d", name, path, time.Since(startTime).Seconds(), e.v.Len(), len(dst)) @@ -961,7 +960,7 @@ func (s *Storage) mustSaveHourMetricIDs(hm *hourMetricIDs, name string) { // Marshal hm.m dst = marshalUint64Set(dst, hm.m) - if err := ioutil.WriteFile(path, dst, 0644); err != nil { + if err := os.WriteFile(path, dst, 0644); err != nil { logger.Panicf("FATAL: cannot write %d bytes to %q: %s", len(dst), path, err) } logger.Infof("saved %s to %q in %.3f seconds; entriesCount: %d; sizeBytes: %d", name, path, time.Since(startTime).Seconds(), hm.m.Len(), len(dst)) @@ -1022,7 +1021,7 @@ func mustGetMinTimestampForCompositeIndex(metadataDir string, isEmptyDB bool) in } func loadMinTimestampForCompositeIndex(path string) (int64, error) { - data, err := ioutil.ReadFile(path) + data, err := os.ReadFile(path) if err != nil { return 0, err }