Repository: Azure/ARO-RP
Pull request #2426 (2022-09-28 08:06:41)

ioutil removal
### Which issue this PR addresses:
Follow up to #2414 to prepare for golangci-lint upgrade (https://go.dev/doc/go1.16#ioutil)
Also related to #2423
<!--
Please include a link to the ADO work item as well as any GitHub issues.

Usage: `Fixes #<GitHub issue number>`, or `Fixes (paste link of issue)`.
-->

### What this PR does / why we need it:

<!--
Include a brief summary of what the PR is intended to accomplish and how the PR
does it. (2-3 sentences)
-->

### Test plan for issue:

<!--
How did you test that this PR works?

- Are there unit tests?
- Are there integration/e2e tests?
- If it is not possible to write automated tests, explain why and document how
  to manually test and verify the feature.
-->

### Is there any documentation that needs to be updated for this PR?

<!--
- If yes and the docs are in GitHub, include doc updates in the PR.
- If yes and the docs are not in GitHub (i.e. ADO wiki), include a link to the
  docs.
- If no, explain why (e.g. "tech debt cleanup, N/A").
-->



Patch:
diff --git a/hack/fakecluster/fakecluster.go b/hack/fakecluster/fakecluster.go
index 90f7b5436..58070393b 100644
--- a/hack/fakecluster/fakecluster.go
+++ b/hack/fakecluster/fakecluster.go
@@ -10,10 +10,10 @@ import (
 	"encoding/json"
 	"flag"
 	"fmt"
-	"io/ioutil"
 	"log"
 	"net"
 	"net/http"
+	"os"
 	"time"
 
 	"github.com/gorilla/mux"
@@ -38,12 +38,12 @@ func run(ctx context.Context, l *logrus.Entry) error {
 
 	flag.Parse()
 
-	cert, err := ioutil.ReadFile(*certFile)
+	cert, err := os.ReadFile(*certFile)
 	if err != nil {
 		panic(err)
 	}
 
-	b, err := ioutil.ReadFile(*keyFile)
+	b, err := os.ReadFile(*keyFile)
 	if err != nil {
 		panic(err)
 	}
diff --git a/hack/gendiscoverycache/gendiscoverycache.go b/hack/gendiscoverycache/gendiscoverycache.go
index 01dbefa0d..d183572d2 100644
--- a/hack/gendiscoverycache/gendiscoverycache.go
+++ b/hack/gendiscoverycache/gendiscoverycache.go
@@ -5,7 +5,6 @@ package main
 
 import (
 	"encoding/json"
-	"io/ioutil"
 	"os"
 	"path/filepath"
 	"sort"
@@ -53,7 +52,7 @@ func canonicalizeAssets() error {
 }
 
 func formatServerGroups(path string) error {
-	b, err := ioutil.ReadFile(path)
+	b, err := os.ReadFile(path)
 	if err != nil {
 		return err
 	}
@@ -70,11 +69,11 @@ func formatServerGroups(path string) error {
 		return err
 	}
 
-	return ioutil.WriteFile(path, append(b, '\n'), 0666)
+	return os.WriteFile(path, append(b, '\n'), 0666)
 }
 
 func canonicalizeServerResources(path string) error {
-	b, err := ioutil.ReadFile(path)
+	b, err := os.ReadFile(path)
 	if err != nil {
 		return err
 	}
@@ -101,5 +100,5 @@ func canonicalizeServerResources(path string) error {
 		return err
 	}
 
-	return ioutil.WriteFile(path, append(b, '\n'), 0666)
+	return os.WriteFile(path, append(b, '\n'), 0666)
 }
diff --git a/hack/gendiscoverycache/main.go b/hack/gendiscoverycache/main.go
index 9ba2964d2..f87f323d0 100644
--- a/hack/gendiscoverycache/main.go
+++ b/hack/gendiscoverycache/main.go
@@ -5,7 +5,6 @@ package main
 
 import (
 	"context"
-	"io/ioutil"
 	"os"
 	"path/filepath"
 
@@ -61,7 +60,7 @@ func writeVersion(ctx context.Context, restconfig *rest.Config) error {
 	}
 
 	versionPath := filepath.Join(discoveryCacheDir, "assets_version")
-	return ioutil.WriteFile(versionPath, []byte(clusterVersion.String()+"\n"), 0666)
+	return os.WriteFile(versionPath, []byte(clusterVersion.String()+"\n"), 0666)
 }
 
 func main() {
diff --git a/hack/genkey/genkey.go b/hack/genkey/genkey.go
index 76f4ae089..bff5be8a2 100644
--- a/hack/genkey/genkey.go
+++ b/hack/genkey/genkey.go
@@ -10,7 +10,6 @@ import (
 	"encoding/pem"
 	"flag"
 	"fmt"
-	"io/ioutil"
 	"os"
 
 	utiltls "github.com/Azure/ARO-RP/pkg/util/tls"
@@ -28,7 +27,7 @@ func run(name string) error {
 	var signingCert *x509.Certificate
 
 	if *keyFile != "" {
-		b, err := ioutil.ReadFile(*keyFile)
+		b, err := os.ReadFile(*keyFile)
 		if err != nil {
 			return err
 		}
@@ -40,7 +39,7 @@ func run(name string) error {
 	}
 
 	if *certFile != "" {
-		b, err := ioutil.ReadFile(*certFile)
+		b, err := os.ReadFile(*certFile)
 		if err != nil {
 			return err
 		}
@@ -57,13 +56,13 @@ func run(name string) error {
 	}
 
 	// key in der format
-	err = ioutil.WriteFile(name+".key", x509.MarshalPKCS1PrivateKey(key), 0600)
+	err = os.WriteFile(name+".key", x509.MarshalPKCS1PrivateKey(key), 0600)
 	if err != nil {
 		return err
 	}
 
 	// cert in der format
-	err = ioutil.WriteFile(name+".crt", cert[0].Raw, 0666)
+	err = os.WriteFile(name+".crt", cert[0].Raw, 0666)
 	if err != nil {
 		return err
 	}
@@ -85,7 +84,7 @@ func run(name string) error {
 	}
 
 	// key and cert in PKCS#8 PEM format for Azure Key Vault.
-	return ioutil.WriteFile(name+".pem", buf.Bytes(), 0600)
+	return os.WriteFile(name+".pem", buf.Bytes(), 0600)
 }
 
 func usage() {
diff --git a/hack/licenses/main.go b/hack/licenses/main.go
index e4e9243c6..674504a69 100644
--- a/hack/licenses/main.go
+++ b/hack/licenses/main.go
@@ -8,7 +8,6 @@ import (
 	"errors"
 	"flag"
 	"fmt"
-	"io/ioutil"
 	"log"
 	"os"
 	"path/filepath"
@@ -67,7 +66,7 @@ func applyGoLicense() error {
 			return nil
 		}
 
-		b, err := ioutil.ReadFile(path)
+		b, err := os.ReadFile(path)
 		if err != nil {
 			return err
 		}
@@ -86,7 +85,7 @@ func applyGoLicense() error {
 			bb = append(bb, goLicense...)
 			bb = append(bb, b[i:]...)
 
-			err = ioutil.WriteFile(path, bb, 0666)
+			err = os.WriteFile(path, bb, 0666)
 			if err != nil {
 				return err
 			}
@@ -115,7 +114,7 @@ func applyPythonLicense() error {
 			return nil
 		}
 
-		b, err := ioutil.ReadFile(path)
+		b, err := os.ReadFile(path)
 		if err != nil {
 			return err
 		}
@@ -136,7 +135,7 @@ func applyPythonLicense() error {
 				bb = append(bb, b...)
 			}
 
-			err = ioutil.WriteFile(path, bb, 0666)
+			err = os.WriteFile(path, bb, 0666)
 			if err != nil {
 				return err
 			}
@@ -146,7 +145,7 @@ func applyPythonLicense() error {
 	})
 }
 
-//returns the lists of files that don't have a license but should
+// returns the lists of files that don't have a license but should
 func validateGoLicenses(ignored map[string]bool, dirs []string) []string {
 	unlicensedFiles := make([]string, 0)
 	for _, dir := range dirs {
@@ -163,7 +162,7 @@ func validateGoLicenses(ignored map[string]bool, dirs []string) []string {
 				return nil
 			}
 
-			b, err := ioutil.ReadFile(path)
+			b, err := os.ReadFile(path)
 			if err != nil {
 				return err
 			}
@@ -182,7 +181,7 @@ func validateGoLicenses(ignored map[string]bool, dirs []string) []string {
 	return unlicensedFiles
 }
 
-//returns the lists of files that don't have a license but should
+// returns the lists of files that don't have a license but should
 func validatePythonLicenses(ignored map[string]bool, dirs []string) []string {
 	unlicensedFiles := make([]string, 0)
 
@@ -204,7 +203,7 @@ func validatePythonLicenses(ignored map[string]bool, dirs []string) []string {
 				return nil
 			}
 
-			b, err := ioutil.ReadFile(path)
+			b, err := os.ReadFile(path)
 			if err != nil {
 				return err
 			}
diff --git a/hack/tunnel/tunnel.go b/hack/tunnel/tunnel.go
index 216a1b193..ae1c618e5 100644
--- a/hack/tunnel/tunnel.go
+++ b/hack/tunnel/tunnel.go
@@ -10,7 +10,6 @@ import (
 	"flag"
 	"fmt"
 	"io"
-	"io/ioutil"
 	"net"
 	"os"
 
@@ -48,7 +47,7 @@ func run(ctx context.Context, log *logrus.Entry) error {
 		return fmt.Errorf("usage: %s IP", os.Args[0])
 	}
 
-	certb, err := ioutil.ReadFile(*certFile)
+	certb, err := os.ReadFile(*certFile)
 	if err != nil {
 		return err
 	}
@@ -61,7 +60,7 @@ func run(ctx context.Context, log *logrus.Entry) error {
 	pool := x509.NewCertPool()
 	pool.AddCert(cert)
 
-	keyb, err := ioutil.ReadFile(*keyFile)
+	keyb, err := os.ReadFile(*keyFile)
 	if err != nil {
 		return err
 	}
@@ -71,12 +70,12 @@ func run(ctx context.Context, log *logrus.Entry) error {
 		return err
 	}
 
-	clientCertb, err := ioutil.ReadFile(*clientCertFile)
+	clientCertb, err := os.ReadFile(*clientCertFile)
 	if err != nil {
 		return err
 	}
 
-	clientKeyb, err := ioutil.ReadFile(*clientKeyFile)
+	clientKeyb, err := os.ReadFile(*clientKeyFile)
 	if err != nil {
 		return err
 	}
diff --git a/pkg/swagger/examples.go b/pkg/swagger/examples.go
index b978e7ef7..a57f6d6f4 100644
--- a/pkg/swagger/examples.go
+++ b/pkg/swagger/examples.go
@@ -5,7 +5,6 @@ package swagger
 
 import (
 	"encoding/json"
-	"io/ioutil"
 	"os"
 
 	"github.com/Azure/ARO-RP/pkg/util/stringutils"
@@ -183,7 +182,7 @@ func (g *generator) generateExamples(outputDir string, s *Swagger) error {
 
 			b = append(b, '\n')
 
-			err = ioutil.WriteFile(outputDir+"/examples/"+op.OperationID+".json", b, 0666)
+			err = os.WriteFile(outputDir+"/examples/"+op.OperationID+".json", b, 0666)
 			if err != nil {
 				return err
 			}
diff --git a/pkg/swagger/swagger.go b/pkg/swagger/swagger.go
index 51d9b7fe2..63c3a256a 100644
--- a/pkg/swagger/swagger.go
+++ b/pkg/swagger/swagger.go
@@ -5,8 +5,8 @@ package swagger
 
 import (
 	"encoding/json"
-	"io/ioutil"
 	"net/http"
+	"os"
 	"reflect"
 
 	"github.com/Azure/ARO-RP/pkg/util/stringutils"
@@ -221,7 +221,7 @@ func Run(api, outputDir string) error {
 		return err
 	}
 
-	return ioutil.WriteFile(outputDir+"/redhatopenshift.json", b, 0666)
+	return os.WriteFile(outputDir+"/redhatopenshift.json", b, 0666)
 }
 
 func deepCopy(v interface{}) (interface{}, error) {


Test patch:
diff --git a/test/e2e/adminapi.go b/test/e2e/adminapi.go
index 782f01486..eb71060cd 100644
--- a/test/e2e/adminapi.go
+++ b/test/e2e/adminapi.go
@@ -9,7 +9,7 @@ import (
 	"crypto/tls"
 	"encoding/json"
 	"errors"
-	"io/ioutil"
+	"io"
 	"net/http"
 	"net/url"
 
@@ -48,7 +48,7 @@ func adminRequest(ctx context.Context, method, path string, params url.Values, i
 		if err != nil {
 			return nil, err
 		}
-		req.Body = ioutil.NopCloser(buf)
+		req.Body = io.NopCloser(buf)
 		req.Header.Set("Content-Type", "application/json")
 	}
 
@@ -72,7 +72,7 @@ func adminRequest(ctx context.Context, method, path string, params url.Values, i
 		return resp, json.NewDecoder(resp.Body).Decode(&out)
 	} else if out != nil && resp.Header.Get("Content-Type") == "text/plain" {
 		strOut := out.(*string)
-		p, err := ioutil.ReadAll(resp.Body)
+		p, err := io.ReadAll(resp.Body)
 		if err == nil {
 			*strOut = string(p)
 		}