@@ -14,37 +14,53 @@ func IsImageMatched(s, t string) bool {
1414 // Tag values are limited to [a-zA-Z0-9_.{}-].
1515 // Some tools like Bazel rules_k8s allow tag patterns with {} characters.
1616 // More info: https://github.com/bazelbuild/rules_k8s/pull/423
17- pattern , _ := regexp .Compile ("^" + t + "(@sha256 )?(:[a-zA-Z0-9_.{}-]*)?$" )
17+ pattern , _ := regexp .Compile ("^" + t + "(:[a-zA-Z0-9_.{}-]* )?(@sha256 :[a-zA-Z0-9_.{}-]*)?$" )
1818 return pattern .MatchString (s )
1919}
2020
2121// Split separates and returns the name and tag parts
2222// from the image string using either colon `:` or at `@` separators.
23- // Note that the returned tag keeps its separator.
24- func Split (imageName string ) (name string , tag string ) {
23+ // image reference pattern: [[host[:port]/]component/]component[: tag][@digest]
24+ func Split (imageName string ) (name string , tag string , digest string ) {
2525 // check if image name contains a domain
2626 // if domain is present, ignore domain and check for `:`
27- ic := - 1
28- if slashIndex := strings .Index (imageName , "/" ); slashIndex < 0 {
29- ic = strings .LastIndex (imageName , ":" )
27+ searchName := imageName
28+ slashIndex := strings .Index (imageName , "/" )
29+ if slashIndex > 0 {
30+ searchName = imageName [slashIndex :]
3031 } else {
31- lastIc := strings .LastIndex (imageName [slashIndex :], ":" )
32- // set ic only if `:` is present
33- if lastIc > 0 {
34- ic = slashIndex + lastIc
35- }
32+ slashIndex = 0
3633 }
37- ia := strings .LastIndex (imageName , "@" )
38- if ic < 0 && ia < 0 {
39- return imageName , ""
34+
35+ id := strings .Index (searchName , "@" )
36+ ic := strings .Index (searchName , ":" )
37+
38+ // no tag or digest
39+ if ic < 0 && id < 0 {
40+ return imageName , "" , ""
41+ }
42+
43+ // digest only
44+ if id >= 0 && (id < ic || ic < 0 ) {
45+ id += slashIndex
46+ name = imageName [:id ]
47+ digest = strings .TrimPrefix (imageName [id :], "@" )
48+ return name , "" , digest
4049 }
4150
42- i := ic
43- if ia > 0 {
44- i = ia
51+ // tag and digest
52+ if id >= 0 && ic >= 0 {
53+ id += slashIndex
54+ ic += slashIndex
55+ name = imageName [:ic ]
56+ tag = strings .TrimPrefix (imageName [ic :id ], ":" )
57+ digest = strings .TrimPrefix (imageName [id :], "@" )
58+ return name , tag , digest
4559 }
4660
47- name = imageName [:i ]
48- tag = imageName [i :]
49- return
61+ // tag only
62+ ic += slashIndex
63+ name = imageName [:ic ]
64+ tag = strings .TrimPrefix (imageName [ic :], ":" )
65+ return name , tag , ""
5066}
0 commit comments