Delete ioutil as it is Deprecated since Go 1.16

Signed-off-by: yanggang <gang.yang@daocloud.io>
This commit is contained in:
yanggang
2023-04-02 21:03:55 +08:00
parent 6298ee7b51
commit 6a7169806f
15 changed files with 42 additions and 45 deletions

View File

@@ -17,7 +17,7 @@ limitations under the License.
package app package app
import ( import (
"io/ioutil" "io"
"os" "os"
"github.com/spf13/pflag" "github.com/spf13/pflag"
@@ -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 // if we are in quiet mode, we want to suppress all status output
// only streams.Out should be written to (program output) // only streams.Out should be written to (program output)
logger = log.NoopLogger{} logger = log.NoopLogger{}
streams.ErrOut = ioutil.Discard streams.ErrOut = io.Discard
} }
// actually run the command // actually run the command
c := kind.NewCommand(logger, streams) c := kind.NewCommand(logger, streams)

View File

@@ -17,7 +17,7 @@ limitations under the License.
package nodeimage package nodeimage
import ( import (
"io/ioutil" "io"
"sigs.k8s.io/kind/pkg/exec" "sigs.k8s.io/kind/pkg/exec"
) )
@@ -49,7 +49,7 @@ func (c *containerdImporter) End() error {
func (c *containerdImporter) Pull(image, platform string) error { func (c *containerdImporter) Pull(image, platform string) error {
return c.containerCmder.Command( return c.containerCmder.Command(
"ctr", "--namespace=k8s.io", "content", "fetch", "--platform="+platform, image, "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 { func (c *containerdImporter) LoadCommand() exec.Cmd {

View File

@@ -23,7 +23,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"strings" "strings"
@@ -58,7 +57,7 @@ func GetArchiveTags(path string) ([]string, error) {
} }
} }
// read and parse the tags // read and parse the tags
b, err := ioutil.ReadAll(tr) b, err := io.ReadAll(tr)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -107,7 +106,7 @@ func EditArchive(reader io.Reader, writer io.Writer, editRepositories func(strin
} else if err != nil { } else if err != nil {
return err return err
} }
b, err := ioutil.ReadAll(tarReader) b, err := io.ReadAll(tarReader)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -17,7 +17,7 @@ limitations under the License.
package kubeconfig package kubeconfig
import ( import (
"io/ioutil" "io"
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
@@ -234,7 +234,7 @@ func TestWriteMerged(t *testing.T) {
func testWriteMergedNormal(t *testing.T) { func testWriteMergedNormal(t *testing.T) {
t.Parallel() t.Parallel()
dir, err := ioutil.TempDir("", "kind-testwritemerged") dir, err := os.MkdirTemp("", "kind-testwritemerged")
if err != nil { if err != nil {
t.Fatalf("Failed to create tempdir: %d", err) t.Fatalf("Failed to create tempdir: %d", err)
} }
@@ -262,7 +262,7 @@ users:
client-key-data: yep client-key-data: yep
` `
existingConfigPath := filepath.Join(dir, "existing-kubeconfig") 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) t.Fatalf("Failed to create existing kubeconfig: %d", err)
} }
@@ -313,7 +313,7 @@ users:
if err != nil { if err != nil {
t.Fatalf("Failed to open merged kubeconfig: %v", err) t.Fatalf("Failed to open merged kubeconfig: %v", err)
} }
contents, err := ioutil.ReadAll(f) contents, err := io.ReadAll(f)
if err != nil { if err != nil {
t.Fatalf("Failed to read merged kubeconfig: %v", err) t.Fatalf("Failed to read merged kubeconfig: %v", err)
} }
@@ -354,7 +354,7 @@ users:
func testWriteMergedBogusConfig(t *testing.T) { func testWriteMergedBogusConfig(t *testing.T) {
t.Parallel() t.Parallel()
dir, err := ioutil.TempDir("", "kind-testwritemerged") dir, err := os.MkdirTemp("", "kind-testwritemerged")
if err != nil { if err != nil {
t.Fatalf("Failed to create tempdir: %d", err) t.Fatalf("Failed to create tempdir: %d", err)
} }
@@ -366,7 +366,7 @@ func testWriteMergedBogusConfig(t *testing.T) {
func testWriteMergedNoExistingFile(t *testing.T) { func testWriteMergedNoExistingFile(t *testing.T) {
t.Parallel() t.Parallel()
dir, err := ioutil.TempDir("", "kind-testwritemerged") dir, err := os.MkdirTemp("", "kind-testwritemerged")
if err != nil { if err != nil {
t.Fatalf("Failed to create tempdir: %d", err) t.Fatalf("Failed to create tempdir: %d", err)
} }
@@ -419,7 +419,7 @@ func testWriteMergedNoExistingFile(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Failed to open merged kubeconfig: %v", err) t.Fatalf("Failed to open merged kubeconfig: %v", err)
} }
contents, err := ioutil.ReadAll(f) contents, err := io.ReadAll(f)
if err != nil { if err != nil {
t.Fatalf("Failed to read merged kubeconfig: %v", err) t.Fatalf("Failed to read merged kubeconfig: %v", err)
} }

View File

@@ -17,7 +17,7 @@ limitations under the License.
package kubeconfig package kubeconfig
import ( import (
"io/ioutil" "io"
"os" "os"
yaml "gopkg.in/yaml.v3" yaml "gopkg.in/yaml.v3"
@@ -70,7 +70,7 @@ func read(configPath string) (*Config, error) {
// otherwise read in and deserialize // otherwise read in and deserialize
cfg := &Config{} cfg := &Config{}
rawExisting, err := ioutil.ReadAll(f) rawExisting, err := io.ReadAll(f)
if err != nil { if err != nil {
return nil, errors.WithStack(err) return nil, errors.WithStack(err)
} }

View File

@@ -17,7 +17,7 @@ limitations under the License.
package kubeconfig package kubeconfig
import ( import (
"io/ioutil" "io"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
@@ -150,7 +150,7 @@ func TestRemoveKIND(t *testing.T) {
func testRemoveKINDTrivial(t *testing.T) { func testRemoveKINDTrivial(t *testing.T) {
t.Parallel() t.Parallel()
dir, err := ioutil.TempDir("", "kind-testremovekind") dir, err := os.MkdirTemp("", "kind-testremovekind")
if err != nil { if err != nil {
t.Fatalf("Failed to create tempdir: %d", err) t.Fatalf("Failed to create tempdir: %d", err)
} }
@@ -178,7 +178,7 @@ users:
client-key-data: yep client-key-data: yep
` `
existingConfigPath := filepath.Join(dir, "existing-kubeconfig") 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) t.Fatalf("Failed to create existing kubeconfig: %d", err)
} }
@@ -192,7 +192,7 @@ users:
if err != nil { if err != nil {
t.Fatalf("Failed to open merged kubeconfig: %v", err) t.Fatalf("Failed to open merged kubeconfig: %v", err)
} }
contents, err := ioutil.ReadAll(f) contents, err := io.ReadAll(f)
if err != nil { if err != nil {
t.Fatalf("Failed to read merged kubeconfig: %v", err) t.Fatalf("Failed to read merged kubeconfig: %v", err)
} }
@@ -206,7 +206,7 @@ preferences: {}
func testRemoveKINDKeepOther(t *testing.T) { func testRemoveKINDKeepOther(t *testing.T) {
// tests removing a kind cluster but keeping another cluster // tests removing a kind cluster but keeping another cluster
t.Parallel() t.Parallel()
dir, err := ioutil.TempDir("", "kind-testremovekind") dir, err := os.MkdirTemp("", "kind-testremovekind")
if err != nil { if err != nil {
t.Fatalf("Failed to create tempdir: %d", err) t.Fatalf("Failed to create tempdir: %d", err)
} }
@@ -246,7 +246,7 @@ users:
client-key-data: yep client-key-data: yep
` `
existingConfigPath := filepath.Join(dir, "existing-kubeconfig") 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) t.Fatalf("Failed to create existing kubeconfig: %d", err)
} }
@@ -260,7 +260,7 @@ users:
if err != nil { if err != nil {
t.Fatalf("Failed to open merged kubeconfig: %v", err) t.Fatalf("Failed to open merged kubeconfig: %v", err)
} }
contents, err := ioutil.ReadAll(f) contents, err := io.ReadAll(f)
if err != nil { if err != nil {
t.Fatalf("Failed to read merged kubeconfig: %v", err) t.Fatalf("Failed to read merged kubeconfig: %v", err)
} }

View File

@@ -17,7 +17,6 @@ limitations under the License.
package kubeconfig package kubeconfig
import ( import (
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
@@ -38,7 +37,7 @@ func write(cfg *Config, configPath string) error {
return errors.Wrap(err, "failed to create directory for KUBECONFIG") 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 errors.Wrap(err, "failed to write KUBECONFIG")
} }
return nil return nil

View File

@@ -17,7 +17,7 @@ limitations under the License.
package kubeconfig package kubeconfig
import ( import (
"io/ioutil" "io"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
@@ -32,7 +32,7 @@ func TestWrite(t *testing.T) {
func testWriteNoExistingFile(t *testing.T) { func testWriteNoExistingFile(t *testing.T) {
t.Parallel() t.Parallel()
dir, err := ioutil.TempDir("", "kind-testwritemerged") dir, err := os.MkdirTemp("", "kind-testwritemerged")
if err != nil { if err != nil {
t.Fatalf("Failed to create tempdir: %d", err) t.Fatalf("Failed to create tempdir: %d", err)
} }
@@ -85,7 +85,7 @@ func testWriteNoExistingFile(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Failed to open merged kubeconfig: %v", err) t.Fatalf("Failed to open merged kubeconfig: %v", err)
} }
contents, err := ioutil.ReadAll(f) contents, err := io.ReadAll(f)
if err != nil { if err != nil {
t.Fatalf("Failed to read merged kubeconfig: %v", err) t.Fatalf("Failed to read merged kubeconfig: %v", err)
} }

View File

@@ -20,7 +20,6 @@ import (
"archive/tar" "archive/tar"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
@@ -66,7 +65,7 @@ func untar(logger log.Logger, r io.Reader, dir string) (err error) {
case err == io.EOF: case err == io.EOF:
// drain the reader, which may have trailing null bytes // drain the reader, which may have trailing null bytes
// we don't want to leave the writer hanging // we don't want to leave the writer hanging
_, err := io.Copy(ioutil.Discard, r) _, err := io.Copy(io.Discard, r)
return err return err
case err != nil: case err != nil:
return errors.Wrapf(err, "tar reading error: %v", err) return errors.Wrapf(err, "tar reading error: %v", err)

View File

@@ -17,7 +17,6 @@ limitations under the License.
package cluster package cluster
import ( import (
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"sort" "sort"
@@ -235,7 +234,7 @@ func (p *Provider) CollectLogs(name, dir string) error {
return errors.Wrap(err, "failed to create logs directory") return errors.Wrap(err, "failed to create logs directory")
} }
// write kind version // write kind version
if err := ioutil.WriteFile( if err := os.WriteFile(
filepath.Join(dir, "kind-version.txt"), filepath.Join(dir, "kind-version.txt"),
[]byte(version.DisplayVersion()), []byte(version.DisplayVersion()),
0666, // match os.Create 0666, // match os.Create

View File

@@ -19,7 +19,6 @@ package cluster
import ( import (
"io" "io"
"io/ioutil"
"time" "time"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@@ -132,7 +131,7 @@ func configOption(rawConfigFlag string, stdin io.Reader) (cluster.CreateOption,
return cluster.CreateWithConfigFile(rawConfigFlag), nil return cluster.CreateWithConfigFile(rawConfigFlag), nil
} }
// otherwise read from stdin // otherwise read from stdin
raw, err := ioutil.ReadAll(stdin) raw, err := io.ReadAll(stdin)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "error reading config from stdin") return nil, errors.Wrap(err, "error reading config from stdin")
} }

View File

@@ -19,7 +19,6 @@ package kind
import ( import (
"io" "io"
"io/ioutil"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@@ -106,7 +105,7 @@ func runE(logger log.Logger, flags *flagpole, command *cobra.Command) error {
if flags.Quiet { if flags.Quiet {
// NOTE: if we are coming from app.Run handling this flag is // NOTE: if we are coming from app.Run handling this flag is
// redundant, however it doesn't hurt, and this may be called directly. // 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)) maybeSetVerbosity(logger, log.Level(flags.Verbosity))
// warn about deprecated flag if used // warn about deprecated flag if used

View File

@@ -21,7 +21,6 @@ package fs
import ( import (
"io" "io"
"io/ioutil"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
@@ -29,10 +28,10 @@ import (
"strings" "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) { func TempDir(dir, prefix string) (name string, err error) {
// create a tempdir as normal // create a tempdir as normal
name, err = ioutil.TempDir(dir, prefix) name, err = os.MkdirTemp(dir, prefix)
if err != nil { if err != nil {
return "", err return "", err
} }
@@ -138,14 +137,18 @@ func copyDir(src, dst string, info os.FileInfo) error {
return err return err
} }
// copy every source dir entry // copy every source dir entry
entries, err := ioutil.ReadDir(src) entries, err := os.ReadDir(src)
if err != nil { if err != nil {
return err return err
} }
for _, entry := range entries { for _, entry := range entries {
entrySrc := filepath.Join(src, entry.Name()) entrySrc := filepath.Join(src, entry.Name())
entryDst := filepath.Join(dst, 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 return err
} }
} }

View File

@@ -18,7 +18,7 @@ package encoding
import ( import (
"bytes" "bytes"
"io/ioutil" "os"
yaml "gopkg.in/yaml.v3" yaml "gopkg.in/yaml.v3"
@@ -42,7 +42,7 @@ func Load(path string) (*config.Cluster, error) {
} }
// read in file // read in file
raw, err := ioutil.ReadFile(path) raw, err := os.ReadFile(path)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "error reading file") return nil, errors.Wrap(err, "error reading file")
} }

View File

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