Skip to content

Commit

Permalink
Removing calls to deprecates io/ioutil package
Browse files Browse the repository at this point in the history
  • Loading branch information
iwankgb committed May 8, 2023
1 parent 02e241d commit f4c2421
Show file tree
Hide file tree
Showing 36 changed files with 169 additions and 163 deletions.
2 changes: 0 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ issues:
- "(method|func) [A-Z].* should be .*"
# Stuttering affects exported names:
- "type name will be used as .*\\.[A-Z]{1}.* by other packages, and that stutters"
# TODO: Migrate away from io/ioutil
- "\"io/ioutil\" has been deprecated since Go 1.16"
exclude-rules:
# utils/cpuload/netlink/netlink.go:102:15: Error return value of `binary.Write` is not checked (errcheck)
# There are more similar issues in this file
Expand Down
3 changes: 1 addition & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"path"
"strings"
Expand Down Expand Up @@ -187,7 +186,7 @@ func (c *Client) httpGetJSONData(data, postData interface{}, url, infoName strin
return fmt.Errorf("received empty response for %q from %q", infoName, url)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
err = fmt.Errorf("unable to read all %q from %q: %v", infoName, url, err)
return err
Expand Down
4 changes: 2 additions & 2 deletions client/v2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"path"
Expand Down Expand Up @@ -147,7 +147,7 @@ func (c *Client) httpGetResponse(postData interface{}, urlPath, infoName string)
return nil, fmt.Errorf("received empty response for %q from %q", infoName, urlPath)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
err = fmt.Errorf("unable to read all %q from %q: %v", infoName, urlPath, err)
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions cmd/internal/storage/bigquery/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"context"
"flag"
"fmt"
"io/ioutil"
"os"
"strings"

"golang.org/x/oauth2"
Expand Down Expand Up @@ -62,7 +62,7 @@ func connect() (*oauth2.Token, *bigquery.Service, error) {
if *pemFile == "" {
return nil, nil, fmt.Errorf("no credentials specified")
}
pemBytes, err := ioutil.ReadFile(*pemFile)
pemBytes, err := os.ReadFile(*pemFile)
if err != nil {
return nil, nil, fmt.Errorf("could not access credential file %v - %v", pemFile, err)
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/internal/storage/kafka/kafka.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"crypto/x509"
"encoding/json"
"flag"
"io/ioutil"
"log"
"os"
"strings"
Expand Down Expand Up @@ -110,7 +109,7 @@ func generateTLSConfig() (*tls.Config, error) {
return nil, err
}

caCert, err := ioutil.ReadFile(*caFile)
caCert, err := os.ReadFile(*caFile)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions collector/generic_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package collector
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"regexp"
"strconv"
Expand Down Expand Up @@ -145,7 +145,7 @@ func (collector *GenericCollector) Collect(metrics map[string][]v1.MetricVal) (t

defer response.Body.Close()

pageContent, err := ioutil.ReadAll(response.Body)
pageContent, err := io.ReadAll(response.Body)
if err != nil {
return nextCollectionTime, nil, err
}
Expand Down
21 changes: 10 additions & 11 deletions collector/generic_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package collector

import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
Expand All @@ -40,9 +39,9 @@ func TestEmptyConfig(t *testing.T) {
`

// Create a temporary config file 'temp.json' with invalid json format
assert.NoError(ioutil.WriteFile("temp.json", []byte(emptyConfig), 0777))
assert.NoError(os.WriteFile("temp.json", []byte(emptyConfig), 0777))

configFile, err := ioutil.ReadFile("temp.json")
configFile, err := os.ReadFile("temp.json")
assert.NoError(err)

containerHandler := containertest.NewMockContainerHandler("mockContainer")
Expand Down Expand Up @@ -72,8 +71,8 @@ func TestConfigWithErrors(t *testing.T) {
`

// Create a temporary config file 'temp.json' with invalid json format
assert.NoError(ioutil.WriteFile("temp.json", []byte(invalid), 0777))
configFile, err := ioutil.ReadFile("temp.json")
assert.NoError(os.WriteFile("temp.json", []byte(invalid), 0777))
configFile, err := os.ReadFile("temp.json")
assert.NoError(err)

containerHandler := containertest.NewMockContainerHandler("mockContainer")
Expand Down Expand Up @@ -110,9 +109,9 @@ func TestConfigWithRegexErrors(t *testing.T) {
`

// Create a temporary config file 'temp.json'
assert.NoError(ioutil.WriteFile("temp.json", []byte(invalid), 0777))
assert.NoError(os.WriteFile("temp.json", []byte(invalid), 0777))

configFile, err := ioutil.ReadFile("temp.json")
configFile, err := os.ReadFile("temp.json")
assert.NoError(err)

containerHandler := containertest.NewMockContainerHandler("mockContainer")
Expand All @@ -126,7 +125,7 @@ func TestConfig(t *testing.T) {
assert := assert.New(t)

// Create an nginx collector using the config file 'sample_config.json'
configFile, err := ioutil.ReadFile("config/sample_config.json")
configFile, err := os.ReadFile("config/sample_config.json")
assert.NoError(err)

containerHandler := containertest.NewMockContainerHandler("mockContainer")
Expand All @@ -139,7 +138,7 @@ func TestConfig(t *testing.T) {

func TestEndpointConfig(t *testing.T) {
assert := assert.New(t)
configFile, err := ioutil.ReadFile("config/sample_config_endpoint_config.json")
configFile, err := os.ReadFile("config/sample_config_endpoint_config.json")
assert.NoError(err)

containerHandler := containertest.NewMockContainerHandler("mockContainer")
Expand All @@ -158,7 +157,7 @@ func TestMetricCollection(t *testing.T) {
assert := assert.New(t)

// Collect nginx metrics from a fake nginx endpoint
configFile, err := ioutil.ReadFile("config/sample_config.json")
configFile, err := os.ReadFile("config/sample_config.json")
assert.NoError(err)

containerHandler := containertest.NewMockContainerHandler("mockContainer")
Expand Down Expand Up @@ -194,7 +193,7 @@ func TestMetricCollectionLimit(t *testing.T) {
assert := assert.New(t)

// Collect nginx metrics from a fake nginx endpoint
configFile, err := ioutil.ReadFile("config/sample_config.json")
configFile, err := os.ReadFile("config/sample_config.json")
assert.NoError(err)

containerHandler := containertest.NewMockContainerHandler("mockContainer")
Expand Down
14 changes: 7 additions & 7 deletions collector/prometheus_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ package collector

import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -32,7 +32,7 @@ func TestPrometheus(t *testing.T) {
assert := assert.New(t)

// Create a prometheus collector using the config file 'sample_config_prometheus.json'
configFile, err := ioutil.ReadFile("config/sample_config_prometheus.json")
configFile, err := os.ReadFile("config/sample_config_prometheus.json")
assert.NoError(err)
containerHandler := containertest.NewMockContainerHandler("mockContainer")
collector, err := NewPrometheusCollector("Prometheus", configFile, 100, containerHandler, http.DefaultClient)
Expand Down Expand Up @@ -116,7 +116,7 @@ func TestPrometheusEndpointConfig(t *testing.T) {
assert := assert.New(t)

//Create a prometheus collector using the config file 'sample_config_prometheus.json'
configFile, err := ioutil.ReadFile("config/sample_config_prometheus_endpoint_config.json")
configFile, err := os.ReadFile("config/sample_config_prometheus_endpoint_config.json")
assert.NoError(err)
containerHandler := containertest.NewMockContainerHandler("mockContainer")
containerHandler.On("GetContainerIPAddress").Return(
Expand All @@ -133,7 +133,7 @@ func TestPrometheusShortResponse(t *testing.T) {
assert := assert.New(t)

// Create a prometheus collector using the config file 'sample_config_prometheus.json'
configFile, err := ioutil.ReadFile("config/sample_config_prometheus.json")
configFile, err := os.ReadFile("config/sample_config_prometheus.json")
assert.NoError(err)
containerHandler := containertest.NewMockContainerHandler("mockContainer")
collector, err := NewPrometheusCollector("Prometheus", configFile, 100, containerHandler, http.DefaultClient)
Expand All @@ -157,7 +157,7 @@ func TestPrometheusMetricCountLimit(t *testing.T) {
assert := assert.New(t)

// Create a prometheus collector using the config file 'sample_config_prometheus.json'
configFile, err := ioutil.ReadFile("config/sample_config_prometheus.json")
configFile, err := os.ReadFile("config/sample_config_prometheus.json")
assert.NoError(err)
containerHandler := containertest.NewMockContainerHandler("mockContainer")
collector, err := NewPrometheusCollector("Prometheus", configFile, 10, containerHandler, http.DefaultClient)
Expand Down Expand Up @@ -187,7 +187,7 @@ func TestPrometheusFiltersMetrics(t *testing.T) {
assert := assert.New(t)

// Create a prometheus collector using the config file 'sample_config_prometheus_filtered.json'
configFile, err := ioutil.ReadFile("config/sample_config_prometheus_filtered.json")
configFile, err := os.ReadFile("config/sample_config_prometheus_filtered.json")
assert.NoError(err)
containerHandler := containertest.NewMockContainerHandler("mockContainer")
collector, err := NewPrometheusCollector("Prometheus", configFile, 100, containerHandler, http.DefaultClient)
Expand Down Expand Up @@ -227,7 +227,7 @@ func TestPrometheusFiltersMetricsCountLimit(t *testing.T) {
assert := assert.New(t)

// Create a prometheus collector using the config file 'sample_config_prometheus_filtered.json'
configFile, err := ioutil.ReadFile("config/sample_config_prometheus_filtered.json")
configFile, err := os.ReadFile("config/sample_config_prometheus_filtered.json")
assert.NoError(err)
containerHandler := containertest.NewMockContainerHandler("mockContainer")
_, err = NewPrometheusCollector("Prometheus", configFile, 1, containerHandler, http.DefaultClient)
Expand Down
3 changes: 1 addition & 2 deletions container/common/container_hints.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package common
import (
"encoding/json"
"flag"
"io/ioutil"
"os"
)

Expand All @@ -48,7 +47,7 @@ type networkInterface struct {
}

func GetContainerHintsFromFile(containerHintsFile string) (ContainerHints, error) {
dat, err := ioutil.ReadFile(containerHintsFile)
dat, err := os.ReadFile(containerHintsFile)
if os.IsNotExist(err) {
return ContainerHints{}, nil
}
Expand Down
3 changes: 1 addition & 2 deletions container/common/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package common

import (
"fmt"
"io/ioutil"
"math"
"os"
"path"
Expand Down Expand Up @@ -235,7 +234,7 @@ func readString(dirpath string, file string) string {
cgroupFile := path.Join(dirpath, file)

// Read
out, err := ioutil.ReadFile(cgroupFile)
out, err := os.ReadFile(cgroupFile)
if err != nil {
// Ignore non-existent files
if !os.IsNotExist(err) {
Expand Down
4 changes: 2 additions & 2 deletions container/crio/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net"
"net/http"
"sync"
Expand Down Expand Up @@ -142,7 +142,7 @@ func (c *crioClientImpl) ContainerInfo(id string) (*ContainerInfo, error) {
// golang's http.Do doesn't return an error if non 200 response code is returned
// handle this case here, rather than failing to decode the body
if resp.StatusCode != http.StatusOK {
respBody, err := ioutil.ReadAll(resp.Body)
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("Error finding container %s: Status %d", id, resp.StatusCode)
}
Expand Down
4 changes: 2 additions & 2 deletions container/docker/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package docker

import (
"fmt"
"io/ioutil"
"os"
"path"
"strconv"
"strings"
Expand Down Expand Up @@ -107,7 +107,7 @@ func getRwLayerID(containerID, storageDir string, sd storageDriver, dockerVersio
return containerID, nil
}

bytes, err := ioutil.ReadFile(path.Join(storageDir, "image", string(sd), "layerdb", "mounts", containerID, rwLayerIDFile))
bytes, err := os.ReadFile(path.Join(storageDir, "image", string(sd), "layerdb", "mounts", containerID, rwLayerIDFile))
if err != nil {
return "", fmt.Errorf("failed to identify the read-write layer ID for container %q. - %v", containerID, err)
}
Expand Down
5 changes: 2 additions & 3 deletions container/docker/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package docker

import (
"io/ioutil"
"os"
"path"
"strings"
Expand All @@ -38,13 +37,13 @@ func TestStorageDirDetectionWithOldVersions(t *testing.T) {

func TestStorageDirDetectionWithNewVersions(t *testing.T) {
as := assert.New(t)
testDir, err := ioutil.TempDir("", "")
testDir, err := os.MkdirTemp(os.TempDir(), "")
as.Nil(err)
containerID := "abcd"
randomizedID := "xyz"
randomIDPath := path.Join(testDir, "image/aufs/layerdb/mounts/", containerID)
as.Nil(os.MkdirAll(randomIDPath, os.ModePerm))
as.Nil(ioutil.WriteFile(path.Join(randomIDPath, "mount-id"), []byte(randomizedID), os.ModePerm))
as.Nil(os.WriteFile(path.Join(randomIDPath, "mount-id"), []byte(randomizedID), os.ModePerm))
rwLayer, err := getRwLayerID(containerID, testDir, "aufs", []int{1, 10, 0})
as.Nil(err)
as.Equal(rwLayer, randomizedID)
Expand Down
Loading

0 comments on commit f4c2421

Please sign in to comment.