From 1f86affe020fce0ee4a2020db3930cbd39198099 Mon Sep 17 00:00:00 2001 From: "eric.bjn" Date: Fri, 30 Apr 2021 11:42:10 +0800 Subject: [PATCH 1/3] fix mod image & registry ci lint --- image/default_image.go | 4 +++- image/util.go | 13 ++++++++++--- registry/tokentransport_test.go | 6 +++--- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/image/default_image.go b/image/default_image.go index 44c7920ec54..f4bc717538c 100644 --- a/image/default_image.go +++ b/image/default_image.go @@ -309,7 +309,9 @@ func (d DefaultImageService) uploadLayers(repo string, layers []v1.Layer, blobs errCh <- err return err } - defer utils.CleanFile(file) + defer func() { + _ = utils.CleanFile(file) + }() if _, err = file.Seek(0, 0); err != nil { errCh <- err return err diff --git a/image/util.go b/image/util.go index aa181d6129a..1a6ea7fc9a3 100644 --- a/image/util.go +++ b/image/util.go @@ -90,7 +90,9 @@ func GetClusterFileFromImageManifest(imageName string) string { func GetClusterFileFromBaseImage(imageName string) string { mountTarget, _ := utils.MkTmpdir() mountUpper, _ := utils.MkTmpdir() - defer utils.CleanDirs(mountTarget, mountUpper) + defer func() { + _ = utils.CleanDirs(mountTarget, mountUpper) + }() if err := NewImageService().PullIfNotExist(imageName); err != nil { return "" @@ -107,9 +109,14 @@ func GetClusterFileFromBaseImage(imageName string) string { } err = driver.Mount(mountTarget, mountUpper, layers...) - defer driver.Unmount(mountTarget) - clusterFile := filepath.Join(mountTarget, "etc", common.DefaultClusterFileName) + if err != nil { + return "" + } + defer func() { + _ = driver.Unmount(mountTarget) + }() + clusterFile := filepath.Join(mountTarget, "etc", common.DefaultClusterFileName) data, err := ioutil.ReadFile(clusterFile) if err != nil { return "" diff --git a/registry/tokentransport_test.go b/registry/tokentransport_test.go index a824a2dddad..1cfb6024cc2 100644 --- a/registry/tokentransport_test.go +++ b/registry/tokentransport_test.go @@ -47,13 +47,13 @@ func oauthFlow(w http.ResponseWriter, r *http.Request) { if strings.HasPrefix(r.URL.Path, "/oauth2/accesstoken") { w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) - w.Write([]byte(`{"access_token":"abcdef1234"}`)) + _, _ = w.Write([]byte(`{"access_token":"abcdef1234"}`)) return } if strings.HasPrefix(r.URL.Path, "/oauth2/token") { w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) - w.Write([]byte(`{"token":"abcdef1234"}`)) + _, _ = w.Write([]byte(`{"token":"abcdef1234"}`)) return } auth := r.Header.Get("authorization") @@ -63,7 +63,7 @@ func oauthFlow(w http.ResponseWriter, r *http.Request) { w.Header().Set("www-authenticate", `Bearer realm="`+authURI+`/oauth2/token",service="my.endpoint.here"`) } w.WriteHeader(http.StatusUnauthorized) - w.Write([]byte(`{"errors":[{"code":"UNAUTHORIZED","message":"authentication required","detail":null}]}`)) + _, _ = w.Write([]byte(`{"errors":[{"code":"UNAUTHORIZED","message":"authentication required","detail":null}]}`)) return } w.WriteHeader(http.StatusOK) From e12ee45feda79f69f31cebb85a0f162f1612e9c1 Mon Sep 17 00:00:00 2001 From: "eric.bjn" Date: Fri, 30 Apr 2021 12:51:35 +0800 Subject: [PATCH 2/3] logger warn when failed to clean file --- image/default_image.go | 8 +++----- utils/compress/compress.go | 2 +- utils/file.go | 14 +++++++++++--- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/image/default_image.go b/image/default_image.go index f4bc717538c..24063c49f6b 100644 --- a/image/default_image.go +++ b/image/default_image.go @@ -275,15 +275,15 @@ func (d DefaultImageService) uploadLayers(repo string, layers []v1.Layer, blobs Action: func(cxt progress.Context) error { var file *os.File defer func() { + //file compress failed, clean file if err != nil { - _ = utils.CleanFile(file) + utils.CleanFile(file) } }() if file, err = compress.Compress(filepath.Join(common.DefaultLayerDir, layer.Hash), "", nil); err != nil { errCh <- err return err - //flow.ShowMessage(shortHex+" "+err.Error(), compressBar) } // pass to next progress task cxt.WithReader(file) @@ -309,9 +309,7 @@ func (d DefaultImageService) uploadLayers(repo string, layers []v1.Layer, blobs errCh <- err return err } - defer func() { - _ = utils.CleanFile(file) - }() + defer utils.CleanFile(file) if _, err = file.Seek(0, 0); err != nil { errCh <- err return err diff --git a/utils/compress/compress.go b/utils/compress/compress.go index 1aa11c0fa5e..6661c9ab33a 100644 --- a/utils/compress/compress.go +++ b/utils/compress/compress.go @@ -43,7 +43,7 @@ func Compress(src, newFolder string, existingFile *os.File) (file *os.File, err defer func() { if err != nil { - _ = utils.CleanFile(file) + utils.CleanFile(file) } }() diff --git a/utils/file.go b/utils/file.go index db748fcb3d2..55de6086c20 100644 --- a/utils/file.go +++ b/utils/file.go @@ -146,12 +146,20 @@ func CopySingleFile(src, dst string) (int64, error) { return nBytes, err } -func CleanFile(file *os.File) (err error) { +func CleanFile(file *os.File) { if file == nil { return } - err = file.Close() - return os.Remove(file.Name()) + // the following operation won't failed regularly, if failed, log it + err := file.Close() + if err != nil { + logger.Warn(err) + } + err = os.Remove(file.Name()) + if err != nil { + logger.Warn(err) + } + return } func CleanDir(dir string) (err error) { From 6145df1873bc6727ba59b64952dcfbeb0e61353e Mon Sep 17 00:00:00 2001 From: "eric.bjn" Date: Fri, 30 Apr 2021 15:02:30 +0800 Subject: [PATCH 3/3] warn after CleanDirs/ Unmount --- image/util.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/image/util.go b/image/util.go index 1a6ea7fc9a3..ef5cd1f4d2a 100644 --- a/image/util.go +++ b/image/util.go @@ -6,6 +6,7 @@ import ( "github.com/opencontainers/go-digest" "gitlab.alibaba-inc.com/seadent/pkg/common" imageUtils "gitlab.alibaba-inc.com/seadent/pkg/image/utils" + "gitlab.alibaba-inc.com/seadent/pkg/logger" v1 "gitlab.alibaba-inc.com/seadent/pkg/types/api/v1" "gitlab.alibaba-inc.com/seadent/pkg/utils" "gitlab.alibaba-inc.com/seadent/pkg/utils/mount" @@ -91,7 +92,10 @@ func GetClusterFileFromBaseImage(imageName string) string { mountTarget, _ := utils.MkTmpdir() mountUpper, _ := utils.MkTmpdir() defer func() { - _ = utils.CleanDirs(mountTarget, mountUpper) + err := utils.CleanDirs(mountTarget, mountUpper) + if err != nil { + logger.Warn(err) + } }() if err := NewImageService().PullIfNotExist(imageName); err != nil { @@ -113,7 +117,10 @@ func GetClusterFileFromBaseImage(imageName string) string { return "" } defer func() { - _ = driver.Unmount(mountTarget) + err := driver.Unmount(mountTarget) + if err != nil { + logger.Warn(err) + } }() clusterFile := filepath.Join(mountTarget, "etc", common.DefaultClusterFileName)