Skip to content

Commit

Permalink
Delete ioutil as it is Deprecated since Go 1.16
Browse files Browse the repository at this point in the history
Signed-off-by: yanggang <[email protected]>
  • Loading branch information
yanggang committed Apr 18, 2023
1 parent 6298ee7 commit 6a71698
Show file tree
Hide file tree
Showing 15 changed files with 42 additions and 45 deletions.
4 changes: 2 additions & 2 deletions cmd/kind/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package app

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

"github.com/spf13/pflag"
Expand Down Expand Up @@ -45,7 +45,7 @@ func Run(logger log.Logger, streams cmd.IOStreams, args []string) error {
// if we are in quiet mode, we want to suppress all status output
// only streams.Out should be written to (program output)
logger = log.NoopLogger{}
streams.ErrOut = ioutil.Discard
streams.ErrOut = io.Discard
}
// actually run the command
c := kind.NewCommand(logger, streams)
Expand Down
4 changes: 2 additions & 2 deletions pkg/build/nodeimage/imageimporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package nodeimage

import (
"io/ioutil"
"io"

"sigs.k8s.io/kind/pkg/exec"
)
Expand Down Expand Up @@ -49,7 +49,7 @@ func (c *containerdImporter) End() error {
func (c *containerdImporter) Pull(image, platform string) error {
return c.containerCmder.Command(
"ctr", "--namespace=k8s.io", "content", "fetch", "--platform="+platform, image,
).SetStdout(ioutil.Discard).SetStderr(ioutil.Discard).Run()
).SetStdout(io.Discard).SetStderr(io.Discard).Run()
}

