implemented enableLabel by a Filter

This commit is contained in:
Fabrizio Steiner 2018-03-02 17:22:42 +01:00
parent f5fb4ed6a2
commit 026a04b59b
6 changed files with 79 additions and 46 deletions

View file

@ -5,10 +5,10 @@ import (
"io/ioutil"
"time"
log "github.com/sirupsen/logrus"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/network"
dockerclient "github.com/docker/docker/client"
log "github.com/sirupsen/logrus"
"golang.org/x/net/context"
)
@ -16,10 +16,6 @@ const (
defaultStopSignal = "SIGTERM"
)
// 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(Container) bool
// A Client is the interface through which watchtower interacts with the
// Docker API.
type Client interface {
@ -37,20 +33,19 @@ type Client interface {
// * DOCKER_HOST the docker-engine host to send api requests to
// * DOCKER_TLS_VERIFY whether to verify tls certificates
// * DOCKER_API_VERSION the minimum docker api version to work with
func NewClient(pullImages, enableLabel bool) Client {
func NewClient(pullImages bool) Client {
cli, err := dockerclient.NewEnvClient()
if err != nil {
log.Fatalf("Error instantiating Docker client: %s", err)
}
return dockerClient{api: cli, pullImages: pullImages, enableLabel: enableLabel}
return dockerClient{api: cli, pullImages: pullImages}
}
type dockerClient struct {
api *dockerclient.Client
pullImages bool
enableLabel bool
api *dockerclient.Client
pullImages bool
}
func (client dockerClient) ListContainers(fn Filter) ([]Container, error) {
@ -62,6 +57,7 @@ func (client dockerClient) ListContainers(fn Filter) ([]Container, error) {
runningContainers, err := client.api.ContainerList(
bg,
types.ContainerListOptions{})
if err != nil {
return nil, err
}
@ -79,15 +75,6 @@ func (client dockerClient) ListContainers(fn Filter) ([]Container, error) {
c := Container{containerInfo: &containerInfo, imageInfo: &imageInfo}
if client.enableLabel {
// If label filtering is enabled, containers should only be enabled
// if the label is specifically set to true.
enabledLabel, ok := c.Enabled()
if !ok || !enabledLabel {
continue
}
}
if fn(c) {
cs = append(cs, c)
}

61
container/filters.go Normal file
View file

@ -0,0 +1,61 @@
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)
}
// WatchtowerContainersFilter filters only watchtower containers
func WatchtowerContainersFilter(c FilterableContainer) bool { return c.IsWatchtower() }
// Filter no containers and returns all
func noFilter(FilterableContainer) bool { return true }
// Filters containers which don't have a specified name
func filterByNames(names []string, baseFilter Filter) Filter {
if len(names) == 0 {
return baseFilter
}
return func(c FilterableContainer) bool {
for _, name := range names {
if (name == c.Name()) || (name == c.Name()[1:]) {
return baseFilter(c)
}
}
return false
}
}
// Filters out containers that don't have the 'enableLabel'
func filterByEnableLabel(baseFilter Filter) Filter {
return func(c FilterableContainer) bool {
// If label filtering is enabled, containers should only be enabled
// if the label is specifically set to true.
enabledLabel, ok := c.Enabled()
if !ok || !enabledLabel {
return false
}
return baseFilter(c)
}
}
// BuildFilter creates the needed filter of containers
func BuildFilter(names []string, enableLabel bool) Filter {
filter := noFilter
filter = filterByNames(names, filter)
if enableLabel {
// If label filtering is enabled, containers should only be considered
// if the label is specifically set.
filter = filterByEnableLabel(filter)
}
return filter
}