Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace deprecated ioutil package #6230

Merged
merged 3 commits into from
Oct 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions pkg/agent/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"encoding/hex"
"encoding/pem"
"fmt"
"io/ioutil"
"io"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -145,13 +145,13 @@ func getNodeNamedCrt(nodeName string, nodeIPs []net.IP, nodePasswordFile string)
return nil, fmt.Errorf("%s: %s", u, resp.Status)
}

return ioutil.ReadAll(resp.Body)
return io.ReadAll(resp.Body)
}
}

func ensureNodeID(nodeIDFile string) (string, error) {
if _, err := os.Stat(nodeIDFile); err == nil {
id, err := ioutil.ReadFile(nodeIDFile)
id, err := os.ReadFile(nodeIDFile)
return strings.TrimSpace(string(id)), err
}
id := make([]byte, 4, 4)
Expand All @@ -160,12 +160,12 @@ func ensureNodeID(nodeIDFile string) (string, error) {
return "", err
}
nodeID := hex.EncodeToString(id)
return nodeID, ioutil.WriteFile(nodeIDFile, []byte(nodeID+"\n"), 0644)
return nodeID, os.WriteFile(nodeIDFile, []byte(nodeID+"\n"), 0644)
}

func ensureNodePassword(nodePasswordFile string) (string, error) {
if _, err := os.Stat(nodePasswordFile); err == nil {
password, err := ioutil.ReadFile(nodePasswordFile)
password, err := os.ReadFile(nodePasswordFile)
return strings.TrimSpace(string(password)), err
}
password := make([]byte, 16, 16)
Expand All @@ -174,15 +174,15 @@ func ensureNodePassword(nodePasswordFile string) (string, error) {
return "", err
}
nodePassword := hex.EncodeToString(password)
return nodePassword, ioutil.WriteFile(nodePasswordFile, []byte(nodePassword+"\n"), 0600)
return nodePassword, os.WriteFile(nodePasswordFile, []byte(nodePassword+"\n"), 0600)
}

