Skip to content

Commit

Permalink
Replace deprecated io/ioutil with io and os packages (grafana#7212
Browse files Browse the repository at this point in the history
)

The `io/ioutil` package has been deprecated in Go 1.16 (See
https://pkg.go.dev/io/ioutil). This PR replaces the existing `io/ioutil`
functions with their new definitions in `io` and `os` packages.

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
  • Loading branch information
Juneezee authored and lxwzy committed Nov 7, 2022
1 parent d85784b commit 7b62146
Show file tree
Hide file tree
Showing 97 changed files with 302 additions and 338 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
* [6415](https://github.com/grafana/loki/pull/6415) **salvacorts**: Evenly spread queriers across kubernetes nodes.
* [6349](https://github.com/grafana/loki/pull/6349) **simonswine**: Update the default HTTP listen port from 80 to 3100. Make sure to configure the port explicitly if you are using port 80.
* [6835](https://github.com/grafana/loki/pull/6835) **DylanGuedes**: Add new per-tenant query timeout configuration and remove engine query timeout.
* [7212](https://github.com/grafana/loki/pull/7212) **Juneezee**: Replaces deprecated `io/ioutil` with `io` and `os`.

#### Promtail

Expand Down
3 changes: 1 addition & 2 deletions clients/cmd/docker-driver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"bytes"
"fmt"
"io/ioutil"
"net/url"
"os"
"strconv"
Expand Down Expand Up @@ -385,7 +384,7 @@ func parseBoolean(key string, logCtx logger.Info, defaultValue bool) (bool, erro

// loadConfig read YAML-formatted config from filename into cfg.
func loadConfig(filename string, cfg interface{}) error {
buf, err := ioutil.ReadFile(filename)
buf, err := os.ReadFile(filename)
if err != nil {
return errors.Wrap(err, "Error reading config file")
}
Expand Down
3 changes: 1 addition & 2 deletions clients/cmd/docker-driver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"
"io/ioutil"
"os"
"reflect"
"testing"
Expand Down Expand Up @@ -82,7 +81,7 @@ var pipeline = PipelineConfig{
}

func Test_parsePipeline(t *testing.T) {
f, err := ioutil.TempFile("/tmp", "Test_parsePipeline")
f, err := os.CreateTemp("/tmp", "Test_parsePipeline")
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions clients/cmd/fluent-bit/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -200,7 +200,7 @@ func parseConfig(cfg ConfigGetter) (*config, error) {

labelMapPath := cfg.Get("LabelMapPath")
if labelMapPath != "" {
content, err := ioutil.ReadFile(labelMapPath)
content, err := os.ReadFile(labelMapPath)
if err != nil {
return nil, fmt.Errorf("failed to open LabelMap file: %s", err)
}
Expand Down
3 changes: 1 addition & 2 deletions clients/cmd/fluent-bit/config_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"io/ioutil"
"net/url"
"os"
"reflect"
Expand Down Expand Up @@ -229,7 +228,7 @@ func mustParseDuration(u string) time.Duration {
}

func createTempLabelMap(t *testing.T) string {
file, err := ioutil.TempFile("", "labelmap")
file, err := os.CreateTemp("", "labelmap")
if err != nil {
t.Fatal(err)
}
Expand Down
3 changes: 1 addition & 2 deletions clients/pkg/promtail/positions/positions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package positions
import (
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -215,7 +214,7 @@ func (p *positions) cleanup() {

func readPositionsFile(cfg Config, logger log.Logger) (map[string]string, error) {
cleanfn := filepath.Clean(cfg.PositionsFile)
buf, err := ioutil.ReadFile(cleanfn)
buf, err := os.ReadFile(cleanfn)
if err != nil {
if os.IsNotExist(err) {
return map[string]string{}, nil
Expand Down
13 changes: 6 additions & 7 deletions clients/pkg/promtail/positions/positions_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package positions

import (
"io/ioutil"
"os"
"strings"
"testing"
Expand All @@ -16,7 +15,7 @@ import (
func tempFilename(t *testing.T) string {
t.Helper()

temp, err := ioutil.TempFile("", "positions")
temp, err := os.CreateTemp("", "positions")
if err != nil {
t.Fatal("tempFilename:", err)
}
Expand All @@ -43,7 +42,7 @@ func TestReadPositionsOK(t *testing.T) {
yaml := []byte(`positions:
/tmp/random.log: "17623"
`)
err := ioutil.WriteFile(temp, yaml, 0644)
err := os.WriteFile(temp, yaml, 0644)
if err != nil {
t.Fatal(err)
}
Expand All @@ -63,7 +62,7 @@ func TestReadPositionsEmptyFile(t *testing.T) {
}()

yaml := []byte(``)
err := ioutil.WriteFile(temp, yaml, 0644)
err := os.WriteFile(temp, yaml, 0644)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -104,7 +103,7 @@ func TestReadPositionsFromBadYaml(t *testing.T) {
badYaml := []byte(`positions:
/tmp/random.log: "176
`)
err := ioutil.WriteFile(temp, badYaml, 0644)
err := os.WriteFile(temp, badYaml, 0644)
if err != nil {
t.Fatal(err)
}
Expand All @@ -126,7 +125,7 @@ func TestReadPositionsFromBadYamlIgnoreCorruption(t *testing.T) {
badYaml := []byte(`positions:
/tmp/random.log: "176
`)
err := ioutil.WriteFile(temp, badYaml, 0644)
err := os.WriteFile(temp, badYaml, 0644)
if err != nil {
t.Fatal(err)
}
Expand All @@ -148,7 +147,7 @@ func Test_ReadOnly(t *testing.T) {
yaml := []byte(`positions:
/tmp/random.log: "17623"
`)
err := ioutil.WriteFile(temp, yaml, 0644)
err := os.WriteFile(temp, yaml, 0644)
if err != nil {
t.Fatal(err)
}
Expand Down
3 changes: 1 addition & 2 deletions clients/pkg/promtail/positions/write_positions_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package positions

import (
"io/ioutil"
"os"
"path/filepath"

Expand All @@ -24,7 +23,7 @@ func writePositionFile(filename string, positions map[string]string) error {
target := filepath.Clean(filename)
temp := target + "-new"

err = ioutil.WriteFile(temp, buf, os.FileMode(positionFileMode))
err = os.WriteFile(temp, buf, os.FileMode(positionFileMode))
if err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions clients/pkg/promtail/promtail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"math"
"math/rand"
"net"
Expand Down Expand Up @@ -510,7 +509,7 @@ func getPromMetrics(t *testing.T, httpListenAddr net.Addr) ([]byte, string) {
t.Fatal("Received a non 200 status code from /metrics endpoint", resp.StatusCode)
}

b, err := ioutil.ReadAll(resp.Body)
b, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal("Error reading response body from /metrics endpoint", err)
}
Expand Down Expand Up @@ -656,7 +655,7 @@ func randName() string {
}

func Test_DryRun(t *testing.T) {
f, err := ioutil.TempFile("/tmp", "Test_DryRun")
f, err := os.CreateTemp("/tmp", "Test_DryRun")
require.NoError(t, err)
defer os.Remove(f.Name())

Expand Down
3 changes: 1 addition & 2 deletions clients/pkg/promtail/server/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package server
import (
"context"
"io"
"io/ioutil"
"net/http"
"net/url"
"path"
Expand Down Expand Up @@ -77,7 +76,7 @@ func getTemplate(name string) (string, error) {
defer func() {
_ = f.Close()
}()
b, err := ioutil.ReadAll(f)
b, err := io.ReadAll(f)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions clients/pkg/promtail/targets/journal/journaltarget.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package journal
import (
"fmt"
"io"
"io/ioutil"
"strings"
"syscall"
"time"
Expand Down Expand Up @@ -201,7 +200,7 @@ func journalTargetWithReader(

go func() {
for {
err := t.r.Follow(until, ioutil.Discard)
err := t.r.Follow(until, io.Discard)
if err != nil {
level.Error(t.logger).Log("msg", "received error during sdjournal follow", "err", err.Error())

Expand Down
3 changes: 1 addition & 2 deletions clients/pkg/promtail/targets/syslog/syslogtarget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"crypto/x509"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"testing"
Expand Down Expand Up @@ -656,7 +655,7 @@ func testSyslogTargetWithTLS(t *testing.T, fmtFunc formatFunc) {
}

func createTempFile(data []byte) (*os.File, error) {
tmpFile, err := ioutil.TempFile("", "")
tmpFile, err := os.CreateTemp("", "")
if err != nil {
return nil, fmt.Errorf("failed to create temporary file: %s", err)
}
Expand Down
4 changes: 2 additions & 2 deletions clients/pkg/promtail/targets/syslog/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"crypto/x509"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -207,7 +207,7 @@ func newTLSConfig(certFile string, keyFile string, caFile string) (*tls.Config,
}

if caFile != "" {
caCert, err := ioutil.ReadFile(caFile)
caCert, err := os.ReadFile(caFile)
if err != nil {
return nil, fmt.Errorf("unable to load client CA certificate: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions clients/pkg/promtail/targets/windows/bookmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package windows

import (
"io/ioutil"
"io"
"os"

"github.com/spf13/afero"
Expand Down Expand Up @@ -52,7 +52,7 @@ func newBookMark(path string) (*bookMark, error) {
if err != nil {
return nil, err
}
fileContent, err := ioutil.ReadAll(file)
fileContent, err := io.ReadAll(file)
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/chunks-inspect/loki.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"hash/crc32"
"io"
"io/ioutil"

"github.com/golang/snappy"
"github.com/klauspost/compress/flate"
Expand Down Expand Up @@ -187,7 +186,7 @@ func parseLokiBlock(compression Encoding, data []byte) ([]byte, []LokiEntry, err
return nil, nil, err
}

decompressed, err := ioutil.ReadAll(r)
decompressed, err := io.ReadAll(r)
origDecompressed := decompressed
if err != nil {
return nil, nil, err
Expand Down
3 changes: 1 addition & 2 deletions cmd/chunks-inspect/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"crypto/sha256"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
Expand Down Expand Up @@ -122,7 +121,7 @@ func printFile(filename string, blockDetails, printLines, storeBlocks bool) {
}

func writeBlockToFile(data []byte, blockIndex int, filename string) {
err := ioutil.WriteFile(filename, data, 0644)
err := os.WriteFile(filename, data, 0644)
if err != nil {
log.Println("Failed to store block", blockIndex, "to file", filename, "due to error:", err)
} else {
Expand Down
5 changes: 2 additions & 3 deletions operator/cmd/loki-broker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"path"
"strings"
Expand Down Expand Up @@ -117,7 +116,7 @@ func main() {

cfg.validateFlags(logger)

b, err := ioutil.ReadFile(cfg.crFilepath)
b, err := os.ReadFile(cfg.crFilepath)
if err != nil {
logger.Info("failed to read custom resource file", "path", cfg.crFilepath)
os.Exit(1)
Expand Down Expand Up @@ -169,7 +168,7 @@ func main() {
if cfg.writeToDir != "" {
basename := fmt.Sprintf("%s-%s.yaml", o.GetObjectKind().GroupVersionKind().Kind, o.GetName())
fname := strings.ToLower(path.Join(cfg.writeToDir, basename))
if err := ioutil.WriteFile(fname, b, 0o644); err != nil {
if err := os.WriteFile(fname, b, 0o644); err != nil {
logger.Error(err, "failed to write file to directory", "path", fname)
os.Exit(1)
}
Expand Down
4 changes: 2 additions & 2 deletions operator/controllers/loki/lokistack_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package controllers

import (
"flag"
"io/ioutil"
"io"
"os"
"testing"

Expand Down Expand Up @@ -38,7 +38,7 @@ func TestMain(m *testing.M) {
if testing.Verbose() {
logger = log.NewLogger("testing", log.WithVerbosity(5))
} else {
logger = log.NewLogger("testing", log.WithOutput(ioutil.Discard))
logger = log.NewLogger("testing", log.WithOutput(io.Discard))
}

// Register the clientgo and CRD schemes
Expand Down
4 changes: 2 additions & 2 deletions operator/internal/handlers/lokistack_create_or_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"errors"
"flag"
"io/ioutil"
"io"
"os"
"testing"

Expand Down Expand Up @@ -94,7 +94,7 @@ func TestMain(m *testing.M) {
if testing.Verbose() {
logger = log.NewLogger("testing", log.WithVerbosity(5))
} else {
logger = log.NewLogger("testing", log.WithOutput(ioutil.Discard))
logger = log.NewLogger("testing", log.WithOutput(io.Discard))
}

// Register the clientgo and CRD schemes
Expand Down
Loading

0 comments on commit 7b62146

Please sign in to comment.