File: pull_policy.go

package info (click to toggle)
golang-github-containers-common 0.64.2%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,528 kB
  • sloc: makefile: 130; sh: 102
file content (94 lines) | stat: -rw-r--r-- 2,810 bytes parent folder | download | duplicates (2)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package config

import (
	"fmt"
	"strings"
)

// PullPolicy determines how and which images are being pulled from a container
// registry (i.e., docker transport only).
//
// Supported string values are:
// * "always"  <-> PullPolicyAlways
// * "missing" <-> PullPolicyMissing
// * "newer"  <-> PullPolicyNewer
// * "never"   <-> PullPolicyNever
type PullPolicy int

const (
	// Always pull the image and throw an error if the pull fails.
	PullPolicyAlways PullPolicy = iota
	// Pull the image only if it could not be found in the local containers
	// storage.  Throw an error if no image could be found and the pull fails.
	PullPolicyMissing
	// Never pull the image but use the one from the local containers
	// storage.  Throw an error if no image could be found.
	PullPolicyNever
	// Pull if the image on the registry is newer than the one in the local
	// containers storage.  An image is considered to be newer when the
	// digests are different.  Comparing the time stamps is prone to
	// errors.  Pull errors are suppressed if a local image was found.
	PullPolicyNewer

	// Ideally this should be the first `ioata` but backwards compatibility
	// prevents us from changing the values.
	PullPolicyUnsupported = -1
)

// String converts a PullPolicy into a string.
//
// Supported string values are:
// * "always"  <-> PullPolicyAlways
// * "missing" <-> PullPolicyMissing
// * "newer"   <-> PullPolicyNewer
// * "never"   <-> PullPolicyNever
func (p PullPolicy) String() string {
	switch p {
	case PullPolicyAlways:
		return "always"
	case PullPolicyMissing:
		return "missing"
	case PullPolicyNewer:
		return "newer"
	case PullPolicyNever:
		return "never"
	}
	return fmt.Sprintf("unrecognized policy %d", p)
}

// Validate returns if the pull policy is not supported.
func (p PullPolicy) Validate() error {
	switch p {
	case PullPolicyAlways, PullPolicyMissing, PullPolicyNewer, PullPolicyNever:
		return nil
	default:
		return fmt.Errorf("unsupported pull policy %d", p)
	}
}

// ParsePullPolicy parses the string into a pull policy.
//
// Supported string values are:
// * "always"  <-> PullPolicyAlways
// * "missing" <-> PullPolicyMissing (also "ifnotpresent" and "")
// * "newer"   <-> PullPolicyNewer (also "ifnewer")
// * "never"   <-> PullPolicyNever
func ParsePullPolicy(s string) (PullPolicy, error) {
	switch strings.ToLower(s) {
	case "always":
		return PullPolicyAlways, nil
	case "missing", "ifmissing", "ifnotpresent", "":
		return PullPolicyMissing, nil
	case "newer", "ifnewer":
		return PullPolicyNewer, nil
	case "never":
		return PullPolicyNever, nil
	default:
		return PullPolicyUnsupported, fmt.Errorf("unsupported pull policy %q", s)
	}
}

// Deprecated: please use `ParsePullPolicy` instead.
func ValidatePullPolicy(s string) (PullPolicy, error) {
	return ParsePullPolicy(s)
}