Skip to content

Commit

Permalink
Rename SplitImageName => ParseDomainName
Browse files Browse the repository at this point in the history
A better name based on what it actually does.
  • Loading branch information
frobware committed Jan 12, 2018
1 parent a0cf343 commit 601ade5
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 deletions.
2 changes: 1 addition & 1 deletion pkg/image/admission/imagequalify/api/validation/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const imageRefWithoutDomain = "foo/bar"
// used as the domain component in a docker image reference. Returns
// an error if domain is invalid.
func validateDomain(domain string) error {
matchedDomain, remainder, err := SplitImageName(domain + "/" + imageRefWithoutDomain)
matchedDomain, remainder, err := ParseDomainName(domain + "/" + imageRefWithoutDomain)
if err != nil {
return err
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/image/admission/imagequalify/api/validation/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"k8s.io/kubernetes/pkg/util/parsers"
)

// SplitImageName splits a docker image reference into the domain
// component and everything after the domain. An empty string is
// returned if there is no domain component. This function will first
// validate that image is a valid reference, returning an error if it
// is not.
// ParseDomainName parses a docker image reference into its domain
// component, if any, and everything after the domain. An empty string
// is returned if there is no domain component. This function will
// first validate that image is a valid reference, returning an error
// if it is not.
//
// Examples inputs and results for the domain component:
//
Expand All @@ -23,7 +23,7 @@ import (
// "docker.io/busybox" -> domain is "docker.io"
// "docker.io/library/busybox" -> domain is "docker.io"
// "library/busybox:v1" -> domain is ""
func SplitImageName(image string) (string, string, error) {
func ParseDomainName(image string) (string, string, error) {
// Note: when we call ParseImageName() this gets normalized to
// potentially include "docker.io", and/or "library/" and or
// "latest". We are only interested in discerning the domain
Expand Down
4 changes: 2 additions & 2 deletions pkg/image/admission/imagequalify/api/validation/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"testing"
)

func TestImageSplitImageName(t *testing.T) {
func TestImageParseDomainName(t *testing.T) {
for i, test := range []struct {
input string
domain string
Expand Down Expand Up @@ -65,7 +65,7 @@ func TestImageSplitImageName(t *testing.T) {
remainder: "foo/busybox:v1.2.3",
}} {
t.Logf("test #%v: %s", i, test.input)
domain, remainder, err := SplitImageName(test.input)
domain, remainder, err := ParseDomainName(test.input)
if test.expectErr && err == nil {
t.Errorf("test %#v: expected error", i)
} else if !test.expectErr && err != nil {
Expand Down
26 changes: 16 additions & 10 deletions pkg/image/admission/imagequalify/qualify.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,28 @@ func matchImage(image string, rules []api.ImageQualifyRule) *api.ImageQualifyRul
}

func qualifyImage(image string, rules []api.ImageQualifyRule) (string, error) {
domain, _, err := validation.SplitImageName(image)
domain, _, err := validation.ParseDomainName(image)
if err != nil {
return "", err
}

if domain != "" {
// image is already qualified
return image, nil
}
if matched := matchImage(image, rules); matched != nil {
qname := matched.Domain + "/" + image
// Revalidate qualified image
_, _, err := validation.SplitImageName(qname)
if err != nil {
return "", err
}
return qname, nil

matched := matchImage(image, rules)
if matched == nil {
// no match, return as-is.
return image, nil
}
return image, nil

qname := matched.Domain + "/" + image

// Revalidate qualified image
if _, _, err := validation.ParseDomainName(qname); err != nil {
return "", err
}

return qname, nil
}

0 comments on commit 601ade5

Please sign in to comment.