Unverified Commit 39361a80 authored by Derek Nola's avatar Derek Nola Committed by GitHub

[Release-1.23] Replace deprecated ioutil package (#6236)

* check integration test null pointer Signed-off-by: 's avatarDerek Nola <derek.nola@suse.com> * Remove rotate retries Signed-off-by: 's avatarDerek Nola <derek.nola@suse.com> * Replace ioutil package Signed-off-by: 's avatarDerek Nola <derek.nola@suse.com> * Rebase fix Signed-off-by: 's avatarDerek Nola <derek.nola@suse.com> Signed-off-by: 's avatarDerek Nola <derek.nola@suse.com>
parent 8752dbdc
...@@ -8,7 +8,7 @@ import ( ...@@ -8,7 +8,7 @@ import (
"encoding/hex" "encoding/hex"
"encoding/pem" "encoding/pem"
"fmt" "fmt"
"io/ioutil" "io"
"net" "net"
"net/http" "net/http"
"net/url" "net/url"
...@@ -145,13 +145,13 @@ func getNodeNamedCrt(nodeName string, nodeIPs []net.IP, nodePasswordFile string) ...@@ -145,13 +145,13 @@ func getNodeNamedCrt(nodeName string, nodeIPs []net.IP, nodePasswordFile string)
return nil, fmt.Errorf("%s: %s", u, resp.Status) 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) { func ensureNodeID(nodeIDFile string) (string, error) {
if _, err := os.Stat(nodeIDFile); err == nil { if _, err := os.Stat(nodeIDFile); err == nil {
id, err := ioutil.ReadFile(nodeIDFile) id, err := os.ReadFile(nodeIDFile)
return strings.TrimSpace(string(id)), err return strings.TrimSpace(string(id)), err
} }
id := make([]byte, 4, 4) id := make([]byte, 4, 4)
...@@ -160,12 +160,12 @@ func ensureNodeID(nodeIDFile string) (string, error) { ...@@ -160,12 +160,12 @@ func ensureNodeID(nodeIDFile string) (string, error) {
return "", err return "", err
} }
nodeID := hex.EncodeToString(id) 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) { func ensureNodePassword(nodePasswordFile string) (string, error) {
if _, err := os.Stat(nodePasswordFile); err == nil { if _, err := os.Stat(nodePasswordFile); err == nil {
password, err := ioutil.ReadFile(nodePasswordFile) password, err := os.ReadFile(nodePasswordFile)
return strings.TrimSpace(string(password)), err return strings.TrimSpace(string(password)), err
} }
password := make([]byte, 16, 16) password := make([]byte, 16, 16)
...@@ -174,15 +174,15 @@ func ensureNodePassword(nodePasswordFile string) (string, error) { ...@@ -174,15 +174,15 @@ func ensureNodePassword(nodePasswordFile string) (string, error) {
return "", err return "", err
} }
nodePassword := hex.EncodeToString(password) 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) { func upgradeOldNodePasswordPath(oldNodePasswordFile, newNodePasswordFile string) {
password, err := ioutil.ReadFile(oldNodePasswordFile) password, err := os.ReadFile(oldNodePasswordFile)
if err != nil { if err != nil {
return 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) logrus.Warnf("Unable to write password file: %v", err)
return return
} }
...@@ -200,11 +200,11 @@ func getServingCert(nodeName string, nodeIPs []net.IP, servingCertFile, servingK ...@@ -200,11 +200,11 @@ func getServingCert(nodeName string, nodeIPs []net.IP, servingCertFile, servingK
servingCert, servingKey := splitCertKeyPEM(servingCert) 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") 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") return nil, errors.Wrapf(err, "failed to write node key")
} }
...@@ -222,15 +222,15 @@ func getHostFile(filename, keyFile string, info *clientaccess.Info) error { ...@@ -222,15 +222,15 @@ func getHostFile(filename, keyFile string, info *clientaccess.Info) error {
return err return err
} }
if keyFile == "" { 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) return errors.Wrapf(err, "failed to write cert %s", filename)
} }
} else { } else {
fileBytes, keyBytes := splitCertKeyPEM(fileBytes) 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) 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 errors.Wrapf(err, "failed to write key %s", filename)
} }
} }
...@@ -263,10 +263,10 @@ func getNodeNamedHostFile(filename, keyFile, nodeName string, nodeIPs []net.IP, ...@@ -263,10 +263,10 @@ func getNodeNamedHostFile(filename, keyFile, nodeName string, nodeIPs []net.IP,
} }
fileBytes, keyBytes := splitCertKeyPEM(fileBytes) 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) 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 errors.Wrapf(err, "failed to write key %s", filename)
} }
return nil return nil
......
...@@ -5,7 +5,6 @@ package containerd ...@@ -5,7 +5,6 @@ package containerd
import ( import (
"context" "context"
"io/ioutil"
"os" "os"
"time" "time"
...@@ -79,7 +78,7 @@ func setupContainerdConfig(ctx context.Context, cfg *config.Node) error { ...@@ -79,7 +78,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) 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 { if err == nil {
logrus.Infof("Using containerd template at %s", cfg.Containerd.Template) logrus.Infof("Using containerd template at %s", cfg.Containerd.Template)
containerdTemplate = string(containerdTemplateBytes) containerdTemplate = string(containerdTemplateBytes)
......
...@@ -5,7 +5,6 @@ package containerd ...@@ -5,7 +5,6 @@ package containerd
import ( import (
"context" "context"
"io/ioutil"
"os" "os"
"time" "time"
...@@ -50,7 +49,7 @@ func setupContainerdConfig(ctx context.Context, cfg *config.Node) error { ...@@ -50,7 +49,7 @@ func setupContainerdConfig(ctx context.Context, cfg *config.Node) error {
PrivateRegistryConfig: privRegistries.Registry, PrivateRegistryConfig: privRegistries.Registry,
} }
containerdTemplateBytes, err := ioutil.ReadFile(cfg.Containerd.Template) containerdTemplateBytes, err := os.ReadFile(cfg.Containerd.Template)
if err == nil { if err == nil {
logrus.Infof("Using containerd template at %s", cfg.Containerd.Template) logrus.Infof("Using containerd template at %s", cfg.Containerd.Template)
containerdTemplate = string(containerdTemplateBytes) containerdTemplate = string(containerdTemplateBytes)
......
...@@ -5,7 +5,6 @@ import ( ...@@ -5,7 +5,6 @@ import (
"context" "context"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
...@@ -145,7 +144,7 @@ func preloadImages(ctx context.Context, cfg *config.Node) error { ...@@ -145,7 +144,7 @@ func preloadImages(ctx context.Context, cfg *config.Node) error {
return nil return nil
} }
fileInfos, err := ioutil.ReadDir(cfg.Images) fileInfos, err := os.ReadDir(cfg.Images)
if err != nil { if err != nil {
logrus.Errorf("Unable to read images in %s: %v", cfg.Images, err) logrus.Errorf("Unable to read images in %s: %v", cfg.Images, err)
return nil return nil
......
package flannel package flannel
import ( import (
"io/ioutil"
"net" "net"
"os"
"regexp" "regexp"
"strings" "strings"
"testing" "testing"
...@@ -68,7 +68,7 @@ func Test_createFlannelConf(t *testing.T) { ...@@ -68,7 +68,7 @@ func Test_createFlannelConf(t *testing.T) {
if err := createFlannelConf(nodeConfig); (err != nil) != tt.wantErr { if err := createFlannelConf(nodeConfig); (err != nil) != tt.wantErr {
t.Errorf("createFlannelConf() error = %v, wantErr %v", err, 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 { if err != nil {
t.Errorf("Something went wrong when reading the flannel config file") t.Errorf("Something went wrong when reading the flannel config file")
} }
......
...@@ -2,7 +2,7 @@ package loadbalancer ...@@ -2,7 +2,7 @@ package loadbalancer
import ( import (
"encoding/json" "encoding/json"
"io/ioutil" "os"
"github.com/k3s-io/k3s/pkg/agent/util" "github.com/k3s-io/k3s/pkg/agent/util"
) )
...@@ -17,7 +17,7 @@ func (lb *LoadBalancer) writeConfig() error { ...@@ -17,7 +17,7 @@ func (lb *LoadBalancer) writeConfig() error {
func (lb *LoadBalancer) updateConfig() error { func (lb *LoadBalancer) updateConfig() error {
writeConfig := true writeConfig := true
if configBytes, err := ioutil.ReadFile(lb.configFile); err == nil { if configBytes, err := os.ReadFile(lb.configFile); err == nil {
config := &LoadBalancer{} config := &LoadBalancer{}
if err := json.Unmarshal(configBytes, config); err == nil { if err := json.Unmarshal(configBytes, config); err == nil {
if config.ServerURL == lb.ServerURL { if config.ServerURL == lb.ServerURL {
......
...@@ -5,7 +5,6 @@ import ( ...@@ -5,7 +5,6 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"net" "net"
"net/url" "net/url"
"os" "os"
...@@ -85,7 +84,7 @@ func assertNotEqual(t *testing.T, a interface{}, b interface{}) { ...@@ -85,7 +84,7 @@ func assertNotEqual(t *testing.T, a interface{}, b interface{}) {
} }
func Test_UnitFailOver(t *testing.T) { func Test_UnitFailOver(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "lb-test") tmpDir, err := os.MkdirTemp("", "lb-test")
if err != nil { if err != nil {
assertEqual(t, err, nil) assertEqual(t, err, nil)
} }
...@@ -146,7 +145,7 @@ func Test_UnitFailOver(t *testing.T) { ...@@ -146,7 +145,7 @@ func Test_UnitFailOver(t *testing.T) {
} }
func Test_UnitFailFast(t *testing.T) { func Test_UnitFailFast(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "lb-test") tmpDir, err := os.MkdirTemp("", "lb-test")
if err != nil { if err != nil {
assertEqual(t, err, nil) assertEqual(t, err, nil)
} }
......
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
package agent package agent
import ( import (
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
...@@ -38,5 +37,5 @@ func setupCriCtlConfig(cfg cmds.Agent, nodeConfig *config.Node) error { ...@@ -38,5 +37,5 @@ func setupCriCtlConfig(cfg cmds.Agent, nodeConfig *config.Node) error {
} }
crp := "runtime-endpoint: " + cre + "\n" crp := "runtime-endpoint: " + cre + "\n"
return ioutil.WriteFile(agentConfDir+"/crictl.yaml", []byte(crp), 0600) return os.WriteFile(agentConfDir+"/crictl.yaml", []byte(crp), 0600)
} }
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
package agent package agent
import ( import (
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
...@@ -40,5 +39,5 @@ func setupCriCtlConfig(cfg cmds.Agent, nodeConfig *config.Node) error { ...@@ -40,5 +39,5 @@ func setupCriCtlConfig(cfg cmds.Agent, nodeConfig *config.Node) error {
} }
crp := "runtime-endpoint: " + cre + "\n" 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)
} }
package util package util
import ( import (
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
...@@ -10,7 +9,7 @@ import ( ...@@ -10,7 +9,7 @@ import (
func WriteFile(name string, content string) error { func WriteFile(name string, content string) error {
os.MkdirAll(filepath.Dir(name), 0755) os.MkdirAll(filepath.Dir(name), 0755)
err := ioutil.WriteFile(name, []byte(content), 0644) err := os.WriteFile(name, []byte(content), 0644)
if err != nil { if err != nil {
return errors.Wrapf(err, "writing %s", name) return errors.Wrapf(err, "writing %s", name)
} }
...@@ -19,11 +18,11 @@ func WriteFile(name string, content string) error { ...@@ -19,11 +18,11 @@ func WriteFile(name string, content string) error {
func CopyFile(sourceFile string, destinationFile string) error { func CopyFile(sourceFile string, destinationFile string) error {
os.MkdirAll(filepath.Dir(destinationFile), 0755) os.MkdirAll(filepath.Dir(destinationFile), 0755)
input, err := ioutil.ReadFile(sourceFile) input, err := os.ReadFile(sourceFile)
if err != nil { if err != nil {
return errors.Wrapf(err, "copying %s to %s", sourceFile, destinationFile) 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 { if err != nil {
return errors.Wrapf(err, "copying %s to %s", sourceFile, destinationFile) return errors.Wrapf(err, "copying %s to %s", sourceFile, destinationFile)
} }
......
...@@ -18,7 +18,6 @@ package passwordfile ...@@ -18,7 +18,6 @@ package passwordfile
import ( import (
"context" "context"
"io/ioutil"
"os" "os"
"reflect" "reflect"
"testing" "testing"
...@@ -146,14 +145,14 @@ func Test_UnitInsufficientColumnsPasswordFile(t *testing.T) { ...@@ -146,14 +145,14 @@ func Test_UnitInsufficientColumnsPasswordFile(t *testing.T) {
} }
func newWithContents(t *testing.T, contents string) (auth *PasswordAuthenticator, err error) { 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 { if err != nil {
t.Fatalf("unexpected error creating passwordfile: %v", err) t.Fatalf("unexpected error creating passwordfile: %v", err)
} }
f.Close() f.Close()
defer os.Remove(f.Name()) 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) t.Fatalf("unexpected error writing passwordfile: %v", err)
} }
......
...@@ -3,7 +3,6 @@ package bootstrap ...@@ -3,7 +3,6 @@ package bootstrap
import ( import (
"encoding/json" "encoding/json"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
...@@ -34,7 +33,7 @@ func ReadFromDisk(w io.Writer, bootstrap *config.ControlRuntimeBootstrap) error ...@@ -34,7 +33,7 @@ func ReadFromDisk(w io.Writer, bootstrap *config.ControlRuntimeBootstrap) error
if path == "" { if path == "" {
continue continue
} }
data, err := ioutil.ReadFile(path) data, err := os.ReadFile(path)
if err != nil { if err != nil {
logrus.Warnf("failed to read %s", path) logrus.Warnf("failed to read %s", path)
continue continue
......
...@@ -7,7 +7,6 @@ import ( ...@@ -7,7 +7,6 @@ import (
"bufio" "bufio"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
...@@ -26,7 +25,7 @@ func Validate() error { ...@@ -26,7 +25,7 @@ func Validate() error {
} }
func validateCgroupsV1() error { func validateCgroupsV1() error {
cgroups, err := ioutil.ReadFile("/proc/self/cgroup") cgroups, err := os.ReadFile("/proc/self/cgroup")
if err != nil { if err != nil {
return err return err
} }
......
...@@ -2,7 +2,6 @@ package cert ...@@ -2,7 +2,6 @@ package cert
import ( import (
"errors" "errors"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
...@@ -154,7 +153,7 @@ func rotate(app *cli.Context, cfg *cmds.Server) error { ...@@ -154,7 +153,7 @@ func rotate(app *cli.Context, cfg *cmds.Server) error {
serverConfig.ControlConfig.Runtime.ClientCloudControllerKey) serverConfig.ControlConfig.Runtime.ClientCloudControllerKey)
case version.Program + k3sServerService: case version.Program + k3sServerService:
dynamicListenerRegenFilePath := filepath.Join(serverDataDir, "tls", "dynamic-cert-regenerate") 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 return err
} }
logrus.Infof("Rotating dynamic listener certificate") logrus.Infof("Rotating dynamic listener certificate")
...@@ -199,11 +198,11 @@ func rotate(app *cli.Context, cfg *cmds.Server) error { ...@@ -199,11 +198,11 @@ func rotate(app *cli.Context, cfg *cmds.Server) error {
func copyFile(src, destDir string) error { func copyFile(src, destDir string) error {
_, err := os.Stat(src) _, err := os.Stat(src)
if err == nil { if err == nil {
input, err := ioutil.ReadFile(src) input, err := os.ReadFile(src)
if err != nil { if err != nil {
return err 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) { } else if errors.Is(err, os.ErrNotExist) {
return nil return nil
} }
......
...@@ -4,7 +4,6 @@ import ( ...@@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
...@@ -33,7 +32,7 @@ func commandPrep(app *cli.Context, cfg *cmds.Server) (*clientaccess.Info, error) ...@@ -33,7 +32,7 @@ func commandPrep(app *cli.Context, cfg *cmds.Server) (*clientaccess.Info, error)
if cfg.Token == "" { if cfg.Token == "" {
fp := filepath.Join(dataDir, "token") fp := filepath.Join(dataDir, "token")
tokenByte, err := ioutil.ReadFile(fp) tokenByte, err := os.ReadFile(fp)
if err != nil { if err != nil {
return nil, err return nil, err
} }
......
package clientaccess package clientaccess
import ( import (
"io/ioutil" "os"
"github.com/pkg/errors" "github.com/pkg/errors"
"k8s.io/client-go/tools/clientcmd" "k8s.io/client-go/tools/clientcmd"
...@@ -10,17 +10,17 @@ import ( ...@@ -10,17 +10,17 @@ import (
// WriteClientKubeConfig generates a kubeconfig at destFile that can be used to connect to a server at url with the given certs and keys // WriteClientKubeConfig generates a kubeconfig at destFile that can be used to connect to a server at url with the given certs and keys
func WriteClientKubeConfig(destFile, url, serverCAFile, clientCertFile, clientKeyFile string) error { func WriteClientKubeConfig(destFile, url, serverCAFile, clientCertFile, clientKeyFile string) error {
serverCA, err := ioutil.ReadFile(serverCAFile) serverCA, err := os.ReadFile(serverCAFile)
if err != nil { if err != nil {
return errors.Wrapf(err, "failed to read %s", serverCAFile) return errors.Wrapf(err, "failed to read %s", serverCAFile)
} }
clientCert, err := ioutil.ReadFile(clientCertFile) clientCert, err := os.ReadFile(clientCertFile)
if err != nil { if err != nil {
return errors.Wrapf(err, "failed to read %s", clientCertFile) return errors.Wrapf(err, "failed to read %s", clientCertFile)
} }
clientKey, err := ioutil.ReadFile(clientKeyFile) clientKey, err := os.ReadFile(clientKeyFile)
if err != nil { if err != nil {
return errors.Wrapf(err, "failed to read %s", clientKeyFile) return errors.Wrapf(err, "failed to read %s", clientKeyFile)
} }
......
...@@ -7,9 +7,10 @@ import ( ...@@ -7,9 +7,10 @@ import (
"crypto/x509" "crypto/x509"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"net/url" "net/url"
"os"
"strings" "strings"
"time" "time"
...@@ -296,7 +297,7 @@ func get(u string, client *http.Client, username, password string) ([]byte, erro ...@@ -296,7 +297,7 @@ func get(u string, client *http.Client, username, password string) ([]byte, erro
return nil, fmt.Errorf("%s: %s", u, resp.Status) return nil, fmt.Errorf("%s: %s", u, resp.Status)
} }
return ioutil.ReadAll(resp.Body) return io.ReadAll(resp.Body)
} }
// put makes a request to a url using a provided client, username, and password // put makes a request to a url using a provided client, username, and password
...@@ -317,7 +318,7 @@ func put(u string, body []byte, client *http.Client, username, password string) ...@@ -317,7 +318,7 @@ func put(u string, body []byte, client *http.Client, username, password string)
} }
defer resp.Body.Close() defer resp.Body.Close()
respBody, _ := ioutil.ReadAll(resp.Body) respBody, _ := io.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
return fmt.Errorf("%s: %s %s", u, resp.Status, string(respBody)) return fmt.Errorf("%s: %s %s", u, resp.Status, string(respBody))
} }
...@@ -332,7 +333,7 @@ func FormatToken(token, certFile string) (string, error) { ...@@ -332,7 +333,7 @@ func FormatToken(token, certFile string) (string, error) {
certHash := "" certHash := ""
if len(certFile) > 0 { if len(certFile) > 0 {
b, err := ioutil.ReadFile(certFile) b, err := os.ReadFile(certFile)
if err != nil { if err != nil {
return "", nil return "", nil
} }
......
...@@ -6,7 +6,6 @@ import ( ...@@ -6,7 +6,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net" "net"
"os" "os"
"path/filepath" "path/filepath"
...@@ -340,7 +339,7 @@ func (c *Cluster) ReconcileBootstrapData(ctx context.Context, buf io.ReadSeeker, ...@@ -340,7 +339,7 @@ func (c *Cluster) ReconcileBootstrapData(ctx context.Context, buf io.ReadSeeker,
} }
defer f.Close() defer f.Close()
fData, err := ioutil.ReadAll(f) fData, err := io.ReadAll(f)
if err != nil { if err != nil {
return errors.Wrapf(err, "reconcile failed to read %s", pathKey) return errors.Wrapf(err, "reconcile failed to read %s", pathKey)
} }
......
...@@ -5,7 +5,7 @@ import ( ...@@ -5,7 +5,7 @@ import (
"crypto/tls" "crypto/tls"
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io"
"log" "log"
"net" "net"
"net/http" "net/http"
...@@ -115,7 +115,7 @@ func (c *Cluster) initClusterAndHTTPS(ctx context.Context) error { ...@@ -115,7 +115,7 @@ func (c *Cluster) initClusterAndHTTPS(ctx context.Context) error {
if logrus.IsLevelEnabled(logrus.DebugLevel) { if logrus.IsLevelEnabled(logrus.DebugLevel) {
server.ErrorLog = log.New(logrus.StandardLogger().Writer(), "Cluster-Http-Server ", log.LstdFlags) server.ErrorLog = log.New(logrus.StandardLogger().Writer(), "Cluster-Http-Server ", log.LstdFlags)
} else { } else {
server.ErrorLog = log.New(ioutil.Discard, "Cluster-Http-Server", 0) server.ErrorLog = log.New(io.Discard, "Cluster-Http-Server", 0)
} }
// Start the supervisor http server on the tls listener // Start the supervisor http server on the tls listener
......
...@@ -4,7 +4,6 @@ import ( ...@@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"context" "context"
"errors" "errors"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
...@@ -183,7 +182,7 @@ func getBootstrapKeyFromStorage(ctx context.Context, storageClient client.Client ...@@ -183,7 +182,7 @@ func getBootstrapKeyFromStorage(ctx context.Context, storageClient client.Client
func readTokenFromFile(serverToken, certs, dataDir string) (string, error) { func readTokenFromFile(serverToken, certs, dataDir string) (string, error) {
tokenFile := filepath.Join(dataDir, "token") tokenFile := filepath.Join(dataDir, "token")
b, err := ioutil.ReadFile(tokenFile) b, err := os.ReadFile(tokenFile)
if err != nil { if err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
token, err := clientaccess.FormatToken(serverToken, certs) token, err := clientaccess.FormatToken(serverToken, certs)
......
...@@ -2,7 +2,7 @@ package configfilearg ...@@ -2,7 +2,7 @@ package configfilearg
import ( import (
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
...@@ -183,7 +183,7 @@ func (p *Parser) findStart(args []string) ([]string, []string, bool) { ...@@ -183,7 +183,7 @@ func (p *Parser) findStart(args []string) ([]string, []string, bool) {
} }
func dotDFiles(basefile string) (result []string, _ error) { func dotDFiles(basefile string) (result []string, _ error) {
files, err := ioutil.ReadDir(basefile + ".d") files, err := os.ReadDir(basefile + ".d")
if os.IsNotExist(err) { if os.IsNotExist(err) {
return nil, nil return nil, nil
} else if err != nil { } else if err != nil {
...@@ -295,8 +295,8 @@ func readConfigFileData(file string) ([]byte, error) { ...@@ -295,8 +295,8 @@ func readConfigFileData(file string) ([]byte, error) {
return nil, fmt.Errorf("failed to read http config %s: %w", file, err) return nil, fmt.Errorf("failed to read http config %s: %w", file, err)
} }
defer resp.Body.Close() defer resp.Body.Close()
return ioutil.ReadAll(resp.Body) return io.ReadAll(resp.Body)
default: default:
return ioutil.ReadFile(file) return os.ReadFile(file)
} }
} }
...@@ -10,7 +10,6 @@ import ( ...@@ -10,7 +10,6 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"net" "net"
"os" "os"
"path/filepath" "path/filepath"
...@@ -254,7 +253,7 @@ func genUsers(config *config.Control) error { ...@@ -254,7 +253,7 @@ func genUsers(config *config.Control) error {
func genEncryptedNetworkInfo(controlConfig *config.Control) error { func genEncryptedNetworkInfo(controlConfig *config.Control) error {
runtime := controlConfig.Runtime runtime := controlConfig.Runtime
if s, err := os.Stat(runtime.IPSECKey); err == nil && s.Size() > 0 { if s, err := os.Stat(runtime.IPSECKey); err == nil && s.Size() > 0 {
psk, err := ioutil.ReadFile(runtime.IPSECKey) psk, err := os.ReadFile(runtime.IPSECKey)
if err != nil { if err != nil {
return err return err
} }
...@@ -268,7 +267,7 @@ func genEncryptedNetworkInfo(controlConfig *config.Control) error { ...@@ -268,7 +267,7 @@ func genEncryptedNetworkInfo(controlConfig *config.Control) error {
} }
controlConfig.IPSECPSK = psk controlConfig.IPSECPSK = psk
return ioutil.WriteFile(runtime.IPSECKey, []byte(psk+"\n"), 0600) return os.WriteFile(runtime.IPSECKey, []byte(psk+"\n"), 0600)
} }
func getServerPass(passwd *passwd.Passwd, config *config.Control) (string, error) { func getServerPass(passwd *passwd.Passwd, config *config.Control) (string, error) {
...@@ -672,13 +671,13 @@ func genEncryptionConfigAndState(controlConfig *config.Control) error { ...@@ -672,13 +671,13 @@ func genEncryptionConfigAndState(controlConfig *config.Control) error {
if s, err := os.Stat(runtime.EncryptionConfig); err == nil && s.Size() > 0 { if s, err := os.Stat(runtime.EncryptionConfig); err == nil && s.Size() > 0 {
// On upgrade from older versions, the encryption hash may not exist, create it // On upgrade from older versions, the encryption hash may not exist, create it
if _, err := os.Stat(runtime.EncryptionHash); errors.Is(err, os.ErrNotExist) { if _, err := os.Stat(runtime.EncryptionHash); errors.Is(err, os.ErrNotExist) {
curEncryptionByte, err := ioutil.ReadFile(runtime.EncryptionConfig) curEncryptionByte, err := os.ReadFile(runtime.EncryptionConfig)
if err != nil { if err != nil {
return err return err
} }
encryptionConfigHash := sha256.Sum256(curEncryptionByte) encryptionConfigHash := sha256.Sum256(curEncryptionByte)
ann := "start-" + hex.EncodeToString(encryptionConfigHash[:]) ann := "start-" + hex.EncodeToString(encryptionConfigHash[:])
return ioutil.WriteFile(controlConfig.Runtime.EncryptionHash, []byte(ann), 0600) return os.WriteFile(controlConfig.Runtime.EncryptionHash, []byte(ann), 0600)
} }
return nil return nil
} }
...@@ -720,12 +719,12 @@ func genEncryptionConfigAndState(controlConfig *config.Control) error { ...@@ -720,12 +719,12 @@ func genEncryptionConfigAndState(controlConfig *config.Control) error {
if err != nil { if err != nil {
return err return err
} }
if err := ioutil.WriteFile(runtime.EncryptionConfig, b, 0600); err != nil { if err := os.WriteFile(runtime.EncryptionConfig, b, 0600); err != nil {
return err return err
} }
encryptionConfigHash := sha256.Sum256(b) encryptionConfigHash := sha256.Sum256(b)
ann := "start-" + hex.EncodeToString(encryptionConfigHash[:]) ann := "start-" + hex.EncodeToString(encryptionConfigHash[:])
return ioutil.WriteFile(controlConfig.Runtime.EncryptionHash, []byte(ann), 0600) return os.WriteFile(controlConfig.Runtime.EncryptionHash, []byte(ann), 0600)
} }
func genEgressSelectorConfig(controlConfig *config.Control) error { func genEgressSelectorConfig(controlConfig *config.Control) error {
...@@ -768,7 +767,7 @@ func genEgressSelectorConfig(controlConfig *config.Control) error { ...@@ -768,7 +767,7 @@ func genEgressSelectorConfig(controlConfig *config.Control) error {
if err != nil { if err != nil {
return err return err
} }
return ioutil.WriteFile(controlConfig.Runtime.EgressSelectorConfig, b, 0600) return os.WriteFile(controlConfig.Runtime.EgressSelectorConfig, b, 0600)
} }
func genCloudConfig(controlConfig *config.Control) error { func genCloudConfig(controlConfig *config.Control) error {
...@@ -786,6 +785,6 @@ func genCloudConfig(controlConfig *config.Control) error { ...@@ -786,6 +785,6 @@ func genCloudConfig(controlConfig *config.Control) error {
if err != nil { if err != nil {
return err return err
} }
return ioutil.WriteFile(controlConfig.Runtime.CloudControllerConfig, b, 0600) return os.WriteFile(controlConfig.Runtime.CloudControllerConfig, b, 0600)
} }
...@@ -3,7 +3,7 @@ package executor ...@@ -3,7 +3,7 @@ package executor
import ( import (
"context" "context"
"errors" "errors"
"io/ioutil" "os"
"path/filepath" "path/filepath"
daemonconfig "github.com/k3s-io/k3s/pkg/daemons/config" daemonconfig "github.com/k3s-io/k3s/pkg/daemons/config"
...@@ -37,7 +37,7 @@ func (e *Embedded) ETCD(ctx context.Context, args ETCDConfig, extraArgs []string ...@@ -37,7 +37,7 @@ func (e *Embedded) ETCD(ctx context.Context, args ETCDConfig, extraArgs []string
case err := <-etcd.Server.ErrNotify(): case err := <-etcd.Server.ErrNotify():
if errors.Is(err, rafthttp.ErrMemberRemoved) { if errors.Is(err, rafthttp.ErrMemberRemoved) {
tombstoneFile := filepath.Join(args.DataDir, "tombstone") tombstoneFile := filepath.Join(args.DataDir, "tombstone")
if err := ioutil.WriteFile(tombstoneFile, []byte{}, 0600); err != nil { if err := os.WriteFile(tombstoneFile, []byte{}, 0600); err != nil {
logrus.Fatalf("failed to write tombstone file to %s", tombstoneFile) logrus.Fatalf("failed to write tombstone file to %s", tombstoneFile)
} }
logrus.Infof("this node has been removed from the cluster please restart %s to rejoin the cluster", version.Program) logrus.Infof("this node has been removed from the cluster please restart %s to rejoin the cluster", version.Program)
......
...@@ -2,7 +2,6 @@ package executor ...@@ -2,7 +2,6 @@ package executor
import ( import (
"context" "context"
"io/ioutil"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
...@@ -123,7 +122,7 @@ func (e ETCDConfig) ToConfigFile(extraArgs []string) (string, error) { ...@@ -123,7 +122,7 @@ func (e ETCDConfig) ToConfigFile(extraArgs []string) (string, error) {
if err := os.MkdirAll(e.DataDir, 0700); err != nil { if err := os.MkdirAll(e.DataDir, 0700); err != nil {
return "", err return "", err
} }
return confFile, ioutil.WriteFile(confFile, bytes, 0600) return confFile, os.WriteFile(confFile, bytes, 0600)
} }
func Set(driver Executor) { func Set(driver Executor) {
......
...@@ -7,7 +7,6 @@ import ( ...@@ -7,7 +7,6 @@ import (
"crypto/sha256" "crypto/sha256"
"encoding/hex" "encoding/hex"
"io" "io"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"sort" "sort"
...@@ -176,7 +175,7 @@ func (w *watcher) deploy(path string, compareChecksum bool) error { ...@@ -176,7 +175,7 @@ func (w *watcher) deploy(path string, compareChecksum bool) error {
addon = *newAddon addon = *newAddon
} }
content, err := ioutil.ReadFile(path) content, err := os.ReadFile(path)
if err != nil { if err != nil {
w.recorder.Eventf(&addon, corev1.EventTypeWarning, "ReadManifestFailed", "Read manifest at %q failed: %v", path, err) w.recorder.Eventf(&addon, corev1.EventTypeWarning, "ReadManifestFailed", "Read manifest at %q failed: %v", path, err)
return err return err
...@@ -224,7 +223,7 @@ func (w *watcher) delete(path string) error { ...@@ -224,7 +223,7 @@ func (w *watcher) delete(path string) error {
return err return err
} }
content, err := ioutil.ReadFile(path) content, err := os.ReadFile(path)
if err != nil { if err != nil {
w.recorder.Eventf(&addon, corev1.EventTypeWarning, "ReadManifestFailed", "Read manifest at %q failed: %v", path, err) w.recorder.Eventf(&addon, corev1.EventTypeWarning, "ReadManifestFailed", "Read manifest at %q failed: %v", path, err)
} else { } else {
......
...@@ -4,7 +4,6 @@ package deploy ...@@ -4,7 +4,6 @@ package deploy
import ( import (
"bytes" "bytes"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
...@@ -38,7 +37,7 @@ staging: ...@@ -38,7 +37,7 @@ staging:
p := filepath.Join(dataDir, name) p := filepath.Join(dataDir, name)
os.MkdirAll(filepath.Dir(p), 0700) os.MkdirAll(filepath.Dir(p), 0700)
logrus.Info("Writing manifest: ", p) logrus.Info("Writing manifest: ", p)
if err := ioutil.WriteFile(p, content, 0600); err != nil { if err := os.WriteFile(p, content, 0600); err != nil {
return errors.Wrapf(err, "failed to write to %s", name) return errors.Wrapf(err, "failed to write to %s", name)
} }
} }
......
...@@ -9,7 +9,7 @@ import ( ...@@ -9,7 +9,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/fs"
"net" "net"
"net/http" "net/http"
"net/url" "net/url"
...@@ -352,7 +352,7 @@ func (e *ETCD) Reset(ctx context.Context, rebootstrap func() error) error { ...@@ -352,7 +352,7 @@ func (e *ETCD) Reset(ctx context.Context, rebootstrap func() error) error {
return err return err
} }
// touch a file to avoid multiple resets // touch a file to avoid multiple resets
if err := ioutil.WriteFile(ResetFile(e.config), []byte{}, 0600); err != nil { if err := os.WriteFile(ResetFile(e.config), []byte{}, 0600); err != nil {
return err return err
} }
return e.newCluster(ctx, true) return e.newCluster(ctx, true)
...@@ -561,13 +561,13 @@ func (e *ETCD) Register(ctx context.Context, config *config.Control, handler htt ...@@ -561,13 +561,13 @@ func (e *ETCD) Register(ctx context.Context, config *config.Control, handler htt
// name is used on subsequent calls. // name is used on subsequent calls.
func (e *ETCD) setName(force bool) error { func (e *ETCD) setName(force bool) error {
fileName := nameFile(e.config) fileName := nameFile(e.config)
data, err := ioutil.ReadFile(fileName) data, err := os.ReadFile(fileName)
if os.IsNotExist(err) || force { if os.IsNotExist(err) || force {
e.name = e.config.ServerNodeName + "-" + uuid.New().String()[:8] e.name = e.config.ServerNodeName + "-" + uuid.New().String()[:8]
if err := os.MkdirAll(filepath.Dir(fileName), 0700); err != nil { if err := os.MkdirAll(filepath.Dir(fileName), 0700); err != nil {
return err return err
} }
return ioutil.WriteFile(fileName, []byte(e.name), 0600) return os.WriteFile(fileName, []byte(e.name), 0600)
} else if err != nil { } else if err != nil {
return err return err
} }
...@@ -1488,22 +1488,26 @@ func (e *ETCD) listLocalSnapshots() (map[string]snapshotFile, error) { ...@@ -1488,22 +1488,26 @@ func (e *ETCD) listLocalSnapshots() (map[string]snapshotFile, error) {
return snapshots, errors.Wrap(err, "failed to get the snapshot dir") return snapshots, errors.Wrap(err, "failed to get the snapshot dir")
} }
files, err := ioutil.ReadDir(snapshotDir) dirEntries, err := os.ReadDir(snapshotDir)
if err != nil { if err != nil {
return nil, err return nil, err
} }
nodeName := os.Getenv("NODE_NAME") nodeName := os.Getenv("NODE_NAME")
for _, f := range files { for _, de := range dirEntries {
file, err := de.Info()
if err != nil {
return nil, err
}
sf := snapshotFile{ sf := snapshotFile{
Name: f.Name(), Name: file.Name(),
Location: "file://" + filepath.Join(snapshotDir, f.Name()), Location: "file://" + filepath.Join(snapshotDir, file.Name()),
NodeName: nodeName, NodeName: nodeName,
CreatedAt: &metav1.Time{ CreatedAt: &metav1.Time{
Time: f.ModTime(), Time: file.ModTime(),
}, },
Size: f.Size(), Size: file.Size(),
Status: successfulSnapshotStatus, Status: successfulSnapshotStatus,
} }
sfKey := generateSnapshotConfigMapKey(sf) sfKey := generateSnapshotConfigMapKey(sf)
...@@ -2024,7 +2028,18 @@ func backupDirWithRetention(dir string, maxBackupRetention int) (string, error) ...@@ -2024,7 +2028,18 @@ func backupDirWithRetention(dir string, maxBackupRetention int) (string, error)
if _, err := os.Stat(dir); err != nil { if _, err := os.Stat(dir); err != nil {
return "", nil return "", nil
} }
files, err := ioutil.ReadDir(filepath.Dir(dir)) entries, err := os.ReadDir(filepath.Dir(dir))
if err != nil {
return "", err
}
files := make([]fs.FileInfo, 0, len(entries))
for _, entry := range entries {
info, err := entry.Info()
if err != nil {
return "", err
}
files = append(files, info)
}
if err != nil { if err != nil {
return "", err return "", err
} }
......
...@@ -8,7 +8,6 @@ import ( ...@@ -8,7 +8,6 @@ import (
"encoding/pem" "encoding/pem"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
...@@ -269,7 +268,7 @@ func (s *S3) snapshotRetention(ctx context.Context) error { ...@@ -269,7 +268,7 @@ func (s *S3) snapshotRetention(ctx context.Context) error {
func readS3EndpointCA(endpointCA string) ([]byte, error) { func readS3EndpointCA(endpointCA string) ([]byte, error) {
ca, err := base64.StdEncoding.DecodeString(endpointCA) ca, err := base64.StdEncoding.DecodeString(endpointCA)
if err != nil { if err != nil {
return ioutil.ReadFile(endpointCA) return os.ReadFile(endpointCA)
} }
return ca, nil return ca, nil
} }
......
...@@ -2,7 +2,6 @@ package nodepassword ...@@ -2,7 +2,6 @@ package nodepassword
import ( import (
"fmt" "fmt"
"io/ioutil"
"log" "log"
"os" "os"
"runtime" "runtime"
...@@ -209,7 +208,7 @@ func assertNotEqual(t *testing.T, a interface{}, b interface{}) { ...@@ -209,7 +208,7 @@ func assertNotEqual(t *testing.T, a interface{}, b interface{}) {
} }
func generateNodePasswordFile(migrateNumNodes int) string { func generateNodePasswordFile(migrateNumNodes int) string {
tempFile, err := ioutil.TempFile("", "node-password-test.*") tempFile, err := os.CreateTemp("", "node-password-test.*")
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
...@@ -219,7 +218,7 @@ func generateNodePasswordFile(migrateNumNodes int) string { ...@@ -219,7 +218,7 @@ func generateNodePasswordFile(migrateNumNodes int) string {
for i := 1; i <= migrateNumNodes; i++ { for i := 1; i <= migrateNumNodes; i++ {
passwordEntries += fmt.Sprintf("node%d,node%d\n", i, i) passwordEntries += fmt.Sprintf("node%d,node%d\n", i, i)
} }
if err := ioutil.WriteFile(tempFile.Name(), []byte(passwordEntries), 0600); err != nil { if err := os.WriteFile(tempFile.Name(), []byte(passwordEntries), 0600); err != nil {
log.Fatal(err) log.Fatal(err)
} }
......
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
package rootless package rootless
import ( import (
"io/ioutil"
"net" "net"
"os" "os"
"os/exec" "os/exec"
...@@ -100,7 +99,7 @@ func validateSysctl() error { ...@@ -100,7 +99,7 @@ func validateSysctl() error {
func readSysctl(key string) (string, error) { func readSysctl(key string) (string, error) {
p := "/proc/sys/" + strings.ReplaceAll(key, ".", "/") p := "/proc/sys/" + strings.ReplaceAll(key, ".", "/")
b, err := ioutil.ReadFile(p) b, err := os.ReadFile(p)
if err != nil { if err != nil {
return "", err return "", err
} }
...@@ -126,7 +125,7 @@ func createParentOpt(stateDir string) (*parent.Opt, error) { ...@@ -126,7 +125,7 @@ func createParentOpt(stateDir string) (*parent.Opt, error) {
return nil, errors.Wrapf(err, "failed to mkdir %s", stateDir) return nil, errors.Wrapf(err, "failed to mkdir %s", stateDir)
} }
stateDir, err := ioutil.TempDir("", "rootless") stateDir, err := os.MkdirTemp("", "rootless")
if err != nil { if err != nil {
return nil, err return nil, err
} }
......
...@@ -5,7 +5,7 @@ import ( ...@@ -5,7 +5,7 @@ import (
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "os"
"github.com/k3s-io/k3s/pkg/daemons/config" "github.com/k3s-io/k3s/pkg/daemons/config"
"github.com/k3s-io/k3s/pkg/version" "github.com/k3s-io/k3s/pkg/version"
...@@ -28,7 +28,7 @@ const ( ...@@ -28,7 +28,7 @@ const (
var EncryptionHashAnnotation = version.Program + ".io/encryption-config-hash" var EncryptionHashAnnotation = version.Program + ".io/encryption-config-hash"
func GetEncryptionProviders(runtime *config.ControlRuntime) ([]apiserverconfigv1.ProviderConfiguration, error) { func GetEncryptionProviders(runtime *config.ControlRuntime) ([]apiserverconfigv1.ProviderConfiguration, error) {
curEncryptionByte, err := ioutil.ReadFile(runtime.EncryptionConfig) curEncryptionByte, err := os.ReadFile(runtime.EncryptionConfig)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -106,11 +106,11 @@ func WriteEncryptionConfig(runtime *config.ControlRuntime, keys []apiserverconfi ...@@ -106,11 +106,11 @@ func WriteEncryptionConfig(runtime *config.ControlRuntime, keys []apiserverconfi
if err != nil { if err != nil {
return err return err
} }
return ioutil.WriteFile(runtime.EncryptionConfig, jsonfile, 0600) return os.WriteFile(runtime.EncryptionConfig, jsonfile, 0600)
} }
func GenEncryptionConfigHash(runtime *config.ControlRuntime) (string, error) { func GenEncryptionConfigHash(runtime *config.ControlRuntime) (string, error) {
curEncryptionByte, err := ioutil.ReadFile(runtime.EncryptionConfig) curEncryptionByte, err := os.ReadFile(runtime.EncryptionConfig)
if err != nil { if err != nil {
return "", err return "", err
} }
...@@ -140,7 +140,7 @@ func GenReencryptHash(runtime *config.ControlRuntime, keyName string) (string, e ...@@ -140,7 +140,7 @@ func GenReencryptHash(runtime *config.ControlRuntime, keyName string) (string, e
} }
func getEncryptionHashFile(runtime *config.ControlRuntime) (string, error) { func getEncryptionHashFile(runtime *config.ControlRuntime) (string, error) {
curEncryptionByte, err := ioutil.ReadFile(runtime.EncryptionHash) curEncryptionByte, err := os.ReadFile(runtime.EncryptionHash)
if err != nil { if err != nil {
return "", err return "", err
} }
...@@ -170,5 +170,5 @@ func WriteEncryptionHashAnnotation(runtime *config.ControlRuntime, node *corev1. ...@@ -170,5 +170,5 @@ func WriteEncryptionHashAnnotation(runtime *config.ControlRuntime, node *corev1.
return err return err
} }
logrus.Debugf("encryption hash annotation set successfully on node: %s\n", node.ObjectMeta.Name) logrus.Debugf("encryption hash annotation set successfully on node: %s\n", node.ObjectMeta.Name)
return ioutil.WriteFile(runtime.EncryptionHash, []byte(ann), 0600) return os.WriteFile(runtime.EncryptionHash, []byte(ann), 0600)
} }
...@@ -2,7 +2,6 @@ package server ...@@ -2,7 +2,6 @@ package server
import ( import (
"context" "context"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"time" "time"
...@@ -75,7 +74,7 @@ func setETCDLabelsAndAnnotations(ctx context.Context, config *Config) error { ...@@ -75,7 +74,7 @@ func setETCDLabelsAndAnnotations(ctx context.Context, config *Config) error {
} }
fileName := filepath.Join(controlConfig.DataDir, "db", "etcd", "name") fileName := filepath.Join(controlConfig.DataDir, "db", "etcd", "name")
data, err := ioutil.ReadFile(fileName) data, err := os.ReadFile(fileName)
if err != nil { if err != nil {
logrus.Infof("Waiting for etcd node name file to be available: %v", err) logrus.Infof("Waiting for etcd node name file to be available: %v", err)
continue continue
......
...@@ -5,7 +5,6 @@ import ( ...@@ -5,7 +5,6 @@ import (
"crypto" "crypto"
"crypto/x509" "crypto/x509"
"fmt" "fmt"
"io/ioutil"
"net" "net"
"net/http" "net/http"
"os" "os"
...@@ -131,7 +130,7 @@ func cacerts(serverCA string) http.Handler { ...@@ -131,7 +130,7 @@ func cacerts(serverCA string) http.Handler {
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) { return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
if ca == nil { if ca == nil {
var err error var err error
ca, err = ioutil.ReadFile(serverCA) ca, err = os.ReadFile(serverCA)
if err != nil { if err != nil {
sendError(err, resp) sendError(err, resp)
return return
...@@ -157,7 +156,7 @@ func getNodeInfo(req *http.Request) (string, string, error) { ...@@ -157,7 +156,7 @@ func getNodeInfo(req *http.Request) (string, string, error) {
} }
func getCACertAndKeys(caCertFile, caKeyFile, signingKeyFile string) ([]*x509.Certificate, crypto.Signer, crypto.Signer, error) { func getCACertAndKeys(caCertFile, caKeyFile, signingKeyFile string) ([]*x509.Certificate, crypto.Signer, crypto.Signer, error) {
keyBytes, err := ioutil.ReadFile(signingKeyFile) keyBytes, err := os.ReadFile(signingKeyFile)
if err != nil { if err != nil {
return nil, nil, nil, err return nil, nil, nil, err
} }
...@@ -167,7 +166,7 @@ func getCACertAndKeys(caCertFile, caKeyFile, signingKeyFile string) ([]*x509.Cer ...@@ -167,7 +166,7 @@ func getCACertAndKeys(caCertFile, caKeyFile, signingKeyFile string) ([]*x509.Cer
return nil, nil, nil, err return nil, nil, nil, err
} }
caKeyBytes, err := ioutil.ReadFile(caKeyFile) caKeyBytes, err := os.ReadFile(caKeyFile)
if err != nil { if err != nil {
return nil, nil, nil, err return nil, nil, nil, err
} }
...@@ -177,7 +176,7 @@ func getCACertAndKeys(caCertFile, caKeyFile, signingKeyFile string) ([]*x509.Cer ...@@ -177,7 +176,7 @@ func getCACertAndKeys(caCertFile, caKeyFile, signingKeyFile string) ([]*x509.Cer
return nil, nil, nil, err return nil, nil, nil, err
} }
caBytes, err := ioutil.ReadFile(caCertFile) caBytes, err := os.ReadFile(caCertFile)
if err != nil { if err != nil {
return nil, nil, nil, err return nil, nil, nil, err
} }
...@@ -235,7 +234,7 @@ func servingKubeletCert(server *config.Control, keyFile string, auth nodePassBoo ...@@ -235,7 +234,7 @@ func servingKubeletCert(server *config.Control, keyFile string, auth nodePassBoo
return return
} }
keyBytes, err := ioutil.ReadFile(keyFile) keyBytes, err := os.ReadFile(keyFile)
if err != nil { if err != nil {
http.Error(resp, err.Error(), http.StatusInternalServerError) http.Error(resp, err.Error(), http.StatusInternalServerError)
return return
...@@ -275,7 +274,7 @@ func clientKubeletCert(server *config.Control, keyFile string, auth nodePassBoot ...@@ -275,7 +274,7 @@ func clientKubeletCert(server *config.Control, keyFile string, auth nodePassBoot
return return
} }
keyBytes, err := ioutil.ReadFile(keyFile) keyBytes, err := os.ReadFile(keyFile)
if err != nil { if err != nil {
http.Error(resp, err.Error(), http.StatusInternalServerError) http.Error(resp, err.Error(), http.StatusInternalServerError)
return return
...@@ -300,7 +299,7 @@ func fileHandler(fileName ...string) http.Handler { ...@@ -300,7 +299,7 @@ func fileHandler(fileName ...string) http.Handler {
} }
for _, f := range fileName { for _, f := range fileName {
bytes, err := ioutil.ReadFile(f) bytes, err := os.ReadFile(f)
if err != nil { if err != nil {
logrus.Errorf("Failed to read %s: %v", f, err) logrus.Errorf("Failed to read %s: %v", f, err)
resp.WriteHeader(http.StatusInternalServerError) resp.WriteHeader(http.StatusInternalServerError)
...@@ -442,7 +441,7 @@ func verifyLocalPassword(ctx context.Context, config *Config, mu *sync.Mutex, de ...@@ -442,7 +441,7 @@ func verifyLocalPassword(ctx context.Context, config *Config, mu *sync.Mutex, de
nodeConfigPath := filepath.Join(nodePasswordRoot, "etc", "rancher", "node") nodeConfigPath := filepath.Join(nodePasswordRoot, "etc", "rancher", "node")
nodePasswordFile := filepath.Join(nodeConfigPath, "password") nodePasswordFile := filepath.Join(nodeConfigPath, "password")
passBytes, err := ioutil.ReadFile(nodePasswordFile) passBytes, err := os.ReadFile(nodePasswordFile)
if err != nil { if err != nil {
return "", http.StatusInternalServerError, errors.Wrap(err, "unable to read node password file") return "", http.StatusInternalServerError, errors.Wrap(err, "unable to read node password file")
} }
......
...@@ -6,7 +6,7 @@ import ( ...@@ -6,7 +6,7 @@ import (
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io"
"math/big" "math/big"
"net/http" "net/http"
"os" "os"
...@@ -43,7 +43,7 @@ type EncryptionRequest struct { ...@@ -43,7 +43,7 @@ type EncryptionRequest struct {
} }
func getEncryptionRequest(req *http.Request) (EncryptionRequest, error) { func getEncryptionRequest(req *http.Request) (EncryptionRequest, error) {
b, err := ioutil.ReadAll(req.Body) b, err := io.ReadAll(req.Body)
if err != nil { if err != nil {
return EncryptionRequest{}, err return EncryptionRequest{}, err
} }
......
...@@ -3,7 +3,6 @@ package server ...@@ -3,7 +3,6 @@ package server
import ( import (
"context" "context"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
...@@ -446,7 +445,7 @@ func writeToken(token, file, certs string) error { ...@@ -446,7 +445,7 @@ func writeToken(token, file, certs string) error {
if err != nil { if err != nil {
return err return err
} }
return ioutil.WriteFile(file, []byte(token+"\n"), 0600) return os.WriteFile(file, []byte(token+"\n"), 0600)
} }
func setNoProxyEnv(config *config.Control) error { func setNoProxyEnv(config *config.Control) error {
......
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
package static package static
import ( import (
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
...@@ -20,7 +19,7 @@ func Stage(dataDir string) error { ...@@ -20,7 +19,7 @@ func Stage(dataDir string) error {
p := filepath.Join(dataDir, name) p := filepath.Join(dataDir, name)
logrus.Info("Writing static file: ", p) logrus.Info("Writing static file: ", p)
os.MkdirAll(filepath.Dir(p), 0700) os.MkdirAll(filepath.Dir(p), 0700)
if err := ioutil.WriteFile(p, content, 0600); err != nil { if err := os.WriteFile(p, content, 0600); err != nil {
return errors.Wrapf(err, "failed to write to %s", name) return errors.Wrapf(err, "failed to write to %s", name)
} }
} }
......
...@@ -3,7 +3,6 @@ package token ...@@ -3,7 +3,6 @@ package token
import ( import (
cryptorand "crypto/rand" cryptorand "crypto/rand"
"encoding/hex" "encoding/hex"
"io/ioutil"
"os" "os"
"strings" "strings"
"time" "time"
...@@ -26,7 +25,7 @@ func ReadFile(path string) (string, error) { ...@@ -26,7 +25,7 @@ func ReadFile(path string) (string, error) {
} }
for { for {
tokenBytes, err := ioutil.ReadFile(path) tokenBytes, err := os.ReadFile(path)
if err == nil { if err == nil {
return strings.TrimSpace(string(tokenBytes)), nil return strings.TrimSpace(string(tokenBytes)), nil
} else if os.IsNotExist(err) { } else if os.IsNotExist(err) {
......
...@@ -2,7 +2,7 @@ package e2e ...@@ -2,7 +2,7 @@ package e2e
import ( import (
"fmt" "fmt"
"io/ioutil" "io"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
...@@ -124,7 +124,7 @@ func DeployWorkload(workload, kubeconfig string, hardened bool) (string, error) ...@@ -124,7 +124,7 @@ func DeployWorkload(workload, kubeconfig string, hardened bool) (string, error)
if hardened { if hardened {
resourceDir = "../cis_amd64_resource_files" resourceDir = "../cis_amd64_resource_files"
} }
files, err := ioutil.ReadDir(resourceDir) files, err := os.ReadDir(resourceDir)
if err != nil { if err != nil {
err = fmt.Errorf("%s : Unable to read resource manifest file for %s", err, workload) err = fmt.Errorf("%s : Unable to read resource manifest file for %s", err, workload)
return "", err return "", err
...@@ -207,7 +207,7 @@ func GetVagrantLog() string { ...@@ -207,7 +207,7 @@ func GetVagrantLog() string {
if err != nil { if err != nil {
return err.Error() return err.Error()
} }
bytes, err := ioutil.ReadAll(log) bytes, err := io.ReadAll(log)
if err != nil { if err != nil {
return err.Error() return err.Error()
} }
......
...@@ -6,7 +6,7 @@ import ( ...@@ -6,7 +6,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io"
"os" "os"
"os/exec" "os/exec"
"os/user" "os/user"
...@@ -231,8 +231,10 @@ func K3sStartServer(inputArgs ...string) (*K3sServer, error) { ...@@ -231,8 +231,10 @@ func K3sStartServer(inputArgs ...string) (*K3sServer, error) {
// K3sKillServer terminates the running K3s server and its children // K3sKillServer terminates the running K3s server and its children
func K3sKillServer(server *K3sServer) error { func K3sKillServer(server *K3sServer) error {
server.log.Close() if server.log != nil {
os.Remove(server.log.Name()) server.log.Close()
os.Remove(server.log.Name())
}
pgid, err := syscall.Getpgid(server.cmd.Process.Pid) pgid, err := syscall.Getpgid(server.cmd.Process.Pid)
if err != nil { if err != nil {
if errors.Is(err, syscall.ESRCH) { if errors.Is(err, syscall.ESRCH) {
...@@ -297,7 +299,7 @@ func K3sDumpLog(server *K3sServer) error { ...@@ -297,7 +299,7 @@ func K3sDumpLog(server *K3sServer) error {
return err return err
} }
defer log.Close() defer log.Close()
b, err := ioutil.ReadAll(log) b, err := io.ReadAll(log)
if err != nil { if err != nil {
return err return err
} }
......
...@@ -68,12 +68,13 @@ var _ = Describe("secrets encryption rotation", Ordered, func() { ...@@ -68,12 +68,13 @@ var _ = Describe("secrets encryption rotation", Ordered, func() {
Eventually(func() error { Eventually(func() error {
return testutil.K3sDefaultDeployments() return testutil.K3sDefaultDeployments()
}, "180s", "5s").Should(Succeed()) }, "180s", "5s").Should(Succeed())
Eventually(func() (string, error) {
return testutil.K3sCmd("secrets-encrypt status -d", secretsEncryptionDataDir)
}, "30s", "5s").Should(ContainSubstring("Current Rotation Stage: prepare"))
}) })
It("rotates the keys", func() { It("rotates the keys", func() {
Eventually(func() (string, error) { Expect(testutil.K3sCmd("secrets-encrypt rotate -d", secretsEncryptionDataDir)).
return testutil.K3sCmd("secrets-encrypt rotate -d", secretsEncryptionDataDir) To(ContainSubstring("rotate completed successfully"))
}, "10s", "2s").Should(ContainSubstring("rotate completed successfully"))
result, err := testutil.K3sCmd("secrets-encrypt status -d", secretsEncryptionDataDir) result, err := testutil.K3sCmd("secrets-encrypt status -d", secretsEncryptionDataDir)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(result).To(ContainSubstring("Current Rotation Stage: rotate")) Expect(result).To(ContainSubstring("Current Rotation Stage: rotate"))
......
...@@ -3,8 +3,8 @@ package e2e ...@@ -3,8 +3,8 @@ package e2e
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"io/ioutil"
"log" "log"
"os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strings" "strings"
...@@ -43,7 +43,7 @@ func checkError(e error) { ...@@ -43,7 +43,7 @@ func checkError(e error) {
} }
func publicKey(path string) ssh.AuthMethod { func publicKey(path string) ssh.AuthMethod {
key, err := ioutil.ReadFile(path) key, err := os.ReadFile(path)
if err != nil { if err != nil {
panic(err) panic(err)
} }
...@@ -121,7 +121,7 @@ func DeployWorkload(workload, kubeconfig string, arch bool) (string, error) { ...@@ -121,7 +121,7 @@ func DeployWorkload(workload, kubeconfig string, arch bool) (string, error) {
if arch { if arch {
resourceDir = "./arm64_resource_files" resourceDir = "./arm64_resource_files"
} }
files, err := ioutil.ReadDir(resourceDir) files, err := os.ReadDir(resourceDir)
if err != nil { if err != nil {
err = fmt.Errorf("%s : Unable to read resource manifest file for %s", err, workload) err = fmt.Errorf("%s : Unable to read resource manifest file for %s", err, workload)
return "", err return "", err
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment