Skip to content

Commit

Permalink
add option to set default container pull policy
Browse files Browse the repository at this point in the history
  • Loading branch information
WanzenBug committed Dec 17, 2021
1 parent 9940d49 commit c47a9b9
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 2 deletions.
22 changes: 22 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/spf13/viper"

"github.com/LINBIT/virter/internal/virter"
"github.com/LINBIT/virter/pkg/pullpolicy"
"github.com/LINBIT/virter/pkg/sshkeys"
)

Expand Down Expand Up @@ -92,6 +93,10 @@ user_public_key = "{{ get "auth.user_public_key" }}"
[container]
# provider is the container engine used. Can be either podman or docker.
provider = "{{ get "container.provider" }}"
# default pull policy to apply if non was specified. Can be 'Always', 'IfNotExist' or 'Never'.
# Default value: "{{ get "container.pull" }}"
pull = "{{ get "container.pull" }}"
`

// initConfig reads in config file and ENV variables if set.
Expand All @@ -105,6 +110,7 @@ func initConfig() {
viper.SetDefault("time.shutdown_timeout", 20*time.Second)
viper.SetDefault("auth.user_public_key", "")
viper.SetDefault("container.provider", "docker")
viper.SetDefault("container.pull", "IfNotExist")

viper.SetConfigType("toml")
if cfgFile != "" {
Expand Down Expand Up @@ -225,3 +231,19 @@ func getReadyConfig() virter.VmReadyConfig {
CheckTimeout: viper.GetDuration("time.ssh_ping_period"),
}
}

func getDefaultContainerPullPolicy() pullpolicy.PullPolicy {
opt := viper.GetString("container.pull")

if opt == "" {
return pullpolicy.IfNotExist
}

var policy pullpolicy.PullPolicy
err := policy.UnmarshalText([]byte(opt))
if err != nil {
log.WithError(err).Fatal("invalid default pull policy")
}

return policy
}
1 change: 1 addition & 0 deletions cmd/image_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func imageBuildCommand() *cobra.Command {
provOpt := virter.ProvisionOption{
FilePath: provisionFile,
Overrides: provisionOverrides,
DefaultPullPolicy: getDefaultContainerPullPolicy(),
OverridePullPolicy: containerPullPolicy,
}

Expand Down
1 change: 1 addition & 0 deletions cmd/vm_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func vmExecCommand() *cobra.Command {
provOpt := virter.ProvisionOption{
FilePath: provisionFile,
Overrides: provisionOverrides,
DefaultPullPolicy: getDefaultContainerPullPolicy(),
OverridePullPolicy: containerPullPolicy,
}
if err := execProvision(ctx, provOpt, args); err != nil {
Expand Down
1 change: 1 addition & 0 deletions cmd/vm_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ func vmRunCommand() *cobra.Command {
provOpt := virter.ProvisionOption{
FilePath: provisionFile,
Overrides: provisionOverrides,
DefaultPullPolicy: getDefaultContainerPullPolicy(),
OverridePullPolicy: containerPullPolicy,
}
if err := execProvision(ctx, provOpt, vmNames); err != nil {
Expand Down
3 changes: 2 additions & 1 deletion internal/virter/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func EnvmapToSlice(envMap map[string]string) []string {
type ProvisionOption struct {
FilePath string
Overrides []string
DefaultPullPolicy pullpolicy.PullPolicy
OverridePullPolicy pullpolicy.PullPolicy
}

Expand Down Expand Up @@ -154,7 +155,7 @@ func newProvisionConfigReader(provReader io.Reader, provOpt ProvisionOption) (Pr
}

if s.Docker.Pull == "" {
s.Docker.Pull = pullpolicy.IfNotExist
s.Docker.Pull = provOpt.DefaultPullPolicy
}

if provOpt.OverridePullPolicy != "" {
Expand Down
2 changes: 1 addition & 1 deletion internal/virter/provision_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ foo = "{{.ShellEnv"
Image: "some-image",
Command: []string{"exit", "0"},
Env: map[string]string{"foo": "bar"},
Pull: pullpolicy.IfNotExist,
Pull: "",
},
},
ProvisionStep{
Expand Down

0 comments on commit c47a9b9

Please sign in to comment.