Skip to content

Commit

Permalink
Merge pull request #4399 from stmcginnis/ioutil
Browse files Browse the repository at this point in the history
🌱 Remove deprecated ioutil usage
  • Loading branch information
k8s-ci-robot authored Mar 31, 2021
2 parents c770422 + 5291e10 commit 7f87986
Show file tree
Hide file tree
Showing 34 changed files with 92 additions and 114 deletions.
21 changes: 10 additions & 11 deletions cmd/clusterctl/client/cluster/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package cluster

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -63,11 +62,11 @@ func TestProxyGetConfig(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
dir, err := ioutil.TempDir("", "clusterctl")
dir, err := os.MkdirTemp("", "clusterctl")
g.Expect(err).NotTo(HaveOccurred())
defer os.RemoveAll(dir)
configFile := filepath.Join(dir, ".test-kubeconfig.yaml")
g.Expect(ioutil.WriteFile(configFile, []byte(tt.kubeconfigContents), 0600)).To(Succeed())
g.Expect(os.WriteFile(configFile, []byte(tt.kubeconfigContents), 0600)).To(Succeed())

proxy := newProxy(Kubeconfig{Path: configFile, Context: tt.context})
conf, err := proxy.GetConfig()
Expand All @@ -90,11 +89,11 @@ func TestProxyGetConfig(t *testing.T) {

t.Run("configure timeout", func(t *testing.T) {
g := NewWithT(t)
dir, err := ioutil.TempDir("", "clusterctl")
dir, err := os.MkdirTemp("", "clusterctl")
g.Expect(err).NotTo(HaveOccurred())
defer os.RemoveAll(dir)
configFile := filepath.Join(dir, ".test-kubeconfig.yaml")
g.Expect(ioutil.WriteFile(configFile, []byte(kubeconfig("management", "default")), 0600)).To(Succeed())
g.Expect(os.WriteFile(configFile, []byte(kubeconfig("management", "default")), 0600)).To(Succeed())

proxy := newProxy(Kubeconfig{Path: configFile, Context: "management"}, InjectProxyTimeout(23*time.Second))
conf, err := proxy.GetConfig()
Expand All @@ -117,11 +116,11 @@ func TestKUBECONFIGEnvVar(t *testing.T) {
)

g := NewWithT(t)
dir, err := ioutil.TempDir("", "clusterctl")
dir, err := os.MkdirTemp("", "clusterctl")
g.Expect(err).NotTo(HaveOccurred())
defer os.RemoveAll(dir)
configFile := filepath.Join(dir, ".test-kubeconfig.yaml")
g.Expect(ioutil.WriteFile(configFile, []byte(kubeconfigContents), 0600)).To(Succeed())
g.Expect(os.WriteFile(configFile, []byte(kubeconfigContents), 0600)).To(Succeed())

proxy := newProxy(
// dont't give an explicit path but rather define the file in the
Expand All @@ -145,11 +144,11 @@ func TestKUBECONFIGEnvVar(t *testing.T) {
expectedHost = "https://kind-server:38790"
)
g := NewWithT(t)
dir, err := ioutil.TempDir("", "clusterctl")
dir, err := os.MkdirTemp("", "clusterctl")
g.Expect(err).NotTo(HaveOccurred())
defer os.RemoveAll(dir)
configFile := filepath.Join(dir, ".test-kubeconfig.yaml")
g.Expect(ioutil.WriteFile(configFile, []byte(kubeconfigContents), 0600)).To(Succeed())
g.Expect(os.WriteFile(configFile, []byte(kubeconfigContents), 0600)).To(Succeed())

proxy := newProxy(
// dont't give an explicit path but rather define the file in the
Expand Down Expand Up @@ -222,11 +221,11 @@ func TestProxyCurrentNamespace(t *testing.T) {
if len(tt.kubeconfigPath) != 0 {
configFile = tt.kubeconfigPath
} else {
dir, err := ioutil.TempDir("", "clusterctl")
dir, err := os.MkdirTemp("", "clusterctl")
g.Expect(err).NotTo(HaveOccurred())
defer os.RemoveAll(dir)
configFile = filepath.Join(dir, ".test-kubeconfig.yaml")
g.Expect(ioutil.WriteFile(configFile, []byte(tt.kubeconfigContents), 0600)).To(Succeed())
g.Expect(os.WriteFile(configFile, []byte(tt.kubeconfigContents), 0600)).To(Succeed())
}

proxy := newProxy(Kubeconfig{Path: configFile, Context: tt.kubeconfigContext})
Expand Down
3 changes: 1 addition & 2 deletions cmd/clusterctl/client/cluster/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package cluster
import (
"context"
"encoding/base64"
"io/ioutil"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -152,7 +151,7 @@ func (t *templateClient) getLocalFileContent(rURL *url.URL) ([]byte, error) {
if f.IsDir() {
return nil, errors.Errorf("invalid path: file %q is actually a directory", rURL.Path)
}
content, err := ioutil.ReadFile(rURL.Path)
content, err := os.ReadFile(rURL.Path)
if err != nil {
return nil, errors.Wrapf(err, "failed to read file %q", rURL.Path)
}
Expand Down
9 changes: 4 additions & 5 deletions cmd/clusterctl/client/cluster/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package cluster
import (
"encoding/base64"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -229,12 +228,12 @@ func Test_templateClient_getGitHubFileContent(t *testing.T) {
func Test_templateClient_getLocalFileContent(t *testing.T) {
g := NewWithT(t)

tmpDir, err := ioutil.TempDir("", "cc")
tmpDir, err := os.MkdirTemp("", "cc")
g.Expect(err).NotTo(HaveOccurred())
defer os.RemoveAll(tmpDir)

path := filepath.Join(tmpDir, "cluster-template.yaml")
g.Expect(ioutil.WriteFile(path, []byte(template), 0600)).To(Succeed())
g.Expect(os.WriteFile(path, []byte(template), 0600)).To(Succeed())

type args struct {
rURL *url.URL
Expand Down Expand Up @@ -283,7 +282,7 @@ func Test_templateClient_getLocalFileContent(t *testing.T) {
func Test_templateClient_GetFromURL(t *testing.T) {
g := NewWithT(t)

tmpDir, err := ioutil.TempDir("", "cc")
tmpDir, err := os.MkdirTemp("", "cc")
g.Expect(err).NotTo(HaveOccurred())
defer os.RemoveAll(tmpDir)

Expand All @@ -306,7 +305,7 @@ func Test_templateClient_GetFromURL(t *testing.T) {
})

path := filepath.Join(tmpDir, "cluster-template.yaml")
g.Expect(ioutil.WriteFile(path, []byte(template), 0600)).To(Succeed())
g.Expect(os.WriteFile(path, []byte(template), 0600)).To(Succeed())

type args struct {
templateURL string
Expand Down
3 changes: 1 addition & 2 deletions cmd/clusterctl/client/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package client

import (
"io"
"io/ioutil"
"strconv"

"k8s.io/utils/pointer"
Expand Down Expand Up @@ -82,7 +81,7 @@ func (c *clusterctlClient) ProcessYAML(options ProcessYAMLOptions) (YamlPrinter,
if options.ReaderSource != nil {
// NOTE: Beware of potentially reading in large files all at once
// since this is inefficient and increases memory utilziation.
content, err := ioutil.ReadAll(options.ReaderSource.Reader)
content, err := io.ReadAll(options.ReaderSource.Reader)
if err != nil {
return nil, err
}
Expand Down
23 changes: 11 additions & 12 deletions cmd/clusterctl/client/config/reader_viper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package config

import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
Expand All @@ -34,19 +33,19 @@ func Test_viperReader_Init(t *testing.T) {

// Change HOME dir and do not specify config file
// (.cluster-api/clusterctl) in it.
clusterctlHomeDir, err := ioutil.TempDir("", "clusterctl-default")
clusterctlHomeDir, err := os.MkdirTemp("", "clusterctl-default")
g.Expect(err).NotTo(HaveOccurred())
defer os.RemoveAll(clusterctlHomeDir)

dir, err := ioutil.TempDir("", "clusterctl")
dir, err := os.MkdirTemp("", "clusterctl")
g.Expect(err).NotTo(HaveOccurred())
defer os.RemoveAll(dir)

configFile := filepath.Join(dir, "clusterctl.yaml")
g.Expect(ioutil.WriteFile(configFile, []byte("bar: bar"), 0600)).To(Succeed())
g.Expect(os.WriteFile(configFile, []byte("bar: bar"), 0600)).To(Succeed())

configFileBadContents := filepath.Join(dir, "clusterctl-bad.yaml")
g.Expect(ioutil.WriteFile(configFileBadContents, []byte("bad-contents"), 0600)).To(Succeed())
g.Expect(os.WriteFile(configFileBadContents, []byte("bad-contents"), 0600)).To(Succeed())

// To test the remote config file
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -122,14 +121,14 @@ func Test_viperReader_Init(t *testing.T) {
func Test_viperReader_Get(t *testing.T) {
g := NewWithT(t)

dir, err := ioutil.TempDir("", "clusterctl")
dir, err := os.MkdirTemp("", "clusterctl")
g.Expect(err).NotTo(HaveOccurred())
defer os.RemoveAll(dir)

os.Setenv("FOO", "foo")

configFile := filepath.Join(dir, "clusterctl.yaml")
g.Expect(ioutil.WriteFile(configFile, []byte("bar: bar"), 0600)).To(Succeed())
g.Expect(os.WriteFile(configFile, []byte("bar: bar"), 0600)).To(Succeed())

type args struct {
key string
Expand Down Expand Up @@ -187,7 +186,7 @@ func Test_viperReader_Get(t *testing.T) {

func Test_viperReader_GetWithoutDefaultConfig(t *testing.T) {
g := NewWithT(t)
dir, err := ioutil.TempDir("", "clusterctl")
dir, err := os.MkdirTemp("", "clusterctl")
g.Expect(err).NotTo(HaveOccurred())
defer os.RemoveAll(dir)

Expand All @@ -204,15 +203,15 @@ func Test_viperReader_GetWithoutDefaultConfig(t *testing.T) {
func Test_viperReader_Set(t *testing.T) {
g := NewWithT(t)

dir, err := ioutil.TempDir("", "clusterctl")
dir, err := os.MkdirTemp("", "clusterctl")
g.Expect(err).NotTo(HaveOccurred())
defer os.RemoveAll(dir)

os.Setenv("FOO", "foo")

configFile := filepath.Join(dir, "clusterctl.yaml")

g.Expect(ioutil.WriteFile(configFile, []byte("bar: bar"), 0600)).To(Succeed())
g.Expect(os.WriteFile(configFile, []byte("bar: bar"), 0600)).To(Succeed())

type args struct {
key string
Expand Down Expand Up @@ -251,13 +250,13 @@ func Test_viperReader_Set(t *testing.T) {

func Test_viperReader_checkDefaultConfig(t *testing.T) {
g := NewWithT(t)
dir, err := ioutil.TempDir("", "clusterctl")
dir, err := os.MkdirTemp("", "clusterctl")
g.Expect(err).NotTo(HaveOccurred())
defer os.RemoveAll(dir)
dir = strings.TrimSuffix(dir, "/")

configFile := filepath.Join(dir, "clusterctl.yaml")
g.Expect(ioutil.WriteFile(configFile, []byte("bar: bar"), 0600)).To(Succeed())
g.Expect(os.WriteFile(configFile, []byte("bar: bar"), 0600)).To(Succeed())

type fields struct {
configPaths []string
Expand Down
9 changes: 4 additions & 5 deletions cmd/clusterctl/client/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package client

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -438,12 +437,12 @@ func Test_clusterctlClient_GetClusterTemplate(t *testing.T) {
rawTemplate := templateYAML("ns3", "${ CLUSTER_NAME }")

// Template on a file
tmpDir, err := ioutil.TempDir("", "cc")
tmpDir, err := os.MkdirTemp("", "cc")
g.Expect(err).NotTo(HaveOccurred())
defer os.RemoveAll(tmpDir)

path := filepath.Join(tmpDir, "cluster-template.yaml")
g.Expect(ioutil.WriteFile(path, rawTemplate, 0600)).To(Succeed())
g.Expect(os.WriteFile(path, rawTemplate, 0600)).To(Succeed())

// Template on a repository & in a ConfigMap
configMap := &corev1.ConfigMap{
Expand Down Expand Up @@ -620,12 +619,12 @@ func Test_clusterctlClient_ProcessYAML(t *testing.T) {
template := `v1: ${VAR1:=default1}
v2: ${VAR2=default2}
v3: ${VAR3:-default3}`
dir, err := ioutil.TempDir("", "clusterctl")
dir, err := os.MkdirTemp("", "clusterctl")
g.Expect(err).NotTo(HaveOccurred())
defer os.RemoveAll(dir)

templateFile := filepath.Join(dir, "template.yaml")
g.Expect(ioutil.WriteFile(templateFile, []byte(template), 0600)).To(Succeed())
g.Expect(os.WriteFile(templateFile, []byte(template), 0600)).To(Succeed())

inputReader := strings.NewReader(template)

Expand Down
3 changes: 1 addition & 2 deletions cmd/clusterctl/client/repository/overrides.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package repository

import (
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -86,7 +85,7 @@ func getLocalOverride(info *newOverrideInput) ([]byte, error) {
// it the local override exists, use it
_, err := os.Stat(overridePath)
if err == nil {
content, err := ioutil.ReadFile(overridePath)
content, err := os.ReadFile(overridePath)
if err != nil {
return nil, errors.Wrapf(err, "failed to read local override for %s", overridePath)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/clusterctl/client/repository/repository_github.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package repository
import (
"context"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"path/filepath"
Expand Down Expand Up @@ -345,7 +345,7 @@ func (g *gitHubRepository) downloadFilesFromRelease(release *github.RepositoryRe
defer reader.Close()

// Read contents from the reader (redirect or not), and return.
content, err := ioutil.ReadAll(reader)
content, err := io.ReadAll(reader)
if err != nil {
return nil, errors.Wrapf(err, "failed to read downloaded file %q from %q release", *release.TagName, fileName)
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/clusterctl/client/repository/repository_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package repository

import (
"io/ioutil"
"net/url"
"os"
"path/filepath"
Expand Down Expand Up @@ -102,7 +101,7 @@ func (r *localRepository) GetFile(version, fileName string) ([]byte, error) {
if f.IsDir() {
return nil, errors.Errorf("invalid path: file %q is actually a directory %q", fileName, absolutePath)
}
content, err := ioutil.ReadFile(absolutePath)
content, err := os.ReadFile(absolutePath)
if err != nil {
return nil, errors.Wrapf(err, "failed to read file %q from local release %s", absolutePath, version)
}
Expand All @@ -113,7 +112,7 @@ func (r *localRepository) GetFile(version, fileName string) ([]byte, error) {
func (r *localRepository) GetVersions() ([]string, error) {
// get all the sub-directories under {basepath}/{provider-id}/
releasesPath := filepath.Join(r.basepath, r.providerLabel)
files, err := ioutil.ReadDir(releasesPath)
files, err := os.ReadDir(releasesPath)
if err != nil {
return nil, errors.Wrap(err, "failed to list release directories")
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/clusterctl/client/repository/repository_local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package repository

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -126,7 +125,7 @@ func Test_localRepository_newLocalRepository(t *testing.T) {
}

func createTempDir(t *testing.T) string {
dir, err := ioutil.TempDir("", "cc")
dir, err := os.MkdirTemp("", "cc")
if err != nil {
t.Fatalf("err: %s", err)
}
Expand All @@ -139,7 +138,7 @@ func createLocalTestProviderFile(t *testing.T, tmpDir, path, msg string) string
dst := filepath.Join(tmpDir, path)
// Create all directories in the standard layout
g.Expect(os.MkdirAll(filepath.Dir(dst), 0755)).To(Succeed())
g.Expect(ioutil.WriteFile(dst, []byte(msg), 0600)).To(Succeed())
g.Expect(os.WriteFile(dst, []byte(msg), 0600)).To(Succeed())

return dst
}
Expand Down
Loading

0 comments on commit 7f87986

Please sign in to comment.