-
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.
Adds image util to parse repo, image and tags from container images
- Loading branch information
gab-satchi
committed
Mar 31, 2020
1 parent
ecff70a
commit 24b30f3
Showing
4 changed files
with
211 additions
and
18 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
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
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,56 @@ | ||
/* | ||
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 image | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
) | ||
|
||
// GetRepository returns the repository path given a full container path | ||
func GetRepository(path string) (string, error) { | ||
imageNameIndex := strings.LastIndex(path, "/") | ||
if imageNameIndex == -1 { | ||
return "", errors.New("unable to parse repository") | ||
} | ||
return path[:imageNameIndex], nil | ||
} | ||
|
||
// GetImage returns the image given a full container path | ||
func GetImage(path string) (string, error) { | ||
imageNameIndex := strings.LastIndex(path, "/") | ||
if imageNameIndex == -1 { | ||
return "", errors.New("unable to parse image") | ||
} | ||
remainder := path[imageNameIndex+1:] | ||
if tagIndex := strings.IndexRune(remainder, ':'); tagIndex > -1 { | ||
return remainder[:tagIndex], nil | ||
} | ||
return remainder, nil | ||
} | ||
|
||
// GetTag returns the image tag given a full container path | ||
func GetTag(path string) (string, error) { | ||
i := strings.IndexRune(path, ':') | ||
if i == -1 { | ||
return "", errors.New("unable to parse tag") | ||
} | ||
if digestIndex := strings.IndexRune(path, '@'); digestIndex > -1 { | ||
return path[i+1 : digestIndex], nil | ||
} | ||
return path[i+1:], nil | ||
} |
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 image | ||
|
||
import ( | ||
. "github.com/onsi/gomega" | ||
"testing" | ||
) | ||
|
||
func TestGetRepository(t *testing.T) { | ||
g := NewWithT(t) | ||
|
||
var testcases = []struct { | ||
name string | ||
path string | ||
repositoryName string | ||
expectError bool | ||
}{ | ||
{ | ||
name: "should return repository name", | ||
path: "k8s.gcr.io/coredns:1.6.6", | ||
repositoryName: "k8s.gcr.io", | ||
}, | ||
{ | ||
name: "should return repository name with subpaths", | ||
path: "k8s.gcr.io/org/coredns:1.6.6", | ||
repositoryName: "k8s.gcr.io/org", | ||
}, | ||
{ | ||
name: "should return repository name when tag is missing", | ||
path: "k8s.gcr.io/coredns", | ||
repositoryName: "k8s.gcr.io", | ||
}, | ||
{ | ||
name: "should return repository with port", | ||
path: "k8s.gcr.io:1234/coredns", | ||
repositoryName: "k8s.gcr.io:1234", | ||
}, | ||
{ | ||
name: "should error when image missing", | ||
path: "k8s.gcr.io", | ||
expectError: true, | ||
}, | ||
} | ||
|
||
for _, tc := range testcases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
out, err := GetRepository(tc.path) | ||
g.Expect(err != nil).To(Equal(tc.expectError)) | ||
g.Expect(out).To(Equal(tc.repositoryName)) | ||
}) | ||
} | ||
} | ||
|
||
func TestGetImage(t *testing.T) { | ||
g := NewWithT(t) | ||
|
||
var testcases = []struct { | ||
name string | ||
path string | ||
image string | ||
expectError bool | ||
}{ | ||
{ | ||
name: "should return image name", | ||
path: "k8s.gcr.io/coredns:1.6.6", | ||
image: "coredns", | ||
}, | ||
{ | ||
name: "should return image name when tag is missing", | ||
path: "k8s.gcr.io/org/coredns", | ||
image: "coredns", | ||
}, | ||
{ | ||
name: "should error when repository is missing", | ||
path: "coredns:1.6.6", | ||
expectError: true, | ||
}, | ||
} | ||
|
||
for _, tc := range testcases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
out, err := GetImage(tc.path) | ||
g.Expect(err != nil).To(Equal(tc.expectError)) | ||
g.Expect(out).To(Equal(tc.image)) | ||
}) | ||
} | ||
} | ||
|
||
func TestGetTag(t *testing.T) { | ||
g := NewWithT(t) | ||
|
||
var testcases = []struct { | ||
name string | ||
path string | ||
tag string | ||
expectError bool | ||
}{ | ||
{ | ||
name: "should return tag", | ||
path: "k8s.gcr.io/coredns:v1.6.6+build1", | ||
tag: "v1.6.6+build1", | ||
}, | ||
{ | ||
name: "should return tag when digest is present", | ||
path: "k8s.gcr.io/org/coredns:1.6.6@some-digest", | ||
tag: "1.6.6", | ||
}, | ||
{ | ||
name: "should error when tag separator ':' is missing", | ||
path: "malformed1.6.6", | ||
expectError: true, | ||
}, | ||
} | ||
|
||
for _, tc := range testcases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
out, err := GetTag(tc.path) | ||
g.Expect(err != nil).To(Equal(tc.expectError)) | ||
g.Expect(out).To(Equal(tc.tag)) | ||
}) | ||
} | ||
} |