diff --git a/cmd/nodeproblemdetector/node_problem_detector_test.go b/cmd/nodeproblemdetector/node_problem_detector_test.go index 5ad551a46..ebb92bfc0 100644 --- a/cmd/nodeproblemdetector/node_problem_detector_test.go +++ b/cmd/nodeproblemdetector/node_problem_detector_test.go @@ -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) } diff --git a/pkg/custompluginmonitor/custom_plugin_monitor.go b/pkg/custompluginmonitor/custom_plugin_monitor.go index 6d95d685f..4863714d1 100644 --- a/pkg/custompluginmonitor/custom_plugin_monitor.go +++ b/pkg/custompluginmonitor/custom_plugin_monitor.go @@ -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) } diff --git a/pkg/custompluginmonitor/plugin/plugin.go b/pkg/custompluginmonitor/plugin/plugin.go index 69b382cb6..5a77ab7fe 100644 --- a/pkg/custompluginmonitor/plugin/plugin.go +++ b/pkg/custompluginmonitor/plugin/plugin.go @@ -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 diff --git a/pkg/exporters/k8sexporter/problemclient/configs.go b/pkg/exporters/k8sexporter/problemclient/configs.go index 8e2888623..11f2c49cb 100644 --- a/pkg/exporters/k8sexporter/problemclient/configs.go +++ b/pkg/exporters/k8sexporter/problemclient/configs.go @@ -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) } } diff --git a/pkg/exporters/stackdriver/stackdriver_exporter.go b/pkg/exporters/stackdriver/stackdriver_exporter.go index f14eb0b94..0bf2ff8e6 100644 --- a/pkg/exporters/stackdriver/stackdriver_exporter.go +++ b/pkg/exporters/stackdriver/stackdriver_exporter.go @@ -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) } diff --git a/pkg/systemlogmonitor/log_monitor.go b/pkg/systemlogmonitor/log_monitor.go index 5528a22ab..789a45cc7 100644 --- a/pkg/systemlogmonitor/log_monitor.go +++ b/pkg/systemlogmonitor/log_monitor.go @@ -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) } diff --git a/pkg/systemlogmonitor/logwatchers/filelog/log_watcher_test.go b/pkg/systemlogmonitor/logwatchers/filelog/log_watcher_test.go index 76843bb3e..93c758d12 100644 --- a/pkg/systemlogmonitor/logwatchers/filelog/log_watcher_test.go +++ b/pkg/systemlogmonitor/logwatchers/filelog/log_watcher_test.go @@ -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() diff --git a/pkg/systemstatsmonitor/net_collector_test.go b/pkg/systemstatsmonitor/net_collector_test.go index 1647e7538..f66c04806 100644 --- a/pkg/systemstatsmonitor/net_collector_test.go +++ b/pkg/systemstatsmonitor/net_collector_test.go @@ -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 { diff --git a/pkg/systemstatsmonitor/osfeature_collector.go b/pkg/systemstatsmonitor/osfeature_collector.go index a1d1332f4..67210a842 100644 --- a/pkg/systemstatsmonitor/osfeature_collector.go +++ b/pkg/systemstatsmonitor/osfeature_collector.go @@ -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) diff --git a/pkg/systemstatsmonitor/system_stats_monitor.go b/pkg/systemstatsmonitor/system_stats_monitor.go index 765db3ecd..af728fbb5 100644 --- a/pkg/systemstatsmonitor/system_stats_monitor.go +++ b/pkg/systemstatsmonitor/system_stats_monitor.go @@ -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) } diff --git a/pkg/util/metrics/helpers_test.go b/pkg/util/metrics/helpers_test.go index 59ba6b005..548921ccd 100644 --- a/pkg/util/metrics/helpers_test.go +++ b/pkg/util/metrics/helpers_test.go @@ -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) } diff --git a/pkg/util/nethealth/nethealth.go b/pkg/util/nethealth/nethealth.go index 0f7634454..78a8a5895 100644 --- a/pkg/util/nethealth/nethealth.go +++ b/pkg/util/nethealth/nethealth.go @@ -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) diff --git a/test/e2e/lib/npd/npd.go b/test/e2e/lib/npd/npd.go index 230a3fd7d..aadf7d97b 100644 --- a/test/e2e/lib/npd/npd.go +++ b/test/e2e/lib/npd/npd.go @@ -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 diff --git a/test/e2e/problemmaker/makers/filesystem.go b/test/e2e/problemmaker/makers/filesystem.go index 19e168be5..4d3b72527 100644 --- a/test/e2e/problemmaker/makers/filesystem.go +++ b/test/e2e/problemmaker/makers/filesystem.go @@ -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) } diff --git a/test/e2e/problemmaker/makers/kernel.go b/test/e2e/problemmaker/makers/kernel.go index c4cf13a9f..83ad84d88 100644 --- a/test/e2e/problemmaker/makers/kernel.go +++ b/test/e2e/problemmaker/makers/kernel.go @@ -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) }