-
Notifications
You must be signed in to change notification settings - Fork 10
/
image.go
59 lines (46 loc) · 1.45 KB
/
image.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package utils
import (
"fmt"
"strings"
corev1 "k8s.io/api/core/v1"
// Import the crypto sha256 algorithm for the docker image parser to work
_ "crypto/sha256"
// Import the crypto/sha512 algorithm for the docker image parser to work with 384 and 512 sha hashes
_ "crypto/sha512"
dockerref "github.com/docker/distribution/reference"
)
// ParseImageName parses a docker image string into three parts: repo, tag and digest.
// If both tag and digest are empty, a default image tag will be returned.
func ParseImageName(image string) (string, string, string, error) {
named, err := dockerref.ParseNormalizedNamed(image)
if err != nil {
return "", "", "", fmt.Errorf("couldn't parse image name: %v", err)
}
repoToPull := named.Name()
var tag, digest string
tagged, ok := named.(dockerref.Tagged)
if ok {
tag = tagged.Tag()
}
digested, ok := named.(dockerref.Digested)
if ok {
digest = digested.Digest().String()
}
// If no tag was specified, use the default "latest".
if len(tag) == 0 && len(digest) == 0 {
tag = "latest"
}
return repoToPull, tag, digest, nil
}
// ImagePullPolicyByTag returns the pull policy for the given image tag.
func ImagePullPolicyByTag(image string) corev1.PullPolicy {
_, tag, _, _ := ParseImageName(image)
switch tag {
case "latest", "dev", "edge":
return corev1.PullAlways
}
if strings.HasSuffix(tag, "dev") || strings.HasSuffix(tag, "edge") {
return corev1.PullAlways
}
return corev1.PullIfNotPresent
}