diff --git a/.golangci.yml b/.golangci.yml index 29fe37a03..f2075930d 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -53,6 +53,12 @@ linters-settings: # Default is to use a neutral variety of English. # Setting locale to US will correct the British spelling of 'colour' to 'color'. locale: US + depguard: + rules: + main: + deny: + - pkg: "io/ioutil" + desc: "Use corresponding 'os' or 'io' functions instead." linters: fast: false @@ -67,6 +73,7 @@ linters: - vet - unconvert - staticcheck + - depguard issues: exclude-rules: diff --git a/test/e2e/e2e.go b/test/e2e/e2e.go index 937589477..e00e84adf 100644 --- a/test/e2e/e2e.go +++ b/test/e2e/e2e.go @@ -21,7 +21,6 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" "os" "os/exec" "path" @@ -318,7 +317,7 @@ func logClusterImageSources() { outputBytes, _ := json.MarshalIndent(images, "", " ") filePath := filepath.Join(framework.TestContext.ReportDir, "images.json") - if err := ioutil.WriteFile(filePath, outputBytes, 0644); err != nil { + if err := os.WriteFile(filePath, outputBytes, 0644); err != nil { framework.Logf("cluster images sources, could not write to %q: %v", filePath, err) } } diff --git a/test/e2e/framework/framework.go b/test/e2e/framework/framework.go index d1cbf776b..e44f836bf 100644 --- a/test/e2e/framework/framework.go +++ b/test/e2e/framework/framework.go @@ -25,8 +25,8 @@ package framework import ( "context" "fmt" - "io/ioutil" "math/rand" + "os" "path" "strings" "sync" @@ -332,7 +332,7 @@ func printSummaries(summaries []TestDataSummary, testBaseName string) { } else { // TODO: learn to extract test name and append it to the kind instead of timestamp. filePath := path.Join(TestContext.ReportDir, summaries[i].SummaryKind()+"_"+testBaseName+"_"+now.Format(time.RFC3339)+".txt") - if err := ioutil.WriteFile(filePath, []byte(summaries[i].PrintHumanReadable()), 0644); err != nil { + if err := os.WriteFile(filePath, []byte(summaries[i].PrintHumanReadable()), 0644); err != nil { Logf("Failed to write file %v with test performance data: %v", filePath, err) } } @@ -349,7 +349,7 @@ func printSummaries(summaries []TestDataSummary, testBaseName string) { // TODO: learn to extract test name and append it to the kind instead of timestamp. filePath := path.Join(TestContext.ReportDir, summaries[i].SummaryKind()+"_"+testBaseName+"_"+now.Format(time.RFC3339)+".json") Logf("Writing to %s", filePath) - if err := ioutil.WriteFile(filePath, []byte(summaries[i].PrintJSON()), 0644); err != nil { + if err := os.WriteFile(filePath, []byte(summaries[i].PrintJSON()), 0644); err != nil { Logf("Failed to write file %v with test performance data: %v", filePath, err) } } diff --git a/test/e2e/framework/manifest/manifest.go b/test/e2e/framework/manifest/manifest.go index 7de1784d0..cf45bd4e2 100644 --- a/test/e2e/framework/manifest/manifest.go +++ b/test/e2e/framework/manifest/manifest.go @@ -19,7 +19,7 @@ package manifest import ( "fmt" - "io/ioutil" + "io" "net/http" "time" @@ -119,7 +119,7 @@ func DaemonSetFromURL(url string) (*appsv1.DaemonSet, error) { } defer response.Body.Close() - data, err := ioutil.ReadAll(response.Body) + data, err := io.ReadAll(response.Body) if err != nil { return nil, fmt.Errorf("Failed to read html response body: %v", err) } diff --git a/test/e2e/framework/metrics/kubelet_metrics.go b/test/e2e/framework/metrics/kubelet_metrics.go index c9d4ea3a2..381357067 100644 --- a/test/e2e/framework/metrics/kubelet_metrics.go +++ b/test/e2e/framework/metrics/kubelet_metrics.go @@ -20,7 +20,7 @@ package metrics import ( "context" "fmt" - "io/ioutil" + "io" "net/http" "sort" "strconv" @@ -75,7 +75,7 @@ func GrabKubeletMetricsWithoutProxy(nodeName, path string) (KubeletMetrics, erro return KubeletMetrics{}, err } defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return KubeletMetrics{}, err } diff --git a/test/e2e/framework/pod/dial.go b/test/e2e/framework/pod/dial.go index d0ae2880a..f788fc6e4 100644 --- a/test/e2e/framework/pod/dial.go +++ b/test/e2e/framework/pod/dial.go @@ -20,7 +20,7 @@ import ( "context" "errors" "fmt" - "io/ioutil" + "io" "net" "net/http" "regexp" @@ -116,7 +116,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: klog.ErrorS(err, "error reading from error stream") diff --git a/test/e2e/framework/ssh/ssh.go b/test/e2e/framework/ssh/ssh.go index e7bb55c0d..359e99dfc 100644 --- a/test/e2e/framework/ssh/ssh.go +++ b/test/e2e/framework/ssh/ssh.go @@ -21,7 +21,6 @@ import ( "bytes" "context" "fmt" - "io/ioutil" "net" "os" "path/filepath" @@ -103,7 +102,7 @@ func GetSigner(provider string) (ssh.Signer, error) { } func makePrivateKeySignerFromFile(key string) (ssh.Signer, error) { - buffer, err := ioutil.ReadFile(key) + buffer, err := os.ReadFile(key) if err != nil { return nil, fmt.Errorf("error reading SSH key %s: '%v'", key, err) } diff --git a/test/e2e/framework/test_context.go b/test/e2e/framework/test_context.go index 58c180cd1..37c8e5ce8 100644 --- a/test/e2e/framework/test_context.go +++ b/test/e2e/framework/test_context.go @@ -22,7 +22,6 @@ import ( "errors" "flag" "fmt" - "io/ioutil" "math" "os" "sort" @@ -456,7 +455,7 @@ func AfterReadingAllFlags(t *TestContextType) { if len(t.Host) == 0 && len(t.KubeConfig) == 0 { // Check if we can use the in-cluster config if clusterConfig, err := restclient.InClusterConfig(); err == nil { - if tempFile, err := ioutil.TempFile(os.TempDir(), "kubeconfig-"); err == nil { + if tempFile, err := os.CreateTemp(os.TempDir(), "kubeconfig-"); err == nil { kubeConfig := createKubeConfig(clusterConfig) clientcmd.WriteToFile(*kubeConfig, tempFile.Name()) t.KubeConfig = tempFile.Name() diff --git a/test/e2e/framework/testfiles/testfiles.go b/test/e2e/framework/testfiles/testfiles.go index 155a06f81..1abbbcf6f 100644 --- a/test/e2e/framework/testfiles/testfiles.go +++ b/test/e2e/framework/testfiles/testfiles.go @@ -29,7 +29,6 @@ import ( "errors" "fmt" "io/fs" - "io/ioutil" "os" "path" "path/filepath" @@ -122,7 +121,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/generated/bindata.go b/test/e2e/generated/bindata.go index 33934f512..df1d9823d 100644 --- a/test/e2e/generated/bindata.go +++ b/test/e2e/generated/bindata.go @@ -392,7 +392,6 @@ import ( "compress/gzip" "fmt" "io" - "io/ioutil" "os" "path/filepath" "strings" @@ -9322,7 +9321,7 @@ func RestoreAsset(dir, name string) error { if err != nil { return err } - err = ioutil.WriteFile(_filePath(dir, name), data, info.Mode()) + err = os.WriteFile(_filePath(dir, name), data, info.Mode()) if err != nil { return err } diff --git a/test/e2e/reporters/progress.go b/test/e2e/reporters/progress.go index 915ea2589..da75e4e8d 100644 --- a/test/e2e/reporters/progress.go +++ b/test/e2e/reporters/progress.go @@ -20,7 +20,7 @@ import ( "bytes" "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "strings" "time" @@ -123,7 +123,7 @@ func (reporter *ProgressReporter) postProgressToURL(b []byte) { klog.Errorf("Unexpected response when posting progress update to %v: %v", reporter.progressURL, resp.StatusCode) if resp.Body != nil { defer resp.Body.Close() - respBody, err := ioutil.ReadAll(resp.Body) + respBody, err := io.ReadAll(resp.Body) if err != nil { klog.Errorf("Failed to read response body from posting progress: %v", err) return diff --git a/test/e2e/suites.go b/test/e2e/suites.go index 93f4a073c..131630193 100644 --- a/test/e2e/suites.go +++ b/test/e2e/suites.go @@ -19,7 +19,7 @@ package e2e import ( "fmt" - "io/ioutil" + "os" "path" "time" @@ -82,7 +82,7 @@ func gatherTestSuiteMetrics() error { metricsJSON := metricsForE2E.PrintJSON() if framework.TestContext.ReportDir != "" { filePath := path.Join(framework.TestContext.ReportDir, "MetricsForE2ESuite_"+time.Now().Format(time.RFC3339)+".json") - if err := ioutil.WriteFile(filePath, []byte(metricsJSON), 0644); err != nil { + if err := os.WriteFile(filePath, []byte(metricsJSON), 0644); err != nil { return fmt.Errorf("error writing to %q: %v", filePath, err) } } else {