Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug 2020250: Replacing deprecated ioutil #532

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions cmd/changelog/main.go
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -78,8 +78,8 @@ func createPullRequestLink(id string) string {
func main() {
log.SetFlags(0)
if len(os.Args) != 1 && len(os.Args) != 3 {
log.Fatalf(`Either specify two date arguments, AFTER and UNTIL,
to create a brand new CHANGELOG, or call it without arguments to
log.Fatalf(`Either specify two date arguments, AFTER and UNTIL,
to create a brand new CHANGELOG, or call it without arguments to
update the current one with new changes.`)
}
gitHubToken = os.Getenv("GITHUB_TOKEN")
Expand Down Expand Up @@ -121,7 +121,7 @@ type MarkdownReleaseBlock struct {

func readCHANGELOG() map[string]MarkdownReleaseBlock {
releaseBlocks := make(map[string]MarkdownReleaseBlock)
rawBytes, _ := ioutil.ReadFile("./CHANGELOG.md")
rawBytes, _ := os.ReadFile("./CHANGELOG.md")
rawString := string(rawBytes)
if match := latestHashRegexp.FindStringSubmatch(rawString); len(match) > 0 {
latestHash = match[1]
Expand Down Expand Up @@ -182,7 +182,7 @@ func updateToMarkdownReleaseBlock(releaseBlocks map[string]MarkdownReleaseBlock,
func createCHANGELOG(releaseBlocks map[string]MarkdownReleaseBlock) {
file, _ := os.Create("CHANGELOG.md")
defer file.Close()
_, _ = file.WriteString(`# Note: This CHANGELOG is only for the changes in insights operator.
_, _ = file.WriteString(`# Note: This CHANGELOG is only for the changes in insights operator.
Please see OpenShift release notes for official changes\n`)
_, _ = file.WriteString(fmt.Sprintf("<!--Latest hash: %s-->\n", latestHash))
var releases []string
Expand Down Expand Up @@ -243,7 +243,7 @@ func getPullRequestFromGitHub(id string) *Change {
log.Fatalf(err.Error())
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
defer log.Fatalf(err.Error())
return nil
Expand Down
3 changes: 1 addition & 2 deletions cmd/gendoc/main.go
Expand Up @@ -6,7 +6,6 @@ import (
"go/ast"
"go/parser"
"go/token"
"io/ioutil"
"log"
"math/rand"
"os"
Expand Down Expand Up @@ -219,7 +218,7 @@ func findGoMod(pkgFilePath string) (goModPath, relPkgPath string, err error) {

// getModuleNameFromGoMod parses the go.mod file and returns the name (URL) of the module.
func getModuleNameFromGoMod(goModPath string) (string, error) {
goModBytes, err := ioutil.ReadFile(goModPath)
goModBytes, err := os.ReadFile(goModPath)
if err != nil {
return "", err
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/obfuscate-archive/main.go
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"strings"

Expand Down Expand Up @@ -190,7 +189,7 @@ func readArchive(path string) (map[string]*record.MemoryRecord, error) {
return nil, err
}

content, err := ioutil.ReadAll(tarReader)
content, err := io.ReadAll(tarReader)
if err != nil {
return nil, err
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/cmd/start/start.go
Expand Up @@ -2,7 +2,6 @@ package start

import (
"context"
"io/ioutil"
"math/rand"
"os"
"time"
Expand Down Expand Up @@ -87,7 +86,7 @@ func runGather(operator *controller.GatherJob, cfg *controllercmd.ControllerComm

var clientConfig *rest.Config
if kubeConfigPath := cmd.Flags().Lookup("kubeconfig").Value.String(); len(kubeConfigPath) > 0 {
kubeConfigBytes, err := ioutil.ReadFile(kubeConfigPath) //nolint: govet
kubeConfigBytes, err := os.ReadFile(kubeConfigPath) //nolint: govet
if err != nil {
klog.Exit(err)
}
Expand Down Expand Up @@ -143,7 +142,7 @@ func runOperator(operator *controller.Operator, cfg *controllercmd.ControllerCom
}

// if the service CA is rotated, we want to restart
if data, err := ioutil.ReadFile(serviceCACertPath); err == nil {
if data, err := os.ReadFile(serviceCACertPath); err == nil {
startingFileContent[serviceCACertPath] = data
} else {
klog.V(4).Infof("Unable to read service ca bundle: %v", err)
Expand Down
Expand Up @@ -3,7 +3,7 @@ package clusterconfig
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"os"
"reflect"
"testing"
Expand Down Expand Up @@ -33,7 +33,7 @@ func Test_CSR(t *testing.T) {
t.Fatal("test failed to unmarshal csr data", err)
}
defer f.Close()
bts, err := ioutil.ReadAll(f)
bts, err := io.ReadAll(f)
if err != nil {
t.Fatal("error reading test data file", err)
}
Expand All @@ -48,7 +48,7 @@ func Test_CSR(t *testing.T) {
t.Fatal("test failed to unmarshal csr anonymized data", err)
}
defer f.Close()
bts, err = ioutil.ReadAll(f)
bts, err = io.ReadAll(f)
if err != nil {
t.Fatal("error reading test data file", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/gatherers/clusterconfig/config_maps_test.go
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -119,7 +119,7 @@ func readConfigMapsTestData() (*corev1.ConfigMapList, error) {

defer f.Close()

bts, err := ioutil.ReadAll(f)
bts, err := io.ReadAll(f)
if err != nil {
return nil, fmt.Errorf("error reading test data file %+v ", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/gatherers/clusterconfig/install_plans_test.go
Expand Up @@ -2,7 +2,7 @@ package clusterconfig

import (
"context"
"io/ioutil"
"io"
"os"
"testing"

Expand Down Expand Up @@ -63,7 +63,7 @@ func Test_InstallPlans_Gather(t *testing.T) {
t.Fatal("test failed to read installplan data", err)
}
defer f.Close()
installplancontent, err := ioutil.ReadAll(f)
installplancontent, err := io.ReadAll(f)
if err != nil {
t.Fatal("error reading test data file", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/gatherers/clusterconfig/olm_operators_test.go
Expand Up @@ -3,7 +3,7 @@ package clusterconfig
import (
"context"
"fmt"
"io/ioutil"
"io"
"os"
"reflect"
"testing"
Expand Down Expand Up @@ -154,7 +154,7 @@ func readFromFile(filePath string) ([]byte, error) {

defer f.Close()

content, err := ioutil.ReadAll(f)
content, err := io.ReadAll(f)
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/gatherers/clusterconfig/recent_metrics.go
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"

"k8s.io/client-go/rest"
"k8s.io/klog/v2"
Expand Down Expand Up @@ -76,7 +75,7 @@ func gatherMostRecentMetrics(ctx context.Context, metricsClient rest.Interface)
return nil, []error{err}
}
r := utils.NewLineLimitReader(rsp, metricsAlertsLinesLimit)
alerts, err := ioutil.ReadAll(r)
alerts, err := io.ReadAll(r)
if err != nil && err != io.EOF {
klog.Errorf("Unable to read most recent alerts from metrics: %v", err)
return nil, []error{err}
Expand Down
4 changes: 2 additions & 2 deletions pkg/gatherers/conditional/conditional_gatherer_test.go
Expand Up @@ -3,7 +3,7 @@ package conditional
import (
"context"
"encoding/json"
"io/ioutil"
"io"
"net/http"
"strings"
"testing"
Expand Down Expand Up @@ -175,7 +175,7 @@ func newFakeClientWithMetrics(metrics string) *fake.RESTClient {
Client: fake.CreateHTTPClient(func(request *http.Request) (*http.Response, error) {
resp := &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(strings.NewReader(metrics + "\n")),
Body: io.NopCloser(strings.NewReader(metrics + "\n")),
}
return resp, nil
}),
Expand Down
3 changes: 1 addition & 2 deletions pkg/gatherers/workloads/workloads_info_test.go
Expand Up @@ -5,7 +5,6 @@ import (
"compress/gzip"
"context"
"encoding/json"
"io/ioutil"
"os"
"testing"
"time"
Expand Down Expand Up @@ -50,7 +49,7 @@ func Test_gatherWorkloadInfo(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if err = ioutil.WriteFile("../../../docs/insights-archive-sample/config/workload_info.json", out, 0750); err != nil {
if err = os.WriteFile("../../../docs/insights-archive-sample/config/workload_info.json", out, 0750); err != nil {
t.Fatal(err)
}

Expand Down
15 changes: 7 additions & 8 deletions pkg/insights/insightsclient/insightsclient.go
Expand Up @@ -8,7 +8,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net"
"net/http"
Expand Down Expand Up @@ -102,7 +101,7 @@ func New(client *http.Client, maxBytes int64, metricsName string, authorizer Aut
}

func getTrustedCABundle() (*x509.CertPool, error) {
caBytes, err := ioutil.ReadFile("/var/run/configmaps/trusted-ca-bundle/ca-bundle.crt")
caBytes, err := os.ReadFile("/var/run/configmaps/trusted-ca-bundle/ca-bundle.crt")
if err != nil {
if os.IsNotExist(err) {
return nil, nil
Expand Down Expand Up @@ -251,7 +250,7 @@ func (c *Client) Send(ctx context.Context, endpoint string, source Source) error
requestID := resp.Header.Get(insightsReqId)

defer func() {
if _, err := io.Copy(ioutil.Discard, resp.Body); err != nil {
if _, err := io.Copy(io.Discard, resp.Body); err != nil {
klog.Warningf("error copying body: %v", err)
}
if err := resp.Body.Close(); err != nil {
Expand Down Expand Up @@ -332,15 +331,15 @@ func (c Client) RecvReport(ctx context.Context, endpoint string) (*http.Response
}

if resp.StatusCode == http.StatusBadRequest {
body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)
if len(body) > 1024 {
body = body[:1024]
}
c.IncrementRecvReportMetric(resp.StatusCode)
return nil, fmt.Errorf("gateway server bad request: %s (request=%s): %s", resp.Request.URL, requestID, string(body))
}
if resp.StatusCode == http.StatusNotFound {
body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)
if len(body) > 1024 {
body = body[:1024]
}
Expand All @@ -353,7 +352,7 @@ func (c Client) RecvReport(ctx context.Context, endpoint string) (*http.Response
}

if resp.StatusCode >= 300 || resp.StatusCode < 200 {
body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)
if len(body) > 1024 {
body = body[:1024]
}
Expand Down Expand Up @@ -404,14 +403,14 @@ func (c Client) RecvSCACerts(ctx context.Context, endpoint string) ([]byte, erro
}
}()

return ioutil.ReadAll(resp.Body)
return io.ReadAll(resp.Body)
}

func responseBody(r *http.Response) string {
if r == nil {
return ""
}
body, _ := ioutil.ReadAll(r.Body)
body, _ := io.ReadAll(r.Body)
if len(body) > responseBodyLogLen {
body = body[:responseBodyLogLen]
}
Expand Down
15 changes: 8 additions & 7 deletions pkg/recorder/diskrecorder/diskrecorder.go
Expand Up @@ -6,7 +6,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -102,17 +101,18 @@ func (d *DiskRecorder) SaveAtPath(records record.MemoryRecords, path string) (re

// Prune the archives older than given time
func (d *DiskRecorder) Prune(olderThan time.Time) error {
files, err := ioutil.ReadDir(d.basePath)
files, err := os.ReadDir(d.basePath)
if err != nil {
return err
}
count := 0
var errors []string
for _, file := range files {
if isNotArchiveFile(file) {
fileInfo, _ := file.Info()
if isNotArchiveFile(fileInfo) {
continue
}
if file.ModTime().After(olderThan) {
if fileInfo.ModTime().After(olderThan) {
continue
}
if err := os.Remove(filepath.Join(d.basePath, file.Name())); err != nil {
Expand All @@ -135,7 +135,7 @@ func (d *DiskRecorder) Prune(olderThan time.Time) error {

// Summary implements summarizer interface to insights uploader
func (d *DiskRecorder) Summary(_ context.Context, since time.Time) (io.ReadCloser, bool, error) {
files, err := ioutil.ReadDir(d.basePath)
files, err := os.ReadDir(d.basePath)
if err != nil {
return nil, false, err
}
Expand All @@ -144,10 +144,11 @@ func (d *DiskRecorder) Summary(_ context.Context, since time.Time) (io.ReadClose
}
recentFiles := make([]string, 0, len(files))
for _, file := range files {
if isNotArchiveFile(file) {
fileInfo, _ := file.Info()
rluders marked this conversation as resolved.
Show resolved Hide resolved
if isNotArchiveFile(fileInfo) {
continue
}
if !file.ModTime().After(since) {
if !fileInfo.ModTime().After(since) {
continue
}
recentFiles = append(recentFiles, file.Name())
Expand Down