Skip to content

Commit

Permalink
Add a util library to parse container image name and data
Browse files Browse the repository at this point in the history
  • Loading branch information
nader-ziada committed Apr 6, 2020
1 parent e34f9ac commit b3fa060
Show file tree
Hide file tree
Showing 3 changed files with 224 additions and 5 deletions.
82 changes: 82 additions & 0 deletions util/container/image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package container

import (
"fmt"
"path"

// Import the crypto sha256 algorithm for the docker image parser to work
_ "crypto/sha256"
// Import the crypto/sha512 algorithm for the docker image parser to work with 384 and 512 sha hashes
_ "crypto/sha512"

"github.com/docker/distribution/reference"
dockerref "github.com/docker/distribution/reference"
)

const (
// DefaultImageTag is the default tag for docker image.
DefaultImageTag = "latest"
)

// Image type represents the container image details
type Image struct {
Repository string
Name string
Tag string
Digest string
}

// ParseImageName parses a docker image string into three parts: repo, tag and digest.
// If both tag and digest are empty, a default image tag will be returned.
func ParseImageName(image string) (Image, error) {
named, err := dockerref.ParseNamed(image)
if err != nil {
return Image{}, fmt.Errorf("couldn't parse image name: %v", err)
}

var repo, tag, digest string
_, nameOnly := path.Split(reference.Path(named))
if nameOnly != "" {
repo = named.Name()[:len(named.Name())-len(nameOnly)-1]
}

tagged, ok := named.(dockerref.Tagged)
if ok {
tag = tagged.Tag()
}

digested, ok := named.(dockerref.Digested)
if ok {
digest = digested.Digest().String()
}

return Image{Repository: repo, Name: nameOnly, Tag: tag, Digest: digest}, nil
}

func (i Image) String() string {
// repo/name [ ":" tag ] [ "@" digest ]
ref := fmt.Sprintf("%s/%s", i.Repository, i.Name)
if i.Tag != "" {
ref = fmt.Sprintf("%s:%s", ref, i.Tag)
}
if i.Digest != "" {
ref = fmt.Sprintf("%s:%s", ref, i.Digest)
}
return ref
}
137 changes: 137 additions & 0 deletions util/container/image_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package container

import (
"testing"

. "github.com/onsi/gomega"
)

func TestParseImageName(t *testing.T) {
testCases := []struct {
name string
input string
repo string
imageName string
tag string
digest string
wantError bool
}{
{
name: "input with path and tag",
input: "k8s.gcr.io/dev/coredns:1.6.2",
repo: "k8s.gcr.io/dev",
imageName: "coredns",
tag: "1.6.2",
wantError: false,
},
{
name: "input with name only",
input: "example.com/root",
repo: "example.com",
imageName: "root",
wantError: false,
},
{
name: "input with name and tag without path",
input: "example.com/root:tag",
repo: "example.com",
imageName: "root",
tag: "tag",
wantError: false,
},
{
name: "input with name and digest without tag",
input: "example.com/root@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
repo: "example.com",
imageName: "root",
digest: "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
wantError: false,
},
{
name: "input with path and name",
input: "example.com/user/repo",
repo: "example.com/user",
imageName: "repo",
wantError: false,
},
{
name: "input with path, anme and tag",
input: "example.com/user/repo:tag",
repo: "example.com/user",
imageName: "repo",
tag: "tag",
wantError: false,
},
{
name: "input with path, name, tag and digest",
input: "example.com/user/repo@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
repo: "example.com/user",
imageName: "repo",
digest: "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
wantError: false,
},
{
name: "input with url with port",
input: "url:5000/repo",
repo: "url:5000",
imageName: "repo",
wantError: false,
},
{
name: "input with url with port and tag",
input: "url:5000/repo:tag",
repo: "url:5000",
imageName: "repo",
tag: "tag",
wantError: false,
},
{
name: "input with url with port and digest",
input: "url:5000/repo@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
repo: "url:5000",
imageName: "repo",
digest: "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
wantError: false,
},
{
name: "input with invalid image name",
input: "url$#",
repo: "",
imageName: "",
tag: "",
wantError: true,
},
}
for _, tc := range testCases {
g := NewWithT(t)

t.Run(tc.name, func(t *testing.T) {
image, err := ParseImageName(tc.input)
if tc.wantError {
g.Expect(err).To(HaveOccurred())
} else {
g.Expect(err).NotTo(HaveOccurred())
}
g.Expect(image.Repository).To(Equal(tc.repo))
g.Expect(image.Name).To(Equal(tc.imageName))
g.Expect(image.Tag).To(Equal(tc.tag))
g.Expect(image.Digest).To(Equal(tc.digest))
})
}
}
10 changes: 5 additions & 5 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/version"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
"sigs.k8s.io/cluster-api/util/container"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
Expand Down Expand Up @@ -149,17 +150,16 @@ func ModifyImageTag(imageName, tagName string) (string, error) {

// ModifyImageRepository takes an imageName (e.g., repository/image:tag), and returns an image name with updated repository
func ModifyImageRepository(imageName, repositoryName string) (string, error) {
namedRef, err := reference.ParseNamed(imageName)
imageRef, err := container.ParseImageName(imageName)
if err != nil {
return "", errors.Wrap(err, "failed to parse image name")
}
_, nameOnly := path.Split(reference.Path(namedRef))
nameUpdated, err := reference.WithName(path.Join(repositoryName, nameOnly))
nameUpdated, err := reference.WithName(path.Join(repositoryName, imageRef.Name))
if err != nil {
return "", errors.Wrap(err, "failed to update repository name")
}
if tagged, ok := namedRef.(reference.NamedTagged); ok {
retagged, err := reference.WithTag(nameUpdated, tagged.Tag())
if imageRef.Tag != "" {
retagged, err := reference.WithTag(nameUpdated, imageRef.Tag)
if err != nil {
// this shouldn't be possible since we parsed it already above
return "", errors.Wrap(err, "failed to parse image tag")
Expand Down

0 comments on commit b3fa060

Please sign in to comment.