mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-19 18:02:01 +00:00
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:
commit
6a709cf07b
@ -657,7 +657,7 @@ func mapFromCheckpoint(store *fsStore, uid, resourceVersion string) (map[string]
|
||||
m := map[string]string{}
|
||||
for _, f := range 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)
|
||||
}
|
||||
// read the file contents and build the map
|
||||
|
@ -17,14 +17,13 @@ limitations under the License.
|
||||
package filesystem
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"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 {
|
||||
root string
|
||||
}
|
||||
@ -33,10 +32,7 @@ var _ Filesystem = &DefaultFs{}
|
||||
|
||||
// NewTempFs returns a fake Filesystem in temporary directory, useful for unit tests
|
||||
func NewTempFs() Filesystem {
|
||||
path, _ := ioutil.TempDir(
|
||||
"",
|
||||
"tmpfs",
|
||||
)
|
||||
path, _ := os.MkdirTemp("", "tmpfs")
|
||||
return &DefaultFs{
|
||||
root: path,
|
||||
}
|
||||
@ -94,28 +90,28 @@ func (fs *DefaultFs) Remove(name string) error {
|
||||
return os.Remove(fs.prefix(name))
|
||||
}
|
||||
|
||||
// ReadFile via ioutil.ReadFile
|
||||
// ReadFile via os.ReadFile
|
||||
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) {
|
||||
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) {
|
||||
file, err := ioutil.TempFile(fs.prefix(dir), prefix)
|
||||
file, err := os.CreateTemp(fs.prefix(dir), prefix)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &defaultFile{file}, nil
|
||||
}
|
||||
|
||||
// ReadDir via ioutil.ReadDir
|
||||
func (fs *DefaultFs) ReadDir(dirname string) ([]os.FileInfo, error) {
|
||||
return ioutil.ReadDir(fs.prefix(dirname))
|
||||
// ReadDir via os.ReadDir
|
||||
func (fs *DefaultFs) ReadDir(dirname string) ([]os.DirEntry, error) {
|
||||
return os.ReadDir(fs.prefix(dirname))
|
||||
}
|
||||
|
||||
// Walk via filepath.Walk
|
||||
|
@ -37,7 +37,7 @@ type Filesystem interface {
|
||||
ReadFile(filename string) ([]byte, error)
|
||||
TempDir(dir, prefix string) (string, 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
|
||||
}
|
||||
|
||||
|
@ -21,7 +21,6 @@ package oom
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"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)
|
||||
var err error
|
||||
for i := 0; i < maxTries; i++ {
|
||||
err = ioutil.WriteFile(oomScoreAdjPath, []byte(value), 0700)
|
||||
err = os.WriteFile(oomScoreAdjPath, []byte(value), 0700)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
klog.V(2).Infof("%q does not exist", oomScoreAdjPath)
|
||||
|
@ -23,7 +23,6 @@ import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"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.
|
||||
func (pfs *ProcFS) GetFullContainerName(pid int) (string, error) {
|
||||
filePath := path.Join("/proc", strconv.Itoa(pid), "cgroup")
|
||||
content, err := ioutil.ReadFile(filePath)
|
||||
content, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return "", os.ErrNotExist
|
||||
@ -138,7 +137,7 @@ func getPids(re *regexp.Regexp) []int {
|
||||
continue
|
||||
}
|
||||
|
||||
cmdline, err := ioutil.ReadFile(filepath.Join("/proc", entry.Name(), "cmdline"))
|
||||
cmdline, err := os.ReadFile(filepath.Join("/proc", entry.Name(), "cmdline"))
|
||||
if err != nil {
|
||||
klog.V(4).Infof("Error reading file %s: %+v", filepath.Join("/proc", entry.Name(), "cmdline"), err)
|
||||
continue
|
||||
|
@ -20,7 +20,6 @@ limitations under the License.
|
||||
package procfs
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
@ -56,7 +55,7 @@ func TestContainerNameFromProcCgroup(t *testing.T) {
|
||||
procCgroupEmpty := ""
|
||||
verifyContainerName(procCgroupEmpty, "", true, t)
|
||||
|
||||
content, err := ioutil.ReadFile("example_proc_cgroup")
|
||||
content, err := os.ReadFile("example_proc_cgroup")
|
||||
if err != nil {
|
||||
t.Errorf("Could not read example /proc cgroup file")
|
||||
}
|
||||
|
@ -19,7 +19,6 @@ package tail
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
)
|
||||
|
||||
@ -57,7 +56,7 @@ func ReadAtMost(path string, max int64) ([]byte, bool, error) {
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
data, err := ioutil.ReadAll(f)
|
||||
data, err := io.ReadAll(f)
|
||||
return data, offset > 0, err
|
||||
}
|
||||
|
||||
|
@ -18,14 +18,13 @@ package tail
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestReadAtMost(t *testing.T) {
|
||||
file, err := ioutil.TempFile("", "TestFileReadAtMost")
|
||||
file, err := os.CreateTemp("", "TestFileReadAtMost")
|
||||
if err != nil {
|
||||
t.Fatalf("unable to create temp file")
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user