Skip to content

Commit

Permalink
fix: ci lint for mod registry and image (#1)
Browse files Browse the repository at this point in the history
* fix mod image & registry ci lint

* logger warn when failed to clean file

* warn after CleanDirs/ Unmount
  • Loading branch information
justadogistaken authored May 1, 2021
1 parent ca14e98 commit 889baa6
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 17 deletions.
4 changes: 2 additions & 2 deletions image/default_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 22 additions & 8 deletions image/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 ""
Expand All @@ -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 ""
Expand Down
6 changes: 3 additions & 3 deletions registry/tokentransport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion utils/compress/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}()

Expand Down
14 changes: 11 additions & 3 deletions utils/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 889baa6

Please sign in to comment.