From 7f915a42205c6a55b92c82e47d7e2ab3bf433a0a Mon Sep 17 00:00:00 2001 From: guoguangwu Date: Wed, 17 Jan 2024 17:51:43 +0800 Subject: [PATCH] chore: remove refs to deprecated io/ioutil Signed-off-by: guoguangwu --- config/os.go | 16 +++++++++-- containermanager/container_manager_linux.go | 4 +-- core/helpers_linux_test.go | 5 ++-- core/security_context.go | 4 +-- libdocker/kube_docker_client.go | 5 ++-- network/cni/cni_test.go | 10 +++---- network/hairpin/hairpin.go | 3 +- network/kubenet/kubenet_linux.go | 4 +-- store/fs.go | 31 +++++++++++++++------ streaming/remotecommand/errorstream.go | 3 +- 10 files changed, 53 insertions(+), 32 deletions(-) diff --git a/config/os.go b/config/os.go index 71cecdbd3..b83deb1d9 100644 --- a/config/os.go +++ b/config/os.go @@ -17,7 +17,7 @@ limitations under the License. package config import ( - "io/ioutil" + "io/fs" "os" "path/filepath" "time" @@ -100,7 +100,19 @@ func (RealOS) Pipe() (r *os.File, w *os.File, err error) { // ReadDir will call ioutil.ReadDir to return the files under the directory. func (RealOS) ReadDir(dirname string) ([]os.FileInfo, error) { - return ioutil.ReadDir(dirname) + entries, err := os.ReadDir(dirname) + if err != nil { + return nil, err + } + infos := make([]fs.FileInfo, 0, len(entries)) + for _, entry := range entries { + info, err := entry.Info() + if err != nil { + return nil, err + } + infos = append(infos, info) + } + return infos, nil } // Glob will call filepath.Glob to return the names of all files matching diff --git a/containermanager/container_manager_linux.go b/containermanager/container_manager_linux.go index 66e8eca7e..6b631c853 100644 --- a/containermanager/container_manager_linux.go +++ b/containermanager/container_manager_linux.go @@ -21,7 +21,7 @@ package containermanager import ( "fmt" - "io/ioutil" + "os" "regexp" "strconv" "time" @@ -142,7 +142,7 @@ func createCgroupManager(name string) (cgroups.Manager, error) { // getMemoryCapacity returns the memory capacity on the machine in bytes. func getMemoryCapacity() (uint64, error) { - out, err := ioutil.ReadFile("/proc/meminfo") + out, err := os.ReadFile("/proc/meminfo") if err != nil { return 0, err } diff --git a/core/helpers_linux_test.go b/core/helpers_linux_test.go index 866d3f85d..9b257d639 100644 --- a/core/helpers_linux_test.go +++ b/core/helpers_linux_test.go @@ -21,7 +21,6 @@ package core import ( "fmt" - "io/ioutil" "os" "path/filepath" "testing" @@ -73,11 +72,11 @@ func TestGetSeccompSecurityOpts(t *testing.T) { } func TestLoadSeccompLocalhostProfiles(t *testing.T) { - tmpdir, err := ioutil.TempDir("", "seccomp-local-profile-test") + tmpdir, err := os.MkdirTemp("", "seccomp-local-profile-test") require.NoError(t, err) defer os.RemoveAll(tmpdir) testProfile := `{"foo": "bar"}` - err = ioutil.WriteFile(filepath.Join(tmpdir, "test"), []byte(testProfile), 0644) + err = os.WriteFile(filepath.Join(tmpdir, "test"), []byte(testProfile), 0644) require.NoError(t, err) tests := []struct { diff --git a/core/security_context.go b/core/security_context.go index 405a10790..37ddfa895 100644 --- a/core/security_context.go +++ b/core/security_context.go @@ -21,7 +21,7 @@ import ( "crypto/md5" "encoding/json" "fmt" - "io/ioutil" + "os" "path/filepath" "strconv" "strings" @@ -274,7 +274,7 @@ func getSeccompDockerOpts(seccomp *runtimeapi.SecurityProfile) ([]DockerOpt, err fname, ) } - file, err := ioutil.ReadFile(filepath.FromSlash(fname)) + file, err := os.ReadFile(filepath.FromSlash(fname)) if err != nil { return nil, fmt.Errorf("cannot load seccomp profile %q: %v", fname, err) } diff --git a/libdocker/kube_docker_client.go b/libdocker/kube_docker_client.go index cd8ac2f5b..8b8a6d492 100644 --- a/libdocker/kube_docker_client.go +++ b/libdocker/kube_docker_client.go @@ -23,7 +23,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "regexp" "sync" "time" @@ -639,10 +638,10 @@ func (d *kubeDockerClient) redirectResponseToOutputStream( resp io.Reader, ) error { if outputStream == nil { - outputStream = ioutil.Discard + outputStream = io.Discard } if errorStream == nil { - errorStream = ioutil.Discard + errorStream = io.Discard } var err error if tty { diff --git a/network/cni/cni_test.go b/network/cni/cni_test.go index bf2ba384c..e7d1cef7c 100644 --- a/network/cni/cni_test.go +++ b/network/cni/cni_test.go @@ -1,3 +1,4 @@ +//go:build linux // +build linux /* @@ -22,7 +23,6 @@ import ( "bytes" "encoding/json" "fmt" - "io/ioutil" "math/rand" "net" "os" @@ -327,8 +327,8 @@ func TestCNIPlugin(t *testing.T) { if err != nil { t.Errorf("Expected nil: %v", err) } - eo, eerr := ioutil.ReadFile(outputEnv) - output, err := ioutil.ReadFile(outputFile) + eo, eerr := os.ReadFile(outputEnv) + output, err := os.ReadFile(outputFile) if err != nil || eerr != nil { t.Errorf("Failed to read output file %s: %v (env %s err %v)", outputFile, err, eo, eerr) } @@ -350,7 +350,7 @@ func TestCNIPlugin(t *testing.T) { IPRanges [][]map[string]interface{} `json:"IPRanges"` } `json:"runtimeConfig"` }{} - inputBytes, inerr := ioutil.ReadFile(inputFile) + inputBytes, inerr := os.ReadFile(inputFile) parseerr := json.Unmarshal(inputBytes, &inputConfig) if inerr != nil || parseerr != nil { t.Errorf( @@ -411,7 +411,7 @@ func TestCNIPlugin(t *testing.T) { if err != nil { t.Errorf("Expected nil: %v", err) } - output, err = ioutil.ReadFile(outputFile) + output, err = os.ReadFile(outputFile) require.NoError(t, err) expectedOutput = "DEL /proc/12345/ns/net podNamespace podName test_infra_container" if string(output) != expectedOutput { diff --git a/network/hairpin/hairpin.go b/network/hairpin/hairpin.go index 157ee7af1..75b733377 100644 --- a/network/hairpin/hairpin.go +++ b/network/hairpin/hairpin.go @@ -18,7 +18,6 @@ package hairpin import ( "fmt" - "io/ioutil" "net" "os" "path" @@ -104,5 +103,5 @@ func setUpInterface(ifName string) error { return nil } hairpinModeFile := path.Join(brportPath, hairpinModeRelativePath) - return ioutil.WriteFile(hairpinModeFile, []byte(hairpinEnable), 0644) + return os.WriteFile(hairpinModeFile, []byte(hairpinEnable), 0644) } diff --git a/network/kubenet/kubenet_linux.go b/network/kubenet/kubenet_linux.go index 9f23ee395..bcf98354e 100644 --- a/network/kubenet/kubenet_linux.go +++ b/network/kubenet/kubenet_linux.go @@ -22,8 +22,8 @@ package kubenet import ( "context" "fmt" - "io/ioutil" "net" + "os" "strings" "sync" "time" @@ -744,7 +744,7 @@ func (plugin *kubenetNetworkPlugin) checkRequiredCNIPlugins() bool { // checkRequiredCNIPluginsInOneDir returns true if all required cni plugins are placed in dir func (plugin *kubenetNetworkPlugin) checkRequiredCNIPluginsInOneDir(dir string) bool { - files, err := ioutil.ReadDir(dir) + files, err := os.ReadDir(dir) if err != nil { return false } diff --git a/store/fs.go b/store/fs.go index 66b955c47..e41145550 100644 --- a/store/fs.go +++ b/store/fs.go @@ -17,7 +17,7 @@ limitations under the License. package store import ( - "io/ioutil" + "io/fs" "os" "path/filepath" "time" @@ -67,28 +67,41 @@ func (DefaultFs) Remove(name string) error { return os.Remove(name) } -// ReadFile via ioutil.ReadFile +// ReadFile via os.ReadFile func (DefaultFs) ReadFile(filename string) ([]byte, error) { - return ioutil.ReadFile(filename) + return os.ReadFile(filename) } -// TempDir via ioutil.TempDir +// TempDir via os.MkdirTemp func (DefaultFs) TempDir(dir, prefix string) (string, error) { - return ioutil.TempDir(dir, prefix) + return os.MkdirTemp(dir, prefix) } -// TempFile via ioutil.TempFile +// TempFile via os.CreateTemp func (DefaultFs) TempFile(dir, prefix string) (File, error) { - file, err := ioutil.TempFile(dir, prefix) + file, err := os.CreateTemp(dir, prefix) if err != nil { return nil, err } return &defaultFile{file}, nil } -// ReadDir via ioutil.ReadDir +// ReadDir via os.ReadDir func (DefaultFs) ReadDir(dirname string) ([]os.FileInfo, error) { - return ioutil.ReadDir(dirname) + entries, err := os.ReadDir(dirname) + if err != nil { + return nil, err + } + infos := make([]fs.FileInfo, 0, len(entries)) + for _, entry := range entries { + info, err := entry.Info() + if err != nil { + return nil, err + } + infos = append(infos, info) + } + + return infos, nil } // Walk via filepath.Walk diff --git a/streaming/remotecommand/errorstream.go b/streaming/remotecommand/errorstream.go index 5f64df633..6653ecc3a 100644 --- a/streaming/remotecommand/errorstream.go +++ b/streaming/remotecommand/errorstream.go @@ -19,7 +19,6 @@ package remotecommand import ( "fmt" "io" - "io/ioutil" "k8s.io/apimachinery/pkg/util/runtime" ) @@ -39,7 +38,7 @@ func watchErrorStream(errorStream io.Reader, d errorStreamDecoder) chan error { go func() { defer runtime.HandleCrash() - message, err := ioutil.ReadAll(errorStream) + message, err := io.ReadAll(errorStream) switch { case err != nil && err != io.EOF: errorChan <- fmt.Errorf("error reading from error stream: %s", err)