@@ -14,7 +14,6 @@ import (
1414 "syscall"
1515 "time"
1616
17- "github.com/docker/distribution/registry/storage/driver/factory"
1817 "github.com/gorilla/handlers"
1918 "github.com/gorilla/mux"
2019
@@ -29,7 +28,6 @@ import (
2928 "zotregistry.io/zot/pkg/storage"
3029 "zotregistry.io/zot/pkg/storage/cache"
3130 "zotregistry.io/zot/pkg/storage/constants"
32- "zotregistry.io/zot/pkg/storage/local"
3331 "zotregistry.io/zot/pkg/storage/s3"
3432)
3533
@@ -245,7 +243,7 @@ func (c *Controller) Init(reloadCtx context.Context) error {
245243
246244 c .Metrics = monitoring .NewMetricsServer (enabled , c .Log )
247245
248- if err := c .InitImageStore (reloadCtx ); err != nil {
246+ if err := c .InitImageStore (); err != nil { //nolint:contextcheck
249247 return err
250248 }
251249
@@ -265,185 +263,19 @@ func (c *Controller) InitCVEInfo() {
265263 }
266264}
267265
268- func (c * Controller ) InitImageStore (ctx context.Context ) error {
269- c .StoreController = storage.StoreController {}
270-
266+ func (c * Controller ) InitImageStore () error {
271267 linter := ext .GetLinter (c .Config , c .Log )
272268
273- if c .Config .Storage .RootDirectory != "" {
274- // no need to validate hard links work on s3
275- if c .Config .Storage .Dedupe && c .Config .Storage .StorageDriver == nil {
276- err := local .ValidateHardLink (c .Config .Storage .RootDirectory )
277- if err != nil {
278- c .Log .Warn ().Msg ("input storage root directory filesystem does not supports hardlinking," +
279- "disabling dedupe functionality" )
280-
281- c .Config .Storage .Dedupe = false
282- }
283- }
284-
285- var defaultStore storage.ImageStore
286- if c .Config .Storage .StorageDriver == nil {
287- // false positive lint - linter does not implement Lint method
288- //nolint:typecheck,contextcheck
289- defaultStore = local .NewImageStore (c .Config .Storage .RootDirectory ,
290- c .Config .Storage .GC , c .Config .Storage .GCDelay ,
291- c .Config .Storage .Dedupe , c .Config .Storage .Commit , c .Log , c .Metrics , linter ,
292- CreateCacheDatabaseDriver (c .Config .Storage .StorageConfig , c .Log ),
293- )
294- } else {
295- storeName := fmt .Sprintf ("%v" , c .Config .Storage .StorageDriver ["name" ])
296- if storeName != storage .S3StorageDriverName {
297- c .Log .Fatal ().Err (errors .ErrBadConfig ).Str ("storageDriver" , storeName ).
298- Msg ("unsupported storage driver" )
299- }
300- // Init a Storager from connection string.
301- store , err := factory .Create (storeName , c .Config .Storage .StorageDriver )
302- if err != nil {
303- c .Log .Error ().Err (err ).Str ("rootDir" , c .Config .Storage .RootDirectory ).Msg ("unable to create s3 service" )
304-
305- return err
306- }
307-
308- /* in the case of s3 c.Config.Storage.RootDirectory is used for caching blobs locally and
309- c.Config.Storage.StorageDriver["rootdirectory"] is the actual rootDir in s3 */
310- rootDir := "/"
311- if c .Config .Storage .StorageDriver ["rootdirectory" ] != nil {
312- rootDir = fmt .Sprintf ("%v" , c .Config .Storage .StorageDriver ["rootdirectory" ])
313- }
314-
315- // false positive lint - linter does not implement Lint method
316- //nolint: typecheck,contextcheck
317- defaultStore = s3 .NewImageStore (rootDir , c .Config .Storage .RootDirectory ,
318- c .Config .Storage .GC , c .Config .Storage .GCDelay , c .Config .Storage .Dedupe ,
319- c .Config .Storage .Commit , c .Log , c .Metrics , linter , store ,
320- CreateCacheDatabaseDriver (c .Config .Storage .StorageConfig , c .Log ))
321- }
322-
323- c .StoreController .DefaultStore = defaultStore
324- } else {
325- // we can't proceed without global storage
326- c .Log .Error ().Err (errors .ErrImgStoreNotFound ).Msg ("controller: no storage config provided" )
327-
328- return errors .ErrImgStoreNotFound
269+ storeController , err := storage .New (c .Config , linter , c .Metrics , c .Log )
270+ if err != nil {
271+ return err
329272 }
330273
331- if c .Config .Storage .SubPaths != nil {
332- if len (c .Config .Storage .SubPaths ) > 0 {
333- subPaths := c .Config .Storage .SubPaths
334-
335- //nolint: contextcheck
336- subImageStore , err := c .getSubStore (subPaths , linter )
337- if err != nil {
338- c .Log .Error ().Err (err ).Msg ("controller: error getting sub image store" )
339-
340- return err
341- }
342-
343- c .StoreController .SubStore = subImageStore
344- }
345- }
274+ c .StoreController = storeController
346275
347276 return nil
348277}
349278
350- func (c * Controller ) getSubStore (subPaths map [string ]config.StorageConfig ,
351- linter storage.Lint ,
352- ) (map [string ]storage.ImageStore , error ) {
353- imgStoreMap := make (map [string ]storage.ImageStore , 0 )
354-
355- subImageStore := make (map [string ]storage.ImageStore )
356-
357- // creating image store per subpaths
358- for route , storageConfig := range subPaths {
359- // no need to validate hard links work on s3
360- if storageConfig .Dedupe && storageConfig .StorageDriver == nil {
361- err := local .ValidateHardLink (storageConfig .RootDirectory )
362- if err != nil {
363- c .Log .Warn ().Msg ("input storage root directory filesystem does not supports hardlinking, " +
364- "disabling dedupe functionality" )
365-
366- storageConfig .Dedupe = false
367- }
368- }
369-
370- if storageConfig .StorageDriver == nil {
371- // Compare if subpath root dir is same as default root dir
372- isSame , _ := config .SameFile (c .Config .Storage .RootDirectory , storageConfig .RootDirectory )
373-
374- if isSame {
375- c .Log .Error ().Err (errors .ErrBadConfig ).Msg ("sub path storage directory is same as root directory" )
376-
377- return nil , errors .ErrBadConfig
378- }
379-
380- isUnique := true
381-
382- // Compare subpath unique files
383- for file := range imgStoreMap {
384- // We already have image storage for this file
385- if compareImageStore (file , storageConfig .RootDirectory ) {
386- subImageStore [route ] = imgStoreMap [file ]
387-
388- isUnique = true
389- }
390- }
391-
392- // subpath root directory is unique
393- // add it to uniqueSubFiles
394- // Create a new image store and assign it to imgStoreMap
395- if isUnique {
396- imgStoreMap [storageConfig .RootDirectory ] = local .NewImageStore (storageConfig .RootDirectory ,
397- storageConfig .GC , storageConfig .GCDelay , storageConfig .Dedupe ,
398- storageConfig .Commit , c .Log , c .Metrics , linter , CreateCacheDatabaseDriver (storageConfig , c .Log ))
399-
400- subImageStore [route ] = imgStoreMap [storageConfig .RootDirectory ]
401- }
402- } else {
403- storeName := fmt .Sprintf ("%v" , storageConfig .StorageDriver ["name" ])
404- if storeName != storage .S3StorageDriverName {
405- c .Log .Fatal ().Err (errors .ErrBadConfig ).Str ("storageDriver" , storeName ).
406- Msg ("unsupported storage driver" )
407- }
408-
409- // Init a Storager from connection string.
410- store , err := factory .Create (storeName , storageConfig .StorageDriver )
411- if err != nil {
412- c .Log .Error ().Err (err ).Str ("rootDir" , storageConfig .RootDirectory ).Msg ("Unable to create s3 service" )
413-
414- return nil , err
415- }
416-
417- /* in the case of s3 c.Config.Storage.RootDirectory is used for caching blobs locally and
418- c.Config.Storage.StorageDriver["rootdirectory"] is the actual rootDir in s3 */
419- rootDir := "/"
420- if c .Config .Storage .StorageDriver ["rootdirectory" ] != nil {
421- rootDir = fmt .Sprintf ("%v" , c .Config .Storage .StorageDriver ["rootdirectory" ])
422- }
423-
424- // false positive lint - linter does not implement Lint method
425- //nolint: typecheck
426- subImageStore [route ] = s3 .NewImageStore (rootDir , storageConfig .RootDirectory ,
427- storageConfig .GC , storageConfig .GCDelay ,
428- storageConfig .Dedupe , storageConfig .Commit , c .Log , c .Metrics , linter , store ,
429- CreateCacheDatabaseDriver (storageConfig , c .Log ),
430- )
431- }
432- }
433-
434- return subImageStore , nil
435- }
436-
437- func compareImageStore (root1 , root2 string ) bool {
438- isSameFile , err := config .SameFile (root1 , root2 )
439- // This error is path error that means either of root directory doesn't exist, in that case do string match
440- if err != nil {
441- return strings .EqualFold (root1 , root2 )
442- }
443-
444- return isSameFile
445- }
446-
447279func getUseRelPaths (storageConfig * config.StorageConfig ) bool {
448280 return storageConfig .StorageDriver == nil
449281}
0 commit comments