Skip to content

Replace deprecated ioutil functions #298

@krishnakeshan

Description

@krishnakeshan

Is your feature request related to a problem?/Why is this needed
This project uses the ioutil package for the following functions

ReadAll
TempDir
ReadFile
WriteFile
ReadDir

The functions in ioutil have been deprecated in favour of similar functions in io and os. While some of these deprecated functions internally call the corresponding, recommended, io and os functions, we could benefit by replacing these usages directly in the project code.

Describe the solution you'd like in detail
Replace usages of deprecated functions on a case-by-case basis with functions in os and io. The following command finds all files in the utils project importing the ioutils package:

grep -Rl "io/ioutil" .

and the result is

./temp/dir.go
./temp/dir_test.go
./io/read_test.go
./io/read.go
./inotify/inotify_linux_test.go
./exec/stdiopipe_test.go
./exec/exec_test.go
./mount/mount_windows_test.go
./mount/mount_linux_test.go
./mount/mount_helper_unix_test.go
./mount/safe_format_and_mount_test.go
./mount/mount_helper_test.go
./nsenter/nsenter_test.go

A simple MR replacing usages in these files with relevant (perhaps improved?) tests would be the solution. Almost all of the functions listed in the previous section have become wrappers around the newer functions. However, ioutil.ReadDir's replacement os.ReadDir offers supposedly better efficiency and the return type changes from fs.FileInfo -> fs.DirEntry and it returns a partial result in the case of an error while reading the directory. These changes do not matter in the context of this project and a simple replacement will work with no additional changes.

I've created a branch for this fix in my fork and go test ./... reports no errors

?   	k8s.io/utils/clock	[no test files]
ok  	k8s.io/utils/buffer	0.366s
ok  	k8s.io/utils/clock/testing	0.657s
?   	k8s.io/utils/inotify	[no test files]
?   	k8s.io/utils/internal/third_party/forked/golang/golang-lru	[no test files]
ok  	k8s.io/utils/cpuset	0.934s
ok  	k8s.io/utils/diff	1.485s
ok  	k8s.io/utils/env	1.206s
?   	k8s.io/utils/nsenter	[no test files]
ok  	k8s.io/utils/exec	4.072s
ok  	k8s.io/utils/exec/testing	1.685s
ok  	k8s.io/utils/field	1.661s
ok  	k8s.io/utils/integer	1.915s
ok  	k8s.io/utils/internal/third_party/forked/golang/net	2.192s
ok  	k8s.io/utils/io	2.250s
ok  	k8s.io/utils/keymutex	6.247s
ok  	k8s.io/utils/lru	7.261s
ok  	k8s.io/utils/mount	2.274s
ok  	k8s.io/utils/net	2.153s
ok  	k8s.io/utils/net/ebtables	2.368s
ok  	k8s.io/utils/path	2.328s
ok  	k8s.io/utils/pointer	2.272s
ok  	k8s.io/utils/ptr	1.672s
ok  	k8s.io/utils/semantic	1.706s
ok  	k8s.io/utils/set	1.696s
ok  	k8s.io/utils/strings	1.680s
ok  	k8s.io/utils/strings/slices	1.561s
ok  	k8s.io/utils/temp	1.647s
ok  	k8s.io/utils/temp/temptest	1.545s
ok  	k8s.io/utils/third_party/forked/golang/reflect	1.582s
ok  	k8s.io/utils/trace	1.598s

If this issue gets triaged, I can create a pull request.

Describe alternatives you've considered
N/A

Additional context
N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    lifecycle/rottenDenotes an issue or PR that has aged beyond stale and will be auto-closed.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions