Skip to content

Commit

Permalink
introduce pull policy to refresh image every xx
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <[email protected]>
  • Loading branch information
ndeloof committed Feb 24, 2025
1 parent 23f270a commit 324ce1d
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 4 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/sirupsen/logrus v1.9.0
github.com/stretchr/testify v1.8.4
github.com/xeipuuv/gojsonschema v1.2.0
github.com/xhit/go-str2duration/v2 v2.1.0
golang.org/x/exp v0.0.0-20240112132812-db7319d0e0e3
golang.org/x/sync v0.3.0
gopkg.in/yaml.v3 v3.0.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHo
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74=
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
github.com/xhit/go-str2duration/v2 v2.1.0 h1:lxklc02Drh6ynqX+DdPyp5pCKLUQpRT8bp8Ydu2Bstc=
github.com/xhit/go-str2duration/v2 v2.1.0/go.mod h1:ohY8p+0f07DiV6Em5LKB0s2YpLtXVyJfNt1+BlmyAsU=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand Down
14 changes: 14 additions & 0 deletions loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3744,3 +3744,17 @@ services:
assert.Equal(t, len(p.Services["test"].Gpus), 1)
assert.Equal(t, p.Services["test"].Gpus[0].Count, types.DeviceCount(-1))
}

func TestPullRefresh(t *testing.T) {
p, err := loadYAML(`
name: load-all-gpus
services:
test:
pull_policy: every_2d
`)
assert.NilError(t, err)
policy, duration, err := p.Services["test"].GetPullPolicy()
assert.NilError(t, err)
assert.Equal(t, policy, types.PullPolicyRefresh)
assert.Equal(t, duration, 2*24*time.Hour)
}
7 changes: 4 additions & 3 deletions schema/compose-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,10 @@
"pre_stop": {"type": "array", "items": {"$ref": "#/definitions/service_hook"}},
"privileged": {"type": ["boolean", "string"]},
"profiles": {"$ref": "#/definitions/list_of_strings"},
"pull_policy": {"type": "string", "enum": [
"always", "never", "if_not_present", "build", "missing"
]},
"pull_policy": {"type": "string",
"pattern": "always|never|build|if_not_present|missing|refresh|daily|weekly|every_([0-9]+[wdhms])+"
},
"pull_refresh_after": {"type": "string"},
"read_only": {"type": ["boolean", "string"]},
"restart": {"type": "string"},
"runtime": {
Expand Down
1 change: 1 addition & 0 deletions types/derived.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion types/duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"fmt"
"strings"
"time"

"github.com/xhit/go-str2duration/v2"
)

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

func (d *Duration) DecodeMapstructure(value interface{}) error {
v, err := time.ParseDuration(fmt.Sprint(value))
v, err := str2duration.ParseDuration(fmt.Sprint(value))
if err != nil {
return err
}
Expand Down
25 changes: 25 additions & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ import (
"fmt"
"sort"
"strings"
"time"

"github.com/docker/go-connections/nat"
"github.com/xhit/go-str2duration/v2"
)

// ServiceConfig is the configuration of one service
Expand Down Expand Up @@ -215,6 +217,8 @@ const (
PullPolicyMissing = "missing"
// PullPolicyBuild force building images
PullPolicyBuild = "build"
// PullPolicyRefresh checks image needs to be updatedq
PullPolicyRefresh = "refresh"
)

const (
Expand Down Expand Up @@ -268,6 +272,27 @@ func (s ServiceConfig) GetDependents(p *Project) []string {
return dependent
}

func (s ServiceConfig) GetPullPolicy() (string, time.Duration, error) {
switch s.PullPolicy {
case PullPolicyAlways, PullPolicyNever, PullPolicyIfNotPresent, PullPolicyMissing, PullPolicyBuild:
return s.PullPolicy, 0, nil
case "daily":
return PullPolicyRefresh, 24 * time.Hour, nil
case "weekly":
return PullPolicyRefresh, 7 * 24 * time.Hour, nil
default:
if strings.HasPrefix(s.PullPolicy, "every_") {
delay := s.PullPolicy[6:]
duration, err := str2duration.ParseDuration(delay)
if err != nil {
return "", 0, err
}
return PullPolicyRefresh, duration, nil
}
return PullPolicyMissing, 0, nil
}
}

// BuildConfig is a type for build
type BuildConfig struct {
Context string `yaml:"context,omitempty" json:"context,omitempty"`
Expand Down

0 comments on commit 324ce1d

Please sign in to comment.