diff --git a/cmd/root.go b/cmd/root.go index b18ba06..8052e3b 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -18,10 +18,6 @@ import ( "github.com/spf13/cobra" ) -// DockerAPIMinVersion is the minimum version of the docker api required to -// use watchtower -const DockerAPIMinVersion string = "1.24" - var ( client container.Client scheduleSpec string @@ -90,7 +86,7 @@ func PreRun(cmd *cobra.Command, args []string) { lifecycleHooks, _ = f.GetBool("enable-lifecycle-hooks") // configure environment vars for client - err := flags.EnvConfig(cmd, DockerAPIMinVersion) + err := flags.EnvConfig(cmd) if err != nil { log.Fatal(err) } diff --git a/docs/arguments.md b/docs/arguments.md index 77f1d0c..db51c95 100644 --- a/docs/arguments.md +++ b/docs/arguments.md @@ -75,7 +75,17 @@ Docker daemon socket to connect to. Can be pointed at a remote Docker host by sp Environment Variable: DOCKER_HOST Type: String Default: "unix:///var/run/docker.sock" -``` +``` + +## Docker API version +The API version to use by the Docker client for connecting to the Docker daemon. The minimum supported version is 1.24. + +``` + Argument: --api-version, -a +Environment Variable: DOCKER_API_VERSION + Type: String + Default: "1.24" +``` ## Include stopped Will also include created and exited containers. diff --git a/internal/flags/flags.go b/internal/flags/flags.go index d416243..179bc63 100644 --- a/internal/flags/flags.go +++ b/internal/flags/flags.go @@ -9,11 +9,16 @@ import ( "github.com/spf13/viper" ) +// DockerAPIMinVersion is the minimum version of the docker api required to +// use watchtower +const DockerAPIMinVersion string = "1.24" + // RegisterDockerFlags that are used directly by the docker api client func RegisterDockerFlags(rootCmd *cobra.Command) { flags := rootCmd.PersistentFlags() flags.StringP("host", "H", viper.GetString("DOCKER_HOST"), "daemon socket to connect to") flags.BoolP("tlsverify", "v", viper.GetBool("DOCKER_TLS_VERIFY"), "use TLS and verify the remote") + flags.StringP("api-version", "a", viper.GetString("DOCKER_API_VERSION"), "api version to use by docker client") } // RegisterSystemFlags that are used by watchtower to modify the program flow @@ -215,6 +220,7 @@ Should only be used for testing. func SetDefaults() { viper.AutomaticEnv() viper.SetDefault("DOCKER_HOST", "unix:///var/run/docker.sock") + viper.SetDefault("DOCKER_API_VERSION", DockerAPIMinVersion) viper.SetDefault("WATCHTOWER_POLL_INTERVAL", 300) viper.SetDefault("WATCHTOWER_TIMEOUT", time.Second*10) viper.SetDefault("WATCHTOWER_NOTIFICATIONS", []string{}) @@ -225,10 +231,11 @@ func SetDefaults() { // EnvConfig translates the command-line options into environment variables // that will initialize the api client -func EnvConfig(cmd *cobra.Command, dockerAPIMinVersion string) error { +func EnvConfig(cmd *cobra.Command) error { var err error var host string var tls bool + var version string flags := cmd.PersistentFlags() @@ -238,13 +245,16 @@ func EnvConfig(cmd *cobra.Command, dockerAPIMinVersion string) error { if tls, err = flags.GetBool("tlsverify"); err != nil { return err } + if version, err = flags.GetString("api-version"); err != nil { + return err + } if err = setEnvOptStr("DOCKER_HOST", host); err != nil { return err } if err = setEnvOptBool("DOCKER_TLS_VERIFY", tls); err != nil { return err } - if err = setEnvOptStr("DOCKER_API_VERSION", dockerAPIMinVersion); err != nil { + if err = setEnvOptStr("DOCKER_API_VERSION", version); err != nil { return err } return nil diff --git a/internal/flags/flags_test.go b/internal/flags/flags_test.go new file mode 100644 index 0000000..ac57b30 --- /dev/null +++ b/internal/flags/flags_test.go @@ -0,0 +1,39 @@ +package flags + +import ( + "os" + "testing" + + "github.com/spf13/cobra" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestEnvConfig_Defaults(t *testing.T) { + cmd := new(cobra.Command) + SetDefaults() + RegisterDockerFlags(cmd) + + err := EnvConfig(cmd) + require.NoError(t, err) + + assert.Equal(t, "unix:///var/run/docker.sock", os.Getenv("DOCKER_HOST")) + assert.Equal(t, "", os.Getenv("DOCKER_TLS_VERIFY")) + assert.Equal(t, DockerAPIMinVersion, os.Getenv("DOCKER_API_VERSION")) +} + +func TestEnvConfig_Custom(t *testing.T) { + cmd := new(cobra.Command) + SetDefaults() + RegisterDockerFlags(cmd) + + err := cmd.ParseFlags([]string{"--host", "some-custom-docker-host", "--tlsverify", "--api-version", "1.99"}) + require.NoError(t, err) + + err = EnvConfig(cmd) + require.NoError(t, err) + + assert.Equal(t, "some-custom-docker-host", os.Getenv("DOCKER_HOST")) + assert.Equal(t, "1", os.Getenv("DOCKER_TLS_VERIFY")) + assert.Equal(t, "1.99", os.Getenv("DOCKER_API_VERSION")) +}