2021-06-08 15:11:18 -05:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
2021-12-07 13:26:26 -05:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
2021-06-08 15:11:18 -05:00
|
|
|
"encoding/json"
|
2021-12-17 11:34:22 -05:00
|
|
|
"errors"
|
2022-01-10 11:06:12 -05:00
|
|
|
"fmt"
|
2021-12-07 13:26:26 -05:00
|
|
|
"net/url"
|
2021-06-08 15:11:18 -05:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
2021-11-25 07:04:39 -05:00
|
|
|
glob "github.com/bmatcuk/doublestar/v4"
|
2022-01-10 11:06:12 -05:00
|
|
|
"github.com/containers/image/v5/docker"
|
2021-06-08 15:11:18 -05:00
|
|
|
"github.com/containers/image/v5/docker/reference"
|
2022-01-10 11:06:12 -05:00
|
|
|
"github.com/containers/image/v5/oci/layout"
|
2021-06-08 15:11:18 -05:00
|
|
|
"github.com/containers/image/v5/types"
|
2022-01-10 11:06:12 -05:00
|
|
|
guuid "github.com/gofrs/uuid"
|
2021-10-28 04:10:01 -05:00
|
|
|
ispec "github.com/opencontainers/image-spec/specs-go/v1"
|
2021-12-07 13:26:26 -05:00
|
|
|
artifactspec "github.com/oras-project/artifacts-spec/specs-go/v1"
|
2022-03-07 03:45:10 -05:00
|
|
|
"github.com/sigstore/cosign/pkg/oci/static"
|
2021-12-07 13:26:26 -05:00
|
|
|
"gopkg.in/resty.v1"
|
2021-12-17 11:34:22 -05:00
|
|
|
zerr "zotregistry.io/zot/errors"
|
2021-12-29 10:14:56 -05:00
|
|
|
"zotregistry.io/zot/pkg/common"
|
2021-12-03 22:50:58 -05:00
|
|
|
"zotregistry.io/zot/pkg/extensions/monitoring"
|
|
|
|
"zotregistry.io/zot/pkg/log"
|
|
|
|
"zotregistry.io/zot/pkg/storage"
|
2022-09-30 12:35:16 -05:00
|
|
|
"zotregistry.io/zot/pkg/storage/local"
|
2022-02-18 06:36:50 -05:00
|
|
|
"zotregistry.io/zot/pkg/test"
|
2021-06-08 15:11:18 -05:00
|
|
|
)
|
|
|
|
|
2021-12-07 13:26:26 -05:00
|
|
|
type ReferenceList struct {
|
2022-03-07 03:45:10 -05:00
|
|
|
References []artifactspec.Descriptor `json:"references"`
|
2021-12-07 13:26:26 -05:00
|
|
|
}
|
|
|
|
|
2022-04-21 03:09:08 -05:00
|
|
|
func TypeOf(v interface{}) string {
|
|
|
|
return fmt.Sprintf("%T", v)
|
|
|
|
}
|
|
|
|
|
2021-06-08 15:11:18 -05:00
|
|
|
// getTagFromRef returns a tagged reference from an image reference.
|
|
|
|
func getTagFromRef(ref types.ImageReference, log log.Logger) reference.Tagged {
|
|
|
|
tagged, isTagged := ref.DockerReference().(reference.Tagged)
|
|
|
|
if !isTagged {
|
|
|
|
log.Warn().Msgf("internal server error, reference %s does not have a tag, skipping", ref.DockerReference())
|
|
|
|
}
|
|
|
|
|
|
|
|
return tagged
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseRepositoryReference parses input into a reference.Named, and verifies that it names a repository, not an image.
|
|
|
|
func parseRepositoryReference(input string) (reference.Named, error) {
|
|
|
|
ref, err := reference.ParseNormalizedNamed(input)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reference.IsNameOnly(ref) {
|
2021-12-17 11:34:22 -05:00
|
|
|
return nil, zerr.ErrInvalidRepositoryName
|
2021-06-08 15:11:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return ref, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// filterRepos filters repos based on prefix given in the config.
|
2021-12-13 14:23:31 -05:00
|
|
|
func filterRepos(repos []string, contentList []Content, log log.Logger) map[int][]string {
|
2021-06-08 15:11:18 -05:00
|
|
|
filtered := make(map[int][]string)
|
|
|
|
|
|
|
|
for _, repo := range repos {
|
2021-12-13 14:23:31 -05:00
|
|
|
for contentID, content := range contentList {
|
2021-06-08 15:11:18 -05:00
|
|
|
var prefix string
|
2021-11-25 07:04:39 -05:00
|
|
|
// handle prefixes starting with '/'
|
2021-12-13 14:23:31 -05:00
|
|
|
if strings.HasPrefix(content.Prefix, "/") {
|
|
|
|
prefix = content.Prefix[1:]
|
2021-06-08 15:11:18 -05:00
|
|
|
} else {
|
2021-12-13 14:23:31 -05:00
|
|
|
prefix = content.Prefix
|
2021-06-08 15:11:18 -05:00
|
|
|
}
|
|
|
|
|
2021-11-25 07:04:39 -05:00
|
|
|
matched, err := glob.Match(prefix, repo)
|
|
|
|
if err != nil {
|
2022-04-21 03:09:08 -05:00
|
|
|
log.Error().Str("errorType", TypeOf(err)).
|
|
|
|
Err(err).Str("pattern",
|
2021-11-25 07:04:39 -05:00
|
|
|
prefix).Msg("error while parsing glob pattern, skipping it...")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-06-08 15:11:18 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if matched {
|
|
|
|
filtered[contentID] = append(filtered[contentID], repo)
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-06-08 15:11:18 -05:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return filtered
|
|
|
|
}
|
|
|
|
|
2022-03-10 10:39:11 -05:00
|
|
|
// findRepoContentID return the contentID that maches the localRepo path for a given RegistryConfig in the config file.
|
|
|
|
func findRepoMatchingContentID(localRepo string, contentList []Content) (int, error) {
|
|
|
|
contentID := -1
|
|
|
|
localRepo = strings.Trim(localRepo, "/")
|
|
|
|
|
|
|
|
for cID, content := range contentList {
|
|
|
|
// make sure prefix ends in "/" to extract the meta characters
|
|
|
|
prefix := strings.Trim(content.Prefix, "/") + "/"
|
|
|
|
destination := strings.Trim(content.Destination, "/")
|
|
|
|
|
|
|
|
var patternSlice []string
|
|
|
|
|
|
|
|
if content.StripPrefix {
|
|
|
|
_, metaCharacters := glob.SplitPattern(prefix)
|
|
|
|
patternSlice = append(patternSlice, destination, metaCharacters)
|
|
|
|
} else {
|
|
|
|
patternSlice = append(patternSlice, destination, prefix)
|
|
|
|
}
|
|
|
|
|
|
|
|
pattern := strings.Trim(strings.Join(patternSlice, "/"), "/")
|
|
|
|
|
|
|
|
matched, err := glob.Match(pattern, localRepo)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if matched {
|
|
|
|
contentID = cID
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if contentID == -1 {
|
|
|
|
return -1, zerr.ErrRegistryNoContent
|
|
|
|
}
|
|
|
|
|
|
|
|
return contentID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getRepoSource(localRepo string, content Content) string {
|
|
|
|
localRepo = strings.Trim(localRepo, "/")
|
|
|
|
destination := strings.Trim(content.Destination, "/")
|
|
|
|
prefix := strings.Trim(content.Prefix, "/*")
|
|
|
|
|
|
|
|
var localRepoSlice []string
|
|
|
|
|
|
|
|
localRepo = strings.TrimPrefix(localRepo, destination)
|
|
|
|
localRepo = strings.Trim(localRepo, "/")
|
|
|
|
|
|
|
|
if content.StripPrefix {
|
|
|
|
localRepoSlice = append([]string{prefix}, localRepo)
|
|
|
|
} else {
|
|
|
|
localRepoSlice = []string{localRepo}
|
|
|
|
}
|
|
|
|
|
|
|
|
repoSource := strings.Join(localRepoSlice, "/")
|
|
|
|
if repoSource == "/" {
|
|
|
|
return repoSource
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Trim(repoSource, "/")
|
|
|
|
}
|
|
|
|
|
|
|
|
// getRepoDestination returns the local storage path of the synced repo based on the specified destination.
|
|
|
|
func getRepoDestination(remoteRepo string, content Content) string {
|
|
|
|
remoteRepo = strings.Trim(remoteRepo, "/")
|
|
|
|
destination := strings.Trim(content.Destination, "/")
|
|
|
|
prefix := strings.Trim(content.Prefix, "/*")
|
|
|
|
|
|
|
|
var repoDestSlice []string
|
|
|
|
|
|
|
|
if content.StripPrefix {
|
|
|
|
remoteRepo = strings.TrimPrefix(remoteRepo, prefix)
|
|
|
|
remoteRepo = strings.Trim(remoteRepo, "/")
|
|
|
|
repoDestSlice = append(repoDestSlice, destination, remoteRepo)
|
|
|
|
} else {
|
|
|
|
repoDestSlice = append(repoDestSlice, destination, remoteRepo)
|
|
|
|
}
|
|
|
|
|
|
|
|
repoDestination := strings.Join(repoDestSlice, "/")
|
|
|
|
|
|
|
|
if repoDestination == "/" {
|
|
|
|
return "/"
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Trim(repoDestination, "/")
|
|
|
|
}
|
|
|
|
|
2021-06-08 15:11:18 -05:00
|
|
|
// Get sync.FileCredentials from file.
|
|
|
|
func getFileCredentials(filepath string) (CredentialsFile, error) {
|
2022-09-02 07:56:02 -05:00
|
|
|
credsFile, err := os.ReadFile(filepath)
|
2021-06-08 15:11:18 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var creds CredentialsFile
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = json.Unmarshal(credsFile, &creds)
|
2021-06-08 15:11:18 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return creds, nil
|
|
|
|
}
|
2021-10-28 04:10:01 -05:00
|
|
|
|
2021-12-29 10:14:56 -05:00
|
|
|
func getHTTPClient(regCfg *RegistryConfig, upstreamURL string, credentials Credentials,
|
2022-03-21 12:37:23 -05:00
|
|
|
log log.Logger,
|
|
|
|
) (*resty.Client, *url.URL, error) {
|
2021-12-07 13:26:26 -05:00
|
|
|
client := resty.New()
|
|
|
|
|
2021-12-29 10:14:56 -05:00
|
|
|
if !common.Contains(regCfg.URLs, upstreamURL) {
|
2022-03-07 03:45:10 -05:00
|
|
|
return nil, nil, zerr.ErrSyncInvalidUpstreamURL
|
2021-12-29 10:14:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
registryURL, err := url.Parse(upstreamURL)
|
2022-01-27 07:45:46 -05:00
|
|
|
if err != nil {
|
2022-04-21 03:09:08 -05:00
|
|
|
log.Error().Str("errorType", TypeOf(err)).
|
|
|
|
Err(err).Str("url", upstreamURL).Msg("couldn't parse url")
|
2022-01-27 07:45:46 -05:00
|
|
|
|
2022-03-07 03:45:10 -05:00
|
|
|
return nil, nil, err
|
2022-01-27 07:45:46 -05:00
|
|
|
}
|
|
|
|
|
2021-12-07 13:26:26 -05:00
|
|
|
if regCfg.CertDir != "" {
|
|
|
|
log.Debug().Msgf("sync: using certs directory: %s", regCfg.CertDir)
|
|
|
|
clientCert := path.Join(regCfg.CertDir, "client.cert")
|
|
|
|
clientKey := path.Join(regCfg.CertDir, "client.key")
|
|
|
|
caCertPath := path.Join(regCfg.CertDir, "ca.crt")
|
|
|
|
|
2022-09-02 07:56:02 -05:00
|
|
|
caCert, err := os.ReadFile(caCertPath)
|
2021-12-07 13:26:26 -05:00
|
|
|
if err != nil {
|
2022-04-21 03:09:08 -05:00
|
|
|
log.Error().Str("errorType", TypeOf(err)).
|
|
|
|
Err(err).Msg("couldn't read CA certificate")
|
2021-12-07 13:26:26 -05:00
|
|
|
|
2022-03-07 03:45:10 -05:00
|
|
|
return nil, nil, err
|
2021-12-07 13:26:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
caCertPool := x509.NewCertPool()
|
|
|
|
caCertPool.AppendCertsFromPEM(caCert)
|
|
|
|
|
|
|
|
client.SetTLSClientConfig(&tls.Config{RootCAs: caCertPool, MinVersion: tls.VersionTLS12})
|
|
|
|
|
|
|
|
cert, err := tls.LoadX509KeyPair(clientCert, clientKey)
|
|
|
|
if err != nil {
|
2022-04-21 03:09:08 -05:00
|
|
|
log.Error().Str("errorType", TypeOf(err)).
|
|
|
|
Err(err).Msg("couldn't read certificates key pairs")
|
2021-12-07 13:26:26 -05:00
|
|
|
|
2022-03-07 03:45:10 -05:00
|
|
|
return nil, nil, err
|
2021-12-07 13:26:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
client.SetCertificates(cert)
|
|
|
|
}
|
|
|
|
|
2022-10-05 05:21:14 -05:00
|
|
|
//nolint: gosec
|
2022-01-27 07:45:46 -05:00
|
|
|
if regCfg.TLSVerify != nil && !*regCfg.TLSVerify && registryURL.Scheme == "https" {
|
2021-12-07 13:26:26 -05:00
|
|
|
client.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
|
|
|
|
}
|
|
|
|
|
|
|
|
if credentials.Username != "" && credentials.Password != "" {
|
|
|
|
log.Debug().Msgf("sync: using basic auth")
|
|
|
|
client.SetBasicAuth(credentials.Username, credentials.Password)
|
|
|
|
}
|
|
|
|
|
2022-05-25 05:23:06 -05:00
|
|
|
client.SetRedirectPolicy(resty.FlexibleRedirectPolicy(httpMaxRedirectsCount))
|
|
|
|
|
2022-03-07 03:45:10 -05:00
|
|
|
return client, registryURL, nil
|
2021-12-07 13:26:26 -05:00
|
|
|
}
|
|
|
|
|
2022-03-10 10:39:11 -05:00
|
|
|
func pushSyncedLocalImage(localRepo, tag, localCachePath string,
|
2022-03-21 12:37:23 -05:00
|
|
|
imageStore storage.ImageStore, log log.Logger,
|
|
|
|
) error {
|
2022-03-10 10:39:11 -05:00
|
|
|
log.Info().Msgf("pushing synced local image %s/%s:%s to local registry", localCachePath, localRepo, tag)
|
2021-10-28 04:10:01 -05:00
|
|
|
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
2022-06-24 08:08:47 -05:00
|
|
|
|
2022-09-30 12:35:16 -05:00
|
|
|
cacheImageStore := local.NewImageStore(localCachePath, false,
|
2022-06-24 08:08:47 -05:00
|
|
|
storage.DefaultGCDelay, false, false, log, metrics, nil)
|
2021-10-28 04:10:01 -05:00
|
|
|
|
2022-03-10 10:39:11 -05:00
|
|
|
manifestContent, _, _, err := cacheImageStore.GetImageManifest(localRepo, tag)
|
2021-10-28 04:10:01 -05:00
|
|
|
if err != nil {
|
2022-04-21 03:09:08 -05:00
|
|
|
log.Error().Str("errorType", TypeOf(err)).
|
|
|
|
Err(err).Str("dir", path.Join(cacheImageStore.RootDir(), localRepo)).
|
2022-03-10 10:39:11 -05:00
|
|
|
Msg("couldn't find index.json")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-10-28 04:10:01 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var manifest ispec.Manifest
|
|
|
|
|
|
|
|
if err := json.Unmarshal(manifestContent, &manifest); err != nil {
|
2022-04-21 03:09:08 -05:00
|
|
|
log.Error().Str("errorType", TypeOf(err)).
|
|
|
|
Err(err).Str("dir", path.Join(cacheImageStore.RootDir(), localRepo)).
|
2022-03-10 10:39:11 -05:00
|
|
|
Msg("invalid JSON")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-10-28 04:10:01 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, blob := range manifest.Layers {
|
2022-08-19 05:38:59 -05:00
|
|
|
err = copyBlob(localRepo, blob.Digest.String(), blob.MediaType,
|
|
|
|
cacheImageStore, imageStore, log)
|
2021-10-28 04:10:01 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-19 05:38:59 -05:00
|
|
|
err = copyBlob(localRepo, manifest.Config.Digest.String(), manifest.Config.MediaType,
|
|
|
|
cacheImageStore, imageStore, log)
|
2021-10-28 04:10:01 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-06-24 08:08:47 -05:00
|
|
|
_, err = imageStore.PutImageManifest(localRepo, tag,
|
|
|
|
ispec.MediaTypeImageManifest, manifestContent)
|
2021-10-28 04:10:01 -05:00
|
|
|
if err != nil {
|
2022-06-24 08:08:47 -05:00
|
|
|
if errors.Is(err, zerr.ErrImageLintAnnotations) {
|
|
|
|
log.Error().Str("errorType", TypeOf(err)).
|
|
|
|
Err(err).Msg("couldn't upload manifest because of missing annotations")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-21 03:09:08 -05:00
|
|
|
log.Error().Str("errorType", TypeOf(err)).
|
|
|
|
Err(err).Msg("couldn't upload manifest")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-10-28 04:10:01 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-12-07 13:26:26 -05:00
|
|
|
|
2022-08-19 05:38:59 -05:00
|
|
|
// Copy a blob from one image store to another image store.
|
|
|
|
func copyBlob(localRepo, blobDigest, blobMediaType string,
|
|
|
|
souceImageStore, destinationImageStore storage.ImageStore, log log.Logger,
|
|
|
|
) error {
|
|
|
|
if found, _, _ := destinationImageStore.CheckBlob(localRepo, blobDigest); found {
|
|
|
|
// Blob is already at destination, nothing to do
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
blobReadCloser, _, err := souceImageStore.GetBlob(localRepo, blobDigest, blobMediaType)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Str("errorType", TypeOf(err)).Err(err).
|
|
|
|
Str("dir", path.Join(souceImageStore.RootDir(), localRepo)).
|
|
|
|
Str("blob digest", blobDigest).Str("media type", blobMediaType).
|
|
|
|
Msg("couldn't read blob")
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer blobReadCloser.Close()
|
|
|
|
|
|
|
|
_, _, err = destinationImageStore.FullBlobUpload(localRepo, blobReadCloser, blobDigest)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Str("errorType", TypeOf(err)).Err(err).
|
|
|
|
Str("blob digest", blobDigest).Str("media type", blobMediaType).
|
|
|
|
Msg("couldn't upload blob")
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-29 10:14:56 -05:00
|
|
|
// sync needs transport to be stripped to not be wrongly interpreted as an image reference
|
|
|
|
// at a non-fully qualified registry (hostname as image and port as tag).
|
|
|
|
func StripRegistryTransport(url string) string {
|
|
|
|
return strings.Replace(strings.Replace(url, "http://", "", 1), "https://", "", 1)
|
|
|
|
}
|
2022-01-10 11:06:12 -05:00
|
|
|
|
|
|
|
// get an ImageReference given the registry, repo and tag.
|
|
|
|
func getImageRef(registryDomain, repo, tag string) (types.ImageReference, error) {
|
|
|
|
repoRef, err := parseRepositoryReference(fmt.Sprintf("%s/%s", registryDomain, repo))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
taggedRepoRef, err := reference.WithTag(repoRef, tag)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
imageRef, err := docker.NewReference(taggedRepoRef)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return imageRef, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// get a local ImageReference used to temporary store one synced image.
|
2022-04-05 10:18:31 -05:00
|
|
|
func getLocalImageRef(localCachePath, repo, tag string) (types.ImageReference, error) {
|
|
|
|
if _, err := os.ReadDir(localCachePath); err != nil {
|
|
|
|
return nil, err
|
2022-02-18 06:36:50 -05:00
|
|
|
}
|
|
|
|
|
2022-01-10 11:06:12 -05:00
|
|
|
localRepo := path.Join(localCachePath, repo)
|
|
|
|
localTaggedRepo := fmt.Sprintf("%s:%s", localRepo, tag)
|
|
|
|
|
|
|
|
localImageRef, err := layout.ParseReference(localTaggedRepo)
|
|
|
|
if err != nil {
|
2022-04-05 10:18:31 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return localImageRef, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the localCachePath with an UUID at the end. Only to be called once per repo.
|
|
|
|
func getLocalCachePath(imageStore storage.ImageStore, repo string) (string, error) {
|
|
|
|
localRepoPath := path.Join(imageStore.RootDir(), repo, SyncBlobUploadDir)
|
|
|
|
// check if SyncBlobUploadDir exists, create if not
|
|
|
|
var err error
|
|
|
|
if _, err = os.ReadDir(localRepoPath); os.IsNotExist(err) {
|
2022-09-30 12:35:16 -05:00
|
|
|
if err = os.MkdirAll(localRepoPath, local.DefaultDirPerms); err != nil {
|
2022-04-05 10:18:31 -05:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// create uuid folder
|
|
|
|
uuid, err := guuid.NewV4()
|
|
|
|
// hard to reach test case, injected error, see pkg/test/dev.go
|
|
|
|
if err := test.Error(err); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
localCachePath := path.Join(localRepoPath, uuid.String())
|
|
|
|
|
|
|
|
cachedRepoPath := path.Join(localCachePath, repo)
|
2022-09-30 12:35:16 -05:00
|
|
|
if err = os.MkdirAll(cachedRepoPath, local.DefaultDirPerms); err != nil {
|
2022-04-05 10:18:31 -05:00
|
|
|
return "", err
|
2022-01-10 11:06:12 -05:00
|
|
|
}
|
|
|
|
|
2022-04-05 10:18:31 -05:00
|
|
|
return localCachePath, nil
|
2022-01-10 11:06:12 -05:00
|
|
|
}
|
2021-12-17 11:34:22 -05:00
|
|
|
|
2022-03-07 03:45:10 -05:00
|
|
|
// canSkipImage returns whether or not we already synced this image.
|
|
|
|
func canSkipImage(repo, tag, digest string, imageStore storage.ImageStore, log log.Logger) (bool, error) {
|
|
|
|
// check image already synced
|
|
|
|
_, localImageManifestDigest, _, err := imageStore.GetImageManifest(repo, tag)
|
2021-12-17 11:34:22 -05:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, zerr.ErrRepoNotFound) || errors.Is(err, zerr.ErrManifestNotFound) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2022-04-21 03:09:08 -05:00
|
|
|
log.Error().Str("errorType", TypeOf(err)).
|
|
|
|
Err(err).Msgf("couldn't get local image %s:%s manifest", repo, tag)
|
2021-12-17 11:34:22 -05:00
|
|
|
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2022-03-07 03:45:10 -05:00
|
|
|
if localImageManifestDigest != digest {
|
|
|
|
log.Info().Msgf("upstream image %s:%s digest changed, syncing again", repo, tag)
|
2021-12-17 11:34:22 -05:00
|
|
|
|
2022-03-07 03:45:10 -05:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func manifestsEqual(manifest1, manifest2 ispec.Manifest) bool {
|
|
|
|
if manifest1.Config.Digest == manifest2.Config.Digest &&
|
|
|
|
manifest1.Config.MediaType == manifest2.Config.MediaType &&
|
2022-04-14 14:07:44 -05:00
|
|
|
manifest1.Config.Size == manifest2.Config.Size {
|
|
|
|
if descriptorsEqual(manifest1.Layers, manifest2.Layers) {
|
2022-03-07 03:45:10 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func artifactDescriptorsEqual(desc1, desc2 []artifactspec.Descriptor) bool {
|
2022-04-14 14:07:44 -05:00
|
|
|
if len(desc1) != len(desc2) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for id, desc := range desc1 {
|
|
|
|
if desc.Digest != desc2[id].Digest ||
|
|
|
|
desc.Size != desc2[id].Size ||
|
|
|
|
desc.MediaType != desc2[id].MediaType ||
|
|
|
|
desc.ArtifactType != desc2[id].ArtifactType {
|
|
|
|
return false
|
2022-03-07 03:45:10 -05:00
|
|
|
}
|
2021-12-17 11:34:22 -05:00
|
|
|
}
|
|
|
|
|
2022-04-14 14:07:44 -05:00
|
|
|
return true
|
2022-03-07 03:45:10 -05:00
|
|
|
}
|
2021-12-17 11:34:22 -05:00
|
|
|
|
2022-04-14 14:07:44 -05:00
|
|
|
func descriptorsEqual(desc1, desc2 []ispec.Descriptor) bool {
|
|
|
|
if len(desc1) != len(desc2) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for id, desc := range desc1 {
|
|
|
|
if desc.Digest != desc2[id].Digest ||
|
|
|
|
desc.Size != desc2[id].Size ||
|
|
|
|
desc.MediaType != desc2[id].MediaType ||
|
|
|
|
desc.Annotations[static.SignatureAnnotationKey] != desc2[id].Annotations[static.SignatureAnnotationKey] {
|
|
|
|
return false
|
2022-03-07 03:45:10 -05:00
|
|
|
}
|
2021-12-17 11:34:22 -05:00
|
|
|
}
|
|
|
|
|
2022-04-14 14:07:44 -05:00
|
|
|
return true
|
2021-12-17 11:34:22 -05:00
|
|
|
}
|