-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a util library to parse container image name and data
- Loading branch information
1 parent
e34f9ac
commit 01c7843
Showing
3 changed files
with
227 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters