Skip to content

Commit

Permalink
replace deprecated io/ioutil with os and io for cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
ahrtr committed Feb 1, 2022
1 parent 6dd234d commit 972dc46
Show file tree
Hide file tree
Showing 73 changed files with 187 additions and 239 deletions.
4 changes: 2 additions & 2 deletions cmd/clicheck/check_cli_conventions.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package main

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

"k8s.io/cli-runtime/pkg/genericclioptions"
Expand All @@ -29,7 +29,7 @@ import (
func main() {
var errorCount int

kubectl := cmd.NewKubectlCommand(cmd.KubectlOptions{IOStreams: genericclioptions.IOStreams{In: os.Stdin, Out: ioutil.Discard, ErrOut: ioutil.Discard}})
kubectl := cmd.NewKubectlCommand(cmd.KubectlOptions{IOStreams: genericclioptions.IOStreams{In: os.Stdin, Out: io.Discard, ErrOut: io.Discard}})
errors := cmdsanity.RunCmdChecks(kubectl, cmdsanity.AllCmdChecks, []string{})
for _, err := range errors {
errorCount++
Expand Down
4 changes: 2 additions & 2 deletions cmd/dependencycheck/dependencycheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"regexp"
)

Expand Down Expand Up @@ -65,7 +65,7 @@ func main() {
log.Fatalf("Error compiling excluded package regex: %v", err)
}
}
b, err := ioutil.ReadFile(args[0])
b, err := os.ReadFile(args[0])
if err != nil {
log.Fatalf("Error reading dependencies file: %v", err)
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/dependencyverifier/dependencyverifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -81,7 +80,7 @@ func runCommand(cmd ...string) (string, error) {
}

func readFile(path string) (string, error) {
content, err := ioutil.ReadFile(path)
content, err := os.ReadFile(path)
// Convert []byte to string and print to screen
return string(content), err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/gendocs/gen_kubectl_docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package main
import (
"bytes"
"fmt"
"io/ioutil"
"io"
"os"

"github.com/spf13/cobra/doc"
Expand Down Expand Up @@ -47,6 +47,6 @@ func main() {
// Set environment variables used by kubectl so the output is consistent,
// regardless of where we run.
os.Setenv("HOME", "/home/username")
kubectl := cmd.NewKubectlCommand(cmd.KubectlOptions{IOStreams: genericclioptions.IOStreams{In: bytes.NewReader(nil), Out: ioutil.Discard, ErrOut: ioutil.Discard}})
kubectl := cmd.NewKubectlCommand(cmd.KubectlOptions{IOStreams: genericclioptions.IOStreams{In: bytes.NewReader(nil), Out: io.Discard, ErrOut: io.Discard}})
doc.GenMarkdownTree(kubectl, outDir)
}
4 changes: 2 additions & 2 deletions cmd/genkubedocs/gen_kube_docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package main
import (
"bytes"
"fmt"
"io/ioutil"
"io"
"os"

"github.com/spf13/cobra/doc"
Expand Down Expand Up @@ -79,7 +79,7 @@ func main() {
pflag.CommandLine = pflag.NewFlagSet(os.Args[0], pflag.ExitOnError)

// generate docs for kubeadm
kubeadm := kubeadmapp.NewKubeadmCommand(bytes.NewReader(nil), ioutil.Discard, ioutil.Discard)
kubeadm := kubeadmapp.NewKubeadmCommand(bytes.NewReader(nil), io.Discard, io.Discard)
doc.GenMarkdownTree(kubeadm, outDir)

// cleanup generated code for usage as include in the website
Expand Down
6 changes: 3 additions & 3 deletions cmd/genkubedocs/postprocessing.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package main

import (
"io/ioutil"
"os"
"path/filepath"
"strings"

Expand All @@ -38,14 +38,14 @@ func MarkdownPostProcessing(cmd *cobra.Command, dir string, processor func(strin
basename := strings.Replace(cmd.CommandPath(), " ", "_", -1) + ".md"
filename := filepath.Join(dir, basename)

markdownBytes, err := ioutil.ReadFile(filename)
markdownBytes, err := os.ReadFile(filename)
if err != nil {
return err
}

processedMarkDown := processor(string(markdownBytes))

return ioutil.WriteFile(filename, []byte(processedMarkDown), 0644)
return os.WriteFile(filename, []byte(processedMarkDown), 0644)
}

// cleanupForInclude parts of markdown that will make difficult to use it as include in the website:
Expand Down
6 changes: 3 additions & 3 deletions cmd/genman/gen_kube_man.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package main
import (
"bytes"
"fmt"
"io/ioutil"
"io"
"os"
"strings"

Expand Down Expand Up @@ -97,14 +97,14 @@ func main() {
}
case "kubectl":
// generate manpage for kubectl
kubectl := kubectlcmd.NewKubectlCommand(kubectlcmd.KubectlOptions{IOStreams: genericclioptions.IOStreams{In: bytes.NewReader(nil), Out: ioutil.Discard, ErrOut: ioutil.Discard}})
kubectl := kubectlcmd.NewKubectlCommand(kubectlcmd.KubectlOptions{IOStreams: genericclioptions.IOStreams{In: bytes.NewReader(nil), Out: io.Discard, ErrOut: io.Discard}})
genMarkdown(kubectl, "", outDir)
for _, c := range kubectl.Commands() {
genMarkdown(c, "kubectl", outDir)
}
case "kubeadm":
// generate manpage for kubeadm
kubeadm := kubeadmapp.NewKubeadmCommand(bytes.NewReader(nil), ioutil.Discard, ioutil.Discard)
kubeadm := kubeadmapp.NewKubeadmCommand(bytes.NewReader(nil), io.Discard, io.Discard)
genMarkdown(kubeadm, "", outDir)
for _, c := range kubeadm.Commands() {
genMarkdown(c, "kubeadm", outDir)
Expand Down
4 changes: 2 additions & 2 deletions cmd/genyaml/gen_kubectl_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package main
import (
"bytes"
"fmt"
"io/ioutil"
"io"
"os"
"strings"

Expand Down Expand Up @@ -66,7 +66,7 @@ func main() {
// Set environment variables used by kubectl so the output is consistent,
// regardless of where we run.
os.Setenv("HOME", "/home/username")
kubectl := cmd.NewKubectlCommand(cmd.KubectlOptions{IOStreams: genericclioptions.IOStreams{In: bytes.NewReader(nil), Out: ioutil.Discard, ErrOut: ioutil.Discard}})
kubectl := cmd.NewKubectlCommand(cmd.KubectlOptions{IOStreams: genericclioptions.IOStreams{In: bytes.NewReader(nil), Out: io.Discard, ErrOut: io.Discard}})
genYaml(kubectl, "", outDir)
for _, c := range kubectl.Commands() {
genYaml(c, "kubectl", outDir)
Expand Down
3 changes: 1 addition & 2 deletions cmd/importverifier/importverifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -216,7 +215,7 @@ func main() {
}

func loadImportRestrictions(configFile string) ([]ImportRestriction, error) {
config, err := ioutil.ReadFile(configFile)
config, err := os.ReadFile(configFile)
if err != nil {
return nil, fmt.Errorf("failed to load configuration from %s: %v", configFile, err)
}
Expand Down
11 changes: 5 additions & 6 deletions cmd/kube-apiserver/app/testing/testserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package testing
import (
"context"
"fmt"
"io/ioutil"
"net"
"os"
"path"
Expand Down Expand Up @@ -127,7 +126,7 @@ func StartTestServer(t Logger, instanceOptions *TestServerInstanceOptions, custo
}
}()

result.TmpDir, err = ioutil.TempDir("", "kubernetes-kube-apiserver")
result.TmpDir, err = os.MkdirTemp("", "kubernetes-kube-apiserver")
if err != nil {
return result, fmt.Errorf("failed to create temp dir: %v", err)
}
Expand Down Expand Up @@ -156,7 +155,7 @@ func StartTestServer(t Logger, instanceOptions *TestServerInstanceOptions, custo
return result, err
}
proxyCACertFile := path.Join(s.SecureServing.ServerCert.CertDirectory, "proxy-ca.crt")
if err := ioutil.WriteFile(proxyCACertFile, testutil.EncodeCertPEM(proxySigningCert), 0644); err != nil {
if err := os.WriteFile(proxyCACertFile, testutil.EncodeCertPEM(proxySigningCert), 0644); err != nil {
return result, err
}
s.Authentication.RequestHeader.ClientCAFile = proxyCACertFile
Expand All @@ -169,7 +168,7 @@ func StartTestServer(t Logger, instanceOptions *TestServerInstanceOptions, custo
return result, err
}
clientCACertFile := path.Join(s.SecureServing.ServerCert.CertDirectory, "client-ca.crt")
if err := ioutil.WriteFile(clientCACertFile, testutil.EncodeCertPEM(clientSigningCert), 0644); err != nil {
if err := os.WriteFile(clientCACertFile, testutil.EncodeCertPEM(clientSigningCert), 0644); err != nil {
return result, err
}
s.Authentication.ClientCert.ClientCA = clientCACertFile
Expand All @@ -191,12 +190,12 @@ func StartTestServer(t Logger, instanceOptions *TestServerInstanceOptions, custo
return result, err
}

saSigningKeyFile, err := ioutil.TempFile("/tmp", "insecure_test_key")
saSigningKeyFile, err := os.CreateTemp("/tmp", "insecure_test_key")
if err != nil {
t.Fatalf("create temp file failed: %v", err)
}
defer os.RemoveAll(saSigningKeyFile.Name())
if err = ioutil.WriteFile(saSigningKeyFile.Name(), []byte(ecdsaPrivateKey), 0666); err != nil {
if err = os.WriteFile(saSigningKeyFile.Name(), []byte(ecdsaPrivateKey), 0666); err != nil {
t.Fatalf("write file %s failed: %v", saSigningKeyFile.Name(), err)
}
s.ServiceAccountSigningKeyFile = saSigningKeyFile.Name()
Expand Down
3 changes: 1 addition & 2 deletions cmd/kube-controller-manager/app/controllermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ package app
import (
"context"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"os"
Expand Down Expand Up @@ -646,7 +645,7 @@ func (c serviceAccountTokenControllerStarter) startServiceAccountTokenController
}

func readCA(file string) ([]byte, error) {
rootCA, err := ioutil.ReadFile(file)
rootCA, err := os.ReadFile(file)
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/kube-controller-manager/app/testing/testserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package testing
import (
"context"
"fmt"
"io/ioutil"
"net"
"os"
"time"
Expand Down Expand Up @@ -73,7 +72,7 @@ func StartTestServer(t Logger, customFlags []string) (result TestServer, err err
}
}()

result.TmpDir, err = ioutil.TempDir("", "kube-controller-manager")
result.TmpDir, err = os.MkdirTemp("", "kube-controller-manager")
if err != nil {
return result, fmt.Errorf("failed to create temp dir: %v", err)
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/kube-proxy/app/conntrack.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package app

import (
"errors"
"io/ioutil"
"os"
"strconv"
"strings"

Expand Down Expand Up @@ -132,13 +132,13 @@ func isSysFSWritable() (bool, error) {
}

func readIntStringFile(filename string) (int, error) {
b, err := ioutil.ReadFile(filename)
b, err := os.ReadFile(filename)
if err != nil {
return -1, err
}
return strconv.Atoi(strings.TrimSpace(string(b)))
}

func writeIntStringFile(filename string, value int) error {
return ioutil.WriteFile(filename, []byte(strconv.Itoa(value)), 0640)
return os.WriteFile(filename, []byte(strconv.Itoa(value)), 0640)
}
3 changes: 1 addition & 2 deletions cmd/kube-proxy/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"errors"
goflag "flag"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
Expand Down Expand Up @@ -403,7 +402,7 @@ func newLenientSchemeAndCodecs() (*runtime.Scheme, *serializer.CodecFactory, err
// loadConfigFromFile loads the contents of file and decodes it as a
// KubeProxyConfiguration object.
func (o *Options) loadConfigFromFile(file string) (*kubeproxyconfig.KubeProxyConfiguration, error) {
data, err := ioutil.ReadFile(file)
data, err := os.ReadFile(file)
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/kube-proxy/app/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package app
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -406,7 +405,7 @@ func TestProcessHostnameOverrideFlag(t *testing.T) {

func TestConfigChange(t *testing.T) {
setUp := func() (*os.File, string, error) {
tempDir, err := ioutil.TempDir("", "kubeproxy-config-change")
tempDir, err := os.MkdirTemp("", "kubeproxy-config-change")
if err != nil {
return nil, "", fmt.Errorf("unable to create temporary directory: %v", err)
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/kube-scheduler/app/options/configfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"

"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -32,7 +31,7 @@ import (
)

func loadConfigFromFile(file string) (*config.KubeSchedulerConfiguration, error) {
data, err := ioutil.ReadFile(file)
data, err := os.ReadFile(file)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 972dc46

Please sign in to comment.