Skip to content

Commit

Permalink
Added regex and cleanup string algo for repo based tag generator
Browse files Browse the repository at this point in the history
Signed-off-by: rcmadhankumar <[email protected]>
  • Loading branch information
rcmadhankumar committed Oct 16, 2023
1 parent 8455c2f commit 893073b
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
64 changes: 64 additions & 0 deletions pkg/imgpkg/internal/util/repo_based_tag_generator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2023 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0

package util_test

import (
"testing"

regname "github.com/google/go-containerregistry/pkg/name"
"github.com/stretchr/testify/require"
"github.com/vmware-tanzu/carvel-imgpkg/pkg/imgpkg/imagedigest"
util "github.com/vmware-tanzu/carvel-imgpkg/pkg/imgpkg/internal/util"
)

type testDescribe struct {
description string
origRef string
expectedTag string
}

func TestGenerateTagRepobasedgenerator(t *testing.T) {
allTests := []testDescribe{
{
description: "OrigRef starts with -",
origRef: "index.docker.io/_test-repo/simple-app-test@sha256:61cb2e3a8522bfd9d4b6219cb9e382df151ba6d4fcc4c96f870ee4e1cffbbf9c",
expectedTag: "_test-repo-simple-app-test-sha256-61cb2e3a8522bfd9d4b6219cb9e382df151ba6d4fcc4c96f870ee4e1cffbbf9c.imgpkg",
},
{
description: "OrigRef starts with *-",
origRef: "index.docker.io/*-test-repo/simple-app-test@sha256:61cb2e3a8522bfd9d4b6219cb9e382df151ba6d4fcc4c96f870ee4e1cffbbf9c",
expectedTag: "test-repo-simple-app-test-sha256-61cb2e3a8522bfd9d4b6219cb9e382df151ba6d4fcc4c96f870ee4e1cffbbf9c.imgpkg",
},
{
description: "OrigRef starts with .",
origRef: "index.docker.io/.test-repo/simple-app-test@sha256:61cb2e3a8522bfd9d4b6219cb9e382df151ba6d4fcc4c96f870ee4e1cffbbf9c",
expectedTag: "test-repo-simple-app-test-sha256-61cb2e3a8522bfd9d4b6219cb9e382df151ba6d4fcc4c96f870ee4e1cffbbf9c.imgpkg",
},
{
description: "OrigRef contains 100+ characters",
origRef: "index.docker.io/test-path/sample-path/verification-path/sample-path/test-repo/test-repo/simple-app-test@sha256:61cb2e3a8522bfd9d4b6219cb9e382df151ba6d4fcc4c96f870ee4e1cffbbf9c",
expectedTag: "h-sample-path-test-repo-test-repo-simple-app-test-sha256-61cb2e3a8522bfd9d4b6219cb9e382df151ba6d4fcc4c96f870ee4e1cffbbf9c.imgpkg",
},
{
description: "OrigRef contains 100+ characters with special characters @#$%^&*()",
origRef: "index.docker.io/tes%t-path/sample-path/verific&*ation-path*&^%$/sample-pa(th/test-repo/t)est-repo/simple-app-test@sha256:61cb2e3a8522bfd9d4b6219cb9e382df151ba6d4fcc4c96f870ee4e1cffbbf9c",
expectedTag: "h-sample-path-test-repo-test-repo-simple-app-test-sha256-61cb2e3a8522bfd9d4b6219cb9e382df151ba6d4fcc4c96f870ee4e1cffbbf9c.imgpkg",
},
}

for _, test := range allTests {
t.Run(test.description, func(t *testing.T) {
digestWrap := imagedigest.DigestWrap{}
imgIdxRef := "index.docker.io/test-repo/tert-src-repo@sha256:61cb2e3a8522bfd9d4b6219cb9e382df151ba6d4fcc4c96f870ee4e1cffbbf9c"
digestWrap.DigestWrap(imgIdxRef, test.origRef)
importRepo, err := regname.NewRepository("import-registry/dst-repo")
require.NoError(t, err)
tagGen := util.RepoBasedTagGenerator{}
tag, err := tagGen.GenerateTag(digestWrap, importRepo)
require.NoError(t, err)
require.Equal(t, test.expectedTag, tag.TagStr())
require.LessOrEqual(t, len(tag.TagStr()), 128)
})
}
}
10 changes: 8 additions & 2 deletions pkg/imgpkg/internal/util/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package util

import (
"fmt"
"regexp"
"strings"

regname "github.com/google/go-containerregistry/pkg/name"
Expand Down Expand Up @@ -65,13 +66,18 @@ func (tagGen RepoBasedTagGenerator) GenerateTag(item imagedigest.DigestWrap, imp
}

origRepoPath = strings.Join(strings.Split(origRepoPath, "/")[1:], "-")

pattern := `^[^a-zA-Z0-9_]+|[^a-zA-Z0-9\._-]+`
regex := regexp.MustCompile(pattern)
cleanedOrigRepoPath := regex.ReplaceAllString(origRepoPath, "")

digestArr := strings.Split(item.RegnameDigest().DigestStr(), ":")
tagStartIdx := len(origRepoPath) - 49
tagStartIdx := len(cleanedOrigRepoPath) - 49
if tagStartIdx < 0 {
tagStartIdx = 0
}

dashedRepo := fmt.Sprintf("%s-%s-%s.imgpkg", origRepoPath[tagStartIdx:], digestArr[0], digestArr[1])
dashedRepo := fmt.Sprintf("%s-%s-%s.imgpkg", cleanedOrigRepoPath[tagStartIdx:], digestArr[0], digestArr[1])
// if tag starts with a "-", PUT to /v2/<repo>/manifests/-<foo>
// will give an "un-recognized request" error
if strings.HasPrefix(dashedRepo, "-") {
Expand Down

0 comments on commit 893073b

Please sign in to comment.