Replace usage of io/ioutil package (#9613)

`io/ioutil` has been deprecated since go 1.16 and the linter started to
complain about it.
This commit is contained in:
Alejandro Pedraza 2022-10-13 12:10:58 -05:00 committed by GitHub
parent 90a893194e
commit e6fa5a7156
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
24 changed files with 76 additions and 86 deletions

View File

@ -4,7 +4,7 @@ import (
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
"io/ioutil" "os"
"testing" "testing"
"github.com/linkerd/linkerd2/pkg/healthcheck" "github.com/linkerd/linkerd2/pkg/healthcheck"
@ -33,7 +33,7 @@ func TestCheckStatus(t *testing.T) {
output := bytes.NewBufferString("") output := bytes.NewBufferString("")
healthcheck.RunChecks(output, stderr, hc, tableOutput) healthcheck.RunChecks(output, stderr, hc, tableOutput)
goldenFileBytes, err := ioutil.ReadFile("testdata/check_output.golden") goldenFileBytes, err := os.ReadFile("testdata/check_output.golden")
if err != nil { if err != nil {
t.Fatalf("Unexpected error: %v", err) t.Fatalf("Unexpected error: %v", err)
} }
@ -67,7 +67,7 @@ func TestCheckStatus(t *testing.T) {
output := bytes.NewBufferString("") output := bytes.NewBufferString("")
healthcheck.RunChecks(output, stderr, hc, jsonOutput) healthcheck.RunChecks(output, stderr, hc, jsonOutput)
goldenFileBytes, err := ioutil.ReadFile("testdata/check_output_json.golden") goldenFileBytes, err := os.ReadFile("testdata/check_output_json.golden")
if err != nil { if err != nil {
t.Fatalf("Unexpected error: %v", err) t.Fatalf("Unexpected error: %v", err)
} }

View File

@ -5,7 +5,6 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
@ -630,10 +629,10 @@ func TestWalk(t *testing.T) {
file1 = filepath.Join(tmpFolderRoot, "root.txt") file1 = filepath.Join(tmpFolderRoot, "root.txt")
file2 = filepath.Join(tmpFolderData, "data.txt") file2 = filepath.Join(tmpFolderData, "data.txt")
) )
if err := ioutil.WriteFile(file1, data, 0600); err != nil { if err := os.WriteFile(file1, data, 0600); err != nil {
t.Fatal("Unexpected error: ", err) t.Fatal("Unexpected error: ", err)
} }
if err := ioutil.WriteFile(file2, data, 0600); err != nil { if err := os.WriteFile(file2, data, 0600); err != nil {
t.Fatal("Unexpected error: ", err) t.Fatal("Unexpected error: ", err)
} }

View File

@ -4,7 +4,7 @@ import (
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
"io/ioutil" "os"
"path/filepath" "path/filepath"
"testing" "testing"
@ -319,7 +319,7 @@ func testInstallOptionsHA(ha bool) (*charts.Values, error) {
return nil, err return nil, err
} }
data, err := ioutil.ReadFile(filepath.Join("testdata", "valid-crt.pem")) data, err := os.ReadFile(filepath.Join("testdata", "valid-crt.pem"))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -336,7 +336,7 @@ func testInstallOptionsHA(ha bool) (*charts.Values, error) {
} }
values.Identity.Issuer.TLS.KeyPEM = key values.Identity.Issuer.TLS.KeyPEM = key
data, err = ioutil.ReadFile(filepath.Join("testdata", "valid-trust-anchors.pem")) data, err = os.ReadFile(filepath.Join("testdata", "valid-trust-anchors.pem"))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -376,15 +376,15 @@ func testInstallValues() (*charts.Values, error) {
values.PolicyController.Image.Version = installControlPlaneVersion values.PolicyController.Image.Version = installControlPlaneVersion
values.HeartbeatSchedule = fakeHeartbeatSchedule() values.HeartbeatSchedule = fakeHeartbeatSchedule()
identityCert, err := ioutil.ReadFile(filepath.Join("testdata", "valid-crt.pem")) identityCert, err := os.ReadFile(filepath.Join("testdata", "valid-crt.pem"))
if err != nil { if err != nil {
return nil, err return nil, err
} }
identityKey, err := ioutil.ReadFile(filepath.Join("testdata", "valid-key.pem")) identityKey, err := os.ReadFile(filepath.Join("testdata", "valid-key.pem"))
if err != nil { if err != nil {
return nil, err return nil, err
} }
trustAnchorsPEM, err := ioutil.ReadFile(filepath.Join("testdata", "valid-trust-anchors.pem")) trustAnchorsPEM, err := os.ReadFile(filepath.Join("testdata", "valid-trust-anchors.pem"))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -516,7 +516,7 @@ func TestValidate(t *testing.T) {
} }
values.Identity.Issuer.TLS.KeyPEM = key values.Identity.Issuer.TLS.KeyPEM = key
ca, err := ioutil.ReadFile(filepath.Join("testdata", tc.crtFilePrefix+"-trust-anchors.pem")) ca, err := os.ReadFile(filepath.Join("testdata", tc.crtFilePrefix+"-trust-anchors.pem"))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -4,7 +4,7 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io"
"os" "os"
"testing" "testing"
@ -84,7 +84,7 @@ func Test_obfuscateMetrics(t *testing.T) {
t.Errorf("error opening test input file: %v\n", err) t.Errorf("error opening test input file: %v\n", err)
} }
fileBytes, err := ioutil.ReadAll(file) fileBytes, err := io.ReadAll(file)
if err != nil { if err != nil {
t.Errorf("error reading test input file: %v\n", err) t.Errorf("error reading test input file: %v\n", err)
} }

View File

@ -7,7 +7,6 @@ import (
"path/filepath" "path/filepath"
"fmt" "fmt"
"io/ioutil"
"net" "net"
"strings" "strings"
"time" "time"
@ -152,7 +151,7 @@ func makeInstallUpgradeFlags(defaults *l5dcharts.Values) ([]flag.Flag, *pflag.Fl
"A path to a PEM-encoded file containing Linkerd Identity trust anchors (generated by default)", "A path to a PEM-encoded file containing Linkerd Identity trust anchors (generated by default)",
func(values *l5dcharts.Values, value string) error { func(values *l5dcharts.Values, value string) error {
if value != "" { if value != "" {
data, err := ioutil.ReadFile(filepath.Clean(value)) data, err := os.ReadFile(filepath.Clean(value))
if err != nil { if err != nil {
return err return err
} }
@ -184,7 +183,7 @@ func makeInstallUpgradeFlags(defaults *l5dcharts.Values) ([]flag.Flag, *pflag.Fl
} }
func loadCrtPEM(path string) (string, error) { func loadCrtPEM(path string) (string, error) {
data, err := ioutil.ReadFile(filepath.Clean(path)) data, err := os.ReadFile(filepath.Clean(path))
if err != nil { if err != nil {
return "", err return "", err
} }
@ -197,7 +196,7 @@ func loadCrtPEM(path string) (string, error) {
} }
func loadKeyPEM(path string) (string, error) { func loadKeyPEM(path string) (string, error) {
data, err := ioutil.ReadFile(filepath.Clean(path)) data, err := os.ReadFile(filepath.Clean(path))
if err != nil { if err != nil {
return "", err return "", err
} }

View File

@ -6,7 +6,6 @@ import (
"encoding/base64" "encoding/base64"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"strings" "strings"
"testing" "testing"
@ -462,15 +461,15 @@ func generateCerts(t *testing.T, name string, b64encode bool) issuerCerts {
keyPem := strings.TrimSpace(issuer.Cred.EncodePrivateKeyPEM()) keyPem := strings.TrimSpace(issuer.Cred.EncodePrivateKeyPEM())
crtPem := strings.TrimSpace(issuer.Cred.EncodeCertificatePEM()) crtPem := strings.TrimSpace(issuer.Cred.EncodeCertificatePEM())
caFile, err := ioutil.TempFile("", "ca.*.pem") caFile, err := os.CreateTemp("", "ca.*.pem")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
crtFile, err := ioutil.TempFile("", "crt.*.pem") crtFile, err := os.CreateTemp("", "crt.*.pem")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
keyFile, err := ioutil.TempFile("", "key.*.pem") keyFile, err := os.CreateTemp("", "key.*.pem")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -19,7 +19,6 @@ import (
"bytes" "bytes"
"flag" "flag"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"os/user" "os/user"
@ -58,7 +57,7 @@ func setEnv(key, value string, t *testing.T) {
} }
func mktemp(dir, prefix string, t *testing.T) string { func mktemp(dir, prefix string, t *testing.T) string {
tempDir, err := ioutil.TempDir(dir, prefix) tempDir, err := os.MkdirTemp(dir, prefix)
if err != nil { if err != nil {
testutil.AnnotatedFatalf(t, "couldn't get current working directory", testutil.AnnotatedFatalf(t, "couldn't get current working directory",
"couldn't get current working directory: %v", err) "couldn't get current working directory: %v", err)
@ -77,7 +76,7 @@ func pwd(t *testing.T) string {
} }
func ls(dir string, t *testing.T) []string { func ls(dir string, t *testing.T) []string {
files, err := ioutil.ReadDir(dir) files, err := os.ReadDir(dir)
if err != nil { if err != nil {
testutil.AnnotatedFatalf(t, "failed to list files", testutil.AnnotatedFatalf(t, "failed to list files",
"failed to list files: %v", err) "failed to list files: %v", err)
@ -90,12 +89,12 @@ func ls(dir string, t *testing.T) []string {
} }
func cp(src, dest string, t *testing.T) { func cp(src, dest string, t *testing.T) {
data, err := ioutil.ReadFile(src) data, err := os.ReadFile(src)
if err != nil { if err != nil {
testutil.AnnotatedFatalf(t, fmt.Sprintf("failed to read file %v", src), testutil.AnnotatedFatalf(t, fmt.Sprintf("failed to read file %v", src),
"failed to read file %v: %v", src, err) "failed to read file %v: %v", src, err)
} }
if err = ioutil.WriteFile(dest, data, 0600); err != nil { if err = os.WriteFile(dest, data, 0600); err != nil {
testutil.AnnotatedFatalf(t, fmt.Sprintf("failed to write file %v", dest), testutil.AnnotatedFatalf(t, fmt.Sprintf("failed to write file %v", dest),
"failed to write file %v: %v", dest, err) "failed to write file %v: %v", dest, err)
} }
@ -192,7 +191,7 @@ func startDocker(testNum int, wd string, testWorkRootDir string, tempCNINetDir s
containerID, err := cmd.Output() containerID, err := cmd.Output()
if err != nil { if err != nil {
errFileContents, _ := ioutil.ReadFile(errFileName) errFileContents, _ := os.ReadFile(errFileName)
t.Logf("%v contents:\n\n%v\n\n", errFileName, string(errFileContents)) t.Logf("%v contents:\n\n%v\n\n", errFileName, string(errFileContents))
testutil.Fatalf(t, testutil.Fatalf(t,
"test %v ERROR: failed to start docker container '%v', see %v", testNum, dockerImage, errFileName) "test %v ERROR: failed to start docker container '%v', see %v", testNum, dockerImage, errFileName)
@ -214,13 +213,13 @@ func docker(cmd, containerID string, t *testing.T) {
// compareConfResult does a string compare of 2 test files. // compareConfResult does a string compare of 2 test files.
func compareConfResult(testWorkRootDir string, tempCNINetDir string, result string, expected string, t *testing.T) { func compareConfResult(testWorkRootDir string, tempCNINetDir string, result string, expected string, t *testing.T) {
tempResult := tempCNINetDir + "/" + result tempResult := tempCNINetDir + "/" + result
resultFile, err := ioutil.ReadFile(tempResult) resultFile, err := os.ReadFile(tempResult)
if err != nil { if err != nil {
testutil.AnnotatedFatalf(t, "failed to read file", testutil.AnnotatedFatalf(t, "failed to read file",
"failed to read file %v: %v", tempResult, err) "failed to read file %v: %v", tempResult, err)
} }
expectedFile, err := ioutil.ReadFile(expected) expectedFile, err := os.ReadFile(expected)
if err != nil { if err != nil {
testutil.AnnotatedFatalf(t, fmt.Sprintf("failed to read file %v", expected), testutil.AnnotatedFatalf(t, fmt.Sprintf("failed to read file %v", expected),
"failed to read file %v, err: %v", expected, err) "failed to read file %v, err: %v", expected, err)
@ -265,7 +264,7 @@ func doTest(testNum int, wd string, initialNetConfFile string, finalNetConfFile
if initialNetConfFile != "NONE" { if initialNetConfFile != "NONE" {
setEnv(cniConfName, initialNetConfFile, t) setEnv(cniConfName, initialNetConfFile, t)
} }
defaultData, err := ioutil.ReadFile(wd + "../deployment/linkerd-cni.conf.default") defaultData, err := os.ReadFile(wd + "../deployment/linkerd-cni.conf.default")
if err != nil { if err != nil {
testutil.AnnotatedFatalf(t, "failed to read file linkerd-cni.conf.default", testutil.AnnotatedFatalf(t, "failed to read file linkerd-cni.conf.default",
"failed to read file %v, err: %v", wd+"../deployment/linkerd-cni.conf.default", err) "failed to read file %v, err: %v", wd+"../deployment/linkerd-cni.conf.default", err)

View File

@ -4,7 +4,6 @@ import (
"context" "context"
"flag" "flag"
"fmt" "fmt"
"io/ioutil"
"net" "net"
"os" "os"
"os/signal" "os/signal"
@ -53,7 +52,7 @@ func Main(args []string) {
flags.ConfigureAndParse(cmd, args) flags.ConfigureAndParse(cmd, args)
identityTrustAnchorPEM, err := ioutil.ReadFile(k8s.MountPathTrustRootsPEM) identityTrustAnchorPEM, err := os.ReadFile(k8s.MountPathTrustRootsPEM)
if err != nil { if err != nil {
log.Fatalf("could not read identity trust anchors PEM: %s", err.Error()) log.Fatalf("could not read identity trust anchors PEM: %s", err.Error())
} }

View File

@ -1,7 +1,7 @@
package fake package fake
import ( import (
"io/ioutil" "os"
"path/filepath" "path/filepath"
admissionv1beta1 "k8s.io/api/admission/v1beta1" admissionv1beta1 "k8s.io/api/admission/v1beta1"
@ -31,7 +31,7 @@ func NewFactory(rootDir string) *Factory {
// bytes. If the file doesn't exist in the 'fake/data' folder, an error will be // bytes. If the file doesn't exist in the 'fake/data' folder, an error will be
// returned. // returned.
func (f *Factory) FileContents(filename string) ([]byte, error) { func (f *Factory) FileContents(filename string) ([]byte, error) {
return ioutil.ReadFile(filepath.Join(f.rootDir, filename)) return os.ReadFile(filepath.Join(f.rootDir, filename))
} }
// AdmissionReview returns the content of the specified file as an // AdmissionReview returns the content of the specified file as an
@ -40,7 +40,7 @@ func (f *Factory) FileContents(filename string) ([]byte, error) {
// ii. the file content isn't a valid YAML structure that can be unmarshalled // ii. the file content isn't a valid YAML structure that can be unmarshalled
// into AdmissionReview type // into AdmissionReview type
func (f *Factory) AdmissionReview(filename string) (*admissionv1beta1.AdmissionReview, error) { func (f *Factory) AdmissionReview(filename string) (*admissionv1beta1.AdmissionReview, error) {
b, err := ioutil.ReadFile(filepath.Join(f.rootDir, filename)) b, err := os.ReadFile(filepath.Join(f.rootDir, filename))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -58,7 +58,7 @@ func (f *Factory) AdmissionReview(filename string) (*admissionv1beta1.AdmissionR
// ii. the file content isn't a valid YAML structure that can be unmarshalled // ii. the file content isn't a valid YAML structure that can be unmarshalled
// into Deployment type // into Deployment type
func (f *Factory) Deployment(filename string) (*appsv1.Deployment, error) { func (f *Factory) Deployment(filename string) (*appsv1.Deployment, error) {
b, err := ioutil.ReadFile(filepath.Join(f.rootDir, filename)) b, err := os.ReadFile(filepath.Join(f.rootDir, filename))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -77,7 +77,7 @@ func (f *Factory) Deployment(filename string) (*appsv1.Deployment, error) {
// ii. the file content isn't a valid YAML structure that can be unmarshalled // ii. the file content isn't a valid YAML structure that can be unmarshalled
// into Container type // into Container type
func (f *Factory) Container(filename string) (*corev1.Container, error) { func (f *Factory) Container(filename string) (*corev1.Container, error) {
b, err := ioutil.ReadFile(filepath.Join(f.rootDir, filename)) b, err := os.ReadFile(filepath.Join(f.rootDir, filename))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -96,7 +96,7 @@ func (f *Factory) Container(filename string) (*corev1.Container, error) {
// ii. the file content isn't a valid YAML structure that can be unmarshalled // ii. the file content isn't a valid YAML structure that can be unmarshalled
// into ConfigMap type // into ConfigMap type
func (f *Factory) ConfigMap(filename string) (*corev1.ConfigMap, error) { func (f *Factory) ConfigMap(filename string) (*corev1.ConfigMap, error) {
b, err := ioutil.ReadFile(filepath.Join(f.rootDir, filename)) b, err := os.ReadFile(filepath.Join(f.rootDir, filename))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -115,7 +115,7 @@ func (f *Factory) ConfigMap(filename string) (*corev1.ConfigMap, error) {
// ii. the file content isn't a valid YAML structure that can be unmarshalled // ii. the file content isn't a valid YAML structure that can be unmarshalled
// into Namespace type // into Namespace type
func (f *Factory) Namespace(filename string) (*corev1.Namespace, error) { func (f *Factory) Namespace(filename string) (*corev1.Namespace, error) {
b, err := ioutil.ReadFile(filepath.Join(f.rootDir, filename)) b, err := os.ReadFile(filepath.Join(f.rootDir, filename))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -134,7 +134,7 @@ func (f *Factory) Namespace(filename string) (*corev1.Namespace, error) {
// ii. the file content isn't a valid YAML structure that can be unmarshalled // ii. the file content isn't a valid YAML structure that can be unmarshalled
// into Volume type // into Volume type
func (f *Factory) Volume(filename string) (*corev1.Volume, error) { func (f *Factory) Volume(filename string) (*corev1.Volume, error) {
b, err := ioutil.ReadFile(filepath.Join(f.rootDir, filename)) b, err := os.ReadFile(filepath.Join(f.rootDir, filename))
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -3,7 +3,7 @@ package injector
import ( import (
"context" "context"
"fmt" "fmt"
"io/ioutil" "os"
"strings" "strings"
"github.com/linkerd/linkerd2/controller/k8s" "github.com/linkerd/linkerd2/controller/k8s"
@ -45,7 +45,7 @@ func Inject(linkerdNamespace string) webhook.Handler {
return nil, err return nil, err
} }
caPEM, err := ioutil.ReadFile(pkgK8s.MountPathTrustRootsPEM) caPEM, err := os.ReadFile(pkgK8s.MountPathTrustRootsPEM)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -3,7 +3,7 @@ package config
import ( import (
"context" "context"
"fmt" "fmt"
"io/ioutil" "os"
"path/filepath" "path/filepath"
"github.com/linkerd/linkerd2/pkg/k8s" "github.com/linkerd/linkerd2/pkg/k8s"
@ -19,7 +19,7 @@ import (
// Values returns the Value struct from the linkerd-config ConfigMap // Values returns the Value struct from the linkerd-config ConfigMap
func Values(path string) (*l5dcharts.Values, error) { func Values(path string) (*l5dcharts.Values, error) {
p := filepath.Clean(path) p := filepath.Clean(path)
configYaml, err := ioutil.ReadFile(p) configYaml, err := os.ReadFile(p)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to read config file: %w", err) return nil, fmt.Errorf("failed to read config file: %w", err)
} }

View File

@ -6,7 +6,7 @@ import (
"crypto/rsa" "crypto/rsa"
"crypto/x509" "crypto/x509"
"fmt" "fmt"
"io/ioutil" "os"
"path/filepath" "path/filepath"
"time" "time"
@ -86,12 +86,12 @@ func FetchExternalIssuerData(ctx context.Context, api kubernetes.Interface, cont
// LoadIssuerCrtAndKeyFromFiles loads the issuer certificate and key from files // LoadIssuerCrtAndKeyFromFiles loads the issuer certificate and key from files
func LoadIssuerCrtAndKeyFromFiles(keyPEMFile, crtPEMFile string) (string, string, error) { func LoadIssuerCrtAndKeyFromFiles(keyPEMFile, crtPEMFile string) (string, string, error) {
key, err := ioutil.ReadFile(filepath.Clean(keyPEMFile)) key, err := os.ReadFile(filepath.Clean(keyPEMFile))
if err != nil { if err != nil {
return "", "", err return "", "", err
} }
crt, err := ioutil.ReadFile(filepath.Clean(crtPEMFile)) crt, err := os.ReadFile(filepath.Clean(crtPEMFile))
if err != nil { if err != nil {
return "", "", err return "", "", err
} }
@ -106,7 +106,7 @@ func LoadIssuerDataFromFiles(keyPEMFile, crtPEMFile, trustPEMFile string) (*Issu
return nil, err return nil, err
} }
anchors, err := ioutil.ReadFile(filepath.Clean(trustPEMFile)) anchors, err := os.ReadFile(filepath.Clean(trustPEMFile))
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -2,7 +2,7 @@ package k8s
import ( import (
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"os" "os"
@ -45,5 +45,5 @@ func getResponse(url 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)
} }

View File

@ -3,7 +3,7 @@ package k8s
import ( import (
"context" "context"
"fmt" "fmt"
"io/ioutil" "io"
"net" "net"
"net/http" "net/http"
"net/url" "net/url"
@ -178,8 +178,8 @@ func (pf *PortForward) run() error {
return err return err
} }
out := ioutil.Discard out := io.Discard
errOut := ioutil.Discard errOut := io.Discard
if pf.emitLogs { if pf.emitLogs {
out = os.Stdout out = os.Stdout
errOut = os.Stderr errOut = os.Stderr

View File

@ -3,7 +3,6 @@ package profiles
import ( import (
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"path" "path"
"sort" "sort"
@ -29,7 +28,7 @@ func RenderOpenAPI(fileName, namespace, name, clusterDomain string, w io.Writer)
return err return err
} }
bytes, err := ioutil.ReadAll(input) bytes, err := io.ReadAll(input)
if err != nil { if err != nil {
return fmt.Errorf("Error reading file: %w", err) return fmt.Errorf("Error reading file: %w", err)
} }

View File

@ -6,7 +6,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io"
"log" "log"
"net/http" "net/http"
"strings" "strings"
@ -363,7 +363,7 @@ func TestCheckIfResponseHasError(t *testing.T) {
message := SerializeAsPayload(protoInBytes) message := SerializeAsPayload(protoInBytes)
response := &http.Response{ response := &http.Response{
Header: make(http.Header), Header: make(http.Header),
Body: ioutil.NopCloser(bytes.NewReader(message)), Body: io.NopCloser(bytes.NewReader(message)),
StatusCode: http.StatusInternalServerError, StatusCode: http.StatusInternalServerError,
} }
response.Header.Set(errorHeader, "error") response.Header.Set(errorHeader, "error")
@ -394,7 +394,7 @@ func TestCheckIfResponseHasError(t *testing.T) {
response := &http.Response{ response := &http.Response{
Header: make(http.Header), Header: make(http.Header),
Body: ioutil.NopCloser(bytes.NewReader(j)), Body: io.NopCloser(bytes.NewReader(j)),
StatusCode: http.StatusForbidden, StatusCode: http.StatusForbidden,
Status: "403 Forbidden", Status: "403 Forbidden",
} }

View File

@ -9,7 +9,7 @@ import (
"encoding/pem" "encoding/pem"
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "os"
"path/filepath" "path/filepath"
"time" "time"
) )
@ -195,12 +195,12 @@ func ValidateAndCreateCreds(crt, key string) (*Cred, error) {
// ReadPEMCreds reads PEM-encoded credentials from the named files. // ReadPEMCreds reads PEM-encoded credentials from the named files.
func ReadPEMCreds(keyPath, crtPath string) (*Cred, error) { func ReadPEMCreds(keyPath, crtPath string) (*Cred, error) {
keyb, err := ioutil.ReadFile(filepath.Clean(keyPath)) keyb, err := os.ReadFile(filepath.Clean(keyPath))
if err != nil { if err != nil {
return nil, err return nil, err
} }
crtb, err := ioutil.ReadFile(filepath.Clean(crtPath)) crtb, err := os.ReadFile(filepath.Clean(crtPath))
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -3,7 +3,6 @@ package util
import ( import (
"fmt" "fmt"
"io" "io"
"io/ioutil"
"strings" "strings"
httpPb "github.com/linkerd/linkerd2-proxy-api/go/http_types" httpPb "github.com/linkerd/linkerd2-proxy-api/go/http_types"
@ -55,7 +54,7 @@ func ParseMethod(method string) *httpPb.HttpMethod {
// reached, the full bytes are returned. If the limit is reached, an error is // reached, the full bytes are returned. If the limit is reached, an error is
// returned. // returned.
func ReadAllLimit(r io.Reader, limit int) ([]byte, error) { func ReadAllLimit(r io.Reader, limit int) ([]byte, error) {
bytes, err := ioutil.ReadAll(io.LimitReader(r, int64(limit))) bytes, err := io.ReadAll(io.LimitReader(r, int64(limit)))
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -7,7 +7,6 @@ import (
"crypto/x509/pkix" "crypto/x509/pkix"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"syscall" "syscall"
@ -116,7 +115,7 @@ func generateAndStoreKey(p string) (key *ecdsa.PrivateKey, err error) {
} }
pemb := tls.EncodePrivateKeyP8(key) pemb := tls.EncodePrivateKeyP8(key)
err = ioutil.WriteFile(p, pemb, 0600) err = os.WriteFile(p, pemb, 0600)
return return
} }
@ -138,7 +137,7 @@ func generateAndStoreCSR(p, id string, key *ecdsa.PrivateKey) ([]byte, error) {
return nil, fmt.Errorf("failed to create CSR: %w", err) return nil, fmt.Errorf("failed to create CSR: %w", err)
} }
if err = ioutil.WriteFile(p, csrb, 0600); err != nil { if err = os.WriteFile(p, csrb, 0600); err != nil {
return nil, fmt.Errorf("failed to write CSR: %w", err) return nil, fmt.Errorf("failed to write CSR: %w", err)
} }

View File

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"regexp" "regexp"
"strings" "strings"
@ -108,7 +107,7 @@ func TestDirectEdges(t *testing.T) {
} }
ip = strings.Trim(ip, "\"") // strip quotes ip = strings.Trim(ip, "\"") // strip quotes
b, err := ioutil.ReadFile("testdata/slow-cooker.yaml") b, err := os.ReadFile("testdata/slow-cooker.yaml")
if err != nil { if err != nil {
testutil.AnnotatedError(t, "error reading file slow-cooker.yaml", err) testutil.AnnotatedError(t, "error reading file slow-cooker.yaml", err)
} }

View File

@ -5,7 +5,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
@ -83,7 +82,7 @@ func ReadTestdata(fileName string) string {
panic(fmt.Sprintf("Failed to open expected output file: %v", err)) panic(fmt.Sprintf("Failed to open expected output file: %v", err))
} }
fixture, err := ioutil.ReadAll(file) fixture, err := io.ReadAll(file)
if err != nil { if err != nil {
panic(fmt.Sprintf("Failed to read expected output file: %v", err)) panic(fmt.Sprintf("Failed to read expected output file: %v", err))
} }
@ -110,14 +109,14 @@ func unmarshalYAML(data []byte) ([]interface{}, error) {
func writeTestdata(fileName string, data []byte) { func writeTestdata(fileName string, data []byte) {
p := filepath.Join("testdata", fileName) p := filepath.Join("testdata", fileName)
if err := ioutil.WriteFile(p, data, 0600); err != nil { if err := os.WriteFile(p, data, 0600); err != nil {
panic(err) panic(err)
} }
} }
func writeRejects(origFileName string, data []byte, rejectPath string) { func writeRejects(origFileName string, data []byte, rejectPath string) {
p := filepath.Join(rejectPath, origFileName+".rej") p := filepath.Join(rejectPath, origFileName+".rej")
if err := ioutil.WriteFile(p, data, 0600); err != nil { if err := os.WriteFile(p, data, 0600); err != nil {
panic(err) panic(err)
} }
} }

View File

@ -9,7 +9,6 @@ import (
"flag" "flag"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"os" "os"
"os/exec" "os/exec"
@ -656,7 +655,7 @@ func (h *TestHelper) HTTPGetURL(url string) (string, error) {
} }
defer resp.Body.Close() defer resp.Body.Close()
bytes, err := ioutil.ReadAll(resp.Body) bytes, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return fmt.Errorf("Error reading response body: %w", err) return fmt.Errorf("Error reading response body: %w", err)
} }
@ -728,7 +727,7 @@ func (h *TestHelper) DownloadCLIBinary(filepath, version string) error {
// ReadFile reads a file from disk and returns the contents as a string. // ReadFile reads a file from disk and returns the contents as a string.
func ReadFile(file string) (string, error) { func ReadFile(file string) (string, error) {
b, err := ioutil.ReadFile(file) b, err := os.ReadFile(file)
if err != nil { if err != nil {
return "", err return "", err
} }

View File

@ -3,9 +3,9 @@ package cmd
import ( import (
"bytes" "bytes"
"context" "context"
"io/ioutil"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"os"
"testing" "testing"
"github.com/golang/protobuf/ptypes/duration" "github.com/golang/protobuf/ptypes/duration"
@ -149,7 +149,7 @@ func busyTest(t *testing.T, output string) {
goldenFilePath = "testdata/tap_busy_output.golden" goldenFilePath = "testdata/tap_busy_output.golden"
} }
goldenFileBytes, err := ioutil.ReadFile(goldenFilePath) goldenFileBytes, err := os.ReadFile(goldenFilePath)
if err != nil { if err != nil {
t.Fatalf("Unexpected error: %v", err) t.Fatalf("Unexpected error: %v", err)
} }
@ -207,7 +207,7 @@ func TestRequestTapByResourceFromAPI(t *testing.T) {
t.Fatalf("Unexpected error: %v", err) t.Fatalf("Unexpected error: %v", err)
} }
goldenFileBytes, err := ioutil.ReadFile("testdata/tap_empty_output.golden") goldenFileBytes, err := os.ReadFile("testdata/tap_empty_output.golden")
if err != nil { if err != nil {
t.Fatalf("Unexpected error: %v", err) t.Fatalf("Unexpected error: %v", err)
} }

View File

@ -4,9 +4,10 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors" "errors"
"io/ioutil" "io"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"os"
"testing" "testing"
"github.com/go-test/deep" "github.com/go-test/deep"
@ -71,7 +72,7 @@ func TestHandleApiCheck(t *testing.T) {
h.handleAPICheck(w, req, httprouter.Params{}) h.handleAPICheck(w, req, httprouter.Params{})
resp := w.Result() resp := w.Result()
defer resp.Body.Close() defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
t.Fatalf("not expecting error reading response body but got: %v", err) t.Fatalf("not expecting error reading response body but got: %v", err)
} }
@ -83,7 +84,7 @@ func TestHandleApiCheck(t *testing.T) {
if diff := deep.Equal(resp.Header, expectedHeaders); diff != nil { if diff := deep.Equal(resp.Header, expectedHeaders); diff != nil {
t.Errorf("Unexpected header: %v", diff) t.Errorf("Unexpected header: %v", diff)
} }
apiCheckOutputGolden, err := ioutil.ReadFile("testdata/api_check_output.json") apiCheckOutputGolden, err := os.ReadFile("testdata/api_check_output.json")
if err != nil { if err != nil {
t.Fatalf("not expecting error reading api check output golden file but got: %v", err) t.Fatalf("not expecting error reading api check output golden file but got: %v", err)
} }
@ -129,7 +130,7 @@ func TestHandleApiGateway(t *testing.T) {
handler.handleAPIGateways(recorder, req, httprouter.Params{}) handler.handleAPIGateways(recorder, req, httprouter.Params{})
resp := recorder.Result() resp := recorder.Result()
defer resp.Body.Close() defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
t.Fatalf("not expecting error reading response body but got: %v", err) t.Fatalf("not expecting error reading response body but got: %v", err)
} }
@ -146,7 +147,7 @@ func TestHandleApiGateway(t *testing.T) {
t.Errorf("Unexpected header: %v", diff) t.Errorf("Unexpected header: %v", diff)
} }
apiGatewayOutputGolden, err := ioutil.ReadFile("testdata/api_gateway_output.json") apiGatewayOutputGolden, err := os.ReadFile("testdata/api_gateway_output.json")
if err != nil { if err != nil {
t.Fatalf("not expecting error reading api check output golden file but got: %v", err) t.Fatalf("not expecting error reading api check output golden file but got: %v", err)
} }
@ -171,7 +172,7 @@ func TestHandleApiGateway(t *testing.T) {
handler.handleAPIGateways(recorder, req, httprouter.Params{}) handler.handleAPIGateways(recorder, req, httprouter.Params{})
resp := recorder.Result() resp := recorder.Result()
defer resp.Body.Close() defer resp.Body.Close()
_, err := ioutil.ReadAll(resp.Body) _, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
t.Fatalf("not expecting error reading response body but got: %v", err) t.Fatalf("not expecting error reading response body but got: %v", err)
} }