Debian Codegraph @

api/internal/image/image.go

raw
1 // Copyright 2020 The Kubernetes Authors.
2 // SPDX-License-Identifier: Apache-2.0
3
4 package image
5
6 import (
7 "regexp"
8 "strings"
9 )
10
11 // IsImageMatched returns true if the value of t is identical to the
12 // image name in the full image name and tag as given by s.
13 func IsImageMatched(s, t string) bool {
14 // Tag values are limited to [a-zA-Z0-9_.{}-].
15 // Some tools like Bazel rules_k8s allow tag patterns with {} characters.
16 // More info: https://github.com/bazelbuild/rules_k8s/pull/423
17 pattern, _ := regexp.Compile("^" + t + "(:[a-zA-Z0-9_.{}-]*)?(@sha256:[a-zA-Z0-9_.{}-]*)?$")
18 return pattern.MatchString(s)
19 }
20
21 // Split separates and returns the name and tag parts
22 // from the image string using either colon `:` or at `@` separators.
23 // image reference pattern: [[host[:port]/]component/]component[:tag][@digest]
24 func Split(imageName string) (name string, tag string, digest string) {
25 // check if image name contains a domain
26 // if domain is present, ignore domain and check for `:`
27 searchName := imageName
28 slashIndex := strings.Index(imageName, "/")
29 if slashIndex > 0 {
30 searchName = imageName[slashIndex:]
31 } else {
32 slashIndex = 0
33 }
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
49 }
50
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
59 }
60
61 // tag only
62 ic += slashIndex
63 name = imageName[:ic]
64 tag = strings.TrimPrefix(imageName[ic:], ":")
65 return name, tag, ""
66 }