Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱 cleanup io/ioutil #1519

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions examples/scratch-env/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package main

import (
goflag "flag"
"io/ioutil"
"os"

flag "github.com/spf13/pflag"
Expand Down Expand Up @@ -83,7 +82,7 @@ func runMain() int {
}

// 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")
return 1
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 { //nolint:gosec
if err := os.WriteFile(filepath.Join(dir, path), []byte(data), 0600); 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
5 changes: 4 additions & 1 deletion pkg/envtest/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,9 @@ func renderCRDs(options *CRDInstallOptions) ([]client.Object, error) {

if !info.IsDir() {
filePath, files = filepath.Dir(path), []os.FileInfo{info}

// TODO: Migrate to os.ReadDir
// information: https://go-review.googlesource.com/c/go/+/293649/
} else if files, err = ioutil.ReadDir(path); err != nil {
return nil, err
}
Expand Down Expand Up @@ -602,7 +605,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) //nolint:gosec
b, err := os.ReadFile(fp)
if err != nil {
return nil, err
}
Expand Down
9 changes: 6 additions & 3 deletions pkg/envtest/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func (o *WebhookInstallOptions) setupCA() error {
return 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 fmt.Errorf("unable to create directory for webhook serving certs: %v", err)
Expand All @@ -286,10 +286,10 @@ func (o *WebhookInstallOptions) setupCA() error {
return fmt.Errorf("unable to marshal webhook serving certs: %v", err)
}

if err := ioutil.WriteFile(filepath.Join(localServingCertsDir, "tls.crt"), certData, 0640); err != nil { //nolint:gosec
if err = os.WriteFile(filepath.Join(localServingCertsDir, "tls.crt"), certData, 0600); err != nil {
return 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 { //nolint:gosec
if err = os.WriteFile(filepath.Join(localServingCertsDir, "tls.key"), keyData, 0600); err != nil {
return fmt.Errorf("unable to write webhook serving key to disk: %v", err)
}

Expand Down Expand Up @@ -375,6 +375,9 @@ func readWebhooks(path string) ([]client.Object, []client.Object, error) {
}
if !info.IsDir() {
path, files = filepath.Dir(path), []os.FileInfo{info}

// TODO: Migrate to os.ReadDir
// information: https://go-review.googlesource.com/c/go/+/293649/
} else if files, err = ioutil.ReadDir(path); err != nil {
return nil, nil, err
}
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 @@ -111,7 +110,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 @@ -1129,7 +1129,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 @@ -1170,7 +1170,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 @@ -60,7 +59,7 @@ func (wh *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

defer r.Body.Close()
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 @@ -240,7 +239,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 @@ -136,7 +136,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!")))

Expect(server.StartedChecker()(nil)).To(Succeed())
Expand Down Expand Up @@ -176,7 +176,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 @@ -201,7 +201,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