func (c *containerdImporter) LoadCommand() exec.Cmd {
Expand Down
5 changes: 2 additions & 3 deletions pkg/build/nodeimage/internal/container/docker/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"strings"

Expand Down Expand Up @@ -58,7 +57,7 @@ func GetArchiveTags(path string) ([]string, error) {
}
}
// read and parse the tags
b, err := ioutil.ReadAll(tr)
b, err := io.ReadAll(tr)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -107,7 +106,7 @@ func EditArchive(reader io.Reader, writer io.Writer, editRepositories func(strin
} else if err != nil {
return err
}
b, err := ioutil.ReadAll(tarReader)
b, err := io.ReadAll(tarReader)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package kubeconfig

import (
"io/ioutil"
"io"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -234,7 +234,7 @@ func TestWriteMerged(t *testing.T) {

func testWriteMergedNormal(t *testing.T) {
t.Parallel()
dir, err := ioutil.TempDir("", "kind-testwritemerged")
dir, err := os.MkdirTemp("", "kind-testwritemerged")
if err != nil {
t.Fatalf("Failed to create tempdir: %d", err)
}
Expand Down Expand Up @@ -262,7 +262,7 @@ users:
client-key-data: yep
`
existingConfigPath := filepath.Join(dir, "existing-kubeconfig")
if err := ioutil.WriteFile(existingConfigPath, []byte(existingConfig), os.ModePerm); err != nil {
if err := os.WriteFile(existingConfigPath, []byte(existingConfig), os.ModePerm); err != nil {
t.Fatalf("Failed to create existing kubeconfig: %d", err)
}

Expand Down Expand Up @@ -313,7 +313,7 @@ users:
if err != nil {
t.Fatalf("Failed to open merged kubeconfig: %v", err)
}
contents, err := ioutil.ReadAll(f)
contents, err := io.ReadAll(f)
if err != nil {
t.Fatalf("Failed to read merged kubeconfig: %v", err)
}
Expand Down Expand Up @@ -354,7 +354,7 @@ users:

func testWriteMergedBogusConfig(t *testing.T) {
t.Parallel()
dir, err := ioutil.TempDir("", "kind-testwritemerged")
dir, err := os.MkdirTemp("", "kind-testwritemerged")
if err != nil {
t.Fatalf("Failed to create tempdir: %d", err)
}
Expand All @@ -366,7 +366,7 @@ func testWriteMergedBogusConfig(t *testing.T) {

func testWriteMergedNoExistingFile(t *testing.T) {
t.Parallel()
dir, err := ioutil.TempDir("", "kind-testwritemerged")
dir, err := os.MkdirTemp("", "kind-testwritemerged")
if err != nil {
t.Fatalf("Failed to create tempdir: %d", err)
}
Expand Down Expand Up @@ -419,7 +419,7 @@ func testWriteMergedNoExistingFile(t *testing.T) {
if err != nil {
t.Fatalf("Failed to open merged kubeconfig: %v", err)
}
contents, err := ioutil.ReadAll(f)
contents, err := io.ReadAll(f)
if err != nil {
t.Fatalf("Failed to read merged kubeconfig: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/cluster/internal/kubeconfig/internal/kubeconfig/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package kubeconfig

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

yaml "gopkg.in/yaml.v3"
Expand Down Expand Up @@ -70,7 +70,7 @@ func read(configPath string) (*Config, error) {

// otherwise read in and deserialize
cfg := &Config{}
rawExisting, err := ioutil.ReadAll(f)
rawExisting, err := io.ReadAll(f)
if err != nil {
return nil, errors.WithStack(err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package kubeconfig

import (
"io/ioutil"
"io"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -150,7 +150,7 @@ func TestRemoveKIND(t *testing.T) {

func testRemoveKINDTrivial(t *testing.T) {
t.Parallel()
dir, err := ioutil.TempDir("", "kind-testremovekind")
dir, err := os.MkdirTemp("", "kind-testremovekind")
if err != nil {
t.Fatalf("Failed to create tempdir: %d", err)
}
Expand Down Expand Up @@ -178,7 +178,7 @@ users:
client-key-data: yep
`
existingConfigPath := filepath.Join(dir, "existing-kubeconfig")
if err := ioutil.WriteFile(existingConfigPath, []byte(existingConfig), os.ModePerm); err != nil {
if err := os.WriteFile(existingConfigPath, []byte(existingConfig), os.ModePerm); err != nil {
t.Fatalf("Failed to create existing kubeconfig: %d", err)
}

Expand All @@ -192,7 +192,7 @@ users:
if err != nil {
t.Fatalf("Failed to open merged kubeconfig: %v", err)
}
contents, err := ioutil.ReadAll(f)
contents, err := io.ReadAll(f)
if err != nil {
t.Fatalf("Failed to read merged kubeconfig: %v", err)
}
Expand All @@ -206,7 +206,7 @@ preferences: {}
func testRemoveKINDKeepOther(t *testing.T) {
// tests removing a kind cluster but keeping another cluster
t.Parallel()
dir, err := ioutil.TempDir("", "kind-testremovekind")
dir, err := os.MkdirTemp("", "kind-testremovekind")
if err != nil {
t.Fatalf("Failed to create tempdir: %d", err)
}
Expand Down Expand Up @@ -246,7 +246,7 @@ users:
client-key-data: yep
`
existingConfigPath := filepath.Join(dir, "existing-kubeconfig")
if err := ioutil.WriteFile(existingConfigPath, []byte(existingConfig), os.ModePerm); err != nil {
if err := os.WriteFile(existingConfigPath, []byte(existingConfig), os.ModePerm); err != nil {
t.Fatalf("Failed to create existing kubeconfig: %d", err)
}

Expand All @@ -260,7 +260,7 @@ users:
if err != nil {
t.Fatalf("Failed to open merged kubeconfig: %v", err)
}
contents, err := ioutil.ReadAll(f)
contents, err := io.ReadAll(f)
if err != nil {
t.Fatalf("Failed to read merged kubeconfig: %v", err)
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/cluster/internal/kubeconfig/internal/kubeconfig/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package kubeconfig

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

Expand All @@ -38,7 +37,7 @@ func write(cfg *Config, configPath string) error {
return errors.Wrap(err, "failed to create directory for KUBECONFIG")
}
}
if err := ioutil.WriteFile(configPath, encoded, 0600); err != nil {
if err := os.WriteFile(configPath, encoded, 0600); err != nil {
return errors.Wrap(err, "failed to write KUBECONFIG")
}
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package kubeconfig

import (
"io/ioutil"
"io"
"os"
"path/filepath"
"testing"
Expand All @@ -32,7 +32,7 @@ func TestWrite(t *testing.T) {

func testWriteNoExistingFile(t *testing.T) {
t.Parallel()
dir, err := ioutil.TempDir("", "kind-testwritemerged")
dir, err := os.MkdirTemp("", "kind-testwritemerged")
if err != nil {
t.Fatalf("Failed to create tempdir: %d", err)
}
Expand Down Expand Up @@ -85,7 +85,7 @@ func testWriteNoExistingFile(t *testing.T) {
if err != nil {
t.Fatalf("Failed to open merged kubeconfig: %v", err)
}
contents, err := ioutil.ReadAll(f)
contents, err := io.ReadAll(f)
if err != nil {
t.Fatalf("Failed to read merged kubeconfig: %v", err)
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/cluster/internal/logs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"archive/tar"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -66,7 +65,7 @@ func untar(logger log.Logger, r io.Reader, dir string) (err error) {
case err == io.EOF:
// drain the reader, which may have trailing null bytes
// we don't want to leave the writer hanging
_, err := io.Copy(ioutil.Discard, r)
_, err := io.Copy(io.Discard, r)
return err
case err != nil:
return errors.Wrapf(err, "tar reading error: %v", err)
Expand Down
3 changes: 1 addition & 2 deletions pkg/cluster/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package cluster

import (
"io/ioutil"
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -235,7 +234,7 @@ func (p *Provider) CollectLogs(name, dir string) error {
return errors.Wrap(err, "failed to create logs directory")
}
// write kind version
if err := ioutil.WriteFile(
if err := os.WriteFile(
filepath.Join(dir, "kind-version.txt"),
[]byte(version.DisplayVersion()),
0666, // match os.Create
Expand Down
3 changes: 1 addition & 2 deletions pkg/cmd/kind/create/cluster/createcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package cluster

import (
"io"
"io/ioutil"
"time"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -132,7 +131,7 @@ func configOption(rawConfigFlag string, stdin io.Reader) (cluster.CreateOption,
return cluster.CreateWithConfigFile(rawConfigFlag), nil
}
// otherwise read from stdin
raw, err := ioutil.ReadAll(stdin)
raw, err := io.ReadAll(stdin)
if err != nil {
return nil, errors.Wrap(err, "error reading config from stdin")
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/cmd/kind/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package kind

import (
"io"
"io/ioutil"

"github.com/spf13/cobra"

Expand Down Expand Up @@ -106,7 +105,7 @@ func runE(logger log.Logger, flags *flagpole, command *cobra.Command) error {
if flags.Quiet {
// NOTE: if we are coming from app.Run handling this flag is
// redundant, however it doesn't hurt, and this may be called directly.
maybeSetWriter(logger, ioutil.Discard)
maybeSetWriter(logger, io.Discard)
}
maybeSetVerbosity(logger, log.Level(flags.Verbosity))
// warn about deprecated flag if used
Expand Down
13 changes: 8 additions & 5 deletions pkg/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,17 @@ package fs

import (
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"runtime"
"strings"
)

// TempDir is like ioutil.TempDir, but more docker friendly
// TempDir is like os.MkdirTemp, but more docker friendly
func TempDir(dir, prefix string) (name string, err error) {
// create a tempdir as normal
name, err = ioutil.TempDir(dir, prefix)
name, err = os.MkdirTemp(dir, prefix)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -138,14 +137,18 @@ func copyDir(src, dst string, info os.FileInfo) error {
return err
}
// copy every source dir entry
entries, err := ioutil.ReadDir(src)
entries, err := os.ReadDir(src)
if err != nil {
return err
}
for _, entry := range entries {
entrySrc := filepath.Join(src, entry.Name())
entryDst := filepath.Join(dst, entry.Name())
if err := copy(entrySrc, entryDst, entry); err != nil {
fileInfo, err := entry.Info()
if err != nil {
return err
}
if err := copy(entrySrc, entryDst, fileInfo); err != nil {
return err
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/internal/apis/config/encoding/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package encoding

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

yaml "gopkg.in/yaml.v3"

Expand All @@ -42,7 +42,7 @@ func Load(path string) (*config.Cluster, error) {
}

// read in file
raw, err := ioutil.ReadFile(path)
raw, err := os.ReadFile(path)
if err != nil {
return nil, errors.Wrap(err, "error reading file")
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/internal/env/term_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package env
import (
"bytes"
"io"
"io/ioutil"
"os"
"testing"

"sigs.k8s.io/kind/pkg/internal/assert"
Expand All @@ -36,7 +36,7 @@ func TestIsTerminal(t *testing.T) {
t.Fatalf("IsTerminal should be false for bytes.Buffer")
}
// test a file
f, err := ioutil.TempFile("", "kind-isterminal")
f, err := os.CreateTemp("", "kind-isterminal")
if err != nil {
t.Fatalf("Failed to create tempfile %v", err)
}
Expand Down

0 comments on commit 6a71698

Please sign in to comment.