feat(discovery): tag-agnostic image name normalisation
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
package discovery
|
||||
|
||||
import "strings"
|
||||
|
||||
// NormaliseImage strips the tag and digest from an image reference,
|
||||
// returning the bare repository name.
|
||||
//
|
||||
// Tricky case: "localhost:5000/foo:v1" — the first colon is a port,
|
||||
// not a tag. We disambiguate by splitting on "/" first and only
|
||||
// treating colons in the last segment as tag separators.
|
||||
func NormaliseImage(ref string) string {
|
||||
if at := strings.Index(ref, "@"); at >= 0 {
|
||||
ref = ref[:at]
|
||||
}
|
||||
slash := strings.LastIndex(ref, "/")
|
||||
if slash < 0 {
|
||||
if colon := strings.Index(ref, ":"); colon >= 0 {
|
||||
return ref[:colon]
|
||||
}
|
||||
return ref
|
||||
}
|
||||
prefix, last := ref[:slash], ref[slash+1:]
|
||||
if colon := strings.Index(last, ":"); colon >= 0 {
|
||||
last = last[:colon]
|
||||
}
|
||||
return prefix + "/" + last
|
||||
}
|
||||
|
||||
// ImagesMatch reports whether two image references resolve to the same
|
||||
// repository, ignoring tag and digest. Case-sensitive per spec section 5.2.
|
||||
func ImagesMatch(a, b string) bool {
|
||||
return NormaliseImage(a) == NormaliseImage(b)
|
||||
}
|
||||
Reference in New Issue
Block a user