replace deprecated io/ioutil with os and io for cmd

This commit is contained in:
ahrtr
2021-11-02 08:54:28 +08:00
parent 6dd234d85c
commit 972dc46a1f
73 changed files with 187 additions and 239 deletions

View File

@@ -19,7 +19,6 @@ package config
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
@@ -199,7 +198,7 @@ G+2/lm8TaVjoU7Fi5Ka5G5HY2GLaR7P+IxYcrMHCl62Y7Rqcrnc=
}
func TestGetNodeNameFromKubeletConfig(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "")
tmpdir, err := os.MkdirTemp("", "")
if err != nil {
t.Fatalf("Couldn't create tmpdir")
}
@@ -246,7 +245,7 @@ func TestGetNodeNameFromKubeletConfig(t *testing.T) {
t.Run(rt.name, func(t2 *testing.T) {
if len(rt.pemContent) > 0 {
pemPath := filepath.Join(tmpdir, "kubelet.pem")
err := ioutil.WriteFile(pemPath, rt.pemContent, 0644)
err := os.WriteFile(pemPath, rt.pemContent, 0644)
if err != nil {
t.Errorf("Couldn't create pem file: %v", err)
return
@@ -255,7 +254,7 @@ func TestGetNodeNameFromKubeletConfig(t *testing.T) {
}
kubeconfigPath := filepath.Join(tmpdir, kubeadmconstants.KubeletKubeConfigFileName)
err := ioutil.WriteFile(kubeconfigPath, rt.kubeconfigContent, 0644)
err := os.WriteFile(kubeconfigPath, rt.kubeconfigContent, 0644)
if err != nil {
t.Errorf("Couldn't create kubeconfig: %v", err)
return
@@ -278,7 +277,7 @@ func TestGetNodeNameFromKubeletConfig(t *testing.T) {
}
func TestGetNodeRegistration(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "")
tmpdir, err := os.MkdirTemp("", "")
if err != nil {
t.Fatalf("Couldn't create tmpdir")
}
@@ -320,7 +319,7 @@ func TestGetNodeRegistration(t *testing.T) {
t.Run(rt.name, func(t2 *testing.T) {
cfgPath := filepath.Join(tmpdir, kubeadmconstants.KubeletKubeConfigFileName)
if len(rt.fileContents) > 0 {
err := ioutil.WriteFile(cfgPath, rt.fileContents, 0644)
err := os.WriteFile(cfgPath, rt.fileContents, 0644)
if err != nil {
t.Errorf("Couldn't create file")
return
@@ -491,7 +490,7 @@ func TestGetAPIEndpointWithBackoff(t *testing.T) {
}
func TestGetInitConfigurationFromCluster(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "")
tmpdir, err := os.MkdirTemp("", "")
if err != nil {
t.Fatalf("Couldn't create tmpdir")
}
@@ -680,7 +679,7 @@ func TestGetInitConfigurationFromCluster(t *testing.T) {
t.Run(rt.name, func(t *testing.T) {
cfgPath := filepath.Join(tmpdir, kubeadmconstants.KubeletKubeConfigFileName)
if len(rt.fileContents) > 0 {
err := ioutil.WriteFile(cfgPath, rt.fileContents, 0644)
err := os.WriteFile(cfgPath, rt.fileContents, 0644)
if err != nil {
t.Errorf("Couldn't create file")
return

View File

@@ -18,8 +18,8 @@ package config
import (
"bytes"
"io/ioutil"
"net"
"os"
"strconv"
"strings"
@@ -254,7 +254,7 @@ func DefaultedInitConfiguration(versionedInitCfg *kubeadmapiv1.InitConfiguration
func LoadInitConfigurationFromFile(cfgPath string) (*kubeadmapi.InitConfiguration, error) {
klog.V(1).Infof("loading configuration from %q", cfgPath)
b, err := ioutil.ReadFile(cfgPath)
b, err := os.ReadFile(cfgPath)
if err != nil {
return nil, errors.Wrapf(err, "unable to read config from %q ", cfgPath)
}

View File

@@ -18,7 +18,6 @@ package config
import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"testing"
@@ -33,7 +32,7 @@ import (
func TestLoadInitConfigurationFromFile(t *testing.T) {
// Create temp folder for the test case
tmpdir, err := ioutil.TempDir("", "")
tmpdir, err := os.MkdirTemp("", "")
if err != nil {
t.Fatalf("Couldn't create tmpdir: %v", err)
}
@@ -84,7 +83,7 @@ func TestLoadInitConfigurationFromFile(t *testing.T) {
for _, rt := range tests {
t.Run(rt.name, func(t2 *testing.T) {
cfgPath := filepath.Join(tmpdir, rt.name)
err := ioutil.WriteFile(cfgPath, rt.fileContents, 0644)
err := os.WriteFile(cfgPath, rt.fileContents, 0644)
if err != nil {
t.Errorf("Couldn't create file: %v", err)
return

View File

@@ -17,7 +17,7 @@ limitations under the License.
package config
import (
"io/ioutil"
"os"
"github.com/pkg/errors"
@@ -75,7 +75,7 @@ func LoadOrDefaultJoinConfiguration(cfgPath string, defaultversionedcfg *kubeadm
func LoadJoinConfigurationFromFile(cfgPath string) (*kubeadmapi.JoinConfiguration, error) {
klog.V(1).Infof("loading configuration from %q", cfgPath)
b, err := ioutil.ReadFile(cfgPath)
b, err := os.ReadFile(cfgPath)
if err != nil {
return nil, errors.Wrapf(err, "unable to read config from %q ", cfgPath)
}

View File

@@ -18,7 +18,6 @@ package config
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
@@ -30,7 +29,7 @@ import (
func TestLoadJoinConfigurationFromFile(t *testing.T) {
// Create temp folder for the test case
tmpdir, err := ioutil.TempDir("", "")
tmpdir, err := os.MkdirTemp("", "")
if err != nil {
t.Fatalf("Couldn't create tmpdir: %v", err)
}
@@ -97,7 +96,7 @@ func TestLoadJoinConfigurationFromFile(t *testing.T) {
for _, rt := range tests {
t.Run(rt.name, func(t2 *testing.T) {
cfgPath := filepath.Join(tmpdir, rt.name)
err := ioutil.WriteFile(cfgPath, []byte(rt.fileContents), 0644)
err := os.WriteFile(cfgPath, []byte(rt.fileContents), 0644)
if err != nil {
t.Errorf("Couldn't create file: %v", err)
return

View File

@@ -17,7 +17,7 @@ limitations under the License.
package strict
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
@@ -143,7 +143,7 @@ func TestVerifyUnmarshalStrict(t *testing.T) {
for _, test := range testFiles {
t.Run(test.fileName, func(t *testing.T) {
bytes, err := ioutil.ReadFile(filepath.Join(pathTestData, test.fileName))
bytes, err := os.ReadFile(filepath.Join(pathTestData, test.fileName))
if err != nil {
t.Fatalf("couldn't read test data: %v", err)
}