Skip to content

Commit 8262c46

Browse files
Fix sync extension logging (#2537)
* fix: nil pointer dereference on localimagestore fixes #2527 Signed-off-by: Anders Bennedsgaard <abbennedsgaard@gmail.com> * fix: no logging from sync extension imagestore Signed-off-by: Anders Bennedsgaard <abbennedsgaard@gmail.com> * feat: create local imagestore not found error Signed-off-by: Anders Bennedsgaard <abbennedsgaard@gmail.com> * fix: add test Signed-off-by: Anders Bennedsgaard <abbennedsgaard@gmail.com> --------- Signed-off-by: Anders Bennedsgaard <abbennedsgaard@gmail.com>
1 parent e5eacaa commit 8262c46

5 files changed

Lines changed: 22 additions & 9 deletions

File tree

errors/errors.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ var (
8787
ErrDuplicateConfigName = errors.New("cli config name already added")
8888
ErrInvalidRoute = errors.New("invalid route prefix")
8989
ErrImgStoreNotFound = errors.New("image store not found corresponding to given route")
90+
ErrLocalImgStoreNotFound = errors.New("local image store not found corresponding to given route")
9091
ErrEmptyValue = errors.New("empty cache value")
9192
ErrEmptyRepoList = errors.New("no repository found")
9293
ErrCVESearchDisabled = errors.New("cve search is disabled")

pkg/extensions/sync/destination.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func (registry *DestinationRegistry) GetImageReference(repo, reference string) (
9292
func (registry *DestinationRegistry) CommitImage(imageReference types.ImageReference, repo, reference string) error {
9393
imageStore := registry.storeController.GetImageStore(repo)
9494

95-
tempImageStore := getImageStoreFromImageReference(imageReference, repo, reference)
95+
tempImageStore := getImageStoreFromImageReference(imageReference, repo, reference, registry.log)
9696

9797
defer os.RemoveAll(tempImageStore.RootDir())
9898

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

284284
// use only with local imageReferences.
285-
func getImageStoreFromImageReference(imageReference types.ImageReference, repo, reference string,
285+
func getImageStoreFromImageReference(imageReference types.ImageReference, repo, reference string, log log.Logger,
286286
) storageTypes.ImageStore {
287287
tmpRootDir := getTempRootDirFromImageReference(imageReference, repo, reference)
288288

289-
return getImageStore(tmpRootDir)
289+
return getImageStore(tmpRootDir, log)
290290
}
291291

292292
func getTempRootDirFromImageReference(imageReference types.ImageReference, repo, reference string) string {
@@ -301,8 +301,8 @@ func getTempRootDirFromImageReference(imageReference types.ImageReference, repo,
301301
return tmpRootDir
302302
}
303303

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

307-
return local.NewImageStore(rootDir, false, false, log.Logger{}, metrics, nil, nil)
307+
return local.NewImageStore(rootDir, false, false, log, metrics, nil, nil)
308308
}

pkg/extensions/sync/oci_layout.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/containers/image/v5/types"
1313
"github.com/gofrs/uuid"
1414

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

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

4549
// create session folder

pkg/extensions/sync/service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func New(
8383
service.destination = NewDestinationRegistry(
8484
storeController,
8585
storage.StoreController{
86-
DefaultStore: getImageStore(tmpDir),
86+
DefaultStore: getImageStore(tmpDir, log),
8787
},
8888
metadb,
8989
log,

pkg/extensions/sync/sync_internal_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ func TestInjectSyncUtils(t *testing.T) {
8383
})
8484
}
8585

86+
func TestNilDefaultStore(t *testing.T) {
87+
Convey("Nil default store", t, func() {
88+
ols := NewOciLayoutStorage(storage.StoreController{})
89+
_, err := ols.GetImageReference(testImage, testImageTag)
90+
So(err, ShouldEqual, zerr.ErrLocalImgStoreNotFound)
91+
})
92+
}
93+
8694
func TestSyncInternal(t *testing.T) {
8795
Convey("Verify parseRepositoryReference func", t, func() {
8896
repositoryReference := fmt.Sprintf("%s/%s", host, testImage)
@@ -214,7 +222,7 @@ func TestDestinationRegistry(t *testing.T) {
214222
So(err, ShouldBeNil)
215223
So(imageReference, ShouldNotBeNil)
216224

217-
imgStore := getImageStoreFromImageReference(imageReference, repoName, "1.0")
225+
imgStore := getImageStoreFromImageReference(imageReference, repoName, "1.0", log)
218226

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

396-
imgStore := getImageStoreFromImageReference(imageReference, repoName, "2.0")
404+
imgStore := getImageStoreFromImageReference(imageReference, repoName, "2.0", log)
397405

398406
// upload image
399407

0 commit comments

Comments
 (0)