From ba263af48f9b887e6723157f07e01bccb8642a77 Mon Sep 17 00:00:00 2001 From: Slavik Panasovets Date: Tue, 13 Sep 2022 21:39:29 +0000 Subject: [PATCH] Fix errcheck linter for most of non-test files Add handling errors where it was missing --- .golangci.yaml | 7 ------ .../server-with-metrics.go | 10 ++++++-- cmd/echo/app/handlers.go | 23 +++++++++++++++---- cmd/fuzzer/fuzzer.go | 12 ++++------ cmd/glbc/app/handlers.go | 15 +++++++++--- cmd/workload-daemon/main.go | 5 +++- 6 files changed, 48 insertions(+), 24 deletions(-) diff --git a/.golangci.yaml b/.golangci.yaml index 9fbc3997d7..90580b7192 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -17,12 +17,5 @@ issues: linters-settings: errcheck: exclude-functions: - - fmt.Fprintf - - fmt.Fprint - - (net/http.ResponseWriter).Write - - (*net/http.Server).Shutdown - (*flag.FlagSet).Parse - - (*os.File).Close - - (io.Closer).Close - - (flag.Value).Set - k8s.io/apimachinery/pkg/util/wait.PollUntil \ No newline at end of file diff --git a/cmd/404-server-with-metrics/server-with-metrics.go b/cmd/404-server-with-metrics/server-with-metrics.go index a8ca4834f1..617e2b0a11 100644 --- a/cmd/404-server-with-metrics/server-with-metrics.go +++ b/cmd/404-server-with-metrics/server-with-metrics.go @@ -259,6 +259,12 @@ func gracefulShutdown(s *server) { klog.Infof("received /shutdown message, doing a graceful shutdown of http and metrics servers: \n") } - s.httpServer.Shutdown(context.Background()) - s.metricsServer.Shutdown(context.Background()) + err := s.httpServer.Shutdown(context.Background()) + if err != nil { + klog.Errorf("Error shutting down httpServer: %v", err) + } + err = s.metricsServer.Shutdown(context.Background()) + if err != nil { + klog.Errorf("Error shutting down metricsServer: %v", err) + } } diff --git a/cmd/echo/app/handlers.go b/cmd/echo/app/handlers.go index d082bf7b8f..90156e89f6 100644 --- a/cmd/echo/app/handlers.go +++ b/cmd/echo/app/handlers.go @@ -67,7 +67,10 @@ func RunHTTPServer(ctx context.Context) { } <-ctx.Done() - server.Shutdown(ctx) + err = server.Shutdown(ctx) + if err != nil { + klog.Infof("Error on server shutdown %v", err) + } }() go func() { @@ -82,7 +85,11 @@ func RunHTTPServer(ctx context.Context) { } <-ctx.Done() - server.Shutdown(ctx) + + err = server.Shutdown(ctx) + if err != nil { + klog.Errorf("Error on server shutdown %v", err) + } }() <-ctx.Done() @@ -90,7 +97,11 @@ func RunHTTPServer(ctx context.Context) { func healthCheck(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) - w.Write([]byte("health: OK")) + _, err := w.Write([]byte("health: OK")) + if err != nil { + klog.Errorf("Error writing bytes: %v, UserAgent: %v, RemoteAddr: %v", err, r.UserAgent(), r.RemoteAddr) + return + } klog.V(3).Infof("healthcheck: %v, %v, %v", time.Now(), r.UserAgent(), r.RemoteAddr) } @@ -123,7 +134,11 @@ func echo(w http.ResponseWriter, r *http.Request) { return } - w.Write(processedData) + _, err = w.Write(processedData) + if err != nil { + klog.Errorf("Error writing data: %v, UserAgent: %v, RemoteAddr: %v", err, r.UserAgent(), r.RemoteAddr) + return + } klog.V(3).Infof("echo: %v, %v, %v", time.Now(), r.UserAgent(), r.RemoteAddr) } diff --git a/cmd/fuzzer/fuzzer.go b/cmd/fuzzer/fuzzer.go index 7a2bc46e98..b65110476f 100644 --- a/cmd/fuzzer/fuzzer.go +++ b/cmd/fuzzer/fuzzer.go @@ -24,12 +24,7 @@ import ( _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" _ "k8s.io/client-go/plugin/pkg/client/auth/oidc" "k8s.io/ingress-gce/cmd/fuzzer/app" -) - -var ( - kubeconfig *string - ingressNamespace *string - ingressName *string + "k8s.io/klog/v2" ) func main() { @@ -43,7 +38,10 @@ func main() { switch os.Args[1] { case "validate": - app.ValidateFlagSet.Parse(os.Args[2:]) + err := app.ValidateFlagSet.Parse(os.Args[2:]) + if err != nil { + klog.Errorf("app.ValidateFlagSet.Parse(%v) returned error: %v", os.Args[2:], err) + } default: flag.Usage() os.Exit(1) diff --git a/cmd/glbc/app/handlers.go b/cmd/glbc/app/handlers.go index 94305149f7..22c665761e 100644 --- a/cmd/glbc/app/handlers.go +++ b/cmd/glbc/app/handlers.go @@ -85,11 +85,17 @@ func healthCheckHandler(checker func() context.HealthCheckResults) http.HandlerF } if s.Len() == 0 { - w.Write([]byte("OK - no running controllers")) + _, err := w.Write([]byte("OK - no running controllers")) + if err != nil { + klog.Errorf("Error writing bytes: %v", err) + } return } - w.Write([]byte(s.String())) + _, err := w.Write([]byte(s.String())) + if err != nil { + klog.Errorf("Error writing bytes: %v", err) + } return } } @@ -129,8 +135,11 @@ func putFlag(w http.ResponseWriter, r *http.Request) { } func setVerbosity(v string) { - flag.Lookup("v").Value.Set(v) klog.V(0).Infof("Setting verbosity level to %q", v) + err := flag.Lookup("v").Value.Set(v) + if err != nil { + klog.Errorf("flag.Lookup(\"v\").Value.Set(%v) returned error: %v", v, err) + } } func getFlagPage(w http.ResponseWriter, r *http.Request) { diff --git a/cmd/workload-daemon/main.go b/cmd/workload-daemon/main.go index 098cd0d82f..ff276f21a8 100644 --- a/cmd/workload-daemon/main.go +++ b/cmd/workload-daemon/main.go @@ -39,7 +39,10 @@ func main() { outputHelp() return } - cmdSet.Parse(os.Args[2:]) + err := cmdSet.Parse(os.Args[2:]) + if err != nil { + klog.Errorf("Error parsing os.Args: %v", err) + } if *provider != "gce" { klog.Fatalf("Current implementation only supports gce provider.") }