Skip to content

Commit

Permalink
imagePushToRegistry: update sourceImages and targetImages parameters (#…
Browse files Browse the repository at this point in the history
…4707)

* Add imageTag param

* Make imageTag mandatory if tagArtifactVersion is true && update logic

* Make sourceRegistryURL mandatory if localDockerImagePath is not set

* Make some param mandatoryIf

* Change format of sourceImages param

* Add source image tag

* Update sourceImages and targetImages params

* Delete unused function

* Clean up tests

* Update

* Update metadata file

* Update tests

* Fix test

* Fix tests
  • Loading branch information
vstarostin authored Dec 12, 2023
1 parent a342f49 commit 0838264
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 142 deletions.
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
})
}
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
})
}
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

0 comments on commit 0838264

Please sign in to comment.