1
0
mirror of https://github.com/rancher/rke.git synced 2025-08-10 19:22:41 +00:00

Replace deprecated io/ioutil

Signed-off-by: Manuel Buil <mbuil@suse.com>
This commit is contained in:
Manuel Buil 2023-08-04 13:01:50 +02:00
parent d80fc55b60
commit f923a49d9d
15 changed files with 42 additions and 49 deletions

View File

@ -5,7 +5,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"os" "os"
"os/exec" "os/exec"
@ -260,7 +260,7 @@ func (c *Cluster) deployAddonsInclude(ctx context.Context) error {
manifests = append(manifests, addonYAML...) manifests = append(manifests, addonYAML...)
} else if isFilePath(addon) { } else if isFilePath(addon) {
addonYAML, err := ioutil.ReadFile(addon) addonYAML, err := os.ReadFile(addon)
if err != nil { if err != nil {
return err return err
} }
@ -324,7 +324,7 @@ func getAddonFromURL(yamlURL string) ([]byte, error) {
defer resp.Body.Close() defer resp.Body.Close()
addonYaml, err := ioutil.ReadAll(resp.Body) addonYaml, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -4,7 +4,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
@ -271,7 +271,7 @@ func ReadStateFile(ctx context.Context, statePath string) (*FullState, error) {
return rkeFullState, fmt.Errorf("Can not find RKE state file: %v", err) return rkeFullState, fmt.Errorf("Can not find RKE state file: %v", err)
} }
defer file.Close() defer file.Close()
buf, err := ioutil.ReadAll(file) buf, err := io.ReadAll(file)
if err != nil { if err != nil {
return rkeFullState, fmt.Errorf("failed to read state file: %v", err) return rkeFullState, fmt.Errorf("failed to read state file: %v", err)
} }

View File

@ -3,7 +3,7 @@ package cmd
import ( import (
"context" "context"
"fmt" "fmt"
"io/ioutil" "io"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -40,7 +40,7 @@ func resolveClusterFile(ctx *cli.Context) (string, string, error) {
return "", "", fmt.Errorf("can not find cluster configuration file: %v", err) return "", "", fmt.Errorf("can not find cluster configuration file: %v", err)
} }
defer file.Close() defer file.Close()
buf, err := ioutil.ReadAll(file) buf, err := io.ReadAll(file)
if err != nil { if err != nil {
return "", "", fmt.Errorf("failed to read file: %v", err) return "", "", fmt.Errorf("failed to read file: %v", err)
} }

View File

@ -4,7 +4,6 @@ import (
"bufio" "bufio"
"context" "context"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"reflect" "reflect"
"strconv" "strconv"
@ -97,7 +96,7 @@ func writeConfig(cluster *v3.RancherKubernetesEngineConfig, configFile string, p
fmt.Printf("Configuration File: \n%s", configString) fmt.Printf("Configuration File: \n%s", configString)
return nil return nil
} }
return ioutil.WriteFile(configFile, []byte(configString), 0640) return os.WriteFile(configFile, []byte(configString), 0640)
} }
func clusterConfig(ctx *cli.Context) error { func clusterConfig(ctx *cli.Context) error {

View File

@ -2,7 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"os" "os"
@ -26,13 +26,13 @@ func main() {
} }
defer data.Body.Close() defer data.Body.Close()
b, err := ioutil.ReadAll(data.Body) b, err := io.ReadAll(data.Body)
if err != nil { if err != nil {
panic(err) panic(err)
} }
fmt.Println("Writing data") fmt.Println("Writing data")
if err := ioutil.WriteFile(dataFile, b, 0755); err != nil { if err := os.WriteFile(dataFile, b, 0755); err != nil {
return return
} }
} }

View File

