diff --git a/image/default_image.go b/image/default_image.go index aa8924e10b5..79a38f9324d 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) diff --git a/image/util.go b/image/util.go index 7fa361953d0..ef5cd1f4d2a 100644 --- a/image/util.go +++ b/image/util.go @@ -4,11 +4,12 @@ import ( "fmt" "github.com/docker/distribution" "github.com/opencontainers/go-digest" - "github.com/alibaba/sealer/common" - imageUtils "github.com/alibaba/sealer/image/utils" - v1 "github.com/alibaba/sealer/types/api/v1" - "github.com/alibaba/sealer/utils" - "github.com/alibaba/sealer/utils/mount" + "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" "io/ioutil" "path/filepath" ) @@ -90,7 +91,12 @@ func GetClusterFileFromImageManifest(imageName string) string { func GetClusterFileFromBaseImage(imageName string) string { mountTarget, _ := utils.MkTmpdir() mountUpper, _ := utils.MkTmpdir() - defer utils.CleanDirs(mountTarget, mountUpper) + defer func() { + err := utils.CleanDirs(mountTarget, mountUpper) + if err != nil { + logger.Warn(err) + } + }() if err := NewImageService().PullIfNotExist(imageName); err != nil { return "" @@ -107,9 +113,17 @@ 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() { + err := driver.Unmount(mountTarget) + if err != nil { + logger.Warn(err) + } + }() + 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) diff --git a/utils/compress/compress.go b/utils/compress/compress.go index 1dd6594c70c..336c462160e 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 00fc962314c..19f2da83540 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) {