2021-06-08 15:11:18 -05:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2022-01-10 11:06:12 -05:00
|
|
|
"net/url"
|
2021-10-28 04:10:01 -05:00
|
|
|
"os"
|
2022-01-10 11:06:12 -05:00
|
|
|
"sync"
|
|
|
|
"time"
|
2021-06-08 15:11:18 -05:00
|
|
|
|
|
|
|
"github.com/containers/common/pkg/retry"
|
|
|
|
"github.com/containers/image/v5/copy"
|
2022-01-10 11:06:12 -05:00
|
|
|
"gopkg.in/resty.v1"
|
2021-12-03 22:50:58 -05:00
|
|
|
"zotregistry.io/zot/pkg/log"
|
|
|
|
"zotregistry.io/zot/pkg/storage"
|
2021-06-08 15:11:18 -05:00
|
|
|
)
|
|
|
|
|
2022-01-10 11:06:12 -05:00
|
|
|
// nolint: gochecknoglobals
|
|
|
|
var demandedImgs demandedImages
|
|
|
|
|
|
|
|
type demandedImages struct {
|
|
|
|
syncedMap sync.Map
|
|
|
|
}
|
|
|
|
|
|
|
|
func (di *demandedImages) loadOrStoreChan(key string, value chan error) (chan error, bool) {
|
|
|
|
val, found := di.syncedMap.LoadOrStore(key, value)
|
|
|
|
errChannel, _ := val.(chan error)
|
|
|
|
|
|
|
|
return errChannel, found
|
|
|
|
}
|
|
|
|
|
|
|
|
func (di *demandedImages) loadOrStoreStr(key string, value string) (string, bool) {
|
|
|
|
val, found := di.syncedMap.LoadOrStore(key, value)
|
|
|
|
str, _ := val.(string)
|
|
|
|
|
|
|
|
return str, found
|
|
|
|
}
|
|
|
|
|
|
|
|
func (di *demandedImages) delete(key string) {
|
|
|
|
di.syncedMap.Delete(key)
|
|
|
|
}
|
|
|
|
|
2021-12-02 12:45:26 -05:00
|
|
|
func OneImage(cfg Config, storeController storage.StoreController,
|
2022-01-10 11:06:12 -05:00
|
|
|
repo, tag string, isArtifact bool, log log.Logger) error {
|
|
|
|
// guard against multiple parallel requests
|
|
|
|
demandedImage := fmt.Sprintf("%s:%s", repo, tag)
|
|
|
|
// loadOrStore image-based channel
|
|
|
|
imageChannel, found := demandedImgs.loadOrStoreChan(demandedImage, make(chan error))
|
|
|
|
// if value found wait on channel receive or close
|
|
|
|
if found {
|
|
|
|
log.Info().Msgf("image %s already demanded by another client, waiting on imageChannel", demandedImage)
|
|
|
|
|
|
|
|
err, ok := <-imageChannel
|
|
|
|
// if channel closed exit
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer demandedImgs.delete(demandedImage)
|
|
|
|
defer close(imageChannel)
|
|
|
|
|
|
|
|
go syncOneImage(imageChannel, cfg, storeController, repo, tag, isArtifact, log)
|
2021-06-08 15:11:18 -05:00
|
|
|
|
2022-01-10 11:06:12 -05:00
|
|
|
err, ok := <-imageChannel
|
|
|
|
if !ok {
|
2021-12-07 13:26:26 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-10 11:06:12 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func syncOneImage(imageChannel chan error, cfg Config, storeController storage.StoreController,
|
2022-03-10 10:39:11 -05:00
|
|
|
localRepo, tag string, isArtifact bool, log log.Logger) {
|
2022-01-10 11:06:12 -05:00
|
|
|
var credentialsFile CredentialsFile
|
|
|
|
|
2021-06-08 15:11:18 -05:00
|
|
|
if cfg.CredentialsFile != "" {
|
2021-10-28 04:10:01 -05:00
|
|
|
var err error
|
|
|
|
|
2021-06-08 15:11:18 -05:00
|
|
|
credentialsFile, err = getFileCredentials(cfg.CredentialsFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msgf("couldn't get registry credentials from %s", cfg.CredentialsFile)
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2022-01-10 11:06:12 -05:00
|
|
|
imageChannel <- err
|
|
|
|
|
|
|
|
return
|
2021-06-08 15:11:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-10 11:06:12 -05:00
|
|
|
var copyErr error
|
|
|
|
|
2021-10-28 04:10:01 -05:00
|
|
|
localCtx, policyCtx, err := getLocalContexts(log)
|
|
|
|
if err != nil {
|
2022-01-10 11:06:12 -05:00
|
|
|
imageChannel <- err
|
|
|
|
|
|
|
|
return
|
2021-10-28 04:10:01 -05:00
|
|
|
}
|
|
|
|
|
2022-03-10 10:39:11 -05:00
|
|
|
imageStore := storeController.GetImageStore(localRepo)
|
2021-10-28 04:10:01 -05:00
|
|
|
|
2021-12-07 13:26:26 -05:00
|
|
|
for _, registryCfg := range cfg.Registries {
|
|
|
|
regCfg := registryCfg
|
2021-06-08 15:11:18 -05:00
|
|
|
if !regCfg.OnDemand {
|
2021-12-29 10:14:56 -05:00
|
|
|
log.Info().Msgf("skipping syncing on demand from %v, onDemand flag is false", regCfg.URLs)
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-06-08 15:11:18 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-03-10 10:39:11 -05:00
|
|
|
remoteRepo := localRepo
|
|
|
|
|
2021-10-25 07:05:03 -05:00
|
|
|
// if content config is not specified, then don't filter, just sync demanded image
|
|
|
|
if len(regCfg.Content) != 0 {
|
2022-03-10 10:39:11 -05:00
|
|
|
contentID, err := findRepoMatchingContentID(localRepo, regCfg.Content)
|
|
|
|
if err != nil {
|
2021-12-29 10:14:56 -05:00
|
|
|
log.Info().Msgf("skipping syncing on demand %s from %v registry because it's filtered out by content config",
|
2022-03-10 10:39:11 -05:00
|
|
|
localRepo, regCfg.URLs)
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-10-25 07:05:03 -05:00
|
|
|
continue
|
|
|
|
}
|
2022-03-10 10:39:11 -05:00
|
|
|
|
|
|
|
remoteRepo = getRepoSource(localRepo, regCfg.Content[contentID])
|
2021-10-25 07:05:03 -05:00
|
|
|
}
|
|
|
|
|
2022-01-10 11:06:12 -05:00
|
|
|
retryOptions := &retry.RetryOptions{}
|
|
|
|
|
|
|
|
if regCfg.MaxRetries != nil {
|
|
|
|
retryOptions.MaxRetry = *regCfg.MaxRetries
|
|
|
|
if regCfg.RetryDelay != nil {
|
|
|
|
retryOptions.Delay = *regCfg.RetryDelay
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info().Msgf("syncing on demand with %v", regCfg.URLs)
|
|
|
|
|
|
|
|
for _, regCfgURL := range regCfg.URLs {
|
|
|
|
upstreamURL := regCfgURL
|
2021-06-08 15:11:18 -05:00
|
|
|
|
2021-12-29 10:14:56 -05:00
|
|
|
upstreamAddr := StripRegistryTransport(upstreamURL)
|
2021-06-08 15:11:18 -05:00
|
|
|
|
2022-01-10 11:06:12 -05:00
|
|
|
httpClient, err := getHTTPClient(®Cfg, upstreamURL, credentialsFile[upstreamAddr], log)
|
2021-12-29 10:14:56 -05:00
|
|
|
if err != nil {
|
2022-01-10 11:06:12 -05:00
|
|
|
imageChannel <- err
|
2021-06-08 15:11:18 -05:00
|
|
|
|
2022-01-10 11:06:12 -05:00
|
|
|
return
|
2021-12-29 10:14:56 -05:00
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2022-01-10 11:06:12 -05:00
|
|
|
// demanded 'image' is a signature
|
|
|
|
if isCosignTag(tag) || isArtifact {
|
|
|
|
// at tis point we should already have images synced, but not their signatures.
|
|
|
|
regURL, err := url.Parse(upstreamURL)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msgf("couldn't parse registry URL: %s", upstreamURL)
|
2021-06-08 15:11:18 -05:00
|
|
|
|
2022-01-10 11:06:12 -05:00
|
|
|
imageChannel <- err
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2022-01-10 11:06:12 -05:00
|
|
|
return
|
|
|
|
}
|
2021-10-28 04:10:01 -05:00
|
|
|
|
2022-01-10 11:06:12 -05:00
|
|
|
// is notary signature
|
|
|
|
if isArtifact {
|
2022-03-10 10:39:11 -05:00
|
|
|
err = syncNotarySignature(httpClient, storeController, *regURL, remoteRepo, localRepo, tag, log)
|
2022-01-10 11:06:12 -05:00
|
|
|
if err != nil {
|
2022-03-10 10:39:11 -05:00
|
|
|
log.Error().Err(err).Msgf("couldn't copy image signature %s/%s:%s", upstreamURL, localRepo, tag)
|
2021-06-08 15:11:18 -05:00
|
|
|
|
2022-01-10 11:06:12 -05:00
|
|
|
continue
|
|
|
|
}
|
2021-10-28 04:10:01 -05:00
|
|
|
|
2022-01-10 11:06:12 -05:00
|
|
|
imageChannel <- nil
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2022-01-10 11:06:12 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// is cosign signature
|
2022-03-10 10:39:11 -05:00
|
|
|
err = syncCosignSignature(httpClient, storeController, *regURL, remoteRepo, localRepo, tag, log)
|
2022-01-10 11:06:12 -05:00
|
|
|
if err != nil {
|
2022-03-10 10:39:11 -05:00
|
|
|
log.Error().Err(err).Msgf("couldn't copy image signature %s/%s:%s", upstreamURL, localRepo, tag)
|
2022-01-10 11:06:12 -05:00
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
2021-10-28 04:10:01 -05:00
|
|
|
|
2022-01-10 11:06:12 -05:00
|
|
|
imageChannel <- nil
|
2021-11-18 07:52:32 -05:00
|
|
|
|
2022-01-10 11:06:12 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// it's an image
|
|
|
|
upstreamCtx := getUpstreamContext(®Cfg, credentialsFile[upstreamAddr])
|
|
|
|
options := getCopyOptions(upstreamCtx, localCtx)
|
2021-10-28 04:10:01 -05:00
|
|
|
|
2022-03-10 10:39:11 -05:00
|
|
|
upstreamImageRef, err := getImageRef(upstreamAddr, remoteRepo, tag)
|
2021-12-29 10:14:56 -05:00
|
|
|
if err != nil {
|
2022-01-10 11:06:12 -05:00
|
|
|
log.Error().Err(err).Msgf("error creating docker reference for repository %s/%s:%s",
|
2022-03-10 10:39:11 -05:00
|
|
|
upstreamAddr, remoteRepo, tag)
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2022-01-10 11:06:12 -05:00
|
|
|
imageChannel <- err
|
|
|
|
|
|
|
|
return
|
2021-12-29 10:14:56 -05:00
|
|
|
}
|
2021-06-08 15:11:18 -05:00
|
|
|
|
2022-03-10 10:39:11 -05:00
|
|
|
localImageRef, localCachePath, err := getLocalImageRef(imageStore, localRepo, tag)
|
2022-01-10 11:06:12 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msgf("couldn't obtain a valid image reference for reference %s/%s:%s",
|
2022-03-10 10:39:11 -05:00
|
|
|
localCachePath, localRepo, tag)
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2022-01-10 11:06:12 -05:00
|
|
|
imageChannel <- err
|
2021-06-08 15:11:18 -05:00
|
|
|
|
2022-01-10 11:06:12 -05:00
|
|
|
return
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2022-01-10 11:06:12 -05:00
|
|
|
log.Info().Msgf("copying image %s to %s", upstreamImageRef.DockerReference(), localCachePath)
|
2021-10-28 04:10:01 -05:00
|
|
|
|
2022-03-10 10:39:11 -05:00
|
|
|
demandedImageRef := fmt.Sprintf("%s/%s:%s", upstreamAddr, remoteRepo, tag)
|
2022-01-10 11:06:12 -05:00
|
|
|
|
|
|
|
_, copyErr = copy.Image(context.Background(), policyCtx, localImageRef, upstreamImageRef, &options)
|
|
|
|
if copyErr != nil {
|
|
|
|
log.Error().Err(err).Msgf("error encountered while syncing on demand %s to %s",
|
|
|
|
upstreamImageRef.DockerReference(), localCachePath)
|
|
|
|
|
|
|
|
_, found := demandedImgs.loadOrStoreStr(demandedImageRef, "")
|
|
|
|
if found || retryOptions.MaxRetry == 0 {
|
|
|
|
defer os.RemoveAll(localCachePath)
|
|
|
|
log.Info().Msgf("image %s already demanded in background or sync.registries[].MaxRetries == 0", demandedImageRef)
|
|
|
|
/* we already have a go routine spawned for this image
|
|
|
|
or retryOptions is not configured */
|
|
|
|
continue
|
2021-12-29 10:14:56 -05:00
|
|
|
}
|
2021-12-07 13:26:26 -05:00
|
|
|
|
2022-01-10 11:06:12 -05:00
|
|
|
// spawn goroutine to later pull the image
|
|
|
|
go func() {
|
|
|
|
// remove image after syncing
|
|
|
|
defer func() {
|
|
|
|
_ = os.RemoveAll(localCachePath)
|
|
|
|
|
|
|
|
demandedImgs.delete(demandedImageRef)
|
|
|
|
log.Info().Msgf("sync routine: %s exited", demandedImageRef)
|
|
|
|
}()
|
|
|
|
|
|
|
|
log.Info().Msgf("sync routine: starting routine to copy image %s, cause err: %v",
|
|
|
|
demandedImageRef, copyErr)
|
|
|
|
time.Sleep(retryOptions.Delay)
|
|
|
|
|
|
|
|
if err = retry.RetryIfNecessary(context.Background(), func() error {
|
|
|
|
_, err := copy.Image(context.Background(), policyCtx, localImageRef, upstreamImageRef, &options)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}, retryOptions); err != nil {
|
|
|
|
log.Error().Err(err).Msgf("sync routine: error while copying image %s to %s",
|
|
|
|
demandedImageRef, localCachePath)
|
|
|
|
} else {
|
2022-03-10 10:39:11 -05:00
|
|
|
_ = finishSyncing(localRepo, remoteRepo, tag, localCachePath, upstreamURL, storeController,
|
|
|
|
retryOptions, httpClient, log)
|
2022-01-10 11:06:12 -05:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
} else {
|
2022-03-10 10:39:11 -05:00
|
|
|
err := finishSyncing(localRepo, remoteRepo, tag, localCachePath, upstreamURL, storeController,
|
|
|
|
retryOptions, httpClient, log)
|
2021-12-07 13:26:26 -05:00
|
|
|
|
2022-01-10 11:06:12 -05:00
|
|
|
imageChannel <- err
|
2021-12-07 13:26:26 -05:00
|
|
|
|
2022-01-10 11:06:12 -05:00
|
|
|
return
|
2021-12-07 13:26:26 -05:00
|
|
|
}
|
2021-06-08 15:11:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-10 11:06:12 -05:00
|
|
|
imageChannel <- err
|
|
|
|
}
|
|
|
|
|
|
|
|
// push the local image into the storage, sync signatures.
|
2022-03-10 10:39:11 -05:00
|
|
|
func finishSyncing(localRepo, remoteRepo, tag, localCachePath, upstreamURL string,
|
2022-01-10 11:06:12 -05:00
|
|
|
storeController storage.StoreController, retryOptions *retry.RetryOptions,
|
|
|
|
httpClient *resty.Client, log log.Logger) error {
|
2022-03-10 10:39:11 -05:00
|
|
|
err := pushSyncedLocalImage(localRepo, tag, localCachePath, storeController, log)
|
2022-01-10 11:06:12 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msgf("error while pushing synced cached image %s",
|
2022-03-10 10:39:11 -05:00
|
|
|
fmt.Sprintf("%s/%s:%s", localCachePath, localRepo, tag))
|
2022-01-10 11:06:12 -05:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = retry.RetryIfNecessary(context.Background(), func() error {
|
2022-03-10 10:39:11 -05:00
|
|
|
err = syncSignatures(httpClient, storeController, upstreamURL, remoteRepo, localRepo, tag, log)
|
2022-01-10 11:06:12 -05:00
|
|
|
|
|
|
|
return err
|
|
|
|
}, retryOptions); err != nil {
|
2022-03-10 10:39:11 -05:00
|
|
|
log.Error().Err(err).Msgf("couldn't copy image signature for %s/%s:%s", upstreamURL, remoteRepo, tag)
|
2022-01-10 11:06:12 -05:00
|
|
|
}
|
|
|
|
|
2022-03-10 10:39:11 -05:00
|
|
|
log.Info().Msgf("successfully synced %s/%s:%s", upstreamURL, remoteRepo, tag)
|
2022-01-10 11:06:12 -05:00
|
|
|
|
|
|
|
return nil
|
2021-06-08 15:11:18 -05:00
|
|
|
}
|