Skip to content

Commit 331db8f

Browse files
committed
introduce pull policy to refresh image every xx
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 91e9c91 commit 331db8f

File tree

6 files changed

+49
-4
lines changed

6 files changed

+49
-4
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313
github.com/sirupsen/logrus v1.9.0
1414
github.com/stretchr/testify v1.8.4
1515
github.com/xeipuuv/gojsonschema v1.2.0
16+
github.com/xhit/go-str2duration/v2 v2.1.0
1617
golang.org/x/exp v0.0.0-20240112132812-db7319d0e0e3
1718
golang.org/x/sync v0.3.0
1819
gopkg.in/yaml.v3 v3.0.1

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHo
3131
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
3232
github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74=
3333
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
34+
github.com/xhit/go-str2duration/v2 v2.1.0 h1:lxklc02Drh6ynqX+DdPyp5pCKLUQpRT8bp8Ydu2Bstc=
35+
github.com/xhit/go-str2duration/v2 v2.1.0/go.mod h1:ohY8p+0f07DiV6Em5LKB0s2YpLtXVyJfNt1+BlmyAsU=
3436
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
3537
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
3638
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=

loader/loader_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -3719,3 +3719,17 @@ services:
37193719
assert.NilError(t, err)
37203720
assert.Equal(t, p.Services["test"].Networks["test"].GatewayPriority, 42)
37213721
}
3722+
3723+
func TestPullRefresh(t *testing.T) {
3724+
p, err := loadYAML(`
3725+
name: load-all-gpus
3726+
services:
3727+
test:
3728+
pull_policy: every_2d
3729+
`)
3730+
assert.NilError(t, err)
3731+
policy, duration, err := p.Services["test"].GetPullPolicy()
3732+
assert.NilError(t, err)
3733+
assert.Equal(t, policy, types.PullPolicyRefresh)
3734+
assert.Equal(t, duration, 2*24*time.Hour)
3735+
}

schema/compose-spec.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -370,9 +370,10 @@
370370
"pre_stop": {"type": "array", "items": {"$ref": "#/definitions/service_hook"}},
371371
"privileged": {"type": ["boolean", "string"]},
372372
"profiles": {"$ref": "#/definitions/list_of_strings"},
373-
"pull_policy": {"type": "string", "enum": [
374-
"always", "never", "if_not_present", "build", "missing"
375-
]},
373+
"pull_policy": {"type": "string",
374+
"pattern": "always|never|build|if_not_present|missing|refresh|daily|weekly|every_([0-9]+[wdhms])+"
375+
},
376+
"pull_refresh_after": {"type": "string"},
376377
"read_only": {"type": ["boolean", "string"]},
377378
"restart": {"type": "string"},
378379
"runtime": {

types/duration.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
"fmt"
2222
"strings"
2323
"time"
24+
25+
"github.com/xhit/go-str2duration/v2"
2426
)
2527

2628
// Duration is a thin wrapper around time.Duration with improved JSON marshalling
@@ -31,7 +33,7 @@ func (d Duration) String() string {
3133
}
3234

3335
func (d *Duration) DecodeMapstructure(value interface{}) error {
34-
v, err := time.ParseDuration(fmt.Sprint(value))
36+
v, err := str2duration.ParseDuration(fmt.Sprint(value))
3537
if err != nil {
3638
return err
3739
}

types/types.go

+25
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ import (
2121
"fmt"
2222
"sort"
2323
"strings"
24+
"time"
2425

2526
"github.com/docker/go-connections/nat"
27+
"github.com/xhit/go-str2duration/v2"
2628
)
2729

2830
// ServiceConfig is the configuration of one service
@@ -215,6 +217,8 @@ const (
215217
PullPolicyMissing = "missing"
216218
// PullPolicyBuild force building images
217219
PullPolicyBuild = "build"
220+
// PullPolicyRefresh checks if image needs to be updated
221+
PullPolicyRefresh = "refresh"
218222
)
219223

220224
const (
@@ -268,6 +272,27 @@ func (s ServiceConfig) GetDependents(p *Project) []string {
268272
return dependent
269273
}
270274

275+
func (s ServiceConfig) GetPullPolicy() (string, time.Duration, error) {
276+
switch s.PullPolicy {
277+
case PullPolicyAlways, PullPolicyNever, PullPolicyIfNotPresent, PullPolicyMissing, PullPolicyBuild:
278+
return s.PullPolicy, 0, nil
279+
case "daily":
280+
return PullPolicyRefresh, 24 * time.Hour, nil
281+
case "weekly":
282+
return PullPolicyRefresh, 7 * 24 * time.Hour, nil
283+
default:
284+
if strings.HasPrefix(s.PullPolicy, "every_") {
285+
delay := s.PullPolicy[6:]
286+
duration, err := str2duration.ParseDuration(delay)
287+
if err != nil {
288+
return "", 0, err
289+
}
290+
return PullPolicyRefresh, duration, nil
291+
}
292+
return PullPolicyMissing, 0, nil
293+
}
294+
}
295+
271296
// BuildConfig is a type for build
272297
type BuildConfig struct {
273298
Context string `yaml:"context,omitempty" json:"context,omitempty"`

0 commit comments

Comments
 (0)