Skip to content

Commit

Permalink
chore: remove refs to deprecated io/ioutil
Browse files Browse the repository at this point in the history
Signed-off-by: guoguangwu <[email protected]>
  • Loading branch information
testwill committed Jan 17, 2024
1 parent 77b8604 commit 7f915a4
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 32 deletions.
16 changes: 14 additions & 2 deletions config/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package config

import (
"io/ioutil"
"io/fs"
"os"
"path/filepath"
"time"
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions containermanager/container_manager_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ package containermanager

import (
"fmt"
"io/ioutil"
"os"
"regexp"
"strconv"
"time"
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 2 additions & 3 deletions core/helpers_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package core

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions core/security_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"crypto/md5"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
Expand Down Expand Up @@ -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)
}
Expand Down
5 changes: 2 additions & 3 deletions libdocker/kube_docker_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"regexp"
"sync"
"time"
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions network/cni/cni_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build linux
// +build linux

/*
Expand All @@ -22,7 +23,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"net"
"os"
Expand Down Expand Up @@ -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)
}
Expand All @@ -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(
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions network/hairpin/hairpin.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package hairpin

import (
"fmt"
"io/ioutil"
"net"
"os"
"path"
Expand Down Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions network/kubenet/kubenet_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ package kubenet
import (
"context"
"fmt"
"io/ioutil"
"net"
"os"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -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
}
Expand Down
31 changes: 22 additions & 9 deletions store/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package store

import (
"io/ioutil"
"io/fs"
"os"
"path/filepath"
"time"
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions streaming/remotecommand/errorstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package remotecommand
import (
"fmt"
"io"
"io/ioutil"

"k8s.io/apimachinery/pkg/util/runtime"
)
Expand All @@ -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)
Expand Down

0 comments on commit 7f915a4

Please sign in to comment.