Skip to content

Commit

Permalink
Address ioutil deprecation
Browse files Browse the repository at this point in the history
Signed-off-by: Dale Haiducek <[email protected]>
  • Loading branch information
dhaiducek committed Feb 12, 2024
1 parent 1922c18 commit 53f1875
Show file tree
Hide file tree
Showing 20 changed files with 49 additions and 60 deletions.
4 changes: 2 additions & 2 deletions e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package e2e
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"testing"
)
Expand Down Expand Up @@ -86,7 +86,7 @@ func (r *Runner) Run(runID string) error {
defer resp.Body.Close()

if resp.StatusCode == http.StatusOK {
bodyBytes, err := ioutil.ReadAll(resp.Body)
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/addonmanager/bindata/bindata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pkg/controller/mcmhub/gitrepo_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package mcmhub

import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"strings"

Expand Down Expand Up @@ -227,7 +227,7 @@ func (r *ReconcileSubscription) subscribeResources(
rscFiles []string, objRefMap map[v1.ObjectReference]*v1.ObjectReference) error {
// sync kube resource manifests
for _, rscFile := range rscFiles {
file, err := ioutil.ReadFile(rscFile) // #nosec G304 rscFile is not user input
file, err := os.ReadFile(rscFile) // #nosec G304 rscFile is not user input

if err != nil {
klog.Error(err, "Failed to read YAML file "+rscFile)
Expand Down
3 changes: 1 addition & 2 deletions pkg/controller/mcmhub/hub_git.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package mcmhub
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -699,7 +698,7 @@ func parseAsAnsibleJobs(rscFiles []string, parser func([]byte) [][]byte, logger
jobs := []ansiblejob.AnsibleJob{}
// sync kube resource manifests
for _, rscFile := range rscFiles {
file, err := ioutil.ReadFile(rscFile) // #nosec G304 rscFile is not user input
file, err := os.ReadFile(rscFile) // #nosec G304 rscFile is not user input

if err != nil {
return []ansiblejob.AnsibleJob{}, err
Expand Down
3 changes: 1 addition & 2 deletions pkg/controller/subscription/lease_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package subscription

import (
"context"
"io/ioutil"
"os"
"testing"
"time"
Expand Down Expand Up @@ -73,7 +72,7 @@ func TestLeaseReconcile(t *testing.T) {
addontNs, _ := utils.GetComponentNamespace()
pod.SetNamespace(addontNs)

tmpFile, err := ioutil.TempFile("", "temptest")
tmpFile, err := os.CreateTemp("", "temptest")
g.Expect(err).ShouldNot(gomega.HaveOccurred())

_, err = tmpFile.WriteString("fake kubeconfig data")
Expand Down
3 changes: 1 addition & 2 deletions pkg/helmrelease/utils/helmrepoutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"encoding/pem"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -348,7 +347,7 @@ func getKnownHostFromURL(sshURL string, filepath string) error {

klog.Info("SSH host key: " + string(stdout))

if err := ioutil.WriteFile(filepath, stdout, 0600); err != nil {
if err := os.WriteFile(filepath, stdout, 0600); err != nil {
klog.Error("failed to write known_hosts file: ", err)
return err
}
Expand Down
31 changes: 15 additions & 16 deletions pkg/helmrelease/utils/helmrepoutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"context"
"crypto/tls"
"encoding/pem"
"io/ioutil"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -164,7 +163,7 @@ func TestDownloadChartGitHub(t *testing.T) {
ChartName: "subscription-release-test-1",
},
}
dir, err := ioutil.TempDir("/tmp", "charts")
dir, err := os.MkdirTemp("/tmp", "charts")
assert.NoError(t, err)

defer os.RemoveAll(dir)
Expand Down Expand Up @@ -194,7 +193,7 @@ func TestDownloadChartGit(t *testing.T) {
ChartName: "subscription-release-test-1",
},
}
dir, err := ioutil.TempDir("/tmp", "charts")
dir, err := os.MkdirTemp("/tmp", "charts")
assert.NoError(t, err)

defer os.RemoveAll(dir)
Expand Down Expand Up @@ -224,7 +223,7 @@ func TestDownloadChartHelmRepo(t *testing.T) {
Digest: "long-fake-digest-that-is-very-long",
},
}
dir, err := ioutil.TempDir("/tmp", "charts")
dir, err := os.MkdirTemp("/tmp", "charts")
assert.NoError(t, err)

defer os.RemoveAll(dir)
Expand Down Expand Up @@ -257,7 +256,7 @@ func TestDownloadChartHelmRepoContainsInvalidURL(t *testing.T) {
ChartName: "subscription-release-test-1",
},
}
dir, err := ioutil.TempDir("/tmp", "charts")
dir, err := os.MkdirTemp("/tmp", "charts")
assert.NoError(t, err)

defer os.RemoveAll(dir)
Expand Down Expand Up @@ -286,7 +285,7 @@ func TestDownloadChartHelmRepoContainsInvalidURL2(t *testing.T) {
ChartName: "subscription-release-test-1",
},
}
dir, err := ioutil.TempDir("/tmp", "charts")
dir, err := os.MkdirTemp("/tmp", "charts")
assert.NoError(t, err)

defer os.RemoveAll(dir)
Expand Down Expand Up @@ -314,7 +313,7 @@ func TestDownloadChartHelmRepoAllInvalidURLs(t *testing.T) {
ChartName: "subscription-release-test-1",
},
}
dir, err := ioutil.TempDir("/tmp", "charts")
dir, err := os.MkdirTemp("/tmp", "charts")
assert.NoError(t, err)

defer os.RemoveAll(dir)
Expand All @@ -341,7 +340,7 @@ func TestDownloadChartFromGitHub(t *testing.T) {
ChartName: "subscription-release-test-1",
},
}
dir, err := ioutil.TempDir("/tmp", "charts")
dir, err := os.MkdirTemp("/tmp", "charts")
assert.NoError(t, err)

defer os.RemoveAll(dir)
Expand Down Expand Up @@ -371,7 +370,7 @@ func TestDownloadChartFromGit(t *testing.T) {
ChartName: "subscription-release-test-1",
},
}
dir, err := ioutil.TempDir("/tmp", "charts")
dir, err := os.MkdirTemp("/tmp", "charts")
assert.NoError(t, err)

defer os.RemoveAll(dir)
Expand Down Expand Up @@ -401,7 +400,7 @@ func TestDownloadChartFromHelmRepoHTTP(t *testing.T) {
Digest: "short",
},
}
dir, err := ioutil.TempDir("/tmp", "charts")
dir, err := os.MkdirTemp("/tmp", "charts")
assert.NoError(t, err)

defer os.RemoveAll(dir)
Expand Down Expand Up @@ -434,7 +433,7 @@ func TestDownloadChartFromHelmRepoHTTPConfigMap(t *testing.T) {
Digest: "short",
},
}
dir, err := ioutil.TempDir("/tmp", "charts")
dir, err := os.MkdirTemp("/tmp", "charts")
assert.NoError(t, err)

defer os.RemoveAll(dir)
Expand Down Expand Up @@ -470,7 +469,7 @@ func TestDownloadChartFromHelmRepoHTTPNoDigest(t *testing.T) {
ChartName: "subscription-release-test-1",
},
}
dir, err := ioutil.TempDir("/tmp", "charts")
dir, err := os.MkdirTemp("/tmp", "charts")
assert.NoError(t, err)

defer os.RemoveAll(dir)
Expand Down Expand Up @@ -502,7 +501,7 @@ func TestDownloadChartFromHelmRepoLocal(t *testing.T) {
Digest: "digest",
},
}
dir, err := ioutil.TempDir("/tmp", "charts")
dir, err := os.MkdirTemp("/tmp", "charts")
assert.NoError(t, err)

