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,
|
|
|
|
repo, tag string, isArtifact bool, log log.Logger) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
imageStore := storeController.GetImageStore(repo)
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
repos := filterRepos([]string{repo}, regCfg.Content, log)
|
|
|
|
if len(repos) == 0 {
|
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",
|
|
|
|
repo, regCfg.URLs)
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-10-25 07:05:03 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
err = syncNotarySignature(httpClient, storeController, *regURL, repo, tag, log)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msgf("couldn't copy image signature %s/%s:%s", upstreamURL, repo, 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
|
|
|
|
err = syncCosignSignature(httpClient, storeController, *regURL, repo, tag, log)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msgf("couldn't copy image signature %s/%s:%s", upstreamURL, repo, tag)
|
|
|
|
|
|
|
|
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-01-10 11:06:12 -05:00
|
|
|
upstreamImageRef, err := getImageRef(upstreamAddr, repo, 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",
|
|
|
|
upstreamAddr, repo, 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-01-10 11:06:12 -05:00
|
|
|
localCachePath, err := getLocalCachePath(imageStore, repo)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Str("dir", localCachePath).Msg("couldn't create temporary dir")
|
2021-06-08 15:11:18 -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-29 10:14:56 -05:00
|
|
|
}
|
2021-06-08 15:11:18 -05:00
|
|
|
|
2022-01-10 11:06:12 -05:00
|
|
|
localImageRef, err := getLocalImageRef(localCachePath, repo, tag)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msgf("couldn't obtain a valid image reference for reference %s/%s:%s",
|
|
|
|
localCachePath, repo, 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-01-10 11:06:12 -05:00
|
|
|
demandedImageRef := fmt.Sprintf("%s/%s:%s", upstreamAddr, repo, tag)
|
|
|
|
|
|
|
|
_, 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 {
|
|
|
|
_ = finishSyncing(repo, tag, localCachePath, upstreamURL, storeController, retryOptions, httpClient, log)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
} else {
|
|
|
|
err := finishSyncing(repo, 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.
|
|
|
|
func finishSyncing(repo, tag, localCachePath, upstreamURL string,
|
|
|
|
storeController storage.StoreController, retryOptions *retry.RetryOptions,
|
|
|
|
httpClient *resty.Client, log log.Logger) error {
|
|
|
|
err := pushSyncedLocalImage(repo, tag, localCachePath, storeController, log)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msgf("error while pushing synced cached image %s",
|
|
|
|
fmt.Sprintf("%s/%s:%s", localCachePath, repo, tag))
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = retry.RetryIfNecessary(context.Background(), func() error {
|
|
|
|
err = syncSignatures(httpClient, storeController, upstreamURL, repo, tag, log)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}, retryOptions); err != nil {
|
|
|
|
log.Error().Err(err).Msgf("couldn't copy image signature for %s/%s:%s", upstreamURL, repo, tag)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info().Msgf("successfully synced %s/%s:%s", upstreamURL, repo, tag)
|
|
|
|
|
|
|
|
return nil
|
2021-06-08 15:11:18 -05:00
|
|
|
}
|