Skip to content

Commit

Permalink
refactor(*) move from io/ioutil to io and os package (#1802)
Browse files Browse the repository at this point in the history
The io/ioutil package has been deprecated as of Go 1.16, see
https://golang.org/doc/go1.16#ioutil. This commit replaces the existing
io/ioutil functions with their new definitions in io and os packages.

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

Co-authored-by: Shane Utt <shaneutt@linux.com>
Co-authored-by: Travis Raines <traines@konghq.com>
  • Loading branch information
3 people authored Sep 2, 2021
1 parent 0998cc3 commit 2841c52
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 19 deletions.
5 changes: 2 additions & 3 deletions hack/generators/controllers/networking/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"text/template"

Expand Down Expand Up @@ -227,7 +226,7 @@ func main() {
func header() (*bytes.Buffer, error) {
buf := new(bytes.Buffer)

boilerPlate, err := ioutil.ReadFile("../../hack/boilerplate.go.txt")
boilerPlate, err := os.ReadFile("../../hack/boilerplate.go.txt")
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -280,7 +279,7 @@ func (needed necessary) generate() error {
}
}

return ioutil.WriteFile(outputFile, contents.Bytes(), 0600)
return os.WriteFile(outputFile, contents.Bytes(), 0600)
}

type typeNeeded struct {
Expand Down
5 changes: 2 additions & 3 deletions hack/generators/manifests/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -87,7 +86,7 @@ func processFile(path string, info os.FileInfo, err error) error {
}

// read in the file contents
b, err := ioutil.ReadFile(path)
b, err := os.ReadFile(path)
if err != nil {
return err
}
Expand Down Expand Up @@ -120,5 +119,5 @@ func processFile(path string, info os.FileInfo, err error) error {
patchedCRD := append([]byte("\n---\n"), stdout.Bytes()...)

// write the patched CRD back to the original file
return ioutil.WriteFile(path, patchedCRD, info.Mode().Perm())
return os.WriteFile(path, patchedCRD, info.Mode().Perm())
}
4 changes: 2 additions & 2 deletions internal/adminapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"net/http"
"os"

"github.com/kong/go-kong/kong"
)
Expand Down Expand Up @@ -53,7 +53,7 @@ func MakeHTTPClient(opts *HTTPClientOpts) (*http.Client, error) {
if opts.CACertPath != "" {
certPath := opts.CACertPath
certPool := x509.NewCertPool()
cert, err := ioutil.ReadFile(certPath)
cert, err := os.ReadFile(certPath)
if err != nil {
return nil, fmt.Errorf("failed to read kong-admin-ca-cert from path '%s': %w", certPath, err)
}
Expand Down
9 changes: 5 additions & 4 deletions internal/admission/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"os"

"github.com/sirupsen/logrus"
admission "k8s.io/api/admission/v1"
Expand Down Expand Up @@ -39,11 +40,11 @@ type ServerConfig struct {
}

func readKeyPairFiles(certPath, keyPath string) ([]byte, []byte, error) {
cert, err := ioutil.ReadFile(certPath)
cert, err := os.ReadFile(certPath)
if err != nil {
return nil, nil, fmt.Errorf("read cert from file %q: %w", certPath, err)
}
key, err := ioutil.ReadFile(keyPath)
key, err := os.ReadFile(keyPath)
if err != nil {
return nil, nil, fmt.Errorf("read key from file %q: %w", keyPath, err)
}
Expand Down Expand Up @@ -116,7 +117,7 @@ func (a RequestHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
http.StatusBadRequest)
return
}
data, err := ioutil.ReadAll(r.Body)
data, err := io.ReadAll(r.Body)
if err != nil {
a.Logger.Errorf("failed to read request from client: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand Down
4 changes: 2 additions & 2 deletions test/integration/knative_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"context"
"crypto/tls"
"fmt"
"io/ioutil"
"io"
"net/http"
"testing"
"time"
Expand Down Expand Up @@ -172,7 +172,7 @@ func accessKnativeSrv(ctx context.Context, proxy, nsn string, t *testing.T) bool
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
bodyBytes, err := ioutil.ReadAll(resp.Body)
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return false
}
Expand Down
3 changes: 1 addition & 2 deletions test/integration/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
Expand Down Expand Up @@ -173,7 +172,7 @@ func deployControllers(ctx context.Context, namespace string) error {
exitOnErr(err)

// create a tempfile to hold the cluster kubeconfig that will be used for the controller
kubeconfig, err := ioutil.TempFile(os.TempDir(), "kubeconfig-")
kubeconfig, err := os.CreateTemp(os.TempDir(), "kubeconfig-")
exitOnErr(err)
defer os.Remove(kubeconfig.Name())
defer kubeconfig.Close()
Expand Down
5 changes: 2 additions & 3 deletions test/integration/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -177,7 +176,7 @@ func namespace(t *testing.T) (*corev1.Namespace, func()) {

// identifyTestCasesForDir finds the Go function names for any Go test files in the given directory
func identifyTestCasesForDir(dir string) ([]string, error) {
files, err := ioutil.ReadDir(dir)
files, err := os.ReadDir(dir)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -208,7 +207,7 @@ func identifyTestCasesForFile(filePath string) ([]string, error) {
return nil, fmt.Errorf("%s does not look like a Golang test file", filePath)
}

b, err := ioutil.ReadFile(filePath)
b, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 2841c52

Please sign in to comment.