mirror of
https://github.com/containrrr/watchtower.git
synced 2025-12-14 22:20:12 +01:00
refactor: extract types and pkgs to new files
This commit is contained in:
parent
4a92a03f31
commit
e109a7a6ce
15 changed files with 71 additions and 57 deletions
|
|
@ -5,10 +5,10 @@ import (
|
|||
"io/ioutil"
|
||||
"time"
|
||||
|
||||
t "github.com/containrrr/watchtower/pkg/types"
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"github.com/docker/docker/api/types/filters"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/network"
|
||||
dockerclient "github.com/docker/docker/client"
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
|
@ -22,7 +22,7 @@ const (
|
|||
// A Client is the interface through which watchtower interacts with the
|
||||
// Docker API.
|
||||
type Client interface {
|
||||
ListContainers(Filter) ([]Container, error)
|
||||
ListContainers(t.Filter) ([]Container, error)
|
||||
StopContainer(Container, time.Duration) error
|
||||
StartContainer(Container) error
|
||||
RenameContainer(Container, string) error
|
||||
|
|
@ -58,7 +58,7 @@ type dockerClient struct {
|
|||
includeStopped bool
|
||||
}
|
||||
|
||||
func (client dockerClient) ListContainers(fn Filter) ([]Container, error) {
|
||||
func (client dockerClient) ListContainers(fn t.Filter) ([]Container, error) {
|
||||
cs := []Container{}
|
||||
bg := context.Background()
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package container
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/containrrr/watchtower/internal/util"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
|
|
@ -146,19 +147,19 @@ func (c Container) runtimeConfig() *dockercontainer.Config {
|
|||
config.User = ""
|
||||
}
|
||||
|
||||
if sliceEqual(config.Cmd, imageConfig.Cmd) {
|
||||
if util.SliceEqual(config.Cmd, imageConfig.Cmd) {
|
||||
config.Cmd = nil
|
||||
}
|
||||
|
||||
if sliceEqual(config.Entrypoint, imageConfig.Entrypoint) {
|
||||
if util.SliceEqual(config.Entrypoint, imageConfig.Entrypoint) {
|
||||
config.Entrypoint = nil
|
||||
}
|
||||
|
||||
config.Env = sliceSubtract(config.Env, imageConfig.Env)
|
||||
config.Env = util.SliceSubtract(config.Env, imageConfig.Env)
|
||||
|
||||
config.Labels = stringMapSubtract(config.Labels, imageConfig.Labels)
|
||||
config.Labels = util.StringMapSubtract(config.Labels, imageConfig.Labels)
|
||||
|
||||
config.Volumes = structMapSubtract(config.Volumes, imageConfig.Volumes)
|
||||
config.Volumes = util.StructMapSubtract(config.Volumes, imageConfig.Volumes)
|
||||
|
||||
// subtract ports exposed in image from container
|
||||
for k := range config.ExposedPorts {
|
||||
|
|
|
|||
|
|
@ -1,30 +1,20 @@
|
|||
package container
|
||||
|
||||
// A Filter is a prototype for a function that can be used to filter the
|
||||
// results from a call to the ListContainers() method on the Client.
|
||||
type Filter func(FilterableContainer) bool
|
||||
|
||||
// A FilterableContainer is the interface which is used to filter
|
||||
// containers.
|
||||
type FilterableContainer interface {
|
||||
Name() string
|
||||
IsWatchtower() bool
|
||||
Enabled() (bool, bool)
|
||||
}
|
||||
import t "github.com/containrrr/watchtower/pkg/types"
|
||||
|
||||
// WatchtowerContainersFilter filters only watchtower containers
|
||||
func WatchtowerContainersFilter(c FilterableContainer) bool { return c.IsWatchtower() }
|
||||
func WatchtowerContainersFilter(c t.FilterableContainer) bool { return c.IsWatchtower() }
|
||||
|
||||
// Filter no containers and returns all
|
||||
func noFilter(FilterableContainer) bool { return true }
|
||||
func noFilter(t.FilterableContainer) bool { return true }
|
||||
|
||||
// Filters containers which don't have a specified name
|
||||
func filterByNames(names []string, baseFilter Filter) Filter {
|
||||
func filterByNames(names []string, baseFilter t.Filter) t.Filter {
|
||||
if len(names) == 0 {
|
||||
return baseFilter
|
||||
}
|
||||
|
||||
return func(c FilterableContainer) bool {
|
||||
return func(c t.FilterableContainer) bool {
|
||||
for _, name := range names {
|
||||
if (name == c.Name()) || (name == c.Name()[1:]) {
|
||||
return baseFilter(c)
|
||||
|
|
@ -35,8 +25,8 @@ func filterByNames(names []string, baseFilter Filter) Filter {
|
|||
}
|
||||
|
||||
// Filters out containers that don't have the 'enableLabel'
|
||||
func filterByEnableLabel(baseFilter Filter) Filter {
|
||||
return func(c FilterableContainer) bool {
|
||||
func filterByEnableLabel(baseFilter t.Filter) t.Filter {
|
||||
return func(c t.FilterableContainer) bool {
|
||||
// If label filtering is enabled, containers should only be considered
|
||||
// if the label is specifically set.
|
||||
_, ok := c.Enabled()
|
||||
|
|
@ -49,8 +39,8 @@ func filterByEnableLabel(baseFilter Filter) Filter {
|
|||
}
|
||||
|
||||
// Filters out containers that have a 'enableLabel' and is set to disable.
|
||||
func filterByDisabledLabel(baseFilter Filter) Filter {
|
||||
return func(c FilterableContainer) bool {
|
||||
func filterByDisabledLabel(baseFilter t.Filter) t.Filter {
|
||||
return func(c t.FilterableContainer) bool {
|
||||
enabledLabel, ok := c.Enabled()
|
||||
if ok && !enabledLabel {
|
||||
// If the label has been set and it demands a disable
|
||||
|
|
@ -62,7 +52,7 @@ func filterByDisabledLabel(baseFilter Filter) Filter {
|
|||
}
|
||||
|
||||
// BuildFilter creates the needed filter of containers
|
||||
func BuildFilter(names []string, enableLabel bool) Filter {
|
||||
func BuildFilter(names []string, enableLabel bool) t.Filter {
|
||||
filter := noFilter
|
||||
filter = filterByNames(names, filter)
|
||||
if enableLabel {
|
||||
|
|
|
|||
|
|
@ -1,64 +0,0 @@
|
|||
package container
|
||||
|
||||
func sliceEqual(s1, s2 []string) bool {
|
||||
if len(s1) != len(s2) {
|
||||
return false
|
||||
}
|
||||
|
||||
for i := range s1 {
|
||||
if s1[i] != s2[i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func sliceSubtract(a1, a2 []string) []string {
|
||||
a := []string{}
|
||||
|
||||
for _, e1 := range a1 {
|
||||
found := false
|
||||
|
||||
for _, e2 := range a2 {
|
||||
if e1 == e2 {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
a = append(a, e1)
|
||||
}
|
||||
}
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
func stringMapSubtract(m1, m2 map[string]string) map[string]string {
|
||||
m := map[string]string{}
|
||||
|
||||
for k1, v1 := range m1 {
|
||||
if v2, ok := m2[k1]; ok {
|
||||
if v2 != v1 {
|
||||
m[k1] = v1
|
||||
}
|
||||
} else {
|
||||
m[k1] = v1
|
||||
}
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
func structMapSubtract(m1, m2 map[string]struct{}) map[string]struct{} {
|
||||
m := map[string]struct{}{}
|
||||
|
||||
for k1, v1 := range m1 {
|
||||
if _, ok := m2[k1]; !ok {
|
||||
m[k1] = v1
|
||||
}
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
|
@ -1,66 +0,0 @@
|
|||
package container
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
|
||||
|
||||
func TestSliceEqual_True(t *testing.T) {
|
||||
s1 := []string{"a", "b", "c"}
|
||||
s2 := []string{"a", "b", "c"}
|
||||
|
||||
result := sliceEqual(s1, s2)
|
||||
|
||||
assert.True(t, result)
|
||||
}
|
||||
|
||||
func TestSliceEqual_DifferentLengths(t *testing.T) {
|
||||
s1 := []string{"a", "b", "c"}
|
||||
s2 := []string{"a", "b", "c", "d"}
|
||||
|
||||
result := sliceEqual(s1, s2)
|
||||
|
||||
assert.False(t, result)
|
||||
}
|
||||
|
||||
func TestSliceEqual_DifferentContents(t *testing.T) {
|
||||
s1 := []string{"a", "b", "c"}
|
||||
s2 := []string{"a", "b", "d"}
|
||||
|
||||
result := sliceEqual(s1, s2)
|
||||
|
||||
assert.False(t, result)
|
||||
}
|
||||
|
||||
func TestSliceSubtract(t *testing.T) {
|
||||
a1 := []string{"a", "b", "c"}
|
||||
a2 := []string{"a", "c"}
|
||||
|
||||
result := sliceSubtract(a1, a2)
|
||||
assert.Equal(t, []string{"b"}, result)
|
||||
assert.Equal(t, []string{"a", "b", "c"}, a1)
|
||||
assert.Equal(t, []string{"a", "c"}, a2)
|
||||
}
|
||||
|
||||
func TestStringMapSubtract(t *testing.T) {
|
||||
m1 := map[string]string{"a": "a", "b": "b", "c": "sea"}
|
||||
m2 := map[string]string{"a": "a", "c": "c"}
|
||||
|
||||
result := stringMapSubtract(m1, m2)
|
||||
assert.Equal(t, map[string]string{"b": "b", "c": "sea"}, result)
|
||||
assert.Equal(t, map[string]string{"a": "a", "b": "b", "c": "sea"}, m1)
|
||||
assert.Equal(t, map[string]string{"a": "a", "c": "c"}, m2)
|
||||
}
|
||||
|
||||
func TestStructMapSubtract(t *testing.T) {
|
||||
x := struct{}{}
|
||||
m1 := map[string]struct{}{"a": x, "b": x, "c": x}
|
||||
m2 := map[string]struct{}{"a": x, "c": x}
|
||||
|
||||
result := structMapSubtract(m1, m2)
|
||||
assert.Equal(t, map[string]struct{}{"b": x}, result)
|
||||
assert.Equal(t, map[string]struct{}{"a": x, "b": x, "c": x}, m1)
|
||||
assert.Equal(t, map[string]struct{}{"a": x, "c": x}, m2)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue