Skip to content

Commit 85d9817

Browse files
committed
chore: remove refs to deprecated io/ioutil
Signed-off-by: guoguangwu <[email protected]>
1 parent 3248313 commit 85d9817

File tree

9 files changed

+39
-30
lines changed

9 files changed

+39
-30
lines changed

containermanager/container_manager_linux.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ package containermanager
2121

2222
import (
2323
"fmt"
24-
"io/ioutil"
24+
"os"
2525
"regexp"
2626
"strconv"
2727
"time"
@@ -142,7 +142,7 @@ func createCgroupManager(name string) (cgroups.Manager, error) {
142142

143143
// getMemoryCapacity returns the memory capacity on the machine in bytes.
144144
func getMemoryCapacity() (uint64, error) {
145-
out, err := ioutil.ReadFile("/proc/meminfo")
145+
out, err := os.ReadFile("/proc/meminfo")
146146
if err != nil {
147147
return 0, err
148148
}

core/helpers_linux_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ package core
2121

2222
import (
2323
"fmt"
24-
"io/ioutil"
2524
"os"
2625
"path/filepath"
2726
"testing"
@@ -73,11 +72,11 @@ func TestGetSeccompSecurityOpts(t *testing.T) {
7372
}
7473

7574
func TestLoadSeccompLocalhostProfiles(t *testing.T) {
76-
tmpdir, err := ioutil.TempDir("", "seccomp-local-profile-test")
75+
tmpdir, err := os.MkdirTemp("", "seccomp-local-profile-test")
7776
require.NoError(t, err)
7877
defer os.RemoveAll(tmpdir)
7978
testProfile := `{"foo": "bar"}`
80-
err = ioutil.WriteFile(filepath.Join(tmpdir, "test"), []byte(testProfile), 0644)
79+
err = os.WriteFile(filepath.Join(tmpdir, "test"), []byte(testProfile), 0644)
8180
require.NoError(t, err)
8281

8382
tests := []struct {

core/security_context.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"crypto/md5"
2222
"encoding/json"
2323
"fmt"
24-
"io/ioutil"
24+
"os"
2525
"path/filepath"
2626
"strconv"
2727
"strings"
@@ -274,7 +274,7 @@ func getSeccompDockerOpts(seccomp *runtimeapi.SecurityProfile) ([]DockerOpt, err
274274
fname,
275275
)
276276
}
277-
file, err := ioutil.ReadFile(filepath.FromSlash(fname))
277+
file, err := os.ReadFile(filepath.FromSlash(fname))
278278
if err != nil {
279279
return nil, fmt.Errorf("cannot load seccomp profile %q: %v", fname, err)
280280
}

libdocker/kube_docker_client.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"encoding/json"
2424
"fmt"
2525
"io"
26-
"io/ioutil"
2726
"regexp"
2827
"sync"
2928
"time"
@@ -642,10 +641,10 @@ func (d *kubeDockerClient) redirectResponseToOutputStream(
642641
resp io.Reader,
643642
) error {
644643
if outputStream == nil {
645-
outputStream = ioutil.Discard
644+
outputStream = io.Discard
646645
}
647646
if errorStream == nil {
648-
errorStream = ioutil.Discard
647+
errorStream = io.Discard
649648
}
650649
var err error
651650
if tty {

network/cni/cni_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build linux
12
// +build linux
23

34
/*
@@ -22,7 +23,6 @@ import (
2223
"bytes"
2324
"encoding/json"
2425
"fmt"
25-
"io/ioutil"
2626
"math/rand"
2727
"net"
2828
"os"
@@ -327,8 +327,8 @@ func TestCNIPlugin(t *testing.T) {
327327
if err != nil {
328328
t.Errorf("Expected nil: %v", err)
329329
}
330-
eo, eerr := ioutil.ReadFile(outputEnv)
331-
output, err := ioutil.ReadFile(outputFile)
330+
eo, eerr := os.ReadFile(outputEnv)
331+
output, err := os.ReadFile(outputFile)
332332
if err != nil || eerr != nil {
333333
t.Errorf("Failed to read output file %s: %v (env %s err %v)", outputFile, err, eo, eerr)
334334
}
@@ -350,7 +350,7 @@ func TestCNIPlugin(t *testing.T) {
350350
IPRanges [][]map[string]interface{} `json:"IPRanges"`
351351
} `json:"runtimeConfig"`
352352
}{}
353-
inputBytes, inerr := ioutil.ReadFile(inputFile)
353+
inputBytes, inerr := os.ReadFile(inputFile)
354354
parseerr := json.Unmarshal(inputBytes, &inputConfig)
355355
if inerr != nil || parseerr != nil {
356356
t.Errorf(
@@ -411,7 +411,7 @@ func TestCNIPlugin(t *testing.T) {
411411
if err != nil {
412412
t.Errorf("Expected nil: %v", err)
413413
}
414-
output, err = ioutil.ReadFile(outputFile)
414+
output, err = os.ReadFile(outputFile)
415415
require.NoError(t, err)
416416
expectedOutput = "DEL /proc/12345/ns/net podNamespace podName test_infra_container"
417417
if string(output) != expectedOutput {

network/hairpin/hairpin.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package hairpin
1818

1919
import (
2020
"fmt"
21-
"io/ioutil"
2221
"net"
2322
"os"
2423
"path"
@@ -104,5 +103,5 @@ func setUpInterface(ifName string) error {
104103
return nil
105104
}
106105
hairpinModeFile := path.Join(brportPath, hairpinModeRelativePath)
107-
return ioutil.WriteFile(hairpinModeFile, []byte(hairpinEnable), 0644)
106+
return os.WriteFile(hairpinModeFile, []byte(hairpinEnable), 0644)
108107
}

network/kubenet/kubenet_linux.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ package kubenet
2222
import (
2323
"context"
2424
"fmt"
25-
"io/ioutil"
2625
"net"
26+
"os"
2727
"strings"
2828
"sync"
2929
"time"
@@ -744,7 +744,7 @@ func (plugin *kubenetNetworkPlugin) checkRequiredCNIPlugins() bool {
744744

745745
// checkRequiredCNIPluginsInOneDir returns true if all required cni plugins are placed in dir
746746
func (plugin *kubenetNetworkPlugin) checkRequiredCNIPluginsInOneDir(dir string) bool {
747-
files, err := ioutil.ReadDir(dir)
747+
files, err := os.ReadDir(dir)
748748
if err != nil {
749749
return false
750750
}

store/fs.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ limitations under the License.
1717
package store
1818

1919
import (
20-
"io/ioutil"
20+
"io/fs"
2121
"os"
2222
"path/filepath"
2323
"time"
@@ -67,28 +67,41 @@ func (DefaultFs) Remove(name string) error {
6767
return os.Remove(name)
6868
}
6969

70-
// ReadFile via ioutil.ReadFile
70+
// ReadFile via os.ReadFile
7171
func (DefaultFs) ReadFile(filename string) ([]byte, error) {
72-
return ioutil.ReadFile(filename)
72+
return os.ReadFile(filename)
7373
}
7474

75-
// TempDir via ioutil.TempDir
75+
// TempDir via os.MkdirTemp
7676
func (DefaultFs) TempDir(dir, prefix string) (string, error) {
77-
return ioutil.TempDir(dir, prefix)
77+
return os.MkdirTemp(dir, prefix)
7878
}
7979

80-
// TempFile via ioutil.TempFile
80+
// TempFile via os.CreateTemp
8181
func (DefaultFs) TempFile(dir, prefix string) (File, error) {
82-
file, err := ioutil.TempFile(dir, prefix)
82+
file, err := os.CreateTemp(dir, prefix)
8383
if err != nil {
8484
return nil, err
8585
}
8686
return &defaultFile{file}, nil
8787
}
8888

89-
// ReadDir via ioutil.ReadDir
89+
// ReadDir via os.ReadDir
9090
func (DefaultFs) ReadDir(dirname string) ([]os.FileInfo, error) {
91-
return ioutil.ReadDir(dirname)
91+
entries, err := os.ReadDir(dirname)
92+
if err != nil {
93+
return nil, err
94+
}
95+
infos := make([]fs.FileInfo, 0, len(entries))
96+
for _, entry := range entries {
97+
info, err := entry.Info()
98+
if err != nil {
99+
return nil, err
100+
}
101+
infos = append(infos, info)
102+
}
103+
104+
return infos, nil
92105
}
93106

94107
// Walk via filepath.Walk

streaming/remotecommand/errorstream.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package remotecommand
1919
import (
2020
"fmt"
2121
"io"
22-
"io/ioutil"
2322

2423
"k8s.io/apimachinery/pkg/util/runtime"
2524
)
@@ -39,7 +38,7 @@ func watchErrorStream(errorStream io.Reader, d errorStreamDecoder) chan error {
3938
go func() {
4039
defer runtime.HandleCrash()
4140

42-
message, err := ioutil.ReadAll(errorStream)
41+
message, err := io.ReadAll(errorStream)
4342
switch {
4443
case err != nil && err != io.EOF:
4544
errorChan <- fmt.Errorf("error reading from error stream: %s", err)

0 commit comments

Comments
 (0)