chore: remove refs to deprecated io/ioutil

This commit is contained in:
guoguangwu
2023-06-21 12:12:27 +08:00
parent 339e243472
commit 6dc23ca804
15 changed files with 36 additions and 43 deletions
@@ -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)
}
+2 -3
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
@@ -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)
}
}
@@ -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)
}
+2 -2
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)
}
@@ -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()
+2 -7
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 {
@@ -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)
@@ -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)
}
+2 -2
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)
}
+3 -3
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)