From b3fa06ca438431f4e4777b2f29c8b6257f478c71 Mon Sep 17 00:00:00 2001 From: guoguangwu Date: Sat, 7 Oct 2023 17:43:47 +0800 Subject: [PATCH] chore: remove refs to deprecated io/ioutil Signed-off-by: guoguangwu --- pkg/crd/gen_integration_test.go | 7 +++--- pkg/crd/parser_integration_test.go | 7 +++--- pkg/deepcopy/deepcopy_integration_test.go | 3 +-- pkg/genall/genall.go | 3 +-- pkg/genall/output.go | 3 +-- pkg/loader/loader.go | 3 +-- pkg/rbac/parser_integration_test.go | 3 +-- pkg/schemapatcher/gen.go | 4 ++-- pkg/schemapatcher/gen_integration_test.go | 11 ++++----- pkg/webhook/parser_integration_test.go | 29 +++++++++++------------ 10 files changed, 32 insertions(+), 41 deletions(-) diff --git a/pkg/crd/gen_integration_test.go b/pkg/crd/gen_integration_test.go index 7d256ca5b..e5dc41640 100644 --- a/pkg/crd/gen_integration_test.go +++ b/pkg/crd/gen_integration_test.go @@ -19,7 +19,6 @@ package crd_test import ( "bytes" "io" - "io/ioutil" "os" "path/filepath" @@ -93,7 +92,7 @@ var _ = Describe("CRD Generation proper defaulting", func() { Expect(gen.Generate(ctx)).NotTo(HaveOccurred()) By("loading the desired YAML") - expectedFile, err := ioutil.ReadFile(filepath.Join(genDir, "bar.example.com_foos.yaml")) + expectedFile, err := os.ReadFile(filepath.Join(genDir, "bar.example.com_foos.yaml")) Expect(err).NotTo(HaveOccurred()) expectedFile = fixAnnotations(expectedFile) @@ -109,10 +108,10 @@ var _ = Describe("CRD Generation proper defaulting", func() { Expect(gen.Generate(ctx2)).NotTo(HaveOccurred()) By("loading the desired YAMLs") - expectedFileFoos, err := ioutil.ReadFile(filepath.Join(genDir, "bar.example.com_foos.yaml")) + expectedFileFoos, err := os.ReadFile(filepath.Join(genDir, "bar.example.com_foos.yaml")) Expect(err).NotTo(HaveOccurred()) expectedFileFoos = fixAnnotations(expectedFileFoos) - expectedFileZoos, err := ioutil.ReadFile(filepath.Join(genDir, "zoo", "bar.example.com_zoos.yaml")) + expectedFileZoos, err := os.ReadFile(filepath.Join(genDir, "zoo", "bar.example.com_zoos.yaml")) Expect(err).NotTo(HaveOccurred()) expectedFileZoos = fixAnnotations(expectedFileZoos) diff --git a/pkg/crd/parser_integration_test.go b/pkg/crd/parser_integration_test.go index 76a9e1f3a..e6472e68d 100644 --- a/pkg/crd/parser_integration_test.go +++ b/pkg/crd/parser_integration_test.go @@ -18,7 +18,6 @@ package crd_test import ( "fmt" - "io/ioutil" "os" "github.com/google/go-cmp/cmp" @@ -116,7 +115,7 @@ var _ = Describe("CRD Generation From Parsing to CustomResourceDefinition", func ExpectWithOffset(1, parser.CustomResourceDefinitions).To(HaveKey(groupKind)) By(fmt.Sprintf("loading the desired %s YAML", kind)) - expectedFile, err := ioutil.ReadFile(fileName) + expectedFile, err := os.ReadFile(fileName) ExpectWithOffset(1, err).NotTo(HaveOccurred()) By(fmt.Sprintf("parsing the desired %s YAML", kind)) @@ -219,7 +218,7 @@ var _ = Describe("CRD Generation From Parsing to CustomResourceDefinition", func crd.FixTopLevelMetadata(parser.CustomResourceDefinitions[groupKind]) By("loading the desired YAML") - expectedFile, err := ioutil.ReadFile("plural.example.com_testquotas.yaml") + expectedFile, err := os.ReadFile("plural.example.com_testquotas.yaml") Expect(err).NotTo(HaveOccurred()) By("parsing the desired YAML") @@ -301,7 +300,7 @@ var _ = Describe("CRD Generation From Parsing to CustomResourceDefinition", func crd.FixTopLevelMetadata(parser.CustomResourceDefinitions[groupKind]) By("loading the desired YAML") - expectedFile, err := ioutil.ReadFile("testdata.kubebuilder.io_versionedresources.yaml") + expectedFile, err := os.ReadFile("testdata.kubebuilder.io_versionedresources.yaml") Expect(err).NotTo(HaveOccurred()) By("parsing the desired YAML") diff --git a/pkg/deepcopy/deepcopy_integration_test.go b/pkg/deepcopy/deepcopy_integration_test.go index a81ef190d..608d6e720 100644 --- a/pkg/deepcopy/deepcopy_integration_test.go +++ b/pkg/deepcopy/deepcopy_integration_test.go @@ -18,7 +18,6 @@ package deepcopy_test import ( "io" - "io/ioutil" "os" "sort" @@ -97,7 +96,7 @@ var _ = Describe("CRD Generation From Parsing to CustomResourceDefinition", func Expect(outContents).NotTo(BeNil()) By("loading the desired code") - expectedFile, err := ioutil.ReadFile("zz_generated.deepcopy.go") + expectedFile, err := os.ReadFile("zz_generated.deepcopy.go") Expect(err).NotTo(HaveOccurred()) By("comparing the two") diff --git a/pkg/genall/genall.go b/pkg/genall/genall.go index bed7ec603..b553db14c 100644 --- a/pkg/genall/genall.go +++ b/pkg/genall/genall.go @@ -20,7 +20,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "os" "golang.org/x/tools/go/packages" @@ -214,7 +213,7 @@ func (g GenerationContext) ReadFile(path string) ([]byte, error) { return nil, err } defer file.Close() - return ioutil.ReadAll(file) + return io.ReadAll(file) } // ForRoots produces a Runtime to run the given generators against the diff --git a/pkg/genall/output.go b/pkg/genall/output.go index 4cd29f496..3eb43b0c2 100644 --- a/pkg/genall/output.go +++ b/pkg/genall/output.go @@ -19,7 +19,6 @@ package genall import ( "fmt" "io" - "io/ioutil" "os" "path/filepath" @@ -92,7 +91,7 @@ var OutputToNothing = outputToNothing{} type outputToNothing struct{} func (o outputToNothing) Open(_ *loader.Package, _ string) (io.WriteCloser, error) { - return nopCloser{ioutil.Discard}, nil + return nopCloser{io.Discard}, nil } // +controllertools:marker:generateHelp:category="" diff --git a/pkg/loader/loader.go b/pkg/loader/loader.go index 2efa94c7d..7762e53e7 100644 --- a/pkg/loader/loader.go +++ b/pkg/loader/loader.go @@ -23,7 +23,6 @@ import ( "go/scanner" "go/token" "go/types" - "io/ioutil" "os" "path/filepath" "regexp" @@ -111,7 +110,7 @@ func (p *Package) NeedSyntax() { for i, filename := range p.CompiledGoFiles { go func(i int, filename string) { defer wg.Done() - src, err := ioutil.ReadFile(filename) + src, err := os.ReadFile(filename) if err != nil { p.AddError(err) return diff --git a/pkg/rbac/parser_integration_test.go b/pkg/rbac/parser_integration_test.go index a56c1ec6a..f4f7bf45a 100644 --- a/pkg/rbac/parser_integration_test.go +++ b/pkg/rbac/parser_integration_test.go @@ -3,7 +3,6 @@ package rbac_test import ( "bytes" "fmt" - "io/ioutil" "os" "github.com/google/go-cmp/cmp" @@ -49,7 +48,7 @@ var _ = Describe("ClusterRole generated by the RBAC Generator", func() { Expect(err).NotTo(HaveOccurred()) By("loading the desired YAML") - expectedFile, err := ioutil.ReadFile("role.yaml") + expectedFile, err := os.ReadFile("role.yaml") Expect(err).NotTo(HaveOccurred()) By("parsing the desired YAML") diff --git a/pkg/schemapatcher/gen.go b/pkg/schemapatcher/gen.go index e33ec11ff..8080aeae7 100644 --- a/pkg/schemapatcher/gen.go +++ b/pkg/schemapatcher/gen.go @@ -18,7 +18,7 @@ package schemapatcher import ( "fmt" - "io/ioutil" + "os" "path/filepath" "gopkg.in/yaml.v3" @@ -335,7 +335,7 @@ func (e *partialCRD) setVersionedSchemata(newSchemata map[string]apiext.JSONSche // minimally invasive. Returned CRDs are mapped by group-kind. func crdsFromDirectory(ctx *genall.GenerationContext, dir string) (map[schema.GroupKind]*partialCRDSet, error) { res := map[schema.GroupKind]*partialCRDSet{} - dirEntries, err := ioutil.ReadDir(dir) + dirEntries, err := os.ReadDir(dir) if err != nil { return nil, err } diff --git a/pkg/schemapatcher/gen_integration_test.go b/pkg/schemapatcher/gen_integration_test.go index d1c8485d6..1d15e5f2a 100644 --- a/pkg/schemapatcher/gen_integration_test.go +++ b/pkg/schemapatcher/gen_integration_test.go @@ -17,7 +17,6 @@ limitations under the License. package schemapatcher_test import ( - "io/ioutil" "os" "path/filepath" @@ -44,7 +43,7 @@ var _ = Describe("CRD Patching From Parsing to Editing", func() { rt, err := genall.Generators{&crdSchemaGen}.ForRoots("./...") Expect(err).NotTo(HaveOccurred()) - outputDir, err := ioutil.TempDir("", "controller-tools-test") + outputDir, err := os.MkdirTemp("", "controller-tools-test") Expect(err).NotTo(HaveOccurred()) defer os.RemoveAll(outputDir) rt.OutputRules.Default = genall.OutputToDirectory(outputDir) @@ -70,7 +69,7 @@ var _ = Describe("CRD Patching From Parsing to Editing", func() { rt, err := genall.Generators{&crdSchemaGen}.ForRoots("./...") Expect(err).NotTo(HaveOccurred()) - outputDir, err := ioutil.TempDir("", "controller-tools-test") + outputDir, err := os.MkdirTemp("", "controller-tools-test") Expect(err).NotTo(HaveOccurred()) defer os.RemoveAll(outputDir) rt.OutputRules.Default = genall.OutputToDirectory(outputDir) @@ -80,15 +79,15 @@ var _ = Describe("CRD Patching From Parsing to Editing", func() { Expect(rt.Run()).To(BeFalse(), "unexpectedly had errors") By("loading the output files") - expectedFiles, err := ioutil.ReadDir("expected") + expectedFiles, err := os.ReadDir("expected") Expect(err).NotTo(HaveOccurred()) for _, expectedFile := range expectedFiles { By("reading the expected and actual files for " + expectedFile.Name()) - actualContents, err := ioutil.ReadFile(filepath.Join(outputDir, expectedFile.Name())) + actualContents, err := os.ReadFile(filepath.Join(outputDir, expectedFile.Name())) Expect(err).NotTo(HaveOccurred()) - expectedContents, err := ioutil.ReadFile(filepath.Join("expected", expectedFile.Name())) + expectedContents, err := os.ReadFile(filepath.Join("expected", expectedFile.Name())) Expect(err).NotTo(HaveOccurred()) By("checking that the expected and actual files for " + expectedFile.Name() + " are identical") diff --git a/pkg/webhook/parser_integration_test.go b/pkg/webhook/parser_integration_test.go index 204c0cb43..f020cfc2e 100644 --- a/pkg/webhook/parser_integration_test.go +++ b/pkg/webhook/parser_integration_test.go @@ -18,7 +18,6 @@ package webhook_test import ( "bytes" - "io/ioutil" "os" "path" @@ -59,7 +58,7 @@ var _ = Describe("Webhook Generation From Parsing to CustomResourceDefinition", Expect(reg.Register(webhook.ConfigDefinition)).To(Succeed()) By("requesting that the manifest be generated") - outputDir, err := ioutil.TempDir("", "webhook-integration-test") + outputDir, err := os.MkdirTemp("", "webhook-integration-test") Expect(err).NotTo(HaveOccurred()) defer os.RemoveAll(outputDir) genCtx := &genall.GenerationContext{ @@ -88,7 +87,7 @@ var _ = Describe("Webhook Generation From Parsing to CustomResourceDefinition", Expect(reg.Register(webhook.ConfigDefinition)).To(Succeed()) By("requesting that the manifest be generated") - outputDir, err := ioutil.TempDir("", "webhook-integration-test") + outputDir, err := os.MkdirTemp("", "webhook-integration-test") Expect(err).NotTo(HaveOccurred()) defer os.RemoveAll(outputDir) genCtx := &genall.GenerationContext{ @@ -119,7 +118,7 @@ var _ = Describe("Webhook Generation From Parsing to CustomResourceDefinition", Expect(reg.Register(webhook.ConfigDefinition)).To(Succeed()) By("requesting that the manifest be generated") - outputDir, err := ioutil.TempDir("", "webhook-integration-test") + outputDir, err := os.MkdirTemp("", "webhook-integration-test") Expect(err).NotTo(HaveOccurred()) defer os.RemoveAll(outputDir) genCtx := &genall.GenerationContext{ @@ -148,7 +147,7 @@ var _ = Describe("Webhook Generation From Parsing to CustomResourceDefinition", Expect(reg.Register(webhook.ConfigDefinition)).To(Succeed()) By("requesting that the manifest be generated") - outputDir, err := ioutil.TempDir("", "webhook-integration-test") + outputDir, err := os.MkdirTemp("", "webhook-integration-test") Expect(err).NotTo(HaveOccurred()) defer os.RemoveAll(outputDir) genCtx := &genall.GenerationContext{ @@ -177,7 +176,7 @@ var _ = Describe("Webhook Generation From Parsing to CustomResourceDefinition", Expect(reg.Register(webhook.ConfigDefinition)).To(Succeed()) By("requesting that the manifest be generated") - outputDir, err := ioutil.TempDir("", "webhook-integration-test") + outputDir, err := os.MkdirTemp("", "webhook-integration-test") Expect(err).NotTo(HaveOccurred()) defer os.RemoveAll(outputDir) genCtx := &genall.GenerationContext{ @@ -191,12 +190,12 @@ var _ = Describe("Webhook Generation From Parsing to CustomResourceDefinition", } By("loading the generated v1 YAML") - actualFile, err := ioutil.ReadFile(path.Join(outputDir, "manifests.yaml")) + actualFile, err := os.ReadFile(path.Join(outputDir, "manifests.yaml")) Expect(err).NotTo(HaveOccurred()) actualMutating, actualValidating := unmarshalBothV1(actualFile) By("loading the desired v1 YAML") - expectedFile, err := ioutil.ReadFile("manifests.yaml") + expectedFile, err := os.ReadFile("manifests.yaml") Expect(err).NotTo(HaveOccurred()) expectedMutating, expectedValidating := unmarshalBothV1(expectedFile) @@ -222,7 +221,7 @@ var _ = Describe("Webhook Generation From Parsing to CustomResourceDefinition", Expect(reg.Register(webhook.ConfigDefinition)).To(Succeed()) By("requesting that the manifest be generated") - outputDir, err := ioutil.TempDir("", "webhook-integration-test") + outputDir, err := os.MkdirTemp("", "webhook-integration-test") Expect(err).NotTo(HaveOccurred()) defer os.RemoveAll(outputDir) @@ -238,13 +237,13 @@ var _ = Describe("Webhook Generation From Parsing to CustomResourceDefinition", } By("loading the generated v1 YAML") - actualFile, err := ioutil.ReadFile(path.Join(outputDir, "manifests.yaml")) + actualFile, err := os.ReadFile(path.Join(outputDir, "manifests.yaml")) Expect(err).NotTo(HaveOccurred()) actualManifest := &admissionregv1.ValidatingWebhookConfiguration{} Expect(yaml.UnmarshalStrict(actualFile, actualManifest)).To(Succeed()) By("loading the desired v1 YAML") - expectedFile, err := ioutil.ReadFile("manifests.yaml") + expectedFile, err := os.ReadFile("manifests.yaml") Expect(err).NotTo(HaveOccurred()) expectedManifest := &admissionregv1.ValidatingWebhookConfiguration{} Expect(yaml.UnmarshalStrict(expectedFile, expectedManifest)).To(Succeed()) @@ -271,7 +270,7 @@ var _ = Describe("Webhook Generation From Parsing to CustomResourceDefinition", Expect(reg.Register(webhook.ConfigDefinition)).To(Succeed()) By("requesting that the manifest be generated") - outputDir, err := ioutil.TempDir("", "webhook-integration-test") + outputDir, err := os.MkdirTemp("", "webhook-integration-test") Expect(err).NotTo(HaveOccurred()) defer os.RemoveAll(outputDir) genCtx := &genall.GenerationContext{ @@ -285,12 +284,12 @@ var _ = Describe("Webhook Generation From Parsing to CustomResourceDefinition", } By("loading the generated v1 YAML") - actualFile, err := ioutil.ReadFile(path.Join(outputDir, "manifests.yaml")) + actualFile, err := os.ReadFile(path.Join(outputDir, "manifests.yaml")) Expect(err).NotTo(HaveOccurred()) actualMutating, actualValidating := unmarshalBothV1(actualFile) By("loading the desired v1 YAML") - expectedFile, err := ioutil.ReadFile("manifests.yaml") + expectedFile, err := os.ReadFile("manifests.yaml") Expect(err).NotTo(HaveOccurred()) expectedMutating, expectedValidating := unmarshalBothV1(expectedFile) @@ -316,7 +315,7 @@ var _ = Describe("Webhook Generation From Parsing to CustomResourceDefinition", Expect(reg.Register(webhook.ConfigDefinition)).To(Succeed()) By("requesting that the manifest be generated") - outputDir, err := ioutil.TempDir("", "webhook-integration-test") + outputDir, err := os.MkdirTemp("", "webhook-integration-test") Expect(err).NotTo(HaveOccurred()) defer os.RemoveAll(outputDir) genCtx := &genall.GenerationContext{