Skip to content

Commit 6688005

Browse files
committed
do not use io/ioutil anymore
1 parent c7a98aa commit 6688005

File tree

18 files changed

+63
-58
lines changed

18 files changed

+63
-58
lines changed

.golangci.yml

+4
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ linters-settings:
6464
go: "1.18"
6565
stylecheck:
6666
go: "1.18"
67+
depguard:
68+
include-go-root: true
69+
packages:
70+
- io/ioutil # https://go.dev/doc/go1.16#ioutil
6771

6872
issues:
6973
max-same-issues: 0

examples/scratch-env/main.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package main
1818

1919
import (
2020
goflag "flag"
21-
"io/ioutil"
2221
"os"
2322

2423
flag "github.com/spf13/pflag"
@@ -83,7 +82,7 @@ func runMain() int {
8382
}
8483

8584
// TODO(directxman12): add support for writing to a new context in an existing file
86-
kubeconfigFile, err := ioutil.TempFile("", "scratch-env-kubeconfig-")
85+
kubeconfigFile, err := os.CreateTemp("", "scratch-env-kubeconfig-")
8786
if err != nil {
8887
log.Error(err, "unable to create kubeconfig file, continuing on without it")
8988
return 1

pkg/client/config/config_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package config
1818

1919
import (
20-
"io/ioutil"
2120
"os"
2221
"path/filepath"
2322
"strings"
@@ -45,7 +44,7 @@ var _ = Describe("Config", func() {
4544
BeforeEach(func() {
4645
// create temporary directory for test case
4746
var err error
48-
dir, err = ioutil.TempDir("", "cr-test")
47+
dir, err = os.MkdirTemp("", "cr-test")
4948
Expect(err).NotTo(HaveOccurred())
5049

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

193192
func createFiles(files map[string]string, dir string) error {
194193
for path, data := range files {
195-
if err := ioutil.WriteFile(filepath.Join(dir, path), []byte(data), 0644); err != nil { //nolint:gosec
194+
if err := os.WriteFile(filepath.Join(dir, path), []byte(data), 0644); err != nil { //nolint:gosec
196195
return err
197196
}
198197
}

pkg/config/config.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ package config
1818

1919
import (
2020
"fmt"
21-
ioutil "io/ioutil"
21+
"os"
2222
"sync"
2323

2424
"k8s.io/apimachinery/pkg/runtime"
@@ -96,7 +96,7 @@ func (d *DeferredFileLoader) loadFile() {
9696
return
9797
}
9898

99-
content, err := ioutil.ReadFile(d.path)
99+
content, err := os.ReadFile(d.path)
100100
if err != nil {
101101
d.err = fmt.Errorf("could not read file at %s", d.path)
102102
return

pkg/envtest/crd.go

+15-10
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"errors"
2424
"fmt"
2525
"io"
26-
"io/ioutil"
2726
"os"
2827
"path/filepath"
2928
"time"
@@ -282,7 +281,7 @@ func renderCRDs(options *CRDInstallOptions) ([]*apiextensionsv1.CustomResourceDe
282281
var (
283282
err error
284283
info os.FileInfo
285-
files []os.FileInfo
284+
files []string
286285
)
287286

288287
type GVKN struct {
@@ -304,9 +303,15 @@ func renderCRDs(options *CRDInstallOptions) ([]*apiextensionsv1.CustomResourceDe
304303
}
305304

306305
if !info.IsDir() {
307-
filePath, files = filepath.Dir(path), []os.FileInfo{info}
308-
} else if files, err = ioutil.ReadDir(path); err != nil {
309-
return nil, err
306+
filePath, files = filepath.Dir(path), []string{info.Name()}
307+
} else {
308+
entries, err := os.ReadDir(path)
309+
if err != nil {
310+
return nil, err
311+
}
312+
for _, e := range entries {
313+
files = append(files, e.Name())
314+
}
310315
}
311316

312317
log.V(1).Info("reading CRDs from path", "path", path)
@@ -390,20 +395,20 @@ func modifyConversionWebhooks(crds []*apiextensionsv1.CustomResourceDefinition,
390395
}
391396

392397
// readCRDs reads the CRDs from files and Unmarshals them into structs.
393-
func readCRDs(basePath string, files []os.FileInfo) ([]*apiextensionsv1.CustomResourceDefinition, error) {
398+
func readCRDs(basePath string, files []string) ([]*apiextensionsv1.CustomResourceDefinition, error) {
394399
var crds []*apiextensionsv1.CustomResourceDefinition
395400

396401
// White list the file extensions that may contain CRDs
397402
crdExts := sets.NewString(".json", ".yaml", ".yml")
398403

399404
for _, file := range files {
400405
// Only parse allowlisted file types
401-
if !crdExts.Has(filepath.Ext(file.Name())) {
406+
if !crdExts.Has(filepath.Ext(file)) {
402407
continue
403408
}
404409

405410
// Unmarshal CRDs from file into structs
406-
docs, err := readDocuments(filepath.Join(basePath, file.Name()))
411+
docs, err := readDocuments(filepath.Join(basePath, file))
407412
if err != nil {
408413
return nil, err
409414
}
@@ -420,14 +425,14 @@ func readCRDs(basePath string, files []os.FileInfo) ([]*apiextensionsv1.CustomRe
420425
crds = append(crds, crd)
421426
}
422427

423-
log.V(1).Info("read CRDs from file", "file", file.Name())
428+
log.V(1).Info("read CRDs from file", "file", file)
424429
}
425430
return crds, nil
426431
}
427432

428433
// readDocuments reads documents from file.
429434
func readDocuments(fp string) ([][]byte, error) {
430-
b, err := ioutil.ReadFile(fp)
435+
b, err := os.ReadFile(fp)
431436
if err != nil {
432437
return nil, err
433438
}

pkg/envtest/webhook.go

+16-11
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package envtest
1616
import (
1717
"context"
1818
"fmt"
19-
"io/ioutil"
2019
"net"
2120
"os"
2221
"path/filepath"
@@ -266,7 +265,7 @@ func (o *WebhookInstallOptions) setupCA() error {
266265
return fmt.Errorf("unable to set up webhook serving certs: %w", err)
267266
}
268267

269-
localServingCertsDir, err := ioutil.TempDir("", "envtest-serving-certs-")
268+
localServingCertsDir, err := os.MkdirTemp("", "envtest-serving-certs-")
270269
o.LocalServingCertDir = localServingCertsDir
271270
if err != nil {
272271
return fmt.Errorf("unable to create directory for webhook serving certs: %w", err)
@@ -277,10 +276,10 @@ func (o *WebhookInstallOptions) setupCA() error {
277276
return fmt.Errorf("unable to marshal webhook serving certs: %w", err)
278277
}
279278

280-
if err := ioutil.WriteFile(filepath.Join(localServingCertsDir, "tls.crt"), certData, 0640); err != nil { //nolint:gosec
279+
if err := os.WriteFile(filepath.Join(localServingCertsDir, "tls.crt"), certData, 0640); err != nil { //nolint:gosec
281280
return fmt.Errorf("unable to write webhook serving cert to disk: %w", err)
282281
}
283-
if err := ioutil.WriteFile(filepath.Join(localServingCertsDir, "tls.key"), keyData, 0640); err != nil { //nolint:gosec
282+
if err := os.WriteFile(filepath.Join(localServingCertsDir, "tls.key"), keyData, 0640); err != nil { //nolint:gosec
284283
return fmt.Errorf("unable to write webhook serving key to disk: %w", err)
285284
}
286285

@@ -359,17 +358,23 @@ func parseWebhook(options *WebhookInstallOptions) error {
359358
// returns slice of mutating and validating webhook configurations.
360359
func readWebhooks(path string) ([]*admissionv1.MutatingWebhookConfiguration, []*admissionv1.ValidatingWebhookConfiguration, error) {
361360
// Get the webhook files
362-
var files []os.FileInfo
361+
var files []string
363362
var err error
364363
log.V(1).Info("reading Webhooks from path", "path", path)
365364
info, err := os.Stat(path)
366365
if err != nil {
367366
return nil, nil, err
368367
}
369368
if !info.IsDir() {
370-
path, files = filepath.Dir(path), []os.FileInfo{info}
371-
} else if files, err = ioutil.ReadDir(path); err != nil {
372-
return nil, nil, err
369+
path, files = filepath.Dir(path), []string{info.Name()}
370+
} else {
371+
entries, err := os.ReadDir(path)
372+
if err != nil {
373+
return nil, nil, err
374+
}
375+
for _, e := range entries {
376+
files = append(files, e.Name())
377+
}
373378
}
374379

375380
// file extensions that may contain Webhooks
@@ -379,12 +384,12 @@ func readWebhooks(path string) ([]*admissionv1.MutatingWebhookConfiguration, []*
379384
var valHooks []*admissionv1.ValidatingWebhookConfiguration
380385
for _, file := range files {
381386
// Only parse allowlisted file types
382-
if !resourceExtensions.Has(filepath.Ext(file.Name())) {
387+
if !resourceExtensions.Has(filepath.Ext(file)) {
383388
continue
384389
}
385390

386391
// Unmarshal Webhooks from file into structs
387-
docs, err := readDocuments(filepath.Join(path, file.Name()))
392+
docs, err := readDocuments(filepath.Join(path, file))
388393
if err != nil {
389394
return nil, nil, err
390395
}
@@ -422,7 +427,7 @@ func readWebhooks(path string) ([]*admissionv1.MutatingWebhookConfiguration, []*
422427
}
423428
}
424429

425-
log.V(1).Info("read webhooks from file", "file", file.Name())
430+
log.V(1).Info("read webhooks from file", "file", file)
426431
}
427432
return mutHooks, valHooks, nil
428433
}

pkg/internal/testing/controlplane/apiserver.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package controlplane
1919
import (
2020
"fmt"
2121
"io"
22-
"io/ioutil"
2322
"net/url"
2423
"os"
2524
"path/filepath"
@@ -385,10 +384,10 @@ func (s *APIServer) populateAPIServerCerts() error {
385384
return err
386385
}
387386

388-
if err := ioutil.WriteFile(filepath.Join(s.CertDir, "apiserver.crt"), certData, 0640); err != nil { //nolint:gosec
387+
if err := os.WriteFile(filepath.Join(s.CertDir, "apiserver.crt"), certData, 0640); err != nil { //nolint:gosec
389388
return err
390389
}
391-
if err := ioutil.WriteFile(filepath.Join(s.CertDir, "apiserver.key"), keyData, 0640); err != nil { //nolint:gosec
390+
if err := os.WriteFile(filepath.Join(s.CertDir, "apiserver.key"), keyData, 0640); err != nil { //nolint:gosec
392391
return err
393392
}
394393

@@ -405,10 +404,10 @@ func (s *APIServer) populateAPIServerCerts() error {
405404
return err
406405
}
407406

408-
if err := ioutil.WriteFile(filepath.Join(s.CertDir, saCertFile), saCert, 0640); err != nil { //nolint:gosec
407+
if err := os.WriteFile(filepath.Join(s.CertDir, saCertFile), saCert, 0640); err != nil { //nolint:gosec
409408
return err
410409
}
411-
return ioutil.WriteFile(filepath.Join(s.CertDir, saKeyFile), saKey, 0640) //nolint:gosec
410+
return os.WriteFile(filepath.Join(s.CertDir, saKeyFile), saKey, 0640) //nolint:gosec
412411
}
413412

414413
// Stop stops this process gracefully, waits for its termination, and cleans up

pkg/internal/testing/controlplane/auth.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ package controlplane
1818

1919
import (
2020
"fmt"
21-
"io/ioutil"
21+
"os"
2222
"path/filepath"
2323

2424
"k8s.io/client-go/rest"
@@ -128,7 +128,7 @@ func (c *CertAuthn) Start() error {
128128
return fmt.Errorf("start called before configure")
129129
}
130130
caCrt := c.ca.CA.CertBytes()
131-
if err := ioutil.WriteFile(c.caCrtPath(), caCrt, 0640); err != nil { //nolint:gosec
131+
if err := os.WriteFile(c.caCrtPath(), caCrt, 0640); err != nil { //nolint:gosec
132132
return fmt.Errorf("unable to save the client certificate CA to %s: %w", c.caCrtPath(), err)
133133
}
134134

pkg/internal/testing/controlplane/kubectl_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ limitations under the License.
1717
package controlplane_test
1818

1919
import (
20-
"io/ioutil"
20+
"io"
2121

2222
. "github.com/onsi/ginkgo"
2323
. "github.com/onsi/gomega"
@@ -36,7 +36,7 @@ var _ = Describe("Kubectl", func() {
3636
stdout, stderr, err := k.Run(args...)
3737
Expect(err).NotTo(HaveOccurred())
3838
Expect(stdout).To(ContainSubstring("something"))
39-
bytes, err := ioutil.ReadAll(stderr)
39+
bytes, err := io.ReadAll(stderr)
4040
Expect(err).NotTo(HaveOccurred())
4141
Expect(bytes).To(BeEmpty())
4242
})

pkg/internal/testing/process/process.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"crypto/tls"
2121
"fmt"
2222
"io"
23-
"io/ioutil"
2423
"net"
2524
"net/http"
2625
"net/url"
@@ -109,7 +108,7 @@ func (ps *State) Init(name string) error {
109108
}
110109

111110
if ps.Dir == "" {
112-
newDir, err := ioutil.TempDir("", "k8s_test_framework_")
111+
newDir, err := os.MkdirTemp("", "k8s_test_framework_")
113112
if err != nil {
114113
return err
115114
}

pkg/internal/testing/process/process_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package process_test
1818

1919
import (
2020
"bytes"
21-
"io/ioutil"
2221
"net"
2322
"net/http"
2423
"net/url"
@@ -297,7 +296,7 @@ var _ = Describe("Stop method", func() {
297296
var err error
298297

299298
Expect(processState.Start(nil, nil)).To(Succeed())
300-
processState.Dir, err = ioutil.TempDir("", "k8s_test_framework_")
299+
processState.Dir, err = os.MkdirTemp("", "k8s_test_framework_")
301300
Expect(err).NotTo(HaveOccurred())
302301
processState.DirNeedsCleaning = true
303302
processState.StopTimeout = 400 * time.Millisecond

pkg/leaderelection/leader_election.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package leaderelection
1919
import (
2020
"errors"
2121
"fmt"
22-
"io/ioutil"
2322
"os"
2423

2524
"k8s.io/apimachinery/pkg/util/uuid"
@@ -120,7 +119,7 @@ func getInClusterNamespace() (string, error) {
120119
}
121120

122121
// Load the namespace file and return its content
123-
namespace, err := ioutil.ReadFile(inClusterNamespacePath)
122+
namespace, err := os.ReadFile(inClusterNamespacePath)
124123
if err != nil {
125124
return "", fmt.Errorf("error reading namespace file: %w", err)
126125
}

pkg/manager/manager_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"context"
2121
"errors"
2222
"fmt"
23-
"io/ioutil"
23+
"io"
2424
"net"
2525
"net/http"
2626
"path"
@@ -1186,7 +1186,7 @@ var _ = Describe("manger.Manager", func() {
11861186
defer resp.Body.Close()
11871187
Expect(resp.StatusCode).To(Equal(200))
11881188

1189-
data, err := ioutil.ReadAll(resp.Body)
1189+
data, err := io.ReadAll(resp.Body)
11901190
Expect(err).NotTo(HaveOccurred())
11911191
Expect(string(data)).To(ContainSubstring("%s\n%s\n%s\n",
11921192
`# HELP test_one test metric for testing`,
@@ -1229,7 +1229,7 @@ var _ = Describe("manger.Manager", func() {
12291229
defer resp.Body.Close()
12301230
Expect(resp.StatusCode).To(Equal(http.StatusOK))
12311231

1232-
body, err := ioutil.ReadAll(resp.Body)
1232+
body, err := io.ReadAll(resp.Body)
12331233
Expect(err).NotTo(HaveOccurred())
12341234
Expect(string(body)).To(Equal("Some debug info"))
12351235
})

pkg/webhook/admission/http.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"errors"
2222
"fmt"
2323
"io"
24-
"io/ioutil"
2524
"net/http"
2625

2726
v1 "k8s.io/api/admission/v1"
@@ -60,7 +59,7 @@ func (wh *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) {
6059
}
6160

6261
defer r.Body.Close()
63-
if body, err = ioutil.ReadAll(r.Body); err != nil {
62+
if body, err = io.ReadAll(r.Body); err != nil {
6463
wh.log.Error(err, "unable to read the body from the incoming request")
6564
reviewResponse = Errored(http.StatusBadRequest, err)
6665
wh.writeResponse(w, reviewResponse)

0 commit comments

Comments
 (0)