@@ -14,37 +14,53 @@ func IsImageMatched(s, t string) bool {
14
14
// Tag values are limited to [a-zA-Z0-9_.{}-].
15
15
// Some tools like Bazel rules_k8s allow tag patterns with {} characters.
16
16
// 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_.{}-]*)?$" )
18
18
return pattern .MatchString (s )
19
19
}
20
20
21
21
// Split separates and returns the name and tag parts
22
22
// 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 ) {
25
25
// check if image name contains a domain
26
26
// 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 :]
30
31
} 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
36
33
}
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
40
49
}
41
50
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
45
59
}
46
60
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 , ""
50
66
}
0 commit comments