From 345a5229711150295df2825e369bf544c650fd31 Mon Sep 17 00:00:00 2001 From: guoguangwu Date: Fri, 7 Jul 2023 12:04:10 +0800 Subject: [PATCH] chore: remove refs to deprecated io/ioutil --- pkg/apis/pmemcsi/v1beta1/deployment_types_test.go | 3 +-- pkg/coverage/coverage.go | 3 +-- pkg/imagefile/imagefile.go | 3 +-- pkg/imagefile/imagefile_test.go | 5 ++--- pkg/imagefile/test/imagefiletest.go | 3 +-- pkg/k8sutil/namespace.go | 3 +-- pkg/pmem-state/pmem-state.go | 3 +-- pkg/pmem-state/pmem-state_test.go | 5 ++--- pkg/volumepathhandler/volume_path_handler.go | 3 +-- test/e2e/deploy/deploy.go | 4 ++-- test/e2e/filesource_test.go | 3 +-- test/e2e/metrics/metrics.go | 4 ++-- test/e2e/pod/dial.go | 4 ++-- test/e2e/pod/exec.go | 3 +-- 14 files changed, 19 insertions(+), 30 deletions(-) diff --git a/pkg/apis/pmemcsi/v1beta1/deployment_types_test.go b/pkg/apis/pmemcsi/v1beta1/deployment_types_test.go index 5cc2cd6aec..16379bd847 100644 --- a/pkg/apis/pmemcsi/v1beta1/deployment_types_test.go +++ b/pkg/apis/pmemcsi/v1beta1/deployment_types_test.go @@ -6,7 +6,6 @@ SPDX-License-Identifier: Apache-2.0 package v1beta1_test import ( - "io/ioutil" "os" "testing" @@ -135,7 +134,7 @@ spec: It("should have valid json schema", func() { crdFile := os.Getenv("REPO_ROOT") + "/deploy/crd/pmem-csi.intel.com_pmemcsideployments.yaml" - data, err := ioutil.ReadFile(crdFile) + data, err := os.ReadFile(crdFile) Expect(err).ShouldNot(HaveOccurred(), "load crd data") crd := &apiextensions.CustomResourceDefinition{} diff --git a/pkg/coverage/coverage.go b/pkg/coverage/coverage.go index c83bc07bfb..86d33473df 100644 --- a/pkg/coverage/coverage.go +++ b/pkg/coverage/coverage.go @@ -14,7 +14,6 @@ package coverage import ( "flag" - "io/ioutil" "os" "path/filepath" "syscall" @@ -32,7 +31,7 @@ func Run(main func() int) { if err != nil { klog.Fatalf("cover profile %q: %s", *coverage, err) } - f, err := ioutil.TempFile(filepath.Dir(abspath), filepath.Base(abspath)) + f, err := os.CreateTemp(filepath.Dir(abspath), filepath.Base(abspath)) if err != nil { klog.Fatalf("temporary cover profile %q: %s", abspath, err) } diff --git a/pkg/imagefile/imagefile.go b/pkg/imagefile/imagefile.go index 0470406bb2..f13bee6af3 100644 --- a/pkg/imagefile/imagefile.go +++ b/pkg/imagefile/imagefile.go @@ -165,7 +165,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "os" "os/exec" "path/filepath" @@ -258,7 +257,7 @@ func Create(filename string, size Bytes, fs FsType) error { // We write MBRs and rootfs into temporary files, then copy into the // final image file at the end. - tmp, err := ioutil.TempDir("", "pmem-csi-imagefile") + tmp, err := os.MkdirTemp("", "pmem-csi-imagefile") if err != nil { return fmt.Errorf("temp dir: %w", err) } diff --git a/pkg/imagefile/imagefile_test.go b/pkg/imagefile/imagefile_test.go index 3095d08cea..99861f9013 100644 --- a/pkg/imagefile/imagefile_test.go +++ b/pkg/imagefile/imagefile_test.go @@ -13,7 +13,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "os" "os/exec" "syscall" @@ -99,7 +98,7 @@ func rmTmpfile(file *os.File) { } func TestExtents(t *testing.T) { - file, err := ioutil.TempFile("", "extents") + file, err := os.CreateTemp("", "extents") if err != nil { t.Fatalf("create temp file: %v", err) } @@ -149,7 +148,7 @@ func TestExtents(t *testing.T) { // Now create a sparse copy. This should be almost // instantaneous, despite the nominally large file. - copy, err := ioutil.TempFile(".", "copy") + copy, err := os.CreateTemp(".", "copy") if err != nil { t.Fatalf("create temp file: %v", err) } diff --git a/pkg/imagefile/test/imagefiletest.go b/pkg/imagefile/test/imagefiletest.go index 097f29e04a..e91bf39687 100644 --- a/pkg/imagefile/test/imagefiletest.go +++ b/pkg/imagefile/test/imagefiletest.go @@ -16,7 +16,6 @@ import ( "bufio" "errors" "fmt" - "io/ioutil" "os" "os/exec" "strings" @@ -77,7 +76,7 @@ func testImageFile(t TInterface, fs imagefile.FsType, size imagefile.Bytes, expe t.Skipf("parted not found: %v", err) } - file, err := ioutil.TempFile("", "image") + file, err := os.CreateTemp("", "image") if err != nil { t.Fatalf("create temp file: %v", err) } diff --git a/pkg/k8sutil/namespace.go b/pkg/k8sutil/namespace.go index a3ebb23a53..d6b8deb8c6 100644 --- a/pkg/k8sutil/namespace.go +++ b/pkg/k8sutil/namespace.go @@ -8,7 +8,6 @@ package k8sutil import ( "context" - "io/ioutil" "os" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -29,7 +28,7 @@ func GetNamespace(ctx context.Context) string { if ns == "" { // If environment variable not set, give it a try to fetch it from // mounted filesystem by Kubernetes - data, err := ioutil.ReadFile(namespaceFile) + data, err := os.ReadFile(namespaceFile) if err != nil { logger.Info("Could not read namespace from secret, using fallback "+defaultOperatorNamespace, "error", err, diff --git a/pkg/pmem-state/pmem-state.go b/pkg/pmem-state/pmem-state.go index f49d9c6b1e..fac7a28d37 100644 --- a/pkg/pmem-state/pmem-state.go +++ b/pkg/pmem-state/pmem-state.go @@ -8,7 +8,6 @@ package pmemstate import ( "encoding/json" "fmt" - "io/ioutil" "os" "path" "strings" @@ -108,7 +107,7 @@ func (fs *fileState) Get(id string, dataPtr interface{}) error { // GetAll retrieves the names of all .json files found in fileState.location directory func (fs *fileState) GetAll() ([]string, error) { fs.stateDirLock.Lock() - files, err := ioutil.ReadDir(fs.location) + files, err := os.ReadDir(fs.location) fs.stateDirLock.Unlock() if err != nil { return nil, fmt.Errorf("failed to read metadata from %q: %w", fs.location, err) diff --git a/pkg/pmem-state/pmem-state_test.go b/pkg/pmem-state/pmem-state_test.go index 7349d06c11..281c00207c 100644 --- a/pkg/pmem-state/pmem-state_test.go +++ b/pkg/pmem-state/pmem-state_test.go @@ -7,7 +7,6 @@ package pmemstate_test import ( "fmt" - "io/ioutil" "os" "path" "strings" @@ -59,7 +58,7 @@ var _ = Describe("pmem state", func() { BeforeEach(func() { var err error - stateDir, err = ioutil.TempDir("", "pmemstate-") + stateDir, err = os.MkdirTemp("", "pmemstate-") Expect(err).NotTo(HaveOccurred()) }) @@ -88,7 +87,7 @@ var _ = Describe("pmem state", func() { _, err = pmemstate.NewFileState("/unknown/base/directory/") Expect(err).To(HaveOccurred()) - file, err := ioutil.TempFile("", "pmemstate-file") + file, err := os.CreateTemp("", "pmemstate-file") Expect(err).NotTo(HaveOccurred()) _, err = pmemstate.NewFileState(file.Name()) os.Remove(file.Name()) //nolint: errcheck diff --git a/pkg/volumepathhandler/volume_path_handler.go b/pkg/volumepathhandler/volume_path_handler.go index 01e102bbf3..3017b11156 100644 --- a/pkg/volumepathhandler/volume_path_handler.go +++ b/pkg/volumepathhandler/volume_path_handler.go @@ -19,7 +19,6 @@ package volumepathhandler import ( "context" "fmt" - "io/ioutil" "os" "path/filepath" @@ -294,7 +293,7 @@ func (v VolumePathHandler) IsDeviceBindMountExist(ctx context.Context, mapPath s func (v VolumePathHandler) GetDeviceBindMountRefs(ctx context.Context, devPath string, mapPath string) ([]string, error) { ctx, logger := pmemlog.WithName(ctx, "GetDeviceBindMountRefs") var refs []string - files, err := ioutil.ReadDir(mapPath) + files, err := os.ReadDir(mapPath) if err != nil { return nil, fmt.Errorf("directory cannot read %v", err) } diff --git a/test/e2e/deploy/deploy.go b/test/e2e/deploy/deploy.go index 32ddb66240..ee6299d04b 100644 --- a/test/e2e/deploy/deploy.go +++ b/test/e2e/deploy/deploy.go @@ -10,7 +10,7 @@ import ( "bufio" "context" "fmt" - "io/ioutil" + "io" "net/http" "os" "os/exec" @@ -138,7 +138,7 @@ func GetMetrics(ctx context.Context, c *Cluster, url string) (map[string]*cm.Met return nil, fmt.Errorf("get controller metrics: %v", err) } if resp.StatusCode != 200 { - body, _ := ioutil.ReadAll(resp.Body) + body, _ := io.ReadAll(resp.Body) suffix := "" if len(body) > 0 { suffix = "\n" + string(body) diff --git a/test/e2e/filesource_test.go b/test/e2e/filesource_test.go index 544c09a058..b25ad07c3b 100644 --- a/test/e2e/filesource_test.go +++ b/test/e2e/filesource_test.go @@ -18,7 +18,6 @@ package e2e import ( "fmt" - "io/ioutil" "os" "path" "path/filepath" @@ -40,7 +39,7 @@ func (r RootFileSource) ReadTestFile(filePath string) ([]byte, error) { } else { fullPath = filepath.Join(r.Root, filePath) } - data, err := ioutil.ReadFile(fullPath) + data, err := os.ReadFile(fullPath) if os.IsNotExist(err) { // Not an error (yet), some other provider may have the file. return nil, nil diff --git a/test/e2e/metrics/metrics.go b/test/e2e/metrics/metrics.go index fa676d1d58..95b7c5fe52 100644 --- a/test/e2e/metrics/metrics.go +++ b/test/e2e/metrics/metrics.go @@ -9,7 +9,7 @@ package gotests import ( "context" "fmt" - "io/ioutil" + "io" "net/http" "strings" "time" @@ -90,7 +90,7 @@ var _ = deploy.Describe("direct-testing", "direct-testing-metrics", "", func(d * if err != nil { return } - data, err := ioutil.ReadAll(resp.Body) + data, err := io.ReadAll(resp.Body) framework.ExpectNoError(err, "read GET response for pod %s and url %s", pod, url) name := pod.Name + "/" + container.Name if isPmemCSI { diff --git a/test/e2e/pod/dial.go b/test/e2e/pod/dial.go index deae58bf45..bfb5fc85f5 100644 --- a/test/e2e/pod/dial.go +++ b/test/e2e/pod/dial.go @@ -11,7 +11,7 @@ import ( "context" "errors" "fmt" - "io/ioutil" + "io" "net" "net/http" "regexp" @@ -112,7 +112,7 @@ func (d *Dialer) DialContainerPort(ctx context.Context, addr Addr) (conn net.Con } errorStream.Close() go func() { - message, err := ioutil.ReadAll(errorStream) + message, err := io.ReadAll(errorStream) switch { case err != nil: logger.Error(err, "error reading from error stream") diff --git a/test/e2e/pod/exec.go b/test/e2e/pod/exec.go index 3b8068cf38..0aa0fa6a51 100644 --- a/test/e2e/pod/exec.go +++ b/test/e2e/pod/exec.go @@ -9,7 +9,6 @@ package pod import ( "bytes" "fmt" - "io/ioutil" "os" "path" "strings" @@ -31,7 +30,7 @@ func RunInPod(f *framework.Framework, rootdir string, files []string, command st for _, file := range files { base := path.Base(file) full := path.Join(rootdir, file) - data, err := ioutil.ReadFile(full) + data, err := os.ReadFile(full) framework.ExpectNoError(err, "read input file %q", full) input.Write(data) // Somehow count=1 bs= resulted in truncated data transfers.