Skip to content

Commit

Permalink
feat(imagePushToRegistry): Support imageNameTags (#4853)
Browse files Browse the repository at this point in the history
* add imageNameTags related parameters to step

* fix registry+imageNameTags

* add debug logging

* remove debug logging

* update parameter docs

---------

Co-authored-by: jliempt <>
  • Loading branch information
jliempt authored Mar 18, 2024
1 parent df2e976 commit 8bf6298
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 1 deletion.
47 changes: 46 additions & 1 deletion cmd/imagePushToRegistry.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func imagePushToRegistry(config imagePushToRegistryOptions, telemetryData *telem
}

func runImagePushToRegistry(config *imagePushToRegistryOptions, telemetryData *telemetry.CustomData, utils imagePushToRegistryUtils) error {
if !config.PushLocalDockerImage {
if !config.PushLocalDockerImage && !config.UseImageNameTags {
if len(config.TargetImages) == 0 {
config.TargetImages = mapSourceTargetImages(config.SourceImages)
}
Expand All @@ -91,6 +91,13 @@ func runImagePushToRegistry(config *imagePushToRegistryOptions, telemetryData *t
}
}

if config.UseImageNameTags {
if len(config.TargetImageNameTags) > 0 && len(config.TargetImageNameTags) != len(config.SourceImageNameTags) {
log.SetErrorCategory(log.ErrorConfiguration)
return errors.New("configuration error: please configure targetImageNameTags and sourceImageNameTags properly")
}
}

// Docker image tags don't allow plus signs in tags, thus replacing with dash
config.SourceImageTag = strings.ReplaceAll(config.SourceImageTag, "+", "-")
config.TargetImageTag = strings.ReplaceAll(config.TargetImageTag, "+", "-")
Expand All @@ -115,6 +122,13 @@ func runImagePushToRegistry(config *imagePushToRegistryOptions, telemetryData *t
return errors.Wrap(err, "failed to handle credentials for source registry")
}

if config.UseImageNameTags {
if err := pushImageNameTagsToTargetRegistry(config, utils); err != nil {
return errors.Wrapf(err, "failed to push imageNameTags to target registry")
}
return nil
}

if err := copyImages(config, utils); err != nil {
return errors.Wrap(err, "failed to copy images")
}
Expand Down Expand Up @@ -242,6 +256,37 @@ func pushLocalImageToTargetRegistry(config *imagePushToRegistryOptions, utils im
return nil
}

func pushImageNameTagsToTargetRegistry(config *imagePushToRegistryOptions, utils imagePushToRegistryUtils) error {
g, ctx := errgroup.WithContext(context.Background())
g.SetLimit(10)

for i, sourceImageNameTag := range config.SourceImageNameTags {
src := fmt.Sprintf("%s/%s", config.SourceRegistryURL, sourceImageNameTag)

dst := ""
if len(config.TargetImageNameTags) == 0 {
dst = fmt.Sprintf("%s/%s", config.TargetRegistryURL, sourceImageNameTag)
} else {
dst = fmt.Sprintf("%s/%s", config.TargetRegistryURL, config.TargetImageNameTags[i])
}

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

if err := g.Wait(); err != nil {
return err
}

return nil
}

func mapSourceTargetImages(sourceImages []string) map[string]any {
targetImages := make(map[string]any, len(sourceImages))
for _, sourceImage := range sourceImages {
Expand Down
38 changes: 38 additions & 0 deletions cmd/imagePushToRegistry_generated.go

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

26 changes: 26 additions & 0 deletions cmd/imagePushToRegistry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,32 @@ func TestRunImagePushToRegistry(t *testing.T) {
assert.Equal(t, "1.0.0-123-456", config.TargetImageTag)
})

t.Run("multiple imageNameTags", func(t *testing.T) {
t.Parallel()

config := imagePushToRegistryOptions{
SourceRegistryURL: "https://source.registry",
SourceImages: []string{"source-image"},
SourceImageNameTags: []string{"com.sap.docker/ppiper:240104-20240227184612",
"com.sap.docker/ppiper:240104-20240227184612-amd64",
"com.sap.docker/ppiper:240104-20240227184612-aarch64",
},
SourceRegistryUser: "sourceuser",
SourceRegistryPassword: "sourcepassword",
TargetRegistryURL: "https://target.registry",
TargetImageTag: "1.0.0-123+456",
TargetRegistryUser: "targetuser",
TargetRegistryPassword: "targetpassword",
UseImageNameTags: true,
}
craneMockUtils := &dockermock.CraneMockUtils{}
utils := newImagePushToRegistryMockUtils(craneMockUtils)
err := runImagePushToRegistry(&config, nil, utils)
assert.NoError(t, err)
createdConfig, err := utils.FileRead(targetDockerConfigPath)
assert.Equal(t, customDockerConfig, string(createdConfig))
})

t.Run("failed to copy image", func(t *testing.T) {
t.Parallel()

Expand Down
28 changes: 28 additions & 0 deletions resources/metadata/imagePushToRegistry.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,34 @@ spec:
resourceRef:
- name: commonPipelineEnvironment
param: artifactVersion
- name: useImageNameTags
description: |
Will use the sourceImageNameTags and targetImageNameTags parameters, instead of sourceImages and targetImages.
sourceImageNameTags can be set by a build step, e.g. kanikoExecute, and is then available in the pipeline environment.
type: bool
scope:
- PARAMETERS
- STAGES
- STEPS
- name: sourceImageNameTags
type: "[]string"
description: "List of full names (registry and tag) of the images to be copied. Works in combination with useImageNameTags."
resourceRef:
- name: commonPipelineEnvironment
param: container/imageNameTags
scope:
- PARAMETERS
- STAGES
- STEPS
- name: targetImageNameTags
type: "[]string"
description: |
List of full names (registry and tag) of the images to be deployed. Works in combination with useImageNameTags.
If not set, the value will be the sourceImageNameTags with the targetRegistryUrl incorporated.
scope:
- PARAMETERS
- STAGES
- STEPS
- name: tagLatest
description: "Defines if the image should be tagged as `latest`. The parameter is true if targetImageTag is not specified."
type: bool
Expand Down

0 comments on commit 8bf6298

Please sign in to comment.