From 2a88b5d2f7318ffbcd7165b4f93a9cf244c5becf Mon Sep 17 00:00:00 2001 From: "Alex Ellis (OpenFaaS Ltd)" Date: Tue, 22 Aug 2023 18:29:28 +0100 Subject: [PATCH] Remove ioutil usage This has been deprecated in Go for some time, in favour of the io package. Signed-off-by: Alex Ellis (OpenFaaS Ltd) --- gateway/handlers/alerthandler.go | 4 ++-- gateway/handlers/forwarding_proxy_test.go | 10 +++++----- gateway/handlers/logs_test.go | 6 +++--- gateway/handlers/queue_proxy.go | 4 ++-- gateway/metrics/add_metrics.go | 4 ++-- gateway/metrics/exporter.go | 6 +++--- gateway/metrics/prometheus_query.go | 4 ++-- gateway/scaling/ranges.go | 3 ++- 8 files changed, 21 insertions(+), 20 deletions(-) diff --git a/gateway/handlers/alerthandler.go b/gateway/handlers/alerthandler.go index 55f95744..795557ec 100644 --- a/gateway/handlers/alerthandler.go +++ b/gateway/handlers/alerthandler.go @@ -6,7 +6,7 @@ package handlers import ( "encoding/json" "fmt" - "io/ioutil" + "io" "log" "math" "net/http" @@ -27,7 +27,7 @@ func MakeAlertHandler(service scaling.ServiceQuery, defaultNamespace string) htt defer r.Body.Close() - body, err := ioutil.ReadAll(r.Body) + body, err := io.ReadAll(r.Body) if err != nil { w.WriteHeader(http.StatusBadRequest) w.Write([]byte("Unable to read alert.")) diff --git a/gateway/handlers/forwarding_proxy_test.go b/gateway/handlers/forwarding_proxy_test.go index 7f5dc2ad..c8e756c9 100644 --- a/gateway/handlers/forwarding_proxy_test.go +++ b/gateway/handlers/forwarding_proxy_test.go @@ -6,7 +6,7 @@ package handlers import ( "bytes" "fmt" - "io/ioutil" + "io" "net/http" "net/url" "testing" @@ -33,7 +33,7 @@ func Test_buildUpstreamRequest_Body_Method_Query(t *testing.T) { t.Fail() } - upstreamBytes, _ := ioutil.ReadAll(upstream.Body) + upstreamBytes, _ := io.ReadAll(upstream.Body) if string(upstreamBytes) != string(srcBytes) { t.Errorf("Body - want: %s, got: %s", string(upstreamBytes), string(srcBytes)) @@ -212,7 +212,7 @@ func Test_buildUpstreamRequest_WithPathNoQuery(t *testing.T) { t.Fail() } - upstreamBytes, _ := ioutil.ReadAll(upstream.Body) + upstreamBytes, _ := io.ReadAll(upstream.Body) if string(upstreamBytes) != string(srcBytes) { t.Errorf("Body - want: %s, got: %s", string(upstreamBytes), string(srcBytes)) @@ -268,7 +268,7 @@ func Test_buildUpstreamRequest_WithNoPathNoQuery(t *testing.T) { t.Fail() } - upstreamBytes, _ := ioutil.ReadAll(upstream.Body) + upstreamBytes, _ := io.ReadAll(upstream.Body) if string(upstreamBytes) != string(srcBytes) { t.Errorf("Body - want: %s, got: %s", string(upstreamBytes), string(srcBytes)) @@ -322,7 +322,7 @@ func Test_buildUpstreamRequest_WithPathAndQuery(t *testing.T) { t.Fail() } - upstreamBytes, _ := ioutil.ReadAll(upstream.Body) + upstreamBytes, _ := io.ReadAll(upstream.Body) if string(upstreamBytes) != string(srcBytes) { t.Errorf("Body - want: %s, got: %s", string(upstreamBytes), string(srcBytes)) diff --git a/gateway/handlers/logs_test.go b/gateway/handlers/logs_test.go index 32862455..51755a2b 100644 --- a/gateway/handlers/logs_test.go +++ b/gateway/handlers/logs_test.go @@ -3,7 +3,7 @@ package handlers import ( "context" "fmt" - "io/ioutil" + "io" "net/http" "net/http/httptest" "net/url" @@ -52,7 +52,7 @@ func Test_logsProxyDoesNotLeakGoroutinesWhenProviderClosesConnection(t *testing. t.Fatalf("unexpected error sending log request: %s", err) } - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { t.Fatalf("unexpected error reading the response body: %s", err) } @@ -126,7 +126,7 @@ func Test_logsProxyDoesNotLeakGoroutinesWhenClientClosesConnection(t *testing.T) go func() { defer resp.Body.Close() defer close(errCh) - _, err := ioutil.ReadAll(resp.Body) + _, err := io.ReadAll(resp.Body) errCh <- err }() cancel() diff --git a/gateway/handlers/queue_proxy.go b/gateway/handlers/queue_proxy.go index 1a140bd2..13c2b67c 100644 --- a/gateway/handlers/queue_proxy.go +++ b/gateway/handlers/queue_proxy.go @@ -5,7 +5,7 @@ package handlers import ( "fmt" - "io/ioutil" + "io" "log" "net/http" "net/url" @@ -27,7 +27,7 @@ func MakeQueuedProxy(metrics metrics.MetricOptions, queuer ftypes.RequestQueuer, defer r.Body.Close() var err error - body, err = ioutil.ReadAll(r.Body) + body, err = io.ReadAll(r.Body) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return diff --git a/gateway/metrics/add_metrics.go b/gateway/metrics/add_metrics.go index 3c35652f..18e13a05 100644 --- a/gateway/metrics/add_metrics.go +++ b/gateway/metrics/add_metrics.go @@ -3,7 +3,7 @@ package metrics import ( "encoding/json" "fmt" - "io/ioutil" + "io" "log" "net/http" "net/http/httptest" @@ -28,7 +28,7 @@ func AddMetricsHandler(handler http.HandlerFunc, prometheusQuery PrometheusQuery } defer upstreamCall.Body.Close() - upstreamBody, _ := ioutil.ReadAll(upstreamCall.Body) + upstreamBody, _ := io.ReadAll(upstreamCall.Body) if recorder.Code != http.StatusOK { log.Printf("List functions responded with code %d, body: %s", diff --git a/gateway/metrics/exporter.go b/gateway/metrics/exporter.go index 3e4a9bb2..e8ced9dd 100644 --- a/gateway/metrics/exporter.go +++ b/gateway/metrics/exporter.go @@ -7,7 +7,7 @@ package metrics import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net" "net/http" "net/url" @@ -159,7 +159,7 @@ func (e *Exporter) getFunctions(endpointURL url.URL, namespace string) ([]types. return services, err } - bytesOut, readErr := ioutil.ReadAll(res.Body) + bytesOut, readErr := io.ReadAll(res.Body) if readErr != nil { return services, readErr } @@ -193,7 +193,7 @@ func (e *Exporter) getNamespaces(endpointURL url.URL) ([]string, error) { return namespaces, nil } - bytesOut, readErr := ioutil.ReadAll(res.Body) + bytesOut, readErr := io.ReadAll(res.Body) if readErr != nil { return namespaces, readErr } diff --git a/gateway/metrics/prometheus_query.go b/gateway/metrics/prometheus_query.go index 5b8fd2a5..59ae10d5 100644 --- a/gateway/metrics/prometheus_query.go +++ b/gateway/metrics/prometheus_query.go @@ -3,7 +3,7 @@ package metrics import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" ) @@ -44,7 +44,7 @@ func (q PrometheusQuery) Fetch(query string) (*VectorQueryResponse, error) { defer res.Body.Close() } - bytesOut, readErr := ioutil.ReadAll(res.Body) + bytesOut, readErr := io.ReadAll(res.Body) if readErr != nil { return nil, readErr } diff --git a/gateway/scaling/ranges.go b/gateway/scaling/ranges.go index 33850249..209886d2 100644 --- a/gateway/scaling/ranges.go +++ b/gateway/scaling/ranges.go @@ -3,6 +3,7 @@ package scaling import ( "bytes" "encoding/json" + "io" "io/ioutil" "net/http" @@ -43,7 +44,7 @@ func MakeHorizontalScalingHandler(next http.HandlerFunc) http.HandlerFunc { return } - body, err := ioutil.ReadAll(r.Body) + body, err := io.ReadAll(r.Body) if err != nil { http.Error(w, "Error reading request body", http.StatusBadRequest) return