Skip to content

Commit

Permalink
Merge pull request #790 from austinvazquez/remove-ioutil-references
Browse files Browse the repository at this point in the history
Remove references to io/ioutil package
  • Loading branch information
squeed committed Jan 16, 2023
2 parents 0af8153 + 1a6f478 commit 0924b71
Show file tree
Hide file tree
Showing 23 changed files with 80 additions and 99 deletions.
6 changes: 3 additions & 3 deletions pkg/ip/ipforward_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package ip

import (
"bytes"
"io/ioutil"
"os"

current "github.com/containernetworking/cni/pkg/types/100"
)
Expand Down Expand Up @@ -53,10 +53,10 @@ func EnableForward(ips []*current.IPConfig) error {
}

func echo1(f string) error {
if content, err := ioutil.ReadFile(f); err == nil {
if content, err := os.ReadFile(f); err == nil {
if bytes.Equal(bytes.TrimSpace(content), []byte("1")) {
return nil
}
}
return ioutil.WriteFile(f, []byte("1"), 0644)
return os.WriteFile(f, []byte("1"), 0644)
}
3 changes: 1 addition & 2 deletions pkg/ip/ipforward_linux_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ip

import (
"io/ioutil"
"os"
"time"

Expand All @@ -11,7 +10,7 @@ import (

var _ = Describe("IpforwardLinux", func() {
It("echo1 must not write the file if content is 1", func() {
file, err := ioutil.TempFile(os.TempDir(), "containernetworking")
file, err := os.CreateTemp("", "containernetworking")
Expect(err).NotTo(HaveOccurred())
defer os.Remove(file.Name())
err = echo1(file.Name())
Expand Down
5 changes: 2 additions & 3 deletions pkg/ns/ns_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package ns_test
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sync"
Expand Down Expand Up @@ -208,7 +207,7 @@ var _ = Describe("Linux namespace operations", func() {
})

It("fails when the path is not a namespace", func() {
tempFile, err := ioutil.TempFile("", "nstest")
tempFile, err := os.CreateTemp("", "nstest")
Expect(err).NotTo(HaveOccurred())
defer tempFile.Close()

Expand Down Expand Up @@ -262,7 +261,7 @@ var _ = Describe("Linux namespace operations", func() {
})

It("should refuse other paths", func() {
tempFile, err := ioutil.TempFile("", "nstest")
tempFile, err := os.CreateTemp("", "nstest")
Expect(err).NotTo(HaveOccurred())
defer tempFile.Close()

Expand Down
4 changes: 2 additions & 2 deletions pkg/testutils/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package testutils

import (
"io/ioutil"
"io"
"os"

"github.com/containernetworking/cni/pkg/skel"
Expand Down Expand Up @@ -52,7 +52,7 @@ func CmdAdd(cniNetns, cniContainerID, cniIfname string, conf []byte, f func() er

var out []byte
if err == nil {
out, err = ioutil.ReadAll(r)
out, err = io.ReadAll(r)
}
os.Stdout = oldStdout

Expand Down
3 changes: 1 addition & 2 deletions pkg/testutils/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package testutils

import (
"fmt"
"io/ioutil"
"os"
"strings"

Expand All @@ -28,7 +27,7 @@ import (
// an error if any occurs while creating/writing the file. It is the caller's
// responsibility to remove the file.
func TmpResolvConf(dnsConf types.DNS) (string, error) {
f, err := ioutil.TempFile("", "cni_test_resolv.conf")
f, err := os.CreateTemp("", "cni_test_resolv.conf")
if err != nil {
return "", fmt.Errorf("failed to get temp file for CNI test resolv.conf: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/testutils/echo/echo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main_test

import (
"fmt"
"io/ioutil"
"io"
"net"
"os/exec"
"strings"
Expand Down Expand Up @@ -74,7 +74,7 @@ var _ = Describe("Echosvr", func() {
defer conn.Close()

fmt.Fprintf(conn, "hello\n")
Expect(ioutil.ReadAll(conn)).To(Equal([]byte("hello")))
Expect(io.ReadAll(conn)).To(Equal([]byte("hello")))
})
})

Expand Down
6 changes: 3 additions & 3 deletions pkg/utils/sysctl/sysctl_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package sysctl

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
Expand All @@ -36,7 +36,7 @@ func Sysctl(name string, params ...string) (string, error) {

func getSysctl(name string) (string, error) {
fullName := filepath.Join("/proc/sys", toNormalName(name))
data, err := ioutil.ReadFile(fullName)
data, err := os.ReadFile(fullName)
if err != nil {
return "", err
}
Expand All @@ -46,7 +46,7 @@ func getSysctl(name string) (string, error) {

func setSysctl(name, value string) (string, error) {
fullName := filepath.Join("/proc/sys", toNormalName(name))
if err := ioutil.WriteFile(fullName, []byte(value), 0644); err != nil {
if err := os.WriteFile(fullName, []byte(value), 0644); err != nil {
return "", err
}

Expand Down
5 changes: 2 additions & 3 deletions plugins/ipam/dhcp/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/rpc"
Expand Down Expand Up @@ -150,7 +149,7 @@ func (d *DHCP) setLease(clientID string, l *DHCPLease) {
d.leases[clientID] = l
}

//func (d *DHCP) clearLease(contID, netName, ifName string) {
// func (d *DHCP) clearLease(contID, netName, ifName string) {
func (d *DHCP) clearLease(clientID string) {
d.mux.Lock()
defer d.mux.Unlock()
Expand Down Expand Up @@ -196,7 +195,7 @@ func runDaemon(
if !filepath.IsAbs(pidfilePath) {
return fmt.Errorf("Error writing pidfile %q: path not absolute", pidfilePath)
}
if err := ioutil.WriteFile(pidfilePath, []byte(fmt.Sprintf("%d", os.Getpid())), 0644); err != nil {
if err := os.WriteFile(pidfilePath, []byte(fmt.Sprintf("%d", os.Getpid())), 0644); err != nil {
return fmt.Errorf("Error writing pidfile %q: %v", pidfilePath, err)
}
}
Expand Down
5 changes: 2 additions & 3 deletions plugins/ipam/dhcp/dhcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"os/exec"
Expand All @@ -27,7 +26,7 @@ import (
"time"

"github.com/containernetworking/cni/pkg/skel"
"github.com/containernetworking/cni/pkg/types/100"
types100 "github.com/containernetworking/cni/pkg/types/100"
"github.com/containernetworking/plugins/pkg/ns"
"github.com/containernetworking/plugins/pkg/testutils"

Expand All @@ -43,7 +42,7 @@ import (
)

func getTmpDir() (string, error) {
tmpDir, err := ioutil.TempDir(cniDirPrefix, "dhcp")
tmpDir, err := os.MkdirTemp(cniDirPrefix, "dhcp")
if err == nil {
tmpDir = filepath.ToSlash(tmpDir)
}
Expand Down
11 changes: 5 additions & 6 deletions plugins/ipam/host-local/backend/disk/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package disk

import (
"io/ioutil"
"net"
"os"
"path/filepath"
Expand Down Expand Up @@ -77,7 +76,7 @@ func (s *Store) Reserve(id string, ifname string, ip net.IP, rangeID string) (bo
}
// store the reserved ip in lastIPFile
ipfile := GetEscapedPath(s.dataDir, lastIPFilePrefix+rangeID)
err = ioutil.WriteFile(ipfile, []byte(ip.String()), 0644)
err = os.WriteFile(ipfile, []byte(ip.String()), 0644)
if err != nil {
return false, err
}
Expand All @@ -87,7 +86,7 @@ func (s *Store) Reserve(id string, ifname string, ip net.IP, rangeID string) (bo
// LastReservedIP returns the last reserved IP if exists
func (s *Store) LastReservedIP(rangeID string) (net.IP, error) {
ipfile := GetEscapedPath(s.dataDir, lastIPFilePrefix+rangeID)
data, err := ioutil.ReadFile(ipfile)
data, err := os.ReadFile(ipfile)
if err != nil {
return nil, err
}
Expand All @@ -101,7 +100,7 @@ func (s *Store) FindByKey(id string, ifname string, match string) (bool, error)
if err != nil || info.IsDir() {
return nil
}
data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
if err != nil {
return nil
}
Expand Down Expand Up @@ -137,7 +136,7 @@ func (s *Store) ReleaseByKey(id string, ifname string, match string) (bool, erro
if err != nil || info.IsDir() {
return nil
}
data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
if err != nil {
return nil
}
Expand Down Expand Up @@ -181,7 +180,7 @@ func (s *Store) GetByID(id string, ifname string) []net.IP {
if err != nil || info.IsDir() {
return nil
}
data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
if err != nil {
return nil
}
Expand Down
5 changes: 2 additions & 3 deletions plugins/ipam/host-local/backend/disk/lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package disk

import (
"io/ioutil"
"os"
"path/filepath"

Expand All @@ -25,7 +24,7 @@ import (

var _ = Describe("Lock Operations", func() {
It("locks a file path", func() {
dir, err := ioutil.TempDir("", "")
dir, err := os.MkdirTemp("", "")
Expect(err).ToNot(HaveOccurred())
defer os.RemoveAll(dir)

Expand All @@ -47,7 +46,7 @@ var _ = Describe("Lock Operations", func() {
})

It("locks a folder path", func() {
dir, err := ioutil.TempDir("", "")
dir, err := os.MkdirTemp("", "")
Expect(err).ToNot(HaveOccurred())
defer os.RemoveAll(dir)

Expand Down
3 changes: 1 addition & 2 deletions plugins/ipam/host-local/dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package main

import (
"io/ioutil"
"os"

"github.com/containernetworking/cni/pkg/types"
Expand Down Expand Up @@ -64,7 +63,7 @@ options four
})

func parse(contents string) (*types.DNS, error) {
f, err := ioutil.TempFile("", "host_local_resolv")
f, err := os.CreateTemp("", "host_local_resolv")
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 0924b71

Please sign in to comment.