Add a method of enabling or disabling containers using labels

Switch command line flag from no-enable to label-enable and simplify logic

Add basic documentation for the --label-enable flag
This commit is contained in:
Kaleb Elwert 2017-10-17 06:53:30 +02:00 committed by Fabrizio Steiner
parent f365014b8f
commit de2ac9341d
4 changed files with 56 additions and 5 deletions

View file

@ -37,19 +37,20 @@ 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 bool) Client {
func NewClient(pullImages, enableLabel bool) Client {
cli, err := dockerclient.NewEnvClient()
if err != nil {
log.Fatalf("Error instantiating Docker client: %s", err)
}
return dockerClient{api: cli, pullImages: pullImages}
return dockerClient{api: cli, pullImages: pullImages, enableLabel: enableLabel}
}
type dockerClient struct {
api *dockerclient.Client
pullImages bool
api *dockerclient.Client
pullImages bool
enableLabel bool
}
func (client dockerClient) ListContainers(fn Filter) ([]Container, error) {
@ -77,6 +78,16 @@ 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)
}

View file

@ -2,6 +2,7 @@ package container
import (
"fmt"
"strconv"
"strings"
"github.com/docker/docker/api/types"
@ -11,6 +12,7 @@ import (
const (
watchtowerLabel = "com.centurylinklabs.watchtower"
signalLabel = "com.centurylinklabs.watchtower.stop-signal"
enableLabel = "com.centurylinklabs.watchtower.enable"
zodiacLabel = "com.centurylinklabs.zodiac.original-image"
)
@ -64,6 +66,22 @@ func (c Container) ImageName() string {
return imageName
}
// Enabled returns the value of the container enabled label and if the label
// was set.
func (c Container) Enabled() (bool, bool) {
rawBool, ok := c.containerInfo.Config.Labels[enableLabel]
if !ok {
return false, false
}
parsedBool, err := strconv.ParseBool(rawBool)
if err != nil {
return false, false
}
return parsedBool, true
}
// Links returns a list containing the names of all the containers to which
// this container is linked.
func (c Container) Links() []string {