Skip to content

Commit

Permalink
Fix sync extension logging (#2537)
Browse files Browse the repository at this point in the history
* fix: nil pointer dereference on localimagestore

fixes #2527

Signed-off-by: Anders Bennedsgaard <[email protected]>

* fix: no logging from sync extension imagestore

Signed-off-by: Anders Bennedsgaard <[email protected]>

* feat: create local imagestore not found error

Signed-off-by: Anders Bennedsgaard <[email protected]>

* fix: add test

Signed-off-by: Anders Bennedsgaard <[email protected]>

---------

Signed-off-by: Anders Bennedsgaard <[email protected]>
  • Loading branch information
AndersBennedsgaard authored Jul 15, 2024
1 parent e5eacaa commit 8262c46
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 9 deletions.
1 change: 1 addition & 0 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ var (
ErrDuplicateConfigName = errors.New("cli config name already added")
ErrInvalidRoute = errors.New("invalid route prefix")
ErrImgStoreNotFound = errors.New("image store not found corresponding to given route")
ErrLocalImgStoreNotFound = errors.New("local image store not found corresponding to given route")
ErrEmptyValue = errors.New("empty cache value")
ErrEmptyRepoList = errors.New("no repository found")
ErrCVESearchDisabled = errors.New("cve search is disabled")
Expand Down
12 changes: 6 additions & 6 deletions pkg/extensions/sync/destination.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (registry *DestinationRegistry) GetImageReference(repo, reference string) (
func (registry *DestinationRegistry) CommitImage(imageReference types.ImageReference, repo, reference string) error {
imageStore := registry.storeController.GetImageStore(repo)

tempImageStore := getImageStoreFromImageReference(imageReference, repo, reference)
tempImageStore := getImageStoreFromImageReference(imageReference, repo, reference, registry.log)

defer os.RemoveAll(tempImageStore.RootDir())

Expand Down Expand Up @@ -282,11 +282,11 @@ func (registry *DestinationRegistry) copyBlob(repo string, blobDigest digest.Dig
}

// use only with local imageReferences.
func getImageStoreFromImageReference(imageReference types.ImageReference, repo, reference string,
func getImageStoreFromImageReference(imageReference types.ImageReference, repo, reference string, log log.Logger,
) storageTypes.ImageStore {
tmpRootDir := getTempRootDirFromImageReference(imageReference, repo, reference)

return getImageStore(tmpRootDir)
return getImageStore(tmpRootDir, log)
}

func getTempRootDirFromImageReference(imageReference types.ImageReference, repo, reference string) string {
Expand All @@ -301,8 +301,8 @@ func getTempRootDirFromImageReference(imageReference types.ImageReference, repo,
return tmpRootDir
}

func getImageStore(rootDir string) storageTypes.ImageStore {
metrics := monitoring.NewMetricsServer(false, log.Logger{})
func getImageStore(rootDir string, log log.Logger) storageTypes.ImageStore {
metrics := monitoring.NewMetricsServer(false, log)

return local.NewImageStore(rootDir, false, false, log.Logger{}, metrics, nil, nil)
return local.NewImageStore(rootDir, false, false, log, metrics, nil, nil)
}
4 changes: 4 additions & 0 deletions pkg/extensions/sync/oci_layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/containers/image/v5/types"
"github.com/gofrs/uuid"

zerr "zotregistry.dev/zot/errors"
"zotregistry.dev/zot/pkg/extensions/sync/constants"
"zotregistry.dev/zot/pkg/storage"
storageConstants "zotregistry.dev/zot/pkg/storage/constants"
Expand Down Expand Up @@ -40,6 +41,9 @@ func (oci OciLayoutStorageImpl) GetContext() *types.SystemContext {

func (oci OciLayoutStorageImpl) GetImageReference(repo string, reference string) (types.ImageReference, error) {
localImageStore := oci.storeController.GetImageStore(repo)
if localImageStore == nil {
return nil, zerr.ErrLocalImgStoreNotFound
}
tempSyncPath := path.Join(localImageStore.RootDir(), repo, constants.SyncBlobUploadDir)

// create session folder
Expand Down
2 changes: 1 addition & 1 deletion pkg/extensions/sync/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func New(
service.destination = NewDestinationRegistry(
storeController,
storage.StoreController{
DefaultStore: getImageStore(tmpDir),
DefaultStore: getImageStore(tmpDir, log),
},
metadb,
log,
Expand Down
12 changes: 10 additions & 2 deletions pkg/extensions/sync/sync_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ func TestInjectSyncUtils(t *testing.T) {
})
}

func TestNilDefaultStore(t *testing.T) {
Convey("Nil default store", t, func() {
ols := NewOciLayoutStorage(storage.StoreController{})
_, err := ols.GetImageReference(testImage, testImageTag)
So(err, ShouldEqual, zerr.ErrLocalImgStoreNotFound)
})
}

func TestSyncInternal(t *testing.T) {
Convey("Verify parseRepositoryReference func", t, func() {
repositoryReference := fmt.Sprintf("%s/%s", host, testImage)
Expand Down Expand Up @@ -214,7 +222,7 @@ func TestDestinationRegistry(t *testing.T) {
So(err, ShouldBeNil)
So(imageReference, ShouldNotBeNil)

imgStore := getImageStoreFromImageReference(imageReference, repoName, "1.0")
imgStore := getImageStoreFromImageReference(imageReference, repoName, "1.0", log)

// create a blob/layer
upload, err := imgStore.NewBlobUpload(repoName)
Expand Down Expand Up @@ -393,7 +401,7 @@ func TestDestinationRegistry(t *testing.T) {
So(err, ShouldBeNil)
So(imageReference, ShouldNotBeNil)

imgStore := getImageStoreFromImageReference(imageReference, repoName, "2.0")
imgStore := getImageStoreFromImageReference(imageReference, repoName, "2.0", log)

// upload image

Expand Down

0 comments on commit 8262c46

Please sign in to comment.