From 315ddfeb4d0359db06410cf68166fee1ae24f0cc Mon Sep 17 00:00:00 2001 From: Xian Chaobo Date: Thu, 8 Oct 2015 05:06:39 -0400 Subject: [PATCH] do not try retry with soft-image-affinity when have node constraint Signed-off-by: Xian Chaobo --- cluster/config.go | 12 ++++++++++++ cluster/config_test.go | 8 ++++++++ cluster/swarm/cluster.go | 2 +- test/integration/api/run.bats | 22 ++++++++++++++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) diff --git a/cluster/config.go b/cluster/config.go index 34582b0b50..ce085dd1e3 100644 --- a/cluster/config.go +++ b/cluster/config.go @@ -158,3 +158,15 @@ func (c *ContainerConfig) AddAffinity(affinity string) error { c.Labels[SwarmLabelNamespace+".affinities"] = string(labels) return nil } + +// HaveNodeConstraint in config +func (c *ContainerConfig) HaveNodeConstraint() bool { + constraints := c.extractExprs("constraints") + + for _, constraint := range constraints { + if strings.HasPrefix(constraint, "node==") && !strings.HasPrefix(constraint, "node==~") { + return true + } + } + return false +} diff --git a/cluster/config_test.go b/cluster/config_test.go index 15e0a6fe4f..82a9827f12 100644 --- a/cluster/config_test.go +++ b/cluster/config_test.go @@ -89,3 +89,11 @@ func TestAddAffinity(t *testing.T) { config.AddAffinity("image==~testimage") assert.Len(t, config.Affinities(), 1) } + +func TestHaveNodeConstraint(t *testing.T) { + config := BuildContainerConfig(dockerclient.ContainerConfig{}) + assert.False(t, config.HaveNodeConstraint()) + + config = BuildContainerConfig(dockerclient.ContainerConfig{Env: []string{"constraint:node==node1"}}) + assert.True(t, config.HaveNodeConstraint()) +} diff --git a/cluster/swarm/cluster.go b/cluster/swarm/cluster.go index b41fb405df..40d563d72b 100644 --- a/cluster/swarm/cluster.go +++ b/cluster/swarm/cluster.go @@ -90,7 +90,7 @@ func (c *Cluster) CreateContainer(config *cluster.ContainerConfig, name string) container, err := c.createContainer(config, name, false) // fails with image not found, then try to reschedule with soft-image-affinity - if err != nil && strings.HasSuffix(err.Error(), "not found") { + if err != nil && strings.HasSuffix(err.Error(), "not found") && !config.HaveNodeConstraint() { // Check if the image exists in the cluster // If exists, retry with a soft-image-affinity if image := c.Image(config.Image); image != nil { diff --git a/test/integration/api/run.bats b/test/integration/api/run.bats index 7729c6a972..7da06c3288 100644 --- a/test/integration/api/run.bats +++ b/test/integration/api/run.bats @@ -88,3 +88,25 @@ function teardown() { run docker_swarm ps [[ "${output}" == *"node-0/test_container"* ]] } + +@test "docker run - reschedule with soft-image-affinity(have node constraint))" { + start_docker_with_busybox 1 + start_docker 1 + + docker -H ${HOSTS[0]} tag busybox:latest busyboxabcde:latest + swarm_manage + + # make sure busyboxabcde exists + run docker_swarm images + [ "$status" -eq 0 ] + [[ "${output}" == *"busyboxabcde"* ]] + + # create container on node-1, node-1 does not have busyboxabcde and will pull it + # but can not find busyboxabcde in dockerhub + # because run with node constraint, will not retry with soft-image-affinity + run docker_swarm run -d --name test_container -e constraint:node==node-1 busyboxabcde sleep 1000 + + # check error message + [[ "${output}" != *"unable to find a node that satisfies"* ]] + [[ "${output}" == *"busyboxabcde:latest not found"* ]] +}