Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/pkg/additional/const.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package additional

const (
latestTag = "latest"
collectorPrefix = "[AdditionalImagesCollector] "
errMsg = collectorPrefix + "%s"
)
147 changes: 93 additions & 54 deletions internal/pkg/additional/local_stored_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,68 +52,37 @@ func (o LocalStorageCollector) AdditionalImagesCollector(ctx context.Context) ([

o.Log.Debug(collectorPrefix+"setting copy option o.Opts.MultiArch=%s when collecting releases image", o.Opts.MultiArch)
for _, img := range o.Config.ImageSetConfigurationSpec.Mirror.AdditionalImages {
var src, dest, tmpSrc, tmpDest, origin string
var src, dest, tmpSrc, tmpDest string

origin := img.Name

Comment thread
coderabbitai[bot] marked this conversation as resolved.
imgSpec, err := image.ParseRef(img.Name)
if err != nil {
// OCPBUGS-33081 - skip if parse error (i.e semver and other)
o.Log.Warn("%v : SKIPPING", err)
continue
}
if o.Opts.IsMirrorToDisk() || o.Opts.IsMirrorToMirror() {

tmpSrc = imgSpec.ReferenceWithTransport
origin = img.Name
if imgSpec.Transport == consts.DockerProtocol {
if imgSpec.IsImageByDigestOnly() {
tmpDest = strings.Join([]string{o.destinationRegistry(), imgSpec.PathComponent}, "/") + ":" + imgSpec.Algorithm + "-" + imgSpec.Digest
} else if imgSpec.IsImageByTagAndDigest() { // OCPBUGS-33196 + OCPBUGS-37867- check source image for tag and digest
// use tag only for both src and dest
o.Log.Warn(collectorPrefix+"%s has both tag and digest : using digest to pull, but tag only for mirroring", imgSpec.Reference)
tmpSrc = strings.Join([]string{imgSpec.Domain, imgSpec.PathComponent}, "/") + "@" + imgSpec.Algorithm + ":" + imgSpec.Digest
tmpDest = strings.Join([]string{o.destinationRegistry(), imgSpec.PathComponent}, "/") + ":" + imgSpec.Tag
} else {
tmpDest = strings.Join([]string{o.destinationRegistry(), imgSpec.PathComponent}, "/") + ":" + imgSpec.Tag
}
} else { // oci image
// Although fetching the digest of the oci image (using o.Manifest.GetDigest) might work in mirrorToDisk and mirrorToMirror
// it will not work during diskToMirror as the oci image might not be on the disk any longer
tmpDest = strings.Join([]string{o.destinationRegistry(), strings.TrimPrefix(imgSpec.PathComponent, "/")}, "/") + ":latest"
}

} else if o.Opts.IsDiskToMirror() {
origin = img.Name
imgSpec, err := image.ParseRef(img.Name)
if err != nil {
o.Log.Error(errMsg, err.Error())
return nil, err
}

if imgSpec.Transport == consts.DockerProtocol {

if imgSpec.IsImageByDigestOnly() {
tmpSrc = strings.Join([]string{o.LocalStorageFQDN, imgSpec.PathComponent + ":" + imgSpec.Algorithm + "-" + imgSpec.Digest}, "/")
if o.generateV1DestTags {
tmpDest = strings.Join([]string{o.Opts.Destination, imgSpec.PathComponent + ":latest"}, "/")

} else {
tmpDest = strings.Join([]string{o.Opts.Destination, imgSpec.PathComponent + ":" + imgSpec.Algorithm + "-" + imgSpec.Digest}, "/")
}
} else if imgSpec.IsImageByTagAndDigest() { // OCPBUGS-33196 + OCPBUGS-37867- check source image for tag and digest
// use tag only for both src and dest
o.Log.Warn(collectorPrefix+"%s has both tag and digest : using tag only", imgSpec.Reference)
tmpSrc = strings.Join([]string{o.LocalStorageFQDN, imgSpec.PathComponent}, "/") + ":" + imgSpec.Tag
tmpDest = strings.Join([]string{o.Opts.Destination, imgSpec.PathComponent}, "/") + ":" + imgSpec.Tag
} else {
tmpSrc = strings.Join([]string{o.LocalStorageFQDN, imgSpec.PathComponent}, "/") + ":" + imgSpec.Tag
tmpDest = strings.Join([]string{o.Opts.Destination, imgSpec.PathComponent}, "/") + ":" + imgSpec.Tag
}

} else {
tmpSrc = strings.Join([]string{o.LocalStorageFQDN, strings.TrimPrefix(imgSpec.PathComponent, "/")}, "/") + ":latest"
tmpDest = strings.Join([]string{o.Opts.Destination, strings.TrimPrefix(imgSpec.PathComponent, "/")}, "/") + ":latest"
}

if img.TargetRepo != "" && !v2alpha1.IsValidPathComponent(img.TargetRepo) {
o.Log.Warn("invalid targetRepo %s for image %s : SKIPPING", img.TargetRepo, img.Name)
continue
}

targetRepo := imgSpec.PathComponent
if img.TargetRepo != "" {
targetRepo = img.TargetRepo
}

targetTag := imgSpec.Tag
if img.TargetTag != "" {
targetTag = img.TargetTag
}

switch {
case o.Opts.IsMirrorToDisk(), o.Opts.IsMirrorToMirror():
tmpSrc, tmpDest = o.buildMirrorToDiskPaths(img, imgSpec, targetRepo, targetTag)
case o.Opts.IsDiskToMirror(), o.Opts.IsDelete():
tmpSrc, tmpDest = o.buildDiskToMirrorPaths(img, imgSpec, targetRepo, targetTag)
Comment thread
dorzel marked this conversation as resolved.
}
if tmpSrc == "" || tmpDest == "" {
o.Log.Error(collectorPrefix+"unable to determine src %s or dst %s for %s", tmpSrc, tmpDest, img.Name)
Expand All @@ -140,3 +109,73 @@ func (o LocalStorageCollector) AdditionalImagesCollector(ctx context.Context) ([
}
return allImages, nil
}

// buildMirrorToDiskPaths constructs source and destination paths for mirror-to-disk and mirror-to-mirror operations
func (o LocalStorageCollector) buildMirrorToDiskPaths(img v2alpha1.Image, imgSpec image.ImageSpec, targetRepo, targetTag string) (string, string) {
tmpSrc := imgSpec.ReferenceWithTransport
var tmpDest string

if imgSpec.Transport != consts.DockerProtocol {
// oci image
// Although fetching the digest of the oci image (using o.Manifest.GetDigest) might work in mirrorToDisk and mirrorToMirror
// it will not work during diskToMirror as the oci image might not be on the disk any longer
tag := latestTag
if img.TargetTag != "" {
tag = targetTag
}
tmpDest = fmt.Sprintf("%s/%s:%s", o.destinationRegistry(), strings.TrimPrefix(targetRepo, "/"), tag)
return tmpSrc, tmpDest
}

// Docker protocol
switch {
case imgSpec.IsImageByTagAndDigest():
// OCPBUGS-33196 + OCPBUGS-37867- check source image for tag and digest
// use tag only for both src and dest
o.Log.Warn(collectorPrefix+"%s has both tag and digest : using digest to pull, but tag only for mirroring", imgSpec.Reference)
tmpSrc = fmt.Sprintf("%s/%s@%s:%s", imgSpec.Domain, imgSpec.PathComponent, imgSpec.Algorithm, imgSpec.Digest)
tmpDest = fmt.Sprintf("%s/%s:%s", o.destinationRegistry(), targetRepo, targetTag)
case imgSpec.IsImageByDigestOnly() && img.TargetTag == "":
tmpDest = fmt.Sprintf("%s/%s:%s-%s", o.destinationRegistry(), targetRepo, imgSpec.Algorithm, imgSpec.Digest)
default:
tmpDest = fmt.Sprintf("%s/%s:%s", o.destinationRegistry(), targetRepo, targetTag)
}

return tmpSrc, tmpDest
}

// buildDiskToMirrorPaths constructs source and destination paths for disk-to-mirror operations
func (o LocalStorageCollector) buildDiskToMirrorPaths(img v2alpha1.Image, imgSpec image.ImageSpec, targetRepo, targetTag string) (string, string) {
// Docker protocol
var tmpSrc, tmpDest string

if imgSpec.Transport != consts.DockerProtocol {
// oci image
tag := latestTag
if img.TargetTag != "" {
tag = targetTag
}
tmpSrc = fmt.Sprintf("%s/%s:%s", o.LocalStorageFQDN, strings.TrimPrefix(targetRepo, "/"), tag)
tmpDest = fmt.Sprintf("%s/%s:%s", o.Opts.Destination, strings.TrimPrefix(targetRepo, "/"), tag)
return tmpSrc, tmpDest
}

switch {
case imgSpec.IsImageByDigestOnly() && img.TargetTag == "" && o.generateV1DestTags:
tmpSrc = fmt.Sprintf("%s/%s:%s-%s", o.LocalStorageFQDN, targetRepo, imgSpec.Algorithm, imgSpec.Digest)
tmpDest = fmt.Sprintf("%s/%s:%s", o.Opts.Destination, targetRepo, latestTag)
case imgSpec.IsImageByDigestOnly() && img.TargetTag == "":
digestTag := imgSpec.Algorithm + "-" + imgSpec.Digest
tmpSrc = fmt.Sprintf("%s/%s:%s", o.LocalStorageFQDN, targetRepo, digestTag)
tmpDest = fmt.Sprintf("%s/%s:%s", o.Opts.Destination, targetRepo, digestTag)
default:
// OCPBUGS-33196 + OCPBUGS-37867- check source image for tag and digest
if imgSpec.IsImageByTagAndDigest() {
o.Log.Warn(collectorPrefix+"%s has both tag and digest : using tag only", imgSpec.Reference)
}
tmpSrc = fmt.Sprintf("%s/%s:%s", o.LocalStorageFQDN, targetRepo, targetTag)
tmpDest = fmt.Sprintf("%s/%s:%s", o.Opts.Destination, targetRepo, targetTag)
}

return tmpSrc, tmpDest
}
Loading