Skip to content

Commit

Permalink
cleanup io/ioutil
Browse files Browse the repository at this point in the history
Signed-off-by: cndoit18 <cndoit18@outlook.com>
  • Loading branch information
cndoit18 committed May 8, 2021
1 parent 745c7c9 commit 9ff889b
Show file tree
Hide file tree
Showing 16 changed files with 34 additions and 37 deletions.
3 changes: 1 addition & 2 deletions examples/scratch-env/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
goflag "flag"
"fmt"
"io"
"io/ioutil"
"os"

flag "github.com/spf13/pflag"
Expand Down Expand Up @@ -74,7 +73,7 @@ func runMain() int {
log.Info("apiserver running", "host", cfg.Host)

// TODO(directxman12): add support for writing to a new context in an existing file
kubeconfigFile, err := ioutil.TempFile("", "scratch-env-kubeconfig-")
kubeconfigFile, err := os.CreateTemp("", "scratch-env-kubeconfig-")
if err != nil {
log.Error(err, "unable to create kubeconfig file, continuing on without it")
} else {
Expand Down
5 changes: 2 additions & 3 deletions pkg/client/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package config

import (
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -45,7 +44,7 @@ var _ = Describe("Config", func() {
BeforeEach(func() {
// create temporary directory for test case
var err error
dir, err = ioutil.TempDir("", "cr-test")
dir, err = os.MkdirTemp("", "cr-test")
Expect(err).NotTo(HaveOccurred())

// override $HOME/.kube/config
Expand Down Expand Up @@ -192,7 +191,7 @@ func setConfigs(tc testCase, dir string) {

func createFiles(files map[string]string, dir string) error {
for path, data := range files {
if err := ioutil.WriteFile(filepath.Join(dir, path), []byte(data), 0644); err != nil {
if err := os.WriteFile(filepath.Join(dir, path), []byte(data), 0644); err != nil {
return err
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package config

import (
"fmt"
ioutil "io/ioutil"
"os"
"sync"

"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -96,7 +96,7 @@ func (d *DeferredFileLoader) loadFile() {
return
}

content, err := ioutil.ReadFile(d.path)
content, err := os.ReadFile(d.path)
if err != nil {
d.err = fmt.Errorf("could not read file at %s", d.path)
return
Expand Down
4 changes: 3 additions & 1 deletion pkg/envtest/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ func renderCRDs(options *CRDInstallOptions) ([]client.Object, error) {
if !info.IsDir() {
filePath, files = filepath.Dir(path), []os.FileInfo{info}
} else {
// TODO: Migrate to os.ReadDir
// information: https://go-review.googlesource.com/c/go/+/293649/
if files, err = ioutil.ReadDir(path); err != nil {
return nil, err
}
Expand Down Expand Up @@ -388,7 +390,7 @@ func readCRDs(basePath string, files []os.FileInfo) ([]*unstructured.Unstructure

// readDocuments reads documents from file
func readDocuments(fp string) ([][]byte, error) {
b, err := ioutil.ReadFile(fp)
b, err := os.ReadFile(fp)
if err != nil {
return nil, err
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/envtest/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func (o *WebhookInstallOptions) setupCA() ([]byte, error) {
return nil, fmt.Errorf("unable to set up webhook serving certs: %v", err)
}

localServingCertsDir, err := ioutil.TempDir("", "envtest-serving-certs-")
localServingCertsDir, err := os.MkdirTemp("", "envtest-serving-certs-")
o.LocalServingCertDir = localServingCertsDir
if err != nil {
return nil, fmt.Errorf("unable to create directory for webhook serving certs: %v", err)
Expand All @@ -292,10 +292,10 @@ func (o *WebhookInstallOptions) setupCA() ([]byte, error) {
return nil, fmt.Errorf("unable to marshal webhook serving certs: %v", err)
}

if err := ioutil.WriteFile(filepath.Join(localServingCertsDir, "tls.crt"), certData, 0640); err != nil {
if err := os.WriteFile(filepath.Join(localServingCertsDir, "tls.crt"), certData, 0640); err != nil {
return nil, fmt.Errorf("unable to write webhook serving cert to disk: %v", err)
}
if err := ioutil.WriteFile(filepath.Join(localServingCertsDir, "tls.key"), keyData, 0640); err != nil {
if err := os.WriteFile(filepath.Join(localServingCertsDir, "tls.key"), keyData, 0640); err != nil {
return nil, fmt.Errorf("unable to write webhook serving key to disk: %v", err)
}

Expand Down Expand Up @@ -382,6 +382,8 @@ func readWebhooks(path string) ([]client.Object, []client.Object, error) {
if !info.IsDir() {
path, files = filepath.Dir(path), []os.FileInfo{info}
} else {
// TODO: Migrate to os.ReadDir
// information: https://go-review.googlesource.com/c/go/+/293649/
if files, err = ioutil.ReadDir(path); err != nil {
return nil, nil, err
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/internal/testing/integration/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package integration
import (
"fmt"
"io"
"io/ioutil"
"net/url"
"os"
"path/filepath"
Expand Down Expand Up @@ -150,10 +149,10 @@ func (s *APIServer) populateAPIServerCerts() error {
return err
}

if err := ioutil.WriteFile(filepath.Join(s.CertDir, "apiserver.crt"), certData, 0640); err != nil {
if err := os.WriteFile(filepath.Join(s.CertDir, "apiserver.crt"), certData, 0640); err != nil {
return err
}
if err := ioutil.WriteFile(filepath.Join(s.CertDir, "apiserver.key"), keyData, 0640); err != nil {
if err := os.WriteFile(filepath.Join(s.CertDir, "apiserver.key"), keyData, 0640); err != nil {
return err
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package integrationtests
import (
"context"
"fmt"
"io/ioutil"
"io"
"net"
"time"

Expand Down Expand Up @@ -69,7 +69,7 @@ var _ = Describe("The Testing Framework", func() {
kubeCtl := controlPlane.KubeCtl()
stdout, stderr, err := kubeCtl.Run("get", "pods")
Expect(err).NotTo(HaveOccurred())
bytes, err := ioutil.ReadAll(stdout)
bytes, err := io.ReadAll(stdout)
Expect(err).NotTo(HaveOccurred())
Expect(bytes).To(BeEmpty())
Expect(stderr).To(ContainSubstring("No resources found"))
Expand Down
3 changes: 1 addition & 2 deletions pkg/internal/testing/integration/internal/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package internal
import (
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -95,7 +94,7 @@ func DoDefaulting(
}

if dir == "" {
newDir, err := ioutil.TempDir("", "k8s_test_framework_")
newDir, err := os.MkdirTemp("", "k8s_test_framework_")
if err != nil {
return DefaultedProcessInput{}, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/internal/testing/integration/internal/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package internal_test

import (
"bytes"
"io/ioutil"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -274,8 +273,9 @@ var _ = Describe("Stop method", func() {
processState := &ProcessState{}
processState.Session, err = gexec.Start(getSimpleCommand(), nil, nil)
Expect(err).NotTo(HaveOccurred())
processState.Dir, err = ioutil.TempDir("", "k8s_test_framework_")
tmp, err := os.MkdirTemp("", "k8s_test_framework_")
Expect(err).NotTo(HaveOccurred())
processState.Dir = tmp
processState.DirNeedsCleaning = true
processState.StopTimeout = 400 * time.Millisecond

Expand Down
4 changes: 2 additions & 2 deletions pkg/internal/testing/integration/kubectl_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package integration_test

import (
"io/ioutil"
"io"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand All @@ -16,7 +16,7 @@ var _ = Describe("Kubectl", func() {
stdout, stderr, err := k.Run(args...)
Expect(err).NotTo(HaveOccurred())
Expect(stdout).To(ContainSubstring("something"))
bytes, err := ioutil.ReadAll(stderr)
bytes, err := io.ReadAll(stderr)
Expect(err).NotTo(HaveOccurred())
Expect(bytes).To(BeEmpty())
})
Expand Down
3 changes: 1 addition & 2 deletions pkg/leaderelection/leader_election.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package leaderelection
import (
"errors"
"fmt"
"io/ioutil"
"os"

"k8s.io/apimachinery/pkg/util/uuid"
Expand Down Expand Up @@ -112,7 +111,7 @@ func getInClusterNamespace() (string, error) {
}

// Load the namespace file and return its content
namespace, err := ioutil.ReadFile(inClusterNamespacePath)
namespace, err := os.ReadFile(inClusterNamespacePath)
if err != nil {
return "", fmt.Errorf("error reading namespace file: %w", err)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/manager/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"io"
"net"
"net/http"
"path"
Expand Down Expand Up @@ -1132,7 +1132,7 @@ var _ = Describe("manger.Manager", func() {
Expect(err).NotTo(HaveOccurred())
Expect(resp.StatusCode).To(Equal(200))

data, err := ioutil.ReadAll(resp.Body)
data, err := io.ReadAll(resp.Body)
Expect(err).NotTo(HaveOccurred())
Expect(string(data)).To(ContainSubstring("%s\n%s\n%s\n",
`# HELP test_one test metric for testing`,
Expand Down Expand Up @@ -1174,7 +1174,7 @@ var _ = Describe("manger.Manager", func() {
Expect(err).NotTo(HaveOccurred())
Expect(resp.StatusCode).To(Equal(http.StatusOK))

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
Expect(err).NotTo(HaveOccurred())
Expect(string(body)).To(Equal("Some debug info"))
})
Expand Down
3 changes: 1 addition & 2 deletions pkg/webhook/admission/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"

v1 "k8s.io/api/admission/v1"
Expand Down Expand Up @@ -52,7 +51,7 @@ func (wh *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) {

var reviewResponse Response
if r.Body != nil {
if body, err = ioutil.ReadAll(r.Body); err != nil {
if body, err = io.ReadAll(r.Body); err != nil {
wh.log.Error(err, "unable to read the body from the incoming request")
reviewResponse = Errored(http.StatusBadRequest, err)
wh.writeResponse(w, reviewResponse)
Expand Down
4 changes: 2 additions & 2 deletions pkg/webhook/conversion/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package conversion
import (
"bytes"
"encoding/json"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"

Expand Down Expand Up @@ -70,7 +70,7 @@ var _ = Describe("Conversion Webhook", func() {

convReview := &apix.ConversionReview{}
req := &http.Request{
Body: ioutil.NopCloser(bytes.NewReader(payload.Bytes())),
Body: io.NopCloser(bytes.NewReader(payload.Bytes())),
}
webhook.ServeHTTP(respRecorder, req)
Expect(json.NewDecoder(respRecorder.Result().Body).Decode(convReview)).To(Succeed())
Expand Down
3 changes: 1 addition & 2 deletions pkg/webhook/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
Expand Down Expand Up @@ -205,7 +204,7 @@ func (s *Server) Start(ctx context.Context) error {
// load CA to verify client certificate
if s.ClientCAName != "" {
certPool := x509.NewCertPool()
clientCABytes, err := ioutil.ReadFile(filepath.Join(s.CertDir, s.ClientCAName))
clientCABytes, err := os.ReadFile(filepath.Join(s.CertDir, s.ClientCAName))
if err != nil {
return fmt.Errorf("failed to read client CA cert: %v", err)
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/webhook/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package webhook_test
import (
"context"
"fmt"
"io/ioutil"
"io"
"net"
"net/http"

Expand Down Expand Up @@ -134,7 +134,7 @@ var _ = Describe("Webhook Server", func() {
resp, err := client.Get(fmt.Sprintf("https://%s/somepath", testHostPort))
Expect(err).NotTo(HaveOccurred())
defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
return io.ReadAll(resp.Body)
}).Should(Equal([]byte("gadzooks!")))

ctxCancel()
Expand Down Expand Up @@ -172,7 +172,7 @@ var _ = Describe("Webhook Server", func() {
Expect(err).NotTo(HaveOccurred())
defer resp.Body.Close()

Expect(ioutil.ReadAll(resp.Body)).To(Equal([]byte("gadzooks!")))
Expect(io.ReadAll(resp.Body)).To(Equal([]byte("gadzooks!")))
})

It("should inject dependencies, if an inject func has been provided already", func() {
Expand All @@ -197,7 +197,7 @@ var _ = Describe("Webhook Server", func() {
resp, err := client.Get(fmt.Sprintf("https://%s/somepath", testHostPort))
Expect(err).NotTo(HaveOccurred())
defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
return io.ReadAll(resp.Body)
}).Should(Equal([]byte("gadzooks!")))

ctxCancel()
Expand Down

0 comments on commit 9ff889b

Please sign in to comment.