From 5d35b38943bbb3fae5fbc4b80649a1a0cf269f03 Mon Sep 17 00:00:00 2001 From: Soule BA Date: Tue, 14 Jun 2022 12:21:52 +0200 Subject: [PATCH] Change response body read and close defer order Deferred function calls are executed in Last In First Out order after the surrounding function returns, so we should make sure io.Copy() is executed before close() Signed-off-by: Soule BA --- controllers/imagerepository_controller.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/controllers/imagerepository_controller.go b/controllers/imagerepository_controller.go index 35f28355..6ccedd3a 100644 --- a/controllers/imagerepository_controller.go +++ b/controllers/imagerepository_controller.go @@ -294,8 +294,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() + defer io.Copy(io.Discard, response.Body) if response.StatusCode != http.StatusOK { return authConfig, fmt.Errorf("unexpected status from metadata service: %s", response.Status)