Skip to content

Commit

Permalink
Replace uses of the io/ioutil package (googleforgames#2752)
Browse files Browse the repository at this point in the history
* Deprecate io/ioutil
* Remove unnecessary linter rule
  • Loading branch information
gongmax authored Sep 26, 2022
1 parent c1b863e commit dc3eb37
Show file tree
Hide file tree
Showing 20 changed files with 44 additions and 52 deletions.
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,3 @@ issues:
exclude-use-default: false
exclude:
- Using the variable on range scope .* in function literal
- "SA1019: \"io/ioutil\" has been deprecated since Go 1.16"
11 changes: 5 additions & 6 deletions cmd/allocator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
Expand Down Expand Up @@ -574,17 +573,17 @@ func getCACertPool(path string) (*x509.CertPool, error) {
// Add all certificates under client-certs path because there could be multiple clusters
// and all client certs should be added.
caCertPool := x509.NewCertPool()
filesInfo, err := ioutil.ReadDir(path)
dirEntries, err := os.ReadDir(path)
if err != nil {
return nil, fmt.Errorf("error reading certs from dir %s: %s", path, err.Error())
}

for _, file := range filesInfo {
if !strings.HasSuffix(file.Name(), ".crt") && !strings.HasSuffix(file.Name(), ".pem") {
for _, dirEntry := range dirEntries {
if !strings.HasSuffix(dirEntry.Name(), ".crt") && !strings.HasSuffix(dirEntry.Name(), ".pem") {
continue
}
certFile := filepath.Join(path, file.Name())
caCert, err := ioutil.ReadFile(certFile)
certFile := filepath.Join(path, dirEntry.Name())
caCert, err := os.ReadFile(certFile)
if err != nil {
logger.Errorf("CA cert is not readable or missing: %s", err.Error())
continue
Expand Down
8 changes: 4 additions & 4 deletions examples/allocator-client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"crypto/x509"
"flag"
"fmt"
"io/ioutil"
"os"

pb "agones.dev/agones/pkg/allocation/go"
"github.com/pkg/errors"
Expand All @@ -40,15 +40,15 @@ func main() {
flag.Parse()

endpoint := *externalIP + ":" + *port
cert, err := ioutil.ReadFile(*certFile)
cert, err := os.ReadFile(*certFile)
if err != nil {
panic(err)
}
key, err := ioutil.ReadFile(*keyFile)
key, err := os.ReadFile(*keyFile)
if err != nil {
panic(err)
}
cacert, err := ioutil.ReadFile(*cacertFile)
cacert, err := os.ReadFile(*cacertFile)
if err != nil {
panic(err)
}
Expand Down
5 changes: 2 additions & 3 deletions examples/autoscaler-webhook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//Autoscaler webhook server which handles FleetAutoscaleReview json payload
// Autoscaler webhook server which handles FleetAutoscaleReview json payload
package main

import (
"encoding/json"
"flag"
"io"
"io/ioutil"
"math"
"net/http"
"os"
Expand Down Expand Up @@ -137,7 +136,7 @@ func handleAutoscale(w http.ResponseWriter, r *http.Request) {
}

var faReq autoscalingv1.FleetAutoscaleReview
res, err := ioutil.ReadAll(r.Body)
res, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/fleetautoscalers/fleetautoscalers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"crypto/x509"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"math"
"net/http"
"net/url"
Expand Down Expand Up @@ -173,7 +173,7 @@ func applyWebhookPolicy(w *autoscalingv1.WebhookPolicy, f *agonesv1.Fleet) (repl
if res.StatusCode != http.StatusOK {
return 0, false, fmt.Errorf("bad status code %d from the server: %s", res.StatusCode, u.String())
}
result, err := ioutil.ReadAll(res.Body)
result, err := io.ReadAll(res.Body)
if err != nil {
return 0, false, err
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/fleetautoscalers/fleetautoscalers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
Expand All @@ -46,7 +45,7 @@ func (t testServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {

var faRequest autoscalingv1.FleetAutoscaleReview

res, err := ioutil.ReadAll(r.Body)
res, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
Expand Down
4 changes: 2 additions & 2 deletions pkg/gameserverallocations/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package gameserverallocations

import (
"context"
"io/ioutil"
"io"
"mime"
"net/http"
"time"
Expand Down Expand Up @@ -172,7 +172,7 @@ func (c *Controller) allocationDeserialization(r *http.Request, namespace string
return gsa, errors.New("Could not find deserializer")
}

b, err := ioutil.ReadAll(r.Body)
b, err := io.ReadAll(r.Body)
if err != nil {
return gsa, errors.Wrap(err, "could not read body")
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/sdkserver/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package sdkserver
import (
"context"
"io"
"io/ioutil"
"net/http"
"testing"
"time"
Expand All @@ -42,7 +41,7 @@ func testHTTPHealth(t *testing.T, url string, expectedResponse string, expectedS
assert.NotNil(t, resp)
if resp != nil {
defer resp.Body.Close() // nolint: errcheck
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
assert.Nil(t, err, "(%s) read response error should be nil: %v", url, err)
assert.Equal(t, expectedStatus, resp.StatusCode, "url: %s", url)
assert.Equal(t, []byte(expectedResponse), body, "(%s) response body should be '%s'", url, expectedResponse)
Expand Down
5 changes: 2 additions & 3 deletions pkg/sdkserver/localsdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strconv"
"sync"
Expand Down Expand Up @@ -309,7 +308,7 @@ func TestLocalSDKServerWatchGameServer(t *testing.T) {
j, err := json.Marshal(fixture)
assert.Nil(t, err)

err = ioutil.WriteFile(path, j, os.ModeDevice)
err = os.WriteFile(path, j, os.ModeDevice)
assert.Nil(t, err)

assertWatchUpdate(t, stream, "bar", func(gs *sdk.GameServer) interface{} {
Expand Down Expand Up @@ -705,7 +704,7 @@ func TestAlphaSDKConformanceFunctionality(t *testing.T) {
}

func gsToTmpFile(gs *agonesv1.GameServer) (string, error) {
file, err := ioutil.TempFile(os.TempDir(), "gameserver-")
file, err := os.CreateTemp(os.TempDir(), "gameserver-")
if err != nil {
return file.Name(), err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/util/apiserver/apiserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package apiserver

import (
"encoding/json"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -148,7 +148,7 @@ func TestAPIServerAddAPIResourceDiscovery(t *testing.T) {
assert.Equal(t, "application/vnd.kubernetes.protobuf", resp.Header.Get("Content-Type"))

list := &metav1.APIResourceList{}
b, err := ioutil.ReadAll(resp.Body)
b, err := io.ReadAll(resp.Body)
assert.NoError(t, err)

info, ok := k8sruntime.SerializerInfoForMediaType(Codecs.SupportedMediaTypes(), "application/vnd.kubernetes.protobuf")
Expand Down
4 changes: 2 additions & 2 deletions pkg/util/https/https.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package https

import (
"io/ioutil"
"io"
"net/http"

"agones.dev/agones/pkg/util/runtime"
Expand All @@ -30,7 +30,7 @@ type ErrorHandlerFunc func(http.ResponseWriter, *http.Request) error
// FourZeroFour is the standard 404 handler.
func FourZeroFour(logger *logrus.Entry, w http.ResponseWriter, r *http.Request) {
f := ErrorHTTPHandler(logger, func(writer http.ResponseWriter, request *http.Request) error {
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
return errors.Wrap(err, "error in default handler")
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/util/webhooks/webhooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package webhooks
import (
"bytes"
"encoding/json"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -217,7 +217,7 @@ func TestWebHookFleetValidationHandler(t *testing.T) {
assert.Nil(t, err)
defer resp.Body.Close() // nolint: errcheck
assert.Equal(t, http.StatusOK, resp.StatusCode)
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
assert.Nil(t, err)

expected := "cannot unmarshal bool into Go struct field Container.spec.template.spec.template.spec.containers.name of type string"
Expand Down
4 changes: 2 additions & 2 deletions pkg/util/workerqueue/workerqueue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package workerqueue

import (
"context"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -139,7 +139,7 @@ func TestWorkQueueHealthCheck(t *testing.T) {
return false, nil
}

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
assert.Nil(t, err)
assert.Equal(t, status, resp.StatusCode)
assert.Equal(t, []byte("{}\n"), body)
Expand Down
4 changes: 2 additions & 2 deletions site/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package main

import (
"bytes"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"sort"
Expand Down Expand Up @@ -111,7 +111,7 @@ func TestHandler(t *testing.T) {
t.Errorf("%s: http.Get: %v", test.name, err)
continue
}
data, err := ioutil.ReadAll(resp.Body)
data, err := io.ReadAll(resp.Body)
if err != nil {
t.Errorf("Could not read all: %s", err)
continue
Expand Down
3 changes: 1 addition & 2 deletions site/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package main

import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
Expand All @@ -40,7 +39,7 @@ func main() {
}
log.Printf("loading: %s", configPath)

vanity, err := ioutil.ReadFile(configPath)
vanity, err := os.ReadFile(configPath)
if err != nil {
log.Fatal(err)
}
Expand Down
6 changes: 3 additions & 3 deletions test/e2e/allocator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"encoding/json"
"encoding/pem"
"fmt"
"io/ioutil"
"io"
"math/big"
"net"
"net/http"
Expand Down Expand Up @@ -291,7 +291,7 @@ func TestRestAllocatorWithDeprecatedRequired(t *testing.T) {
logrus.WithError(err).Info("failed Allocate rest request")
return false, nil
}
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
logrus.WithError(err).Info("failed to read Allocate response body")
return false, nil
Expand Down Expand Up @@ -355,7 +355,7 @@ func TestRestAllocatorWithSelectors(t *testing.T) {
logrus.WithError(err).Info("failed Allocate rest request")
return false, nil
}
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
logrus.WithError(err).Info("failed to read Allocate response body")
return false, nil
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/gameserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net"
"os"
"os/exec"
"strings"
"sync/atomic"
Expand Down Expand Up @@ -928,7 +928,7 @@ spec:
- name: simple-game-server
image: gcr.io/agones-images/simple-game-server:0.14
`
err := ioutil.WriteFile("/tmp/invalid.yaml", []byte(gsYaml), 0o644)
err := os.WriteFile("/tmp/invalid.yaml", []byte(gsYaml), 0o644)
require.NoError(t, err)

cmd := exec.Command("kubectl", "apply", "-f", "/tmp/invalid.yaml")
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/ping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package e2e
import (
"context"
"fmt"
"io/ioutil"
"io"
"net/http"
"testing"

Expand Down Expand Up @@ -52,7 +52,7 @@ func TestPingHTTP(t *testing.T) {
defer response.Body.Close() // nolint: errcheck

assert.Equal(t, http.StatusOK, response.StatusCode)
body, err := ioutil.ReadAll(response.Body)
body, err := io.ReadAll(response.Body)
assert.Nil(t, err)
assert.Equal(t, []byte("ok"), body)
}
Expand Down
8 changes: 4 additions & 4 deletions test/load/allocation/grpc/allocationload.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"crypto/x509"
"flag"
"fmt"
"io/ioutil"
"os"
"sync"
"time"

Expand All @@ -45,15 +45,15 @@ func main() {
flag.Parse()

endpoint := *externalIP + ":" + *port
cert, err := ioutil.ReadFile(*certFile)
cert, err := os.ReadFile(*certFile)
if err != nil {
panic(err)
}
key, err := ioutil.ReadFile(*keyFile)
key, err := os.ReadFile(*keyFile)
if err != nil {
panic(err)
}
cacert, err := ioutil.ReadFile(*cacertFile)
cacert, err := os.ReadFile(*cacertFile)
if err != nil {
panic(err)
}
Expand Down
Loading

0 comments on commit dc3eb37

Please sign in to comment.