Skip to content

Commit

Permalink
Merge pull request #1624 from jjbustamante/bugfix/issue-1618-buildpac…
Browse files Browse the repository at this point in the history
…k-URL-formatted-incorrect-when-using-a-registry-mirror

Buildpack URL formatted incorrect when using a registry mirror
Signed-off-by: David Freilich <[email protected]>
  • Loading branch information
dfreilich authored Feb 7, 2023
2 parents 202ea2d + 0224717 commit b3ff849
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
13 changes: 12 additions & 1 deletion internal/name/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ package name

import (
"fmt"
"strings"

gname "github.com/google/go-containerregistry/pkg/name"

"github.com/buildpacks/pack/internal/style"
)

const (
defaultRefFormat = "%s/%s:%s"
digestRefFormat = "%s/%s@%s"
)

type Logger interface {
Infof(fmt string, v ...interface{})
}
Expand All @@ -28,7 +34,12 @@ func TranslateRegistry(name string, registryMirrors map[string]string, logger Lo
return name, nil
}

refName := fmt.Sprintf("%s/%s:%s", registryMirror, srcContext.RepositoryStr(), srcRef.Identifier())
refFormat := defaultRefFormat
if strings.Contains(srcRef.Identifier(), ":") {
refFormat = digestRefFormat
}

refName := fmt.Sprintf(refFormat, registryMirror, srcContext.RepositoryStr(), srcRef.Identifier())
_, err = gname.ParseReference(refName, gname.WeakValidation)
if err != nil {
return "", err
Expand Down
12 changes: 12 additions & 0 deletions internal/name/name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,17 @@ func testTranslateRegistry(t *testing.T, when spec.G, it spec.S) {
assert.Nil(err)
assert.Equal(output, expected)
})

it("translate a buildpack referenced by a digest", func() {
input := "buildpack/bp@sha256:7f48a442c056cd19ea48462e05faa2837ac3a13732c47616d20f11f8c847a8c4"
expected := "myregistry.com/buildpack/bp@sha256:7f48a442c056cd19ea48462e05faa2837ac3a13732c47616d20f11f8c847a8c4"
registryMirrors := map[string]string{
"index.docker.io": "myregistry.com",
}

output, err := name.TranslateRegistry(input, registryMirrors, logger)
assert.Nil(err)
assert.Equal(output, expected)
})
})
}

0 comments on commit b3ff849

Please sign in to comment.