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

add tag when copying image when sourceRef is using both tag and digest #864

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion pkg/registry/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
type Client interface {
CreateRepository(ctx context.Context, name string) error
RepositoryExists() bool
CopyImage(ctx context.Context, src ctypes.ImageReference, srcCreds string, dest ctypes.ImageReference, destCreds string) error
CopyImage(ctx context.Context, src ctypes.ImageReference, srcCreds string, dest ctypes.ImageReference, destCreds string, additionalTag string) error
PullImage() error
PutImage() error
ImageExists(ctx context.Context, ref ctypes.ImageReference) bool
Expand Down
6 changes: 5 additions & 1 deletion pkg/registry/ecr.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func (e *ECRClient) RepositoryExists() bool {
panic("implement me")
}

func (e *ECRClient) CopyImage(ctx context.Context, srcRef ctypes.ImageReference, srcCreds string, destRef ctypes.ImageReference, destCreds string) error {
func (e *ECRClient) CopyImage(ctx context.Context, srcRef ctypes.ImageReference, srcCreds string, destRef ctypes.ImageReference, destCreds string, additionalTag string) error {
src := srcRef.DockerReference().String()
dest := destRef.DockerReference().String()
app := "skopeo"
Expand All @@ -199,6 +199,10 @@ func (e *ECRClient) CopyImage(ctx context.Context, srcRef ctypes.ImageReference,
"docker://" + dest,
}

if len(additionalTag) > 0 {
args = append(args, "--additional-tag", additionalTag)
}

if len(srcCreds) > 0 {
args = append(args, "--src-authfile", srcCreds)
} else {
Expand Down
6 changes: 5 additions & 1 deletion pkg/registry/gar.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (e *GARClient) RepositoryExists() bool {
panic("implement me")
}

func (e *GARClient) CopyImage(ctx context.Context, srcRef ctypes.ImageReference, srcCreds string, destRef ctypes.ImageReference, destCreds string) error {
func (e *GARClient) CopyImage(ctx context.Context, srcRef ctypes.ImageReference, srcCreds string, destRef ctypes.ImageReference, destCreds string, additionalTag string) error {
src := srcRef.DockerReference().String()
dest := destRef.DockerReference().String()

Expand All @@ -89,6 +89,10 @@ func (e *GARClient) CopyImage(ctx context.Context, srcRef ctypes.ImageReference,
"docker://" + dest,
}

if len(additionalTag) > 0 {
args = append(args, "--additional-tag", additionalTag)
}

if len(creds[1]) > 0 {
args = append(args, creds...)
} else {
Expand Down
3 changes: 2 additions & 1 deletion pkg/webhook/image_copier.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type ImageCopier struct {
sourcePod *corev1.Pod
sourceImageRef ctypes.ImageReference
targetImageRef ctypes.ImageReference
additionalTag string

imagePullPolicy corev1.PullPolicy
imageSwapper *ImageSwapper
Expand Down Expand Up @@ -135,5 +136,5 @@ func (ic *ImageCopier) taskCopyImage() error {
//
// or transform registryClient creds into auth compatible form, e.g.
// {"auths":{"aws_account_id.dkr.ecr.region.amazonaws.com":{"username":"AWS","password":"..." }}}
return ic.imageSwapper.registryClient.CopyImage(ctx, ic.sourceImageRef, authFile.Name(), ic.targetImageRef, ic.imageSwapper.registryClient.Credentials())
return ic.imageSwapper.registryClient.CopyImage(ctx, ic.sourceImageRef, authFile.Name(), ic.targetImageRef, ic.imageSwapper.registryClient.Credentials(), ic.additionalTag)
}
15 changes: 9 additions & 6 deletions pkg/webhook/image_swapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,21 +147,23 @@ func NewImageSwapperWebhook(registryClient registry.Client, imagePullSecretProvi
// imageNamesWithDigestOrTag strips the tag from ambiguous image references that have a digest as well (e.g. `image:tag@sha256:123...`).
// Such image references are supported by docker but, due to their ambiguity,
// explicitly not by containers/image.
func imageNamesWithDigestOrTag(imageName string) (string, error) {
func imageNamesWithDigestOrTag(imageName string) (string, string, error) {
ref, err := reference.ParseNormalizedNamed(imageName)
if err != nil {
return "", err
return "", "", err
}
_, isTagged := ref.(reference.NamedTagged)
nameAndTag, isTagged := ref.(reference.NamedTagged)
canonical, isDigested := ref.(reference.Canonical)
tag := ""
if isTagged && isDigested {
tag = nameAndTag.Tag()
canonical, err = reference.WithDigest(reference.TrimNamed(ref), canonical.Digest())
if err != nil {
return "", err
return "", "", err
}
imageName = canonical.String()
}
return imageName, nil
return imageName, tag, nil
}

// Mutate replaces the image ref. Satisfies mutating.Mutator interface.
Expand All @@ -184,7 +186,7 @@ func (p *ImageSwapper) Mutate(ctx context.Context, ar *kwhmodel.AdmissionReview,
for _, containerSet := range containerSets {
containers := *containerSet
for i, container := range containers {
normalizedName, err := imageNamesWithDigestOrTag(container.Image)
normalizedName, tag, err := imageNamesWithDigestOrTag(container.Image)
if err != nil {
log.Ctx(lctx).Warn().Msgf("unable to normalize source name %s: %v", container.Image, err)
continue
Expand Down Expand Up @@ -222,6 +224,7 @@ func (p *ImageSwapper) Mutate(ctx context.Context, ar *kwhmodel.AdmissionReview,
sourcePod: pod,
sourceImageRef: srcRef,
targetImageRef: targetRef,
additionalTag: tag,
imagePullPolicy: container.ImagePullPolicy,
imageSwapper: p,
context: imageCopierContext,
Expand Down
Loading