Skip to content

Commit

Permalink
refactor: move from io/ioutil to io and os packages
Browse files Browse the repository at this point in the history
The io/ioutil package has been deprecated as of Go 1.16, see
https://golang.org/doc/go1.16#ioutil. This commit replaces the existing
io/ioutil functions with their new definitions in io and os packages.

Signed-off-by: Eng Zer Jun <[email protected]>
  • Loading branch information
Juneezee committed Nov 12, 2021
1 parent 7fe3d21 commit 425173a
Show file tree
Hide file tree
Showing 67 changed files with 138 additions and 173 deletions.
5 changes: 2 additions & 3 deletions channels/pkg/channels/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package channels

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
Expand All @@ -31,7 +30,7 @@ import (
// We will likely in future change this to create things directly (or more likely embed this logic into kubectl itself)
func Apply(data []byte) error {
// We copy the manifest to a temp file because it is likely e.g. an s3 URL, which kubectl can't read
tmpDir, err := ioutil.TempDir("", "channel")
tmpDir, err := os.MkdirTemp("", "channel")
if err != nil {
return fmt.Errorf("error creating temp dir: %v", err)
}
Expand All @@ -43,7 +42,7 @@ func Apply(data []byte) error {
}()

localManifestFile := path.Join(tmpDir, "manifest.yaml")
if err := ioutil.WriteFile(localManifestFile, data, 0600); err != nil {
if err := os.WriteFile(localManifestFile, data, 0600); err != nil {
return fmt.Errorf("error writing temp file: %v", err)
}

Expand Down
3 changes: 1 addition & 2 deletions cmd/kops-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"os"

coordinationv1 "k8s.io/api/coordination/v1"
Expand Down Expand Up @@ -76,7 +75,7 @@ func main() {
opt.PopulateDefaults()

{
b, err := ioutil.ReadFile(configPath)
b, err := os.ReadFile(configPath)
if err != nil {
klog.Fatalf("failed to read configuration file %q: %v", configPath, err)
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/kops-controller/pkg/server/keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package server

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

"k8s.io/kops/pkg/pki"
Expand Down Expand Up @@ -49,7 +49,7 @@ func newKeystore(basePath string, cas []string) (pki.Keystore, map[string]string
keys: map[string]keystoreEntry{},
}
for _, name := range cas {
certBytes, err := ioutil.ReadFile(path.Join(basePath, name+".crt"))
certBytes, err := os.ReadFile(path.Join(basePath, name+".crt"))
if err != nil {
return nil, nil, fmt.Errorf("reading %q certificate: %v", name, err)
}
Expand All @@ -58,7 +58,7 @@ func newKeystore(basePath string, cas []string) (pki.Keystore, map[string]string
return nil, nil, fmt.Errorf("parsing %q certificate: %v", name, err)
}

keyBytes, err := ioutil.ReadFile(path.Join(basePath, name+".key"))
keyBytes, err := os.ReadFile(path.Join(basePath, name+".key"))
if err != nil {
return nil, nil, fmt.Errorf("reading %q key: %v", name, err)
}
Expand All @@ -74,7 +74,7 @@ func newKeystore(basePath string, cas []string) (pki.Keystore, map[string]string
}

var keypairIDs map[string]string
keypairIDsBytes, err := ioutil.ReadFile(path.Join(basePath, "keypair-ids.yaml"))
keypairIDsBytes, err := os.ReadFile(path.Join(basePath, "keypair-ids.yaml"))
if err != nil {
return nil, nil, fmt.Errorf("reading keypair-ids.yaml")
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/kops-controller/pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"encoding/pem"
"fmt"
"hash/fnv"
"io/ioutil"
"io"
"net/http"
"runtime/debug"
"time"
Expand Down Expand Up @@ -98,7 +98,7 @@ func (s *Server) bootstrap(w http.ResponseWriter, r *http.Request) {
return
}

body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
klog.Infof("bootstrap %s read err: %v", r.RemoteAddr, err)
w.WriteHeader(http.StatusBadRequest)
Expand Down
3 changes: 1 addition & 2 deletions cmd/kops/create_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"encoding/csv"
"fmt"
"io"
"io/ioutil"
"os"
"sort"
"strings"
Expand Down Expand Up @@ -830,7 +829,7 @@ func loadSSHPublicKeys(sshPublicKey string) (map[string][]byte, error) {
sshPublicKeys := make(map[string][]byte)
if sshPublicKey != "" {
sshPublicKey = utils.ExpandPath(sshPublicKey)
authorized, err := ioutil.ReadFile(sshPublicKey)
authorized, err := os.ReadFile(sshPublicKey)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/kops/create_cluster_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package main
import (
"bytes"
"context"
"io/ioutil"
"os"
"path"
"strings"
"testing"
Expand Down Expand Up @@ -179,7 +179,7 @@ func runCreateClusterIntegrationTest(t *testing.T, srcDir string, version string
factory := util.NewFactory(factoryOptions)

{
optionsBytes, err := ioutil.ReadFile(path.Join(srcDir, optionsYAML))
optionsBytes, err := os.ReadFile(path.Join(srcDir, optionsYAML))
if err != nil {
t.Fatalf("error reading options file: %v", err)
}
Expand All @@ -197,7 +197,7 @@ func runCreateClusterIntegrationTest(t *testing.T, srcDir string, version string

// Use the public key we produced
{
publicKey, err := ioutil.ReadFile(publicKeyPath)
publicKey, err := os.ReadFile(publicKeyPath)
if err != nil {
t.Fatalf("error reading public key %q: %v", publicKeyPath, err)
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/kops/create_keypair.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"crypto/x509/pkix"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"time"
Expand Down Expand Up @@ -189,7 +188,7 @@ func createKeypair(out io.Writer, options *CreateKeypairOptions, name string, ke
var privateKey *pki.PrivateKey
if options.PrivateKeyPath != "" {
options.PrivateKeyPath = utils.ExpandPath(options.PrivateKeyPath)
privateKeyBytes, err := ioutil.ReadFile(options.PrivateKeyPath)
privateKeyBytes, err := os.ReadFile(options.PrivateKeyPath)
if err != nil {
return fmt.Errorf("error reading user provided private key %q: %v", options.PrivateKeyPath, err)
}
Expand Down Expand Up @@ -222,7 +221,7 @@ func createKeypair(out io.Writer, options *CreateKeypairOptions, name string, ke
}
} else {
options.CertPath = utils.ExpandPath(options.CertPath)
certBytes, err := ioutil.ReadFile(options.CertPath)
certBytes, err := os.ReadFile(options.CertPath)
if err != nil {
return fmt.Errorf("error reading user provided cert %q: %v", options.CertPath, err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/kops/create_secret_ciliumpassword.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"os"

"github.com/spf13/cobra"
"k8s.io/kops/cmd/kops/util"
Expand Down Expand Up @@ -108,7 +108,7 @@ func RunCreateSecretCiliumEncryptionConfig(ctx context.Context, f *util.Factory,
return fmt.Errorf("reading Cilium IPSec config from stdin: %v", err)
}
} else {
data, err = ioutil.ReadFile(options.CiliumPasswordFilePath)
data, err = os.ReadFile(options.CiliumPasswordFilePath)
if err != nil {
return fmt.Errorf("reading Cilium IPSec config %v: %v", options.CiliumPasswordFilePath, err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/kops/create_secret_dockerconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"

"github.com/spf13/cobra"
"k8s.io/kops/cmd/kops/util"
Expand Down Expand Up @@ -113,7 +113,7 @@ func RunCreateSecretDockerConfig(ctx context.Context, f *util.Factory, out io.Wr
return fmt.Errorf("reading Docker config from stdin: %v", err)
}
} else {
data, err = ioutil.ReadFile(options.DockerConfigPath)
data, err = os.ReadFile(options.DockerConfigPath)
if err != nil {
return fmt.Errorf("reading Docker config %v: %v", options.DockerConfigPath, err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/kops/create_secret_encryptionconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"os"

"github.com/spf13/cobra"
"k8s.io/kops/cmd/kops/util"
Expand Down Expand Up @@ -106,7 +106,7 @@ func RunCreateSecretEncryptionConfig(ctx context.Context, f *util.Factory, out i
return fmt.Errorf("reading encryption config from stdin: %v", err)
}
} else {
data, err = ioutil.ReadFile(options.EncryptionConfigPath)
data, err = os.ReadFile(options.EncryptionConfigPath)
if err != nil {
return fmt.Errorf("reading encryption config %v: %v", options.EncryptionConfigPath, err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/kops/create_secret_weavepassword.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"os"

"github.com/spf13/cobra"
"k8s.io/kops/pkg/commands/commandutils"
Expand Down Expand Up @@ -117,7 +117,7 @@ func RunCreateSecretWeavePassword(ctx context.Context, f *util.Factory, out io.W
return fmt.Errorf("reading Weave password file from stdin: %v", err)
}
} else {
data, err = ioutil.ReadFile(options.WeavePasswordFilePath)
data, err = os.ReadFile(options.WeavePasswordFilePath)
if err != nil {
return fmt.Errorf("reading Weave password file %v: %v", options.WeavePasswordFilePath, err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/kops/create_sshpublickey.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"os"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -96,7 +96,7 @@ func RunCreateSSHPublicKey(ctx context.Context, f *util.Factory, out io.Writer,
return err
}

data, err := ioutil.ReadFile(options.PublicKeyPath)
data, err := os.ReadFile(options.PublicKeyPath)
if err != nil {
return fmt.Errorf("error reading SSH public key %v: %v", options.PublicKeyPath, err)
}
Expand Down
19 changes: 9 additions & 10 deletions cmd/kops/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"encoding/pem"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"reflect"
Expand Down Expand Up @@ -732,7 +731,7 @@ func (i *integrationTest) runTest(t *testing.T, h *testutils.IntegrationTestHarn

// Compare main files
{
files, err := ioutil.ReadDir(path.Join(h.TempDir, "out"))
files, err := os.ReadDir(path.Join(h.TempDir, "out"))
if err != nil {
t.Fatalf("failed to read dir: %v", err)
}
Expand All @@ -754,7 +753,7 @@ func (i *integrationTest) runTest(t *testing.T, h *testutils.IntegrationTestHarn
t.Fatalf("unexpected files. actual=%q, expected=%q, test=%q", actualFilenames, expectedFilenames, testDataTFPath)
}

actualTF, err := ioutil.ReadFile(path.Join(h.TempDir, "out", actualTFPath))
actualTF, err := os.ReadFile(path.Join(h.TempDir, "out", actualTFPath))
if err != nil {
t.Fatalf("unexpected error reading actual terraform output: %v", err)
}
Expand All @@ -765,7 +764,7 @@ func (i *integrationTest) runTest(t *testing.T, h *testutils.IntegrationTestHarn
// Compare data files if they are provided
if len(expectedDataFilenames) > 0 {
actualDataPath := path.Join(h.TempDir, "out", "data")
files, err := ioutil.ReadDir(actualDataPath)
files, err := os.ReadDir(actualDataPath)
if err != nil {
t.Fatalf("failed to read data dir: %v", err)
}
Expand Down Expand Up @@ -797,15 +796,15 @@ func (i *integrationTest) runTest(t *testing.T, h *testutils.IntegrationTestHarn
{
for _, dataFileName := range expectedDataFilenames {
actualDataContent, err :=
ioutil.ReadFile(path.Join(actualDataPath, dataFileName))
os.ReadFile(path.Join(actualDataPath, dataFileName))
if err != nil {
t.Fatalf("failed to read actual data file: %v", err)
}
golden.AssertMatchesFile(t, string(actualDataContent), path.Join(expectedDataPath, dataFileName))
}
}

existingExpectedFiles, err := ioutil.ReadDir(expectedDataPath)
existingExpectedFiles, err := os.ReadDir(expectedDataPath)
if err != nil {
t.Fatalf("failed to read data dir: %v", err)
}
Expand Down Expand Up @@ -1202,7 +1201,7 @@ func (i *integrationTest) runTestCloudformation(t *testing.T) {

// Compare main files
{
files, err := ioutil.ReadDir(path.Join(h.TempDir, "out"))
files, err := os.ReadDir(path.Join(h.TempDir, "out"))
if err != nil {
t.Fatalf("failed to read dir: %v", err)
}
Expand All @@ -1220,7 +1219,7 @@ func (i *integrationTest) runTestCloudformation(t *testing.T) {
}

actualPath := path.Join(h.TempDir, "out", "kubernetes.json")
actualCF, err := ioutil.ReadFile(actualPath)
actualCF, err := os.ReadFile(actualPath)
if err != nil {
t.Fatalf("unexpected error reading actual cloudformation output: %v", err)
}
Expand Down Expand Up @@ -1295,7 +1294,7 @@ func MakeSSHKeyPair(publicKeyPath string, privateKeyPath string) error {
if err := pem.Encode(&privateKeyBytes, privateKeyPEM); err != nil {
return err
}
if err := ioutil.WriteFile(privateKeyPath, privateKeyBytes.Bytes(), os.FileMode(0700)); err != nil {
if err := os.WriteFile(privateKeyPath, privateKeyBytes.Bytes(), os.FileMode(0700)); err != nil {
return err
}

Expand All @@ -1304,7 +1303,7 @@ func MakeSSHKeyPair(publicKeyPath string, privateKeyPath string) error {
return err
}
publicKeyBytes := ssh.MarshalAuthorizedKey(publicKey)
if err := ioutil.WriteFile(publicKeyPath, publicKeyBytes, os.FileMode(0744)); err != nil {
if err := os.WriteFile(publicKeyPath, publicKeyBytes, os.FileMode(0744)); err != nil {
return err
}

Expand Down
3 changes: 1 addition & 2 deletions cmd/kops/toolbox_dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -138,7 +137,7 @@ func RunToolboxDump(ctx context.Context, f *util.Factory, out io.Writer, options
if strings.HasPrefix(privateKeyPath, "~/") {
privateKeyPath = filepath.Join(os.Getenv("HOME"), privateKeyPath[2:])
}
key, err := ioutil.ReadFile(privateKeyPath)
key, err := os.ReadFile(privateKeyPath)
if err != nil {
return fmt.Errorf("error reading private key %q: %v", privateKeyPath, err)
}
Expand Down
7 changes: 3 additions & 4 deletions cmd/kops/toolbox_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -167,7 +166,7 @@ func RunToolBoxTemplate(f *util.Factory, out io.Writer, options *ToolboxTemplate
}

for _, j := range list {
content, err := ioutil.ReadFile(j)
content, err := os.ReadFile(j)
if err != nil {
return fmt.Errorf("unable to read snippet: %s, error: %s", j, err)
}
Expand All @@ -184,7 +183,7 @@ func RunToolBoxTemplate(f *util.Factory, out io.Writer, options *ToolboxTemplate
r := templater.NewTemplater(channel)
var documents []string
for _, x := range templates {
content, err := ioutil.ReadFile(x)
content, err := os.ReadFile(x)
if err != nil {
return fmt.Errorf("unable to read template: %s, error: %s", x, err)
}
Expand Down Expand Up @@ -249,7 +248,7 @@ func newTemplateContext(files []string, values []string, stringValues []string)
return nil, err
}
for _, j := range list {
content, err := ioutil.ReadFile(j)
content, err := os.ReadFile(j)
if err != nil {
return nil, fmt.Errorf("unable to configuration file: %s, error: %s", j, err)
}
Expand Down
Loading

0 comments on commit 425173a

Please sign in to comment.