Merge pull request #113194 from saltbo/refa-replace-ioutil

Replace the ioutil by the os and io for the pkg/util
This commit is contained in:
Kubernetes Prow Robot 2022-10-23 18:08:24 -07:00 committed by GitHub
commit 6a709cf07b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 19 additions and 28 deletions

View File

@ -657,7 +657,7 @@ func mapFromCheckpoint(store *fsStore, uid, resourceVersion string) (map[string]
m := map[string]string{} m := map[string]string{}
for _, f := range files { for _, f := range files {
// expect no subdirs, only regular files // expect no subdirs, only regular files
if !f.Mode().IsRegular() { if !f.Type().IsRegular() {
return nil, fmt.Errorf("expect only regular files in checkpoint dir %q", uid) return nil, fmt.Errorf("expect only regular files in checkpoint dir %q", uid)
} }
// read the file contents and build the map // read the file contents and build the map

View File

@ -17,14 +17,13 @@ limitations under the License.
package filesystem package filesystem
import ( import (
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"time" "time"
) )
// DefaultFs implements Filesystem using same-named functions from "os" and "io/ioutil" // DefaultFs implements Filesystem using same-named functions from "os" and "io"
type DefaultFs struct { type DefaultFs struct {
root string root string
} }
@ -33,10 +32,7 @@ var _ Filesystem = &DefaultFs{}
// NewTempFs returns a fake Filesystem in temporary directory, useful for unit tests // NewTempFs returns a fake Filesystem in temporary directory, useful for unit tests
func NewTempFs() Filesystem { func NewTempFs() Filesystem {
path, _ := ioutil.TempDir( path, _ := os.MkdirTemp("", "tmpfs")
"",
"tmpfs",
)
return &DefaultFs{ return &DefaultFs{
root: path, root: path,
} }
@ -94,28 +90,28 @@ func (fs *DefaultFs) Remove(name string) error {
return os.Remove(fs.prefix(name)) return os.Remove(fs.prefix(name))
} }
// ReadFile via ioutil.ReadFile // ReadFile via os.ReadFile
func (fs *DefaultFs) ReadFile(filename string) ([]byte, error) { func (fs *DefaultFs) ReadFile(filename string) ([]byte, error) {
return ioutil.ReadFile(fs.prefix(filename)) return os.ReadFile(fs.prefix(filename))
} }
// TempDir via ioutil.TempDir // TempDir via os.MkdirTemp
func (fs *DefaultFs) TempDir(dir, prefix string) (string, error) { func (fs *DefaultFs) TempDir(dir, prefix string) (string, error) {
return ioutil.TempDir(fs.prefix(dir), prefix) return os.MkdirTemp(fs.prefix(dir), prefix)
} }
// TempFile via ioutil.TempFile // TempFile via os.CreateTemp
func (fs *DefaultFs) TempFile(dir, prefix string) (File, error) { func (fs *DefaultFs) TempFile(dir, prefix string) (File, error) {
file, err := ioutil.TempFile(fs.prefix(dir), prefix) file, err := os.CreateTemp(fs.prefix(dir), prefix)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &defaultFile{file}, nil return &defaultFile{file}, nil
} }
// ReadDir via ioutil.ReadDir // ReadDir via os.ReadDir
func (fs *DefaultFs) ReadDir(dirname string) ([]os.FileInfo, error) { func (fs *DefaultFs) ReadDir(dirname string) ([]os.DirEntry, error) {
return ioutil.ReadDir(fs.prefix(dirname)) return os.ReadDir(fs.prefix(dirname))
} }
// Walk via filepath.Walk // Walk via filepath.Walk

View File

@ -37,7 +37,7 @@ type Filesystem interface {
ReadFile(filename string) ([]byte, error) ReadFile(filename string) ([]byte, error)
TempDir(dir, prefix string) (string, error) TempDir(dir, prefix string) (string, error)
TempFile(dir, prefix string) (File, error) TempFile(dir, prefix string) (File, error)
ReadDir(dirname string) ([]os.FileInfo, error) ReadDir(dirname string) ([]os.DirEntry, error)
Walk(root string, walkFn filepath.WalkFunc) error Walk(root string, walkFn filepath.WalkFunc) error
} }

View File

@ -21,7 +21,6 @@ package oom
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
@ -66,7 +65,7 @@ func applyOOMScoreAdj(pid int, oomScoreAdj int) error {
klog.V(4).Infof("attempting to set %q to %q", oomScoreAdjPath, value) klog.V(4).Infof("attempting to set %q to %q", oomScoreAdjPath, value)
var err error var err error
for i := 0; i < maxTries; i++ { for i := 0; i < maxTries; i++ {
err = ioutil.WriteFile(oomScoreAdjPath, []byte(value), 0700) err = os.WriteFile(oomScoreAdjPath, []byte(value), 0700)
if err != nil { if err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
klog.V(2).Infof("%q does not exist", oomScoreAdjPath) klog.V(2).Infof("%q does not exist", oomScoreAdjPath)

View File

@ -23,7 +23,6 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
@ -61,7 +60,7 @@ func containerNameFromProcCgroup(content string) (string, error) {
// return docker/nginx. Assumes that the process is part of exactly one cgroup hierarchy. // return docker/nginx. Assumes that the process is part of exactly one cgroup hierarchy.
func (pfs *ProcFS) GetFullContainerName(pid int) (string, error) { func (pfs *ProcFS) GetFullContainerName(pid int) (string, error) {
filePath := path.Join("/proc", strconv.Itoa(pid), "cgroup") filePath := path.Join("/proc", strconv.Itoa(pid), "cgroup")
content, err := ioutil.ReadFile(filePath) content, err := os.ReadFile(filePath)
if err != nil { if err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
return "", os.ErrNotExist return "", os.ErrNotExist
@ -138,7 +137,7 @@ func getPids(re *regexp.Regexp) []int {
continue continue
} }
cmdline, err := ioutil.ReadFile(filepath.Join("/proc", entry.Name(), "cmdline")) cmdline, err := os.ReadFile(filepath.Join("/proc", entry.Name(), "cmdline"))
if err != nil { if err != nil {
klog.V(4).Infof("Error reading file %s: %+v", filepath.Join("/proc", entry.Name(), "cmdline"), err) klog.V(4).Infof("Error reading file %s: %+v", filepath.Join("/proc", entry.Name(), "cmdline"), err)
continue continue

View File

@ -20,7 +20,6 @@ limitations under the License.
package procfs package procfs
import ( import (
"io/ioutil"
"os" "os"
"os/signal" "os/signal"
"path/filepath" "path/filepath"
@ -56,7 +55,7 @@ func TestContainerNameFromProcCgroup(t *testing.T) {
procCgroupEmpty := "" procCgroupEmpty := ""
verifyContainerName(procCgroupEmpty, "", true, t) verifyContainerName(procCgroupEmpty, "", true, t)
content, err := ioutil.ReadFile("example_proc_cgroup") content, err := os.ReadFile("example_proc_cgroup")
if err != nil { if err != nil {
t.Errorf("Could not read example /proc cgroup file") t.Errorf("Could not read example /proc cgroup file")
} }

View File

@ -19,7 +19,6 @@ package tail
import ( import (
"bytes" "bytes"
"io" "io"
"io/ioutil"
"os" "os"
) )
@ -57,7 +56,7 @@ func ReadAtMost(path string, max int64) ([]byte, bool, error) {
if err != nil { if err != nil {
return nil, false, err return nil, false, err
} }
data, err := ioutil.ReadAll(f) data, err := io.ReadAll(f)
return data, offset > 0, err return data, offset > 0, err
} }

View File

@ -18,14 +18,13 @@ package tail
import ( import (
"bytes" "bytes"
"io/ioutil"
"os" "os"
"strings" "strings"
"testing" "testing"
) )
func TestReadAtMost(t *testing.T) { func TestReadAtMost(t *testing.T) {
file, err := ioutil.TempFile("", "TestFileReadAtMost") file, err := os.CreateTemp("", "TestFileReadAtMost")
if err != nil { if err != nil {
t.Fatalf("unable to create temp file") t.Fatalf("unable to create temp file")
} }