func upgradeOldNodePasswordPath(oldNodePasswordFile, newNodePasswordFile string) {
password, err := ioutil.ReadFile(oldNodePasswordFile)
password, err := os.ReadFile(oldNodePasswordFile)
if err != nil {
return
}
if err := ioutil.WriteFile(newNodePasswordFile, password, 0600); err != nil {
if err := os.WriteFile(newNodePasswordFile, password, 0600); err != nil {
logrus.Warnf("Unable to write password file: %v", err)
return
}
Expand All @@ -200,11 +200,11 @@ func getServingCert(nodeName string, nodeIPs []net.IP, servingCertFile, servingK

servingCert, servingKey := splitCertKeyPEM(servingCert)

if err := ioutil.WriteFile(servingCertFile, servingCert, 0600); err != nil {
if err := os.WriteFile(servingCertFile, servingCert, 0600); err != nil {
return nil, errors.Wrapf(err, "failed to write node cert")
}

if err := ioutil.WriteFile(servingKeyFile, servingKey, 0600); err != nil {
if err := os.WriteFile(servingKeyFile, servingKey, 0600); err != nil {
return nil, errors.Wrapf(err, "failed to write node key")
}

Expand All @@ -222,15 +222,15 @@ func getHostFile(filename, keyFile string, info *clientaccess.Info) error {
return err
}
if keyFile == "" {
if err := ioutil.WriteFile(filename, fileBytes, 0600); err != nil {
if err := os.WriteFile(filename, fileBytes, 0600); err != nil {
return errors.Wrapf(err, "failed to write cert %s", filename)
}
} else {
fileBytes, keyBytes := splitCertKeyPEM(fileBytes)
if err := ioutil.WriteFile(filename, fileBytes, 0600); err != nil {
if err := os.WriteFile(filename, fileBytes, 0600); err != nil {
return errors.Wrapf(err, "failed to write cert %s", filename)
}
if err := ioutil.WriteFile(keyFile, keyBytes, 0600); err != nil {
if err := os.WriteFile(keyFile, keyBytes, 0600); err != nil {
return errors.Wrapf(err, "failed to write key %s", filename)
}
}
Expand Down Expand Up @@ -263,10 +263,10 @@ func getNodeNamedHostFile(filename, keyFile, nodeName string, nodeIPs []net.IP,
}
fileBytes, keyBytes := splitCertKeyPEM(fileBytes)

if err := ioutil.WriteFile(filename, fileBytes, 0600); err != nil {
if err := os.WriteFile(filename, fileBytes, 0600); err != nil {
return errors.Wrapf(err, "failed to write cert %s", filename)
}
if err := ioutil.WriteFile(keyFile, keyBytes, 0600); err != nil {
if err := os.WriteFile(keyFile, keyBytes, 0600); err != nil {
return errors.Wrapf(err, "failed to write key %s", filename)
}
return nil
Expand Down
3 changes: 1 addition & 2 deletions pkg/agent/containerd/config_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package containerd

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

Expand Down Expand Up @@ -83,7 +82,7 @@ func setupContainerdConfig(ctx context.Context, cfg *config.Node) error {
logrus.Warnf("SELinux is enabled for "+version.Program+" but process is not running in context '%s', "+version.Program+"-selinux policy may need to be applied", SELinuxContextType)
}

containerdTemplateBytes, err := ioutil.ReadFile(cfg.Containerd.Template)
containerdTemplateBytes, err := os.ReadFile(cfg.Containerd.Template)
if err == nil {
logrus.Infof("Using containerd template at %s", cfg.Containerd.Template)
containerdTemplate = string(containerdTemplateBytes)
Expand Down
3 changes: 1 addition & 2 deletions pkg/agent/containerd/config_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package containerd

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

Expand Down Expand Up @@ -50,7 +49,7 @@ func setupContainerdConfig(ctx context.Context, cfg *config.Node) error {
PrivateRegistryConfig: privRegistries.Registry,
}

containerdTemplateBytes, err := ioutil.ReadFile(cfg.Containerd.Template)
containerdTemplateBytes, err := os.ReadFile(cfg.Containerd.Template)
if err == nil {
logrus.Infof("Using containerd template at %s", cfg.Containerd.Template)
containerdTemplate = string(containerdTemplateBytes)
Expand Down
3 changes: 1 addition & 2 deletions pkg/agent/containerd/containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -144,7 +143,7 @@ func preloadImages(ctx context.Context, cfg *config.Node) error {
return nil
}

fileInfos, err := ioutil.ReadDir(cfg.Images)
fileInfos, err := os.ReadDir(cfg.Images)
if err != nil {
logrus.Errorf("Unable to read images in %s: %v", cfg.Images, err)
return nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/agent/flannel/setup_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package flannel

import (
"io/ioutil"
"net"
"os"
"regexp"
"strings"
"testing"
Expand Down Expand Up @@ -68,7 +68,7 @@ func Test_createFlannelConf(t *testing.T) {
if err := createFlannelConf(nodeConfig); (err != nil) != tt.wantErr {
t.Errorf("createFlannelConf() error = %v, wantErr %v", err, tt.wantErr)
}
data, err := ioutil.ReadFile("test_file")
data, err := os.ReadFile("test_file")
if err != nil {
t.Errorf("Something went wrong when reading the flannel config file")
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/agent/loadbalancer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package loadbalancer

import (
"encoding/json"
"io/ioutil"
"os"

"github.com/k3s-io/k3s/pkg/agent/util"
)
Expand All @@ -17,7 +17,7 @@ func (lb *LoadBalancer) writeConfig() error {

func (lb *LoadBalancer) updateConfig() error {
writeConfig := true
if configBytes, err := ioutil.ReadFile(lb.configFile); err == nil {
if configBytes, err := os.ReadFile(lb.configFile); err == nil {
config := &LoadBalancer{}
if err := json.Unmarshal(configBytes, config); err == nil {
if config.ServerURL == lb.ServerURL {
Expand Down
5 changes: 2 additions & 3 deletions pkg/agent/loadbalancer/loadbalancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"net"
"net/url"
"os"
Expand Down Expand Up @@ -85,7 +84,7 @@ func assertNotEqual(t *testing.T, a interface{}, b interface{}) {
}

func Test_UnitFailOver(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "lb-test")
tmpDir, err := os.MkdirTemp("", "lb-test")
if err != nil {
assertEqual(t, err, nil)
}
Expand Down Expand Up @@ -146,7 +145,7 @@ func Test_UnitFailOver(t *testing.T) {
}

func Test_UnitFailFast(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "lb-test")
tmpDir, err := os.MkdirTemp("", "lb-test")
if err != nil {
assertEqual(t, err, nil)
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/agent/run_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package agent

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

Expand Down Expand Up @@ -38,5 +37,5 @@ func setupCriCtlConfig(cfg cmds.Agent, nodeConfig *config.Node) error {
}

crp := "runtime-endpoint: " + cre + "\n"
return ioutil.WriteFile(agentConfDir+"/crictl.yaml", []byte(crp), 0600)
return os.WriteFile(agentConfDir+"/crictl.yaml", []byte(crp), 0600)
}
3 changes: 1 addition & 2 deletions pkg/agent/run_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package agent

import (
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -40,5 +39,5 @@ func setupCriCtlConfig(cfg cmds.Agent, nodeConfig *config.Node) error {
}

crp := "runtime-endpoint: " + cre + "\n"
return ioutil.WriteFile(filepath.Join(agentConfDir, "crictl.yaml"), []byte(crp), 0600)
return os.WriteFile(filepath.Join(agentConfDir, "crictl.yaml"), []byte(crp), 0600)
}
7 changes: 3 additions & 4 deletions pkg/agent/util/file.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package util

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

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

func WriteFile(name string, content string) error {
os.MkdirAll(filepath.Dir(name), 0755)
err := ioutil.WriteFile(name, []byte(content), 0644)
err := os.WriteFile(name, []byte(content), 0644)
if err != nil {
return errors.Wrapf(err, "writing %s", name)
}
Expand All @@ -19,11 +18,11 @@ func WriteFile(name string, content string) error {

func CopyFile(sourceFile string, destinationFile string) error {
os.MkdirAll(filepath.Dir(destinationFile), 0755)
input, err := ioutil.ReadFile(sourceFile)
input, err := os.ReadFile(sourceFile)
if err != nil {
return errors.Wrapf(err, "copying %s to %s", sourceFile, destinationFile)
}
err = ioutil.WriteFile(destinationFile, input, 0644)
err = os.WriteFile(destinationFile, input, 0644)
if err != nil {
return errors.Wrapf(err, "copying %s to %s", sourceFile, destinationFile)
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/authenticator/passwordfile/passwordfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package passwordfile

import (
"context"
"io/ioutil"
"os"
"reflect"
"testing"
Expand Down Expand Up @@ -146,14 +145,14 @@ func Test_UnitInsufficientColumnsPasswordFile(t *testing.T) {
}

func newWithContents(t *testing.T, contents string) (auth *PasswordAuthenticator, err error) {
f, err := ioutil.TempFile("", "passwordfile_test")
f, err := os.CreateTemp("", "passwordfile_test")
if err != nil {
t.Fatalf("unexpected error creating passwordfile: %v", err)
}
f.Close()
defer os.Remove(f.Name())

if err := ioutil.WriteFile(f.Name(), []byte(contents), 0700); err != nil {
if err := os.WriteFile(f.Name(), []byte(contents), 0700); err != nil {
t.Fatalf("unexpected error writing passwordfile: %v", err)
}

Expand Down
3 changes: 1 addition & 2 deletions pkg/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package bootstrap
import (
"encoding/json"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -34,7 +33,7 @@ func ReadFromDisk(w io.Writer, bootstrap *config.ControlRuntimeBootstrap) error
if path == "" {
continue
}
data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
if err != nil {
logrus.Warnf("failed to read %s", path)
continue
Expand Down
3 changes: 1 addition & 2 deletions pkg/cgroups/cgroups_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"bufio"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand All @@ -26,7 +25,7 @@ func Validate() error {
}

func validateCgroupsV1() error {
cgroups, err := ioutil.ReadFile("/proc/self/cgroup")
cgroups, err := os.ReadFile("/proc/self/cgroup")
if err != nil {
return err
}
Expand Down
7 changes: 3 additions & 4 deletions pkg/cli/cert/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cert

import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -154,7 +153,7 @@ func rotate(app *cli.Context, cfg *cmds.Server) error {
serverConfig.ControlConfig.Runtime.ClientCloudControllerKey)
case version.Program + k3sServerService:
dynamicListenerRegenFilePath := filepath.Join(serverDataDir, "tls", "dynamic-cert-regenerate")
if err := ioutil.WriteFile(dynamicListenerRegenFilePath, []byte{}, 0600); err != nil {
if err := os.WriteFile(dynamicListenerRegenFilePath, []byte{}, 0600); err != nil {
return err
}
logrus.Infof("Rotating dynamic listener certificate")
Expand Down Expand Up @@ -199,11 +198,11 @@ func rotate(app *cli.Context, cfg *cmds.Server) error {
func copyFile(src, destDir string) error {
_, err := os.Stat(src)
if err == nil {
input, err := ioutil.ReadFile(src)
input, err := os.ReadFile(src)
if err != nil {
return err
}
return ioutil.WriteFile(filepath.Join(destDir, filepath.Base(src)), input, 0644)
return os.WriteFile(filepath.Join(destDir, filepath.Base(src)), input, 0644)
} else if errors.Is(err, os.ErrNotExist) {
return nil
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/cli/secretsencrypt/secrets_encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -33,7 +32,7 @@ func commandPrep(app *cli.Context, cfg *cmds.Server) (*clientaccess.Info, error)

if cfg.Token == "" {
fp := filepath.Join(dataDir, "token")
tokenByte, err := ioutil.ReadFile(fp)
tokenByte, err := os.ReadFile(fp)
if err != nil {
return nil, err
}
Expand Down
Loading