Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

imagePushToRegistry: update sourceImages and targetImages parameters #4707

Merged
merged 15 commits into from
Dec 12, 2023
Merged
103 changes: 58 additions & 45 deletions cmd/imagePushToRegistry.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,14 @@ func imagePushToRegistry(config imagePushToRegistryOptions, telemetryData *telem
}

func runImagePushToRegistry(config *imagePushToRegistryOptions, telemetryData *telemetry.CustomData, utils imagePushToRegistryUtils) error {
if len(config.TargetImages) == 0 {
config.TargetImages = config.SourceImages
}

if len(config.TargetImages) != len(config.SourceImages) {
log.SetErrorCategory(log.ErrorConfiguration)
return errors.New("configuration error: please configure targetImage and sourceImage properly")
if !config.PushLocalDockerImage {
if len(config.TargetImages) == 0 {
config.TargetImages = mapSourceTargetImages(config.SourceImages)
}
if len(config.TargetImages) != len(config.SourceImages) {
log.SetErrorCategory(log.ErrorConfiguration)
return errors.New("configuration error: please configure targetImage and sourceImage properly")
}
}

re := regexp.MustCompile(`^https?://`)
Expand All @@ -98,7 +99,7 @@ func runImagePushToRegistry(config *imagePushToRegistryOptions, telemetryData *t
return errors.Wrap(err, "failed to handle credentials for target registry")
}

if len(config.LocalDockerImagePath) > 0 {
if config.PushLocalDockerImage {
if err := pushLocalImageToTargetRegistry(config, utils); err != nil {
return errors.Wrapf(err, "failed to push local image to %q", config.TargetRegistryURL)
}
Expand Down Expand Up @@ -145,28 +146,35 @@ func copyImages(config *imagePushToRegistryOptions, utils imagePushToRegistryUti
g.SetLimit(10)
platform := config.TargetArchitecture

for i := 0; i < len(config.SourceImages); i++ {
src := fmt.Sprintf("%s/%s", config.SourceRegistryURL, config.SourceImages[i])
dst := fmt.Sprintf("%s/%s", config.TargetRegistryURL, config.TargetImages[i])
for _, sourceImage := range config.SourceImages {
sourceImage := sourceImage
src := fmt.Sprintf("%s/%s:%s", config.SourceRegistryURL, sourceImage, config.SourceImageTag)

g.Go(func() error {
log.Entry().Infof("Copying %s to %s...", src, dst)
if err := utils.CopyImage(ctx, src, dst, platform); err != nil {
return err
}
log.Entry().Infof("Copying %s to %s... Done", src, dst)
return nil
})
targetImage, ok := config.TargetImages[sourceImage].(string)
if !ok {
return fmt.Errorf("incorrect name of target image: %v", config.TargetImages[sourceImage])
}

if config.TargetImageTag != "" {
g.Go(func() error {
dst := fmt.Sprintf("%s/%s:%s", config.TargetRegistryURL, targetImage, config.TargetImageTag)
log.Entry().Infof("Copying %s to %s...", src, dst)
if err := utils.CopyImage(ctx, src, dst, platform); err != nil {
return err
}
log.Entry().Infof("Copying %s to %s... Done", src, dst)
return nil
})
}

if config.TagLatest {
g.Go(func() error {
// imageName is repository + image, e.g test.registry/testImage
imageName := parseDockerImageName(dst)
log.Entry().Infof("Copying %s to %s...", src, imageName)
if err := utils.CopyImage(ctx, src, imageName, platform); err != nil {
dst := fmt.Sprintf("%s/%s", config.TargetRegistryURL, config.TargetImages[sourceImage])
log.Entry().Infof("Copying %s to %s...", src, dst)
if err := utils.CopyImage(ctx, src, dst, platform); err != nil {
return err
}
log.Entry().Infof("Copying %s to %s... Done", src, imageName)
log.Entry().Infof("Copying %s to %s... Done", src, dst)
return nil
})
}
m1ron0xFF marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -191,27 +199,33 @@ func pushLocalImageToTargetRegistry(config *imagePushToRegistryOptions, utils im
}
log.Entry().Infof("Loading local image... Done")

for i := 0; i < len(config.TargetImages); i++ {
dst := fmt.Sprintf("%s/%s", config.TargetRegistryURL, config.TargetImages[i])
for _, trgImage := range config.TargetImages {
trgImage := trgImage
targetImage, ok := trgImage.(string)
if !ok {
return fmt.Errorf("incorrect name of target image: %v", trgImage)
}

g.Go(func() error {
log.Entry().Infof("Pushing %s...", dst)
if err := utils.PushImage(ctx, img, dst, platform); err != nil {
return err
}
log.Entry().Infof("Pushing %s... Done", dst)
return nil
})
if config.TargetImageTag != "" {
g.Go(func() error {
dst := fmt.Sprintf("%s/%s:%s", config.TargetRegistryURL, targetImage, config.TargetImageTag)
log.Entry().Infof("Pushing %s...", dst)
if err := utils.PushImage(ctx, img, dst, platform); err != nil {
return err
}
log.Entry().Infof("Pushing %s... Done", dst)
return nil
})
}

if config.TagLatest {
g.Go(func() error {
// imageName is repository + image, e.g test.registry/testImage
imageName := parseDockerImageName(dst)
log.Entry().Infof("Pushing %s...", imageName)
if err := utils.PushImage(ctx, img, imageName, platform); err != nil {
dst := fmt.Sprintf("%s/%s", config.TargetRegistryURL, targetImage)
log.Entry().Infof("Pushing %s...", dst)
if err := utils.PushImage(ctx, img, dst, platform); err != nil {
return err
}
log.Entry().Infof("Pushing %s... Done", imageName)
log.Entry().Infof("Pushing %s... Done", dst)
return nil
})
m1ron0xFF marked this conversation as resolved.
Show resolved Hide resolved
}
Expand All @@ -224,12 +238,11 @@ func pushLocalImageToTargetRegistry(config *imagePushToRegistryOptions, utils im
return nil
}

func parseDockerImageName(image string) string {
re := regexp.MustCompile(`^(.*?)(?::([^:/]+))?$`)
matches := re.FindStringSubmatch(image)
if len(matches) > 1 {
return matches[1]
func mapSourceTargetImages(sourceImages []string) map[string]any {
targetImages := make(map[string]any, len(sourceImages))
for _, sourceImage := range sourceImages {
targetImages[sourceImage] = sourceImage
}

return image
return targetImages
}
97 changes: 63 additions & 34 deletions cmd/imagePushToRegistry_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading