Merge pull request #767 from testwill/ioutil

chore: remove refs to deprecated io/ioutil
This commit is contained in:
Kubernetes Prow Robot
2023-06-20 21:40:20 -07:00
committed by GitHub
15 changed files with 36 additions and 43 deletions

View File

@@ -1,3 +1,4 @@
//go:build !disable_system_log_monitor
// +build !disable_system_log_monitor
/*
@@ -21,7 +22,6 @@ package main
import (
"errors"
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
@@ -91,14 +91,14 @@ func TestNPDMain(t *testing.T) {
}
func writeTempFile(t *testing.T, ext string, contents string) (string, error) {
f, err := ioutil.TempFile("", "*."+ext)
f, err := os.CreateTemp("", "*."+ext)
if err != nil {
return "", fmt.Errorf("cannot create temp file, %v", err)
}
fileName := f.Name()
if err := ioutil.WriteFile(fileName, []byte(contents), 0644); err != nil {
if err := os.WriteFile(fileName, []byte(contents), 0644); err != nil {
os.Remove(fileName)
return "", fmt.Errorf("cannot write config to temp file %s, %v", fileName, err)
}

View File

@@ -18,7 +18,7 @@ package custompluginmonitor
import (
"encoding/json"
"io/ioutil"
"os"
"time"
"github.com/golang/glog"
@@ -57,7 +57,7 @@ func NewCustomPluginMonitorOrDie(configPath string) types.Monitor {
configPath: configPath,
tomb: tomb.NewTomb(),
}
f, err := ioutil.ReadFile(configPath)
f, err := os.ReadFile(configPath)
if err != nil {
glog.Fatalf("Failed to read configuration file %q: %v", configPath, err)
}

View File

@@ -20,7 +20,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"os/exec"
"strings"
"sync"
@@ -132,12 +131,12 @@ func (p *Plugin) runRules() {
// readFromReader reads the maxBytes from the reader and drains the rest.
func readFromReader(reader io.ReadCloser, maxBytes int64) ([]byte, error) {
limitReader := io.LimitReader(reader, maxBytes)
data, err := ioutil.ReadAll(limitReader)
data, err := io.ReadAll(limitReader)
if err != nil {
return []byte{}, err
}
// Drain the reader
if _, err := io.Copy(ioutil.Discard, reader); err != nil {
if _, err := io.Copy(io.Discard, reader); err != nil {
return []byte{}, err
}
return data, nil

View File

@@ -16,8 +16,8 @@ package problemclient
import (
"fmt"
"io/ioutil"
"net/url"
"os"
"strconv"
"k8s.io/apimachinery/pkg/runtime/schema"
@@ -137,7 +137,7 @@ func getKubeClientConfig(uri *url.URL) (*kube_rest.Config, error) {
if useServiceAccount {
// If a readable service account token exists, then use it
if contents, err := ioutil.ReadFile(defaultServiceAccountFile); err == nil {
if contents, err := os.ReadFile(defaultServiceAccountFile); err == nil {
kubeConfig.BearerToken = string(contents)
}
}

View File

@@ -18,7 +18,7 @@ package stackdriverexporter
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"time"
@@ -209,7 +209,7 @@ func NewExporterOrDie(clo types.CommandLineOptions) types.Exporter {
se := stackdriverExporter{}
// Apply configurations.
f, err := ioutil.ReadFile(options.configPath)
f, err := os.ReadFile(options.configPath)
if err != nil {
glog.Fatalf("Failed to read configuration file %q: %v", options.configPath, err)
}

View File

@@ -18,7 +18,7 @@ package systemlogmonitor
import (
"encoding/json"
"io/ioutil"
"os"
"time"
"github.com/golang/glog"
@@ -62,7 +62,7 @@ func NewLogMonitorOrDie(configPath string) types.Monitor {
tomb: tomb.NewTomb(),
}
f, err := ioutil.ReadFile(configPath)
f, err := os.ReadFile(configPath)
if err != nil {
glog.Fatalf("Failed to read configuration file %q: %v", configPath, err)
}

View File

@@ -17,7 +17,6 @@ limitations under the License.
package filelog
import (
"io/ioutil"
"os"
"testing"
"time"
@@ -139,7 +138,7 @@ Jan 2 03:04:05 kernel: [2.000000] 3
}
for c, test := range testCases {
t.Logf("TestCase #%d: %#v", c+1, test)
f, err := ioutil.TempFile("", "log_watcher_test")
f, err := os.CreateTemp("", "log_watcher_test")
assert.NoError(t, err)
defer func() {
f.Close()

View File

@@ -1,7 +1,6 @@
package systemstatsmonitor
import (
"io/ioutil"
"os"
"path"
"regexp"
@@ -48,12 +47,8 @@ func newFakeInt64Metric(metricID metrics.MetricID, viewName string, description
// testCollectAux is a test auxiliary function used for testing netCollector.Collect
func testCollectAux(t *testing.T, name string, excludeInterfaceRegexp ssmtypes.NetStatsInterfaceRegexp, validate func(*testing.T, *netCollector)) {
// mkdir /tmp/proc-X
procDir, err := ioutil.TempDir(os.TempDir(), "proc-")
if err != nil {
t.Fatalf("Failed to create temp proc directory: %v", err)
}
// rm -r /tmp/proc-X
defer os.RemoveAll(procDir)
procDir := t.TempDir()
// mkdir -C /tmp/proc-X/net
procNetDir := path.Join(procDir, "net")
if err := os.Mkdir(procNetDir, 0777); err != nil {

View File

@@ -15,7 +15,7 @@ package systemstatsmonitor
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
@@ -102,7 +102,7 @@ func (ofc *osFeatureCollector) recordFeaturesFromCmdline(cmdlineArgs []system.Cm
func (ofc *osFeatureCollector) recordFeaturesFromModules(modules []system.Module) {
// Collect known modules (default modules based on guest OS present in known-modules.json)
var knownModules []system.Module
f, err := ioutil.ReadFile(ofc.config.KnownModulesConfigPath)
f, err := os.ReadFile(ofc.config.KnownModulesConfigPath)
if err != nil {
glog.Warningf("Failed to read configuration file %s: %v",
ofc.config.KnownModulesConfigPath, err)

View File

@@ -18,7 +18,7 @@ package systemstatsmonitor
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"time"
@@ -58,7 +58,7 @@ func NewSystemStatsMonitorOrDie(configPath string) types.Monitor {
}
// Apply configurations.
f, err := ioutil.ReadFile(configPath)
f, err := os.ReadFile(configPath)
if err != nil {
glog.Fatalf("Failed to read configuration file %q: %v", configPath, err)
}

View File

@@ -16,7 +16,7 @@ limitations under the License.
package metrics
import (
"io/ioutil"
"os"
"testing"
)
@@ -121,7 +121,7 @@ func TestPrometheusMetricsParsingAndMatching(t *testing.T) {
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
b, err := ioutil.ReadFile(test.metricsTextPath)
b, err := os.ReadFile(test.metricsTextPath)
if err != nil {
t.Errorf("Unexpected error reading file %s: %v", test.metricsTextPath, err)
}

View File

@@ -28,7 +28,7 @@ import (
"crypto/sha512"
"encoding/hex"
"flag"
"io/ioutil"
"io"
"log"
"net/http"
"strings"
@@ -92,7 +92,7 @@ func main() {
if res.ContentLength != objectLength {
log.Fatalf("Length reported (%d) is not equal to expected length (%d)", res.ContentLength, objectLength)
}
blobData, err := ioutil.ReadAll(res.Body)
blobData, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil {
log.Fatal("Failed to read full content", err)
@@ -110,7 +110,7 @@ func main() {
if err != nil {
log.Fatalf("Failure (%s) while reading %s", err, objectHashUrl)
}
content, err := ioutil.ReadAll(res.Body)
content, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil {
log.Fatal("Failed to read full content of hash file", err)

View File

@@ -18,7 +18,7 @@ package npd
import (
"fmt"
"io/ioutil"
"os"
"path"
"strings"
"time"
@@ -32,11 +32,11 @@ import (
// SetupNPD installs NPD from the test tarball onto the provided GCE instance.
//
// Here is how it works:
// 1. SetupNPD will SCP the NPD build tarball onto the VM.
// 2. SetupNPD will extract the tarball in the VM, to expose the test/e2e-install.sh on the VM.
// 3. SetupNPD will then call the e2e-install.sh script, and feed the NPD build tarball as input.
// 4. Finally, the e2e-install.sh script will do the heavy lifting of installing NPD (setting up
// binary/config directories, setting up systemd config file, etc).
// 1. SetupNPD will SCP the NPD build tarball onto the VM.
// 2. SetupNPD will extract the tarball in the VM, to expose the test/e2e-install.sh on the VM.
// 3. SetupNPD will then call the e2e-install.sh script, and feed the NPD build tarball as input.
// 4. Finally, the e2e-install.sh script will do the heavy lifting of installing NPD (setting up
// binary/config directories, setting up systemd config file, etc).
func SetupNPD(ins gce.Instance, npdBuildTar string) error {
tmpDirCmd := ins.RunCommand("mktemp -d")
if tmpDirCmd.SSHError != nil || tmpDirCmd.Code != 0 {
@@ -152,7 +152,7 @@ func saveCommandResultAsArtifact(ins gce.Instance, artifactDirectory string, tes
if result.SSHError != nil || result.Code != 0 {
return fmt.Errorf("Error running command: %v\n", result)
}
if err := ioutil.WriteFile(artifactPath, []byte(result.Stdout), 0644); err != nil {
if err := os.WriteFile(artifactPath, []byte(result.Stdout), 0644); err != nil {
return fmt.Errorf("Error writing artifact to %v: %v\n", artifactPath, err)
}
return nil

View File

@@ -17,7 +17,7 @@ limitations under the License.
package makers
import (
"io/ioutil"
"os"
"github.com/golang/glog"
)
@@ -30,7 +30,7 @@ const ext4ErrorTrigger = "/sys/fs/ext4/sda1/trigger_fs_error"
func makeFilesystemError() {
msg := []byte("fake filesystem error from problem-maker")
err := ioutil.WriteFile(ext4ErrorTrigger, msg, 0200)
err := os.WriteFile(ext4ErrorTrigger, msg, 0200)
if err != nil {
glog.Fatalf("Failed writing log to %q: %v", ext4ErrorTrigger, err)
}

View File

@@ -17,7 +17,7 @@ limitations under the License.
package makers
import (
"io/ioutil"
"os"
"strings"
"github.com/golang/glog"
@@ -38,7 +38,7 @@ Killed process 1012 (heapster) total-vm:327128kB, anon-rss:306328kB, file-rss:11
func writeKernelMessageOrDie(msg string) {
for _, line := range strings.Split(msg, "\n") {
err := ioutil.WriteFile(kmsgPath, []byte(line), 0644)
err := os.WriteFile(kmsgPath, []byte(line), 0644)
if err != nil {
glog.Fatalf("Failed writing to %q: %v", kmsgPath, err)
}