defer os.RemoveAll(dir)
Expand Down Expand Up @@ -533,7 +532,7 @@ func TestDownloadChartFromHelmRepoLocalNoDigest(t *testing.T) {
ChartName: "subscription-release-test-1",
},
}
dir, err := ioutil.TempDir("/tmp", "charts")
dir, err := os.MkdirTemp("/tmp", "charts")
assert.NoError(t, err)

defer os.RemoveAll(dir)
Expand All @@ -551,7 +550,7 @@ func TestDownloadChartFromHelmRepoLocalNoDigest(t *testing.T) {
func TestDownloadGitRepo(t *testing.T) {
httpURLs := []string{"https://" + testutils.GetTestGitRepoURLFromEnvVar() + ".git"}
sshURLs := []string{"ssh://" + testutils.GetTestGitRepoURLFromEnvVar() + ".git"}
dir, err := ioutil.TempDir("/tmp", "charts")
dir, err := os.MkdirTemp("/tmp", "charts")
assert.NoError(t, err)

defer os.RemoveAll(dir)
Expand Down Expand Up @@ -653,7 +652,7 @@ tYny6pJJNYEhf7HPmb2O3zBuuqsCC0O2SHrgFYH350zA4To9Ez5nifkZ0CBx0pn9jWn02V
}

func TestGetKnownHostFromURL(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "temptest")
tmpfile, err := os.CreateTemp("", "temptest")
if err != nil {
t.Error("error creating temp file")
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/subscriber/git/git_subscriber_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
Expand Down Expand Up @@ -519,7 +519,7 @@ func checkSubscriptionAnnotation(resource kubeResource) error {
func (ghsi *SubscriberItem) subscribeResources(rscFiles []string) error {
// sync kube resource manifests
for _, rscFile := range rscFiles {
file, err := ioutil.ReadFile(rscFile) // #nosec G304 rscFile is not user input
file, err := os.ReadFile(rscFile) // #nosec G304 rscFile is not user input

if err != nil {
klog.Error(err, "Failed to read YAML file "+rscFile)
Expand Down
4 changes: 2 additions & 2 deletions pkg/subscriber/helmrepo/helm_subscriber_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"crypto/sha1" // #nosec G505 Used only to generate random value to be used to generate hash string
"crypto/tls"
"fmt"
"io/ioutil"
"io"
"net"
"net/http"
"strconv"
Expand Down Expand Up @@ -524,7 +524,7 @@ func getHelmRepoIndex(client rest.HTTPClient, sub *appv1.Subscription,

klog.V(5).Info("Get succeeded: ", cleanRepoURL)

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
klog.Error(err, "Unable to read body: ", cleanRepoURL)

Expand Down
4 changes: 2 additions & 2 deletions pkg/utils/aws/objectstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package aws
import (
"bytes"
"context"
"io/ioutil"
"io"
"strings"

"github.com/aws/aws-sdk-go-v2/aws"
Expand Down Expand Up @@ -258,7 +258,7 @@ func (h *Handler) Get(bucket, name string) (DeployableObject, error) {

generateName := resp.Metadata[DployableMateGenerateNameKey]
version := resp.Metadata[DeployableMetaVersionKey]
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)

if err != nil {
klog.Error("Failed to parse Get request. error: ", err)
Expand Down
5 changes: 2 additions & 3 deletions pkg/utils/gitrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"encoding/pem"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -426,7 +425,7 @@ func getKnownHostFromURL(sshURL string, filepath string) error {

klog.Info("SSH host key: " + string(stdout))

if err := ioutil.WriteFile(filepath, stdout, 0600); err != nil {
if err := os.WriteFile(filepath, stdout, 0600); err != nil {
klog.Error("failed to write known_hosts file: ", err)
return err
}
Expand Down Expand Up @@ -812,7 +811,7 @@ func sortKubeResource(crdsAndNamespaceFiles, rbacFiles, otherFiles []string, pat
if strings.EqualFold(filepath.Ext(path), ".yml") || strings.EqualFold(filepath.Ext(path), ".yaml") {
klog.V(4).Info("Reading file: ", path)

file, err := ioutil.ReadFile(path) // #nosec G304 path is not user input
file, err := os.ReadFile(path) // #nosec G304 path is not user input

if err != nil {
klog.Error(err, "Failed to read YAML file "+path)
Expand Down
7 changes: 3 additions & 4 deletions pkg/utils/gitrepo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"crypto/tls"
"encoding/pem"
"io"
"io/ioutil"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -290,7 +289,7 @@ func TestParseMultiDocYAML(t *testing.T) {
// This tests that a multi document YAML can be parsed properly
// and handle the --- delimiter correctly
// The test file contains --- characters in a resource and delimeters --- with trailing spaces
content, err := ioutil.ReadFile("../../test/github/multiresource/multiresource.yaml")
content, err := os.ReadFile("../../test/github/multiresource/multiresource.yaml")
g.Expect(err).NotTo(gomega.HaveOccurred())

items := ParseYAML(content)
Expand Down Expand Up @@ -1054,7 +1053,7 @@ func TestSkipHooksOnManaged(t *testing.T) {
}

func TestGetKnownHostFromURL(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "temptest")
tmpfile, err := os.CreateTemp("", "temptest")
if err != nil {
t.Error("error creating temp file")
}
Expand Down Expand Up @@ -1197,7 +1196,7 @@ tYny6pJJNYEhf7HPmb2O3zBuuqsCC0O2SHrgFYH350zA4To9Ez5nifkZ0CBx0pn9jWn02V
}

// Create Temp directory
tempDir, err := ioutil.TempDir("", "gitrepo")
tempDir, err := os.MkdirTemp("", "gitrepo")
if err != nil {
t.Error(err, " unable to create temp dir to clone repo")
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/utils/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package utils
import (
"crypto/sha256"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"

Expand Down Expand Up @@ -62,7 +62,7 @@ func ConvertLabels(labelSelector *metav1.LabelSelector) (labels.Selector, error)
}

func GetComponentNamespace() (string, error) {
nsBytes, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace")
nsBytes, err := os.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace")
if err != nil {
return "open-cluster-management-agent-addon", err
}
Expand All @@ -72,7 +72,7 @@ func GetComponentNamespace() (string, error) {

// GetCheckSum generates a checksum of a kube config file
func GetCheckSum(kubeconfigfile string) ([32]byte, error) {
content, err := ioutil.ReadFile(filepath.Clean(kubeconfigfile))
content, err := os.ReadFile(filepath.Clean(kubeconfigfile))
if err != nil {
return [32]byte{}, fmt.Errorf("read %s failed, %w", kubeconfigfile, err)
}
Expand Down
Loading

0 comments on commit 53f1875

Please sign in to comment.