Add support for whitelist of monitored containers

This commit is contained in:
Brian DeHamer 2015-08-04 16:32:01 +00:00
parent d6321bf8dc
commit b0910ee20b
5 changed files with 60 additions and 9 deletions

View file

@ -15,14 +15,29 @@ var (
func allContainersFilter(container.Container) bool { return true }
func containerFilter(names []string) container.Filter {
if len(names) == 0 {
return allContainersFilter
}
return func(c container.Container) bool {
for _, name := range names {
if (name == c.Name()) || (name == c.Name()[1:]) {
return true
}
}
return false
}
}
// Update looks at the running Docker containers to see if any of the images
// used to start those containers have been updated. If a change is detected in
// any of the images, the associated containers are stopped and restarted with
// the new image.
func Update(client container.Client, cleanup bool) error {
func Update(client container.Client, names []string, cleanup bool) error {
log.Info("Checking containers for updated images")
containers, err := client.ListContainers(allContainersFilter)
containers, err := client.ListContainers(containerFilter(names))
if err != nil {
return err
}

View file

@ -9,6 +9,30 @@ import (
"github.com/stretchr/testify/assert"
)
func TestContainerFilter_StraightMatch(t *testing.T) {
c := newTestContainer("foo", []string{})
f := containerFilter([]string{"foo"})
assert.True(t, f(c))
}
func TestContainerFilter_SlashMatch(t *testing.T) {
c := newTestContainer("/foo", []string{})
f := containerFilter([]string{"foo"})
assert.True(t, f(c))
}
func TestContainerFilter_NoMatch(t *testing.T) {
c := newTestContainer("/bar", []string{})
f := containerFilter([]string{"foo"})
assert.False(t, f(c))
}
func TestContainerFilter_NoFilters(t *testing.T) {
c := newTestContainer("/bar", []string{})
f := containerFilter([]string{})
assert.True(t, f(c))
}
func TestCheckDependencies(t *testing.T) {
cs := []container.Container{
newTestContainer("1", []string{}),