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

cleanup: io/ioutil #813

Merged
merged 1 commit into from
Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 5 additions & 6 deletions pkg/node-servant/components/kubelet.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package components

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -93,7 +92,7 @@ func (op *kubeletOperator) writeYurthubKubeletConfig() (string, error) {
return "", err
}
fullPath := op.getYurthubKubeletConf()
err = ioutil.WriteFile(fullPath, []byte(enutil.OpenyurtKubeletConf), fileMode)
err = os.WriteFile(fullPath, []byte(enutil.OpenyurtKubeletConf), fileMode)
if err != nil {
return "", err
}
Expand All @@ -117,7 +116,7 @@ func (op *kubeletOperator) appendConfig() error {
kubeConfigSetup := op.getAppendSetting()

// if wrote, return
content, err := ioutil.ReadFile(kubeAdmFlagsEnvFile)
content, err := os.ReadFile(kubeAdmFlagsEnvFile)
if err != nil {
return err
}
Expand All @@ -135,7 +134,7 @@ func (op *kubeletOperator) appendConfig() error {
}

r := strings.Replace(args, finding[1], fmt.Sprintf("%s %s", finding[1], kubeConfigSetup), 1)
err = ioutil.WriteFile(kubeAdmFlagsEnvFile, []byte(r), fileMode)
err = os.WriteFile(kubeAdmFlagsEnvFile, []byte(r), fileMode)
if err != nil {
return err
}
Expand All @@ -145,13 +144,13 @@ func (op *kubeletOperator) appendConfig() error {

func (op *kubeletOperator) undoAppendConfig() error {
kubeConfigSetup := op.getAppendSetting()
contentbyte, err := ioutil.ReadFile(kubeAdmFlagsEnvFile)
contentbyte, err := os.ReadFile(kubeAdmFlagsEnvFile)
if err != nil {
return err
}

content := strings.ReplaceAll(string(contentbyte), kubeConfigSetup, "")
err = ioutil.WriteFile(kubeAdmFlagsEnvFile, []byte(content), fileMode)
err = os.WriteFile(kubeAdmFlagsEnvFile, []byte(content), fileMode)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/node-servant/components/yurthub.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package components

import (
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -84,7 +84,7 @@ func (op *yurtHubOperator) Install() error {
if err := enutil.EnsureDir(podManifestPath); err != nil {
return err
}
if err := ioutil.WriteFile(getYurthubYaml(podManifestPath), []byte(yurthubTemplate), fileMode); err != nil {
if err := os.WriteFile(getYurthubYaml(podManifestPath), []byte(yurthubTemplate), fileMode); err != nil {
return err
}
klog.Infof("create the %s/yurt-hub.yaml", podManifestPath)
Expand Down Expand Up @@ -196,7 +196,7 @@ func pingClusterHealthz(client *http.Client, addr string) (bool, error) {
return false, err
}

b, err := ioutil.ReadAll(resp.Body)
b, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return false, fmt.Errorf("failed to read response of cluster healthz, %v", err)
Expand Down
3 changes: 1 addition & 2 deletions pkg/util/certmanager/pki.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"crypto/x509"
"errors"
"fmt"
"io/ioutil"
"net"
"os"

Expand Down Expand Up @@ -167,7 +166,7 @@ func GenCertPoolUseCA(caFile string) (*x509.CertPool, error) {
return nil, fmt.Errorf("fail to stat the CA file(%s): %s", caFile, err)
}

caData, err := ioutil.ReadFile(caFile)
caData, err := os.ReadFile(caFile)
if err != nil {
return nil, err
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/yurtctl/cmd/join/phases/joinnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package phases

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

Expand Down Expand Up @@ -148,7 +147,7 @@ func writeConfigBytesToDisk(b []byte, kubeletDir string) error {
return errors.Wrapf(err, "failed to create directory %q", kubeletDir)
}

if err := ioutil.WriteFile(configFile, b, 0644); err != nil {
if err := os.WriteFile(configFile, b, 0644); err != nil {
return errors.Wrapf(err, "failed to write kubelet configuration to the file %q", configFile)
}
return nil
Expand Down Expand Up @@ -182,7 +181,7 @@ func addYurthubStaticYaml(data joindata.YurtJoinData, podManifestPath string) er
return err
}

if err := ioutil.WriteFile(filepath.Join(podManifestPath, yurtconstants.YurthubStaticPodFileName), []byte(yurthubTemplate), 0600); err != nil {
if err := os.WriteFile(filepath.Join(podManifestPath, yurtconstants.YurthubStaticPodFileName), []byte(yurthubTemplate), 0600); err != nil {
return err
}
klog.Info("[join-node] Add hub agent static yaml is ok")
Expand Down
4 changes: 2 additions & 2 deletions pkg/yurtctl/cmd/join/phases/postcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package phases

import (
"fmt"
"io/ioutil"
"io"
"net/http"
"path/filepath"
"time"
Expand Down Expand Up @@ -96,7 +96,7 @@ func checkYurthubHealthz() error {
if err != nil {
return false, nil
}
ok, err := ioutil.ReadAll(resp.Body)
ok, err := io.ReadAll(resp.Body)
if err != nil {
return false, nil
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/yurtctl/cmd/reset/phases/unmount_linux.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build linux
// +build linux

/*
Expand All @@ -19,7 +20,7 @@ limitations under the License.
package phases

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

Expand All @@ -28,7 +29,7 @@ import (

// unmountKubeletDirectory unmounts all paths that contain KubeletRunDirectory
func unmountKubeletDirectory(absoluteKubeletRunDirectory string) error {
raw, err := ioutil.ReadFile("/proc/mounts")
raw, err := os.ReadFile("/proc/mounts")
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/yurtctl/cmd/yurtinit/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"runtime"
Expand Down Expand Up @@ -252,7 +251,7 @@ func (ci *clusterInitializer) PrepareClusterfile() error {
return err
}

err = ioutil.WriteFile(fmt.Sprintf("%s/Clusterfile", TmpDownloadDir), []byte(clusterfile), constants.FileMode)
err = os.WriteFile(fmt.Sprintf("%s/Clusterfile", TmpDownloadDir), []byte(clusterfile), constants.FileMode)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/yurtctl/cmd/yurttest/kindinit/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package kindinit

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -321,7 +320,7 @@ func (i *Initializer) prepareKindConfigFile(kindConfigPath string) error {
kindConfigContent = strings.Join([]string{kindConfigContent, worker}, "\n")
}

if err = ioutil.WriteFile(kindConfigPath, []byte(kindConfigContent), constants.FileMode); err != nil {
if err = os.WriteFile(kindConfigPath, []byte(kindConfigContent), constants.FileMode); err != nil {
return err
}
klog.V(1).Infof("generated new kind config file at %s", kindConfigPath)
Expand Down
3 changes: 1 addition & 2 deletions pkg/yurtctl/cmd/yurttest/kindinit/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package kindinit

import (
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -149,7 +148,7 @@ nodes:
t.Errorf("TestPrepareKindConfigFile failed at case %s for %s", name, err)
continue
}
buf, err := ioutil.ReadFile(c.kindConfigPath)
buf, err := os.ReadFile(c.kindConfigPath)
if err != nil {
t.Errorf("TestPrepareKindConfigFile failed at case %s, when reading file %s, %s", name, c.kindConfigPath, err)
continue
Expand Down
3 changes: 1 addition & 2 deletions pkg/yurtctl/kubernetes/kubeadm/app/phases/kubelet/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package kubelet

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -109,7 +108,7 @@ func writeKubeletFlagBytesToDisk(b []byte, kubeletDir string) error {
if err := os.MkdirAll(kubeletDir, 0700); err != nil {
return errors.Wrapf(err, "failed to create directory %q", kubeletDir)
}
if err := ioutil.WriteFile(kubeletEnvFilePath, b, 0644); err != nil {
if err := os.WriteFile(kubeletEnvFilePath, b, 0644); err != nil {
return errors.Wrapf(err, "failed to write kubelet configuration to the file %q", kubeletEnvFilePath)
}
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package kubeconfig

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

"github.com/pkg/errors"
clientset "k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -150,15 +150,15 @@ func EnsureAuthenticationInfoAreEmbedded(config *clientcmdapi.Config) error {
}

if len(authInfo.ClientCertificateData) == 0 && len(authInfo.ClientCertificate) != 0 {
clientCert, err := ioutil.ReadFile(authInfo.ClientCertificate)
clientCert, err := os.ReadFile(authInfo.ClientCertificate)
if err != nil {
return errors.Wrap(err, "error while reading client cert file defined in kubeconfig")
}
authInfo.ClientCertificateData = clientCert
authInfo.ClientCertificate = ""
}
if len(authInfo.ClientKeyData) == 0 && len(authInfo.ClientKey) != 0 {
clientKey, err := ioutil.ReadFile(authInfo.ClientKey)
clientKey, err := os.ReadFile(authInfo.ClientKey)
if err != nil {
return errors.Wrap(err, "error while reading client key file defined in kubeconfig")
}
Expand All @@ -177,7 +177,7 @@ func EnsureCertificateAuthorityIsEmbedded(cluster *clientcmdapi.Cluster) error {
}

if len(cluster.CertificateAuthorityData) == 0 && len(cluster.CertificateAuthority) != 0 {
ca, err := ioutil.ReadFile(cluster.CertificateAuthority)
ca, err := os.ReadFile(cluster.CertificateAuthority)
if err != nil {
return errors.Wrap(err, "error while reading certificate authority file defined in kubeconfig")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package kubeconfig
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"testing"

Expand Down Expand Up @@ -143,7 +142,7 @@ func TestCreateWithToken(t *testing.T) {
}

func TestWriteKubeconfigToDisk(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "")
tmpdir, err := os.MkdirTemp("", "")
if err != nil {
t.Fatalf("Couldn't create tmpdir")
}
Expand Down Expand Up @@ -177,7 +176,7 @@ func TestWriteKubeconfigToDisk(t *testing.T) {
err,
)
}
newFile, _ := ioutil.ReadFile(configPath)
newFile, _ := os.ReadFile(configPath)
if !bytes.Equal(newFile, rt.file) {
t.Errorf(
"failed WriteToDisk config write:\n\texpected: %s\n\t actual: %s",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ limitations under the License.
package util

import (
"io/ioutil"
"net"
"os"
"reflect"
Expand Down Expand Up @@ -337,7 +336,7 @@ func TestIsExistingSocket(t *testing.T) {
{
name: "Valid domain socket is detected as such",
proc: func(t *testing.T) {
tmpFile, err := ioutil.TempFile("", tempPrefix)
tmpFile, err := os.CreateTemp(os.TempDir(), tempPrefix)
if err != nil {
t.Fatalf("unexpected error by TempFile: %v", err)
}
Expand All @@ -359,7 +358,7 @@ func TestIsExistingSocket(t *testing.T) {
{
name: "Regular file is not a domain socket",
proc: func(t *testing.T) {
tmpFile, err := ioutil.TempFile("", tempPrefix)
tmpFile, err := os.CreateTemp(os.TempDir(), tempPrefix)
if err != nil {
t.Fatalf("unexpected error by TempFile: %v", err)
}
Expand Down
9 changes: 4 additions & 5 deletions pkg/yurtctl/util/edgenode/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package edgenode

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -50,7 +49,7 @@ func FileExists(filename string) (bool, error) {

// GetContentFormFile returns all strings that match the regular expression regularExpression
func GetContentFormFile(filename string, regularExpression string) ([]string, error) {
content, err := ioutil.ReadFile(filename)
content, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -88,11 +87,11 @@ func EnsureDir(dirname string) error {

// CopyFile copys sourceFile to destinationFile
func CopyFile(sourceFile string, destinationFile string, perm os.FileMode) error {
content, err := ioutil.ReadFile(sourceFile)
content, err := os.ReadFile(sourceFile)
if err != nil {
return fmt.Errorf("failed to read source file %s: %v", sourceFile, err)
}
err = ioutil.WriteFile(destinationFile, content, perm)
err = os.WriteFile(destinationFile, content, perm)
if err != nil {
return fmt.Errorf("failed to write destination file %s: %v", destinationFile, err)
}
Expand Down Expand Up @@ -143,7 +142,7 @@ func GetNodeName(kubeadmConfPath string) (string, error) {
}

//4. read nodeName from /etc/hostname
content, err := ioutil.ReadFile(Hostname)
content, err := os.ReadFile(Hostname)
if err != nil {
return "", err
}
Expand Down
Loading