2023-05-31 12:26:23 -05:00
|
|
|
//go:build sync
|
|
|
|
// +build sync
|
|
|
|
|
2021-06-08 15:11:18 -05:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/containers/common/pkg/retry"
|
|
|
|
"github.com/containers/image/v5/types"
|
2023-05-31 12:26:23 -05:00
|
|
|
"github.com/opencontainers/go-digest"
|
2022-10-20 11:39:20 -05:00
|
|
|
|
2021-12-03 22:50:58 -05:00
|
|
|
"zotregistry.io/zot/pkg/log"
|
2023-05-31 12:26:23 -05:00
|
|
|
"zotregistry.io/zot/pkg/scheduler"
|
2021-06-08 15:11:18 -05:00
|
|
|
)
|
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
// below types are used by containers/image to copy images
|
|
|
|
// types.ImageReference - describes a registry/repo:tag
|
|
|
|
// types.SystemContext - describes a registry/oci layout config
|
|
|
|
|
|
|
|
// Sync general functionalities, one service per registry config.
|
|
|
|
type Service interface {
|
|
|
|
// Get next repo from remote /v2/_catalog, will return empty string when there is no repo left.
|
|
|
|
GetNextRepo(lastRepo string) (string, error) // used by task scheduler
|
|
|
|
// Sync a repo with all of its tags and references (signatures, artifacts, sboms) into ImageStore.
|
|
|
|
SyncRepo(repo string) error // used by periodically sync
|
|
|
|
// Sync an image (repo:tag || repo:digest) into ImageStore.
|
|
|
|
SyncImage(repo, reference string) error // used by sync on demand
|
|
|
|
// Sync a single reference for an image.
|
|
|
|
SyncReference(repo string, subjectDigestStr string, referenceType string) error // used by sync on demand
|
|
|
|
// Remove all internal catalog entries.
|
|
|
|
ResetCatalog() // used by scheduler to empty out the catalog after a sync periodically roundtrip finishes
|
|
|
|
// Sync supports multiple urls per registry, before a sync repo/image/ref 'ping' each url.
|
|
|
|
SetNextAvailableURL() error // used by all sync methods
|
|
|
|
// Returns retry options from registry config.
|
|
|
|
GetRetryOptions() *retry.Options // used by sync on demand to retry in background
|
2021-06-08 15:11:18 -05:00
|
|
|
}
|
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
// Local and remote registries must implement this interface.
|
|
|
|
type Registry interface {
|
|
|
|
// Get temporary ImageReference, is used by functions in containers/image package
|
|
|
|
GetImageReference(repo string, tag string) (types.ImageReference, error)
|
|
|
|
// Get local oci layout context, is used by functions in containers/image package
|
|
|
|
GetContext() *types.SystemContext
|
2022-10-22 02:26:14 -05:00
|
|
|
}
|
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
/*
|
|
|
|
Temporary oci layout, sync first pulls an image to this oci layout (using oci:// transport)
|
|
|
|
then moves them into ImageStore.
|
|
|
|
*/
|
|
|
|
type OciLayoutStorage interface {
|
|
|
|
Registry
|
2021-06-08 15:11:18 -05:00
|
|
|
}
|
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
// Remote registry.
|
|
|
|
type Remote interface {
|
|
|
|
Registry
|
|
|
|
// Get a list of repos (catalog)
|
|
|
|
GetRepositories(ctx context.Context) ([]string, error)
|
|
|
|
// Get a list of tags given a repo
|
|
|
|
GetRepoTags(repo string) ([]string, error)
|
|
|
|
// Get manifest content, mediaType, digest given an ImageReference
|
|
|
|
GetManifestContent(imageReference types.ImageReference) ([]byte, string, digest.Digest, error)
|
2021-06-08 15:11:18 -05:00
|
|
|
}
|
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
// Local registry.
|
|
|
|
type Local interface {
|
|
|
|
Registry
|
|
|
|
// Check if an image is already synced
|
|
|
|
CanSkipImage(repo, tag string, imageDigest digest.Digest) (bool, error)
|
|
|
|
// CommitImage moves a synced repo/ref from temporary oci layout to ImageStore
|
|
|
|
CommitImage(imageReference types.ImageReference, repo, tag string) error
|
2021-06-08 15:11:18 -05:00
|
|
|
}
|
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
type TaskGenerator struct {
|
|
|
|
Service Service
|
|
|
|
lastRepo string
|
|
|
|
done bool
|
|
|
|
log log.Logger
|
2021-06-08 15:11:18 -05:00
|
|
|
}
|
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
func NewTaskGenerator(service Service, log log.Logger) *TaskGenerator {
|
|
|
|
return &TaskGenerator{
|
|
|
|
Service: service,
|
|
|
|
done: false,
|
|
|
|
lastRepo: "",
|
|
|
|
log: log,
|
2022-12-22 13:19:42 -05:00
|
|
|
}
|
2023-05-31 12:26:23 -05:00
|
|
|
}
|
2022-12-22 13:19:42 -05:00
|
|
|
|
2023-07-04 03:03:29 -05:00
|
|
|
func (gen *TaskGenerator) Next() (scheduler.Task, error) {
|
2023-05-31 12:26:23 -05:00
|
|
|
if err := gen.Service.SetNextAvailableURL(); err != nil {
|
|
|
|
return nil, err
|
2022-12-22 13:19:42 -05:00
|
|
|
}
|
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
repo, err := gen.Service.GetNextRepo(gen.lastRepo)
|
2021-12-07 13:26:26 -05:00
|
|
|
if err != nil {
|
2023-05-31 12:26:23 -05:00
|
|
|
return nil, err
|
2021-12-07 13:26:26 -05:00
|
|
|
}
|
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
if repo == "" {
|
|
|
|
gen.log.Info().Msg("sync: finished syncing all repos")
|
|
|
|
gen.done = true
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
return nil, nil
|
2021-06-08 15:11:18 -05:00
|
|
|
}
|
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
gen.lastRepo = repo
|
2022-04-05 10:18:31 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
return newSyncRepoTask(gen.lastRepo, gen.Service), nil
|
2021-06-08 15:11:18 -05:00
|
|
|
}
|
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
func (gen *TaskGenerator) IsDone() bool {
|
|
|
|
return gen.done
|
2021-06-08 15:11:18 -05:00
|
|
|
}
|
|
|
|
|
2023-08-07 14:55:19 -05:00
|
|
|
func (gen *TaskGenerator) IsReady() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
func (gen *TaskGenerator) Reset() {
|
|
|
|
gen.lastRepo = ""
|
|
|
|
gen.Service.ResetCatalog()
|
|
|
|
gen.done = false
|
|
|
|
}
|
2022-02-10 09:17:49 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
type syncRepoTask struct {
|
|
|
|
repo string
|
|
|
|
service Service
|
|
|
|
}
|
2021-06-08 15:11:18 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
func newSyncRepoTask(repo string, service Service) *syncRepoTask {
|
|
|
|
return &syncRepoTask{repo, service}
|
|
|
|
}
|
2021-06-08 15:11:18 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
func (srt *syncRepoTask) DoWork() error {
|
|
|
|
return srt.service.SyncRepo(srt.repo)
|
2021-06-08 15:11:18 -05:00
|
|
|
}
|