@ -8,7 +8,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"strings" "strings"
"time" "time"
@ -311,7 +310,7 @@ func pullImage(ctx context.Context, dClient *client.Client, hostname string, con
if logrus.GetLevel() == logrus.TraceLevel { if logrus.GetLevel() == logrus.TraceLevel {
io.Copy(os.Stdout, out) io.Copy(os.Stdout, out)
} else { } else {
io.Copy(ioutil.Discard, out) io.Copy(io.Discard, out)
} }
return nil return nil
} }
@ -647,7 +646,7 @@ func ReadFileFromContainer(ctx context.Context, dClient *client.Client, hostname
if _, err := tarReader.Next(); err != nil { if _, err := tarReader.Next(); err != nil {
return "", err return "", err
} }
file, err := ioutil.ReadAll(tarReader) file, err := io.ReadAll(tarReader)
if err != nil { if err != nil {
return "", err return "", err
} }

View File

@ -3,7 +3,6 @@ package hosts
import ( import (
"context" "context"
"fmt" "fmt"
"io/ioutil"
"net" "net"
"os" "os"
"path/filepath" "path/filepath"
@ -136,7 +135,7 @@ func privateKeyPath(sshKeyPath string) (string, error) {
if sshKeyPath[:2] == "~/" { if sshKeyPath[:2] == "~/" {
sshKeyPath = filepath.Join(userHome(), sshKeyPath[2:]) sshKeyPath = filepath.Join(userHome(), sshKeyPath[2:])
} }
buff, err := ioutil.ReadFile(sshKeyPath) buff, err := os.ReadFile(sshKeyPath)
if err != nil { if err != nil {
return "", fmt.Errorf("Error while reading SSH key file: %v", err) return "", fmt.Errorf("Error while reading SSH key file: %v", err)
} }
@ -147,7 +146,7 @@ func certificatePath(sshCertPath string) (string, error) {
if sshCertPath[:2] == "~/" { if sshCertPath[:2] == "~/" {
sshCertPath = filepath.Join(userHome(), sshCertPath[2:]) sshCertPath = filepath.Join(userHome(), sshCertPath[2:])
} }
buff, err := ioutil.ReadFile(sshCertPath) buff, err := os.ReadFile(sshCertPath)
if err != nil { if err != nil {
return "", fmt.Errorf("Error while reading SSH certificate file: %v", err) return "", fmt.Errorf("Error while reading SSH certificate file: %v", err)
} }

View File

@ -4,7 +4,7 @@
package main package main
import ( import (
"io/ioutil" "io"
"os" "os"
"regexp" "regexp"
@ -34,7 +34,7 @@ func mainErr() error {
app.Usage = "Rancher Kubernetes Engine, an extremely simple, lightning fast Kubernetes installer that works everywhere" app.Usage = "Rancher Kubernetes Engine, an extremely simple, lightning fast Kubernetes installer that works everywhere"
app.Before = func(ctx *cli.Context) error { app.Before = func(ctx *cli.Context) error {
if ctx.GlobalBool("quiet") { if ctx.GlobalBool("quiet") {
logrus.SetOutput(ioutil.Discard) logrus.SetOutput(io.Discard)
} else { } else {
if ctx.GlobalBool("debug") { if ctx.GlobalBool("debug") {
logrus.SetLevel(logrus.DebugLevel) logrus.SetLevel(logrus.DebugLevel)

View File

@ -4,7 +4,7 @@ import (
"context" "context"
"crypto/sha256" "crypto/sha256"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"os" "os"
"strings" "strings"
@ -85,9 +85,9 @@ func readFile(file string) ([]byte, error) {
return nil, err return nil, err
} }
defer resp.Body.Close() defer resp.Body.Close()
return ioutil.ReadAll(resp.Body) return io.ReadAll(resp.Body)
} }
return ioutil.ReadFile(file) return os.ReadFile(file)
} }
const RKEVersionDev = "v1.4.99" const RKEVersionDev = "v1.4.99"

View File

@ -19,7 +19,6 @@ package cert
import ( import (
"crypto/x509" "crypto/x509"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
) )
@ -66,7 +65,7 @@ func WriteCert(certPath string, data []byte) error {
if err := os.MkdirAll(filepath.Dir(certPath), os.FileMode(0755)); err != nil { if err := os.MkdirAll(filepath.Dir(certPath), os.FileMode(0755)); err != nil {
return err return err
} }
return ioutil.WriteFile(certPath, data, os.FileMode(0644)) return os.WriteFile(certPath, data, os.FileMode(0644))
} }
// WriteKey writes the pem-encoded key data to keyPath. // WriteKey writes the pem-encoded key data to keyPath.
@ -77,13 +76,13 @@ func WriteKey(keyPath string, data []byte) error {
if err := os.MkdirAll(filepath.Dir(keyPath), os.FileMode(0755)); err != nil { if err := os.MkdirAll(filepath.Dir(keyPath), os.FileMode(0755)); err != nil {
return err return err
} }
return ioutil.WriteFile(keyPath, data, os.FileMode(0600)) return os.WriteFile(keyPath, data, os.FileMode(0600))
} }
// LoadOrGenerateKeyFile looks for a key in the file at the given path. If it // LoadOrGenerateKeyFile looks for a key in the file at the given path. If it
// can't find one, it will generate a new key and store it there. // can't find one, it will generate a new key and store it there.
func LoadOrGenerateKeyFile(keyPath string) (data []byte, wasGenerated bool, err error) { func LoadOrGenerateKeyFile(keyPath string) (data []byte, wasGenerated bool, err error) {
loadedData, err := ioutil.ReadFile(keyPath) loadedData, err := os.ReadFile(keyPath)
if err == nil { if err == nil {
return loadedData, false, err return loadedData, false, err
} }
@ -118,7 +117,7 @@ func NewPool(filename string) (*x509.CertPool, error) {
// CertsFromFile returns the x509.Certificates contained in the given PEM-encoded file. // CertsFromFile returns the x509.Certificates contained in the given PEM-encoded file.
// Returns an error if the file could not be read, a certificate could not be parsed, or if the file does not contain any certificates // Returns an error if the file could not be read, a certificate could not be parsed, or if the file does not contain any certificates
func CertsFromFile(file string) ([]*x509.Certificate, error) { func CertsFromFile(file string) ([]*x509.Certificate, error) {
pemBlock, err := ioutil.ReadFile(file) pemBlock, err := os.ReadFile(file)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -132,7 +131,7 @@ func CertsFromFile(file string) ([]*x509.Certificate, error) {
// PrivateKeyFromFile returns the private key in rsa.PrivateKey or ecdsa.PrivateKey format from a given PEM-encoded file. // PrivateKeyFromFile returns the private key in rsa.PrivateKey or ecdsa.PrivateKey format from a given PEM-encoded file.
// Returns an error if the file could not be read or if the private key could not be parsed. // Returns an error if the file could not be read or if the private key could not be parsed.
func PrivateKeyFromFile(file string) (interface{}, error) { func PrivateKeyFromFile(file string) (interface{}, error) {
data, err := ioutil.ReadFile(file) data, err := os.ReadFile(file)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -146,7 +145,7 @@ func PrivateKeyFromFile(file string) (interface{}, error) {
// PublicKeysFromFile returns the public keys in rsa.PublicKey or ecdsa.PublicKey format from a given PEM-encoded file. // PublicKeysFromFile returns the public keys in rsa.PublicKey or ecdsa.PublicKey format from a given PEM-encoded file.
// Reads public keys from both public and private key files. // Reads public keys from both public and private key files.
func PublicKeysFromFile(file string) ([]interface{}, error) { func PublicKeysFromFile(file string) ([]interface{}, error) {
data, err := ioutil.ReadFile(file) data, err := os.ReadFile(file)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -4,7 +4,6 @@ import (
"context" "context"
"crypto/rsa" "crypto/rsa"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path" "path"
"strings" "strings"
@ -209,7 +208,7 @@ func DeployAdminConfig(ctx context.Context, kubeConfig, localConfigPath string)
} }
logrus.Debugf("Deploying admin Kubeconfig locally at [%s]", localConfigPath) logrus.Debugf("Deploying admin Kubeconfig locally at [%s]", localConfigPath)
logrus.Tracef("Deploying admin Kubeconfig locally: %s", kubeConfig) logrus.Tracef("Deploying admin Kubeconfig locally: %s", kubeConfig)
err := ioutil.WriteFile(localConfigPath, []byte(kubeConfig), 0600) err := os.WriteFile(localConfigPath, []byte(kubeConfig), 0600)
if err != nil { if err != nil {
return fmt.Errorf("Failed to create local admin kubeconfig file: %v", err) return fmt.Errorf("Failed to create local admin kubeconfig file: %v", err)
} }

View File

@ -9,7 +9,6 @@ import (
"encoding/pem" "encoding/pem"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"math" "math"
"math/big" "math/big"
"net" "net"
@ -558,7 +557,7 @@ func ReadCSRsAndKeysFromDir(certDir string) (map[string]CertificatePKI, error) {
return certMap, nil return certMap, nil
} }
files, err := ioutil.ReadDir(certDir) files, err := os.ReadDir(certDir)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -590,7 +589,7 @@ func ReadCertsAndKeysFromDir(certDir string) (map[string]CertificatePKI, error)
return certMap, nil return certMap, nil
} }
files, err := ioutil.ReadDir(certDir) files, err := os.ReadDir(certDir)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -638,7 +637,7 @@ func getOUName(certName string) string {
func getCertFromFile(certDir string, fileName string) (*x509.Certificate, error) { func getCertFromFile(certDir string, fileName string) (*x509.Certificate, error) {
var certificate *x509.Certificate var certificate *x509.Certificate
certPEM, _ := ioutil.ReadFile(filepath.Join(certDir, fileName)) certPEM, _ := os.ReadFile(filepath.Join(certDir, fileName))
if len(certPEM) > 0 { if len(certPEM) > 0 {
logrus.Debugf("Certificate file [%s/%s] content is greater than 0", certDir, fileName) logrus.Debugf("Certificate file [%s/%s] content is greater than 0", certDir, fileName)
certificates, err := cert.ParseCertsPEM(certPEM) certificates, err := cert.ParseCertsPEM(certPEM)
@ -652,7 +651,7 @@ func getCertFromFile(certDir string, fileName string) (*x509.Certificate, error)
func getKeyFromFile(certDir string, fileName string) (*rsa.PrivateKey, error) { func getKeyFromFile(certDir string, fileName string) (*rsa.PrivateKey, error) {
var key *rsa.PrivateKey var key *rsa.PrivateKey
keyPEM, _ := ioutil.ReadFile(filepath.Join(certDir, fileName)) keyPEM, _ := os.ReadFile(filepath.Join(certDir, fileName))
if len(keyPEM) > 0 { if len(keyPEM) > 0 {
keyInterface, err := cert.ParsePrivateKeyPEM(keyPEM) keyInterface, err := cert.ParsePrivateKeyPEM(keyPEM)
if err != nil { if err != nil {
@ -664,7 +663,7 @@ func getKeyFromFile(certDir string, fileName string) (*rsa.PrivateKey, error) {
} }
func getCSRFromFile(certDir string, fileName string) ([]byte, error) { func getCSRFromFile(certDir string, fileName string) ([]byte, error) {
csrPEM, err := ioutil.ReadFile(filepath.Join(certDir, fileName)) csrPEM, err := os.ReadFile(filepath.Join(certDir, fileName))
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to read csr [%s]: %v", fileName, err) return nil, fmt.Errorf("failed to read csr [%s]: %v", fileName, err)
} }
@ -683,7 +682,7 @@ func WriteCertificates(certDirPath string, certBundle map[string]CertificatePKI)
for certName, cert := range certBundle { for certName, cert := range certBundle {
if cert.CertificatePEM != "" { if cert.CertificatePEM != "" {
certificatePath := filepath.Join(certDirPath, certName+".pem") certificatePath := filepath.Join(certDirPath, certName+".pem")
if err := ioutil.WriteFile(certificatePath, []byte(cert.CertificatePEM), 0640); err != nil { if err := os.WriteFile(certificatePath, []byte(cert.CertificatePEM), 0640); err != nil {
return fmt.Errorf("Failed to write certificate to path %v: %v", certificatePath, err) return fmt.Errorf("Failed to write certificate to path %v: %v", certificatePath, err)
} }
logrus.Debugf("Successfully Deployed certificate file at [%s]", certificatePath) logrus.Debugf("Successfully Deployed certificate file at [%s]", certificatePath)
@ -691,7 +690,7 @@ func WriteCertificates(certDirPath string, certBundle map[string]CertificatePKI)
if cert.KeyPEM != "" { if cert.KeyPEM != "" {
keyPath := filepath.Join(certDirPath, certName+"-key.pem") keyPath := filepath.Join(certDirPath, certName+"-key.pem")
if err := ioutil.WriteFile(keyPath, []byte(cert.KeyPEM), 0640); err != nil { if err := os.WriteFile(keyPath, []byte(cert.KeyPEM), 0640); err != nil {
return fmt.Errorf("Failed to write key to path %v: %v", keyPath, err) return fmt.Errorf("Failed to write key to path %v: %v", keyPath, err)
} }
logrus.Debugf("Successfully Deployed key file at [%s]", keyPath) logrus.Debugf("Successfully Deployed key file at [%s]", keyPath)
@ -699,7 +698,7 @@ func WriteCertificates(certDirPath string, certBundle map[string]CertificatePKI)
if cert.CSRPEM != "" { if cert.CSRPEM != "" {
csrPath := filepath.Join(certDirPath, certName+"-csr.pem") csrPath := filepath.Join(certDirPath, certName+"-csr.pem")
if err := ioutil.WriteFile(csrPath, []byte(cert.CSRPEM), 0640); err != nil { if err := os.WriteFile(csrPath, []byte(cert.CSRPEM), 0640); err != nil {
return fmt.Errorf("Failed to write csr to path %v: %v", csrPath, err) return fmt.Errorf("Failed to write csr to path %v: %v", csrPath, err)
} }
logrus.Debugf("Successfully Deployed csr file at [%s]", csrPath) logrus.Debugf("Successfully Deployed csr file at [%s]", csrPath)
@ -789,7 +788,7 @@ func validateCAIssuer(rkeConfig *v3.RancherKubernetesEngineConfig, certBundle ma
} }
func ReadCertToStr(file string) (string, error) { func ReadCertToStr(file string) (string, error) {
certStr, err := ioutil.ReadFile(file) certStr, err := os.ReadFile(file)
if err != nil { if err != nil {
return "", fmt.Errorf("failed to read certificate [%s]: %v", file, err) return "", fmt.Errorf("failed to read certificate [%s]: %v", file, err)
} }

View File

@ -5,7 +5,7 @@ import (
"crypto/tls" "crypto/tls"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io"
"net" "net"
"net/http" "net/http"
"strings" "strings"
@ -116,7 +116,7 @@ func getHealthEtcd(hc http.Client, host *hosts.Host, url string) (string, error)
if err != nil { if err != nil {
return healthy.Health, fmt.Errorf("failed to get /health for host [%s]: %v", host.Address, err) return healthy.Health, fmt.Errorf("failed to get /health for host [%s]: %v", host.Address, err)
} }
bytes, err := ioutil.ReadAll(resp.Body) bytes, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return healthy.Health, fmt.Errorf("failed to read response of /health for host [%s]: %v", host.Address, err) return healthy.Health, fmt.Errorf("failed to read response of /health for host [%s]: %v", host.Address, err)
} }

View File

@ -4,7 +4,7 @@ import (
"context" "context"
"crypto/tls" "crypto/tls"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
@ -96,7 +96,7 @@ func getHealthz(client *http.Client, serviceName, hostAddress, url string) error
return fmt.Errorf("Failed to check %s for service [%s] on host [%s]: %v", url, serviceName, hostAddress, err) return fmt.Errorf("Failed to check %s for service [%s] on host [%s]: %v", url, serviceName, hostAddress, err)
} }
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
statusBody, _ := ioutil.ReadAll(resp.Body) statusBody, _ := io.ReadAll(resp.Body)
return fmt.Errorf("Service [%s] is not healthy on host [%s]. Response code: [%d], response body: %s", serviceName, hostAddress, resp.StatusCode, statusBody) return fmt.Errorf("Service [%s] is not healthy on host [%s]. Response code: [%d], response body: %s", serviceName, hostAddress, resp.StatusCode, statusBody)
} }
return nil return nil

View File

@ -3,7 +3,6 @@ package util
import ( import (
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/url" "net/url"
"os" "os"
"reflect" "reflect"
@ -244,7 +243,7 @@ func ReplaceFileWithBackup(originalFile, prefixBackupFile string) error {
if !fileExists { if !fileExists {
return nil return nil
} }
tmpfile, err := ioutil.TempFile(".", prefixBackupFile) tmpfile, err := os.CreateTemp(".", prefixBackupFile)
if err != nil { if err != nil {
return err return err
} }
@ -280,7 +279,7 @@ func CopyFileWithPrefix(originalFile, prefixDestFile string) error {
} }
defer source.Close() defer source.Close()
destFile, err := ioutil.TempFile(".", prefixDestFile) destFile, err := os.CreateTemp(".", prefixDestFile)
if err != nil { if err != nil {
return err return err
} }