From 51c97e259ff74ca3688dd294a521e90e1991c90f Mon Sep 17 00:00:00 2001 From: Somtochi Onyekwere Date: Thu, 20 Jan 2022 11:30:58 +0100 Subject: [PATCH] Discard and close body regardless of status code Signed-off-by: Somtochi Onyekwere --- controllers/imagerepository_controller.go | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/controllers/imagerepository_controller.go b/controllers/imagerepository_controller.go index ea8c418e..9f7690c2 100644 --- a/controllers/imagerepository_controller.go +++ b/controllers/imagerepository_controller.go @@ -280,6 +280,8 @@ func getGCRLoginAuth(ctx context.Context) (authn.AuthConfig, error) { if err != nil { return authConfig, err } + defer io.Copy(io.Discard, response.Body) + defer response.Body.Close() if response.StatusCode != http.StatusOK { return authConfig, fmt.Errorf("unexpected status from metadata service: %s", response.Status) @@ -291,14 +293,6 @@ func getGCRLoginAuth(ctx context.Context) (authn.AuthConfig, error) { return authConfig, err } - if _, err := io.Copy(io.Discard, response.Body); err != nil { - return authConfig, err - } - - if err := response.Body.Close(); err != nil { - return authConfig, err - } - authConfig = authn.AuthConfig{ Username: "oauth2accesstoken", Password: accessToken.AccessToken,