mirror of https://github.com/docker/docs.git
Merge pull request #1587 from vieux/do_not_save_image_aff
do not save image affinity on reschedule
This commit is contained in:
commit
3aa302d706
|
|
@ -159,6 +159,22 @@ func (c *ContainerConfig) AddAffinity(affinity string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RemoveAffinity from config
|
||||||
|
func (c *ContainerConfig) RemoveAffinity(affinity string) error {
|
||||||
|
affinities := []string{}
|
||||||
|
for _, a := range c.extractExprs("affinities") {
|
||||||
|
if a != affinity {
|
||||||
|
affinities = append(affinities, a)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
labels, err := json.Marshal(affinities)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
c.Labels[SwarmLabelNamespace+".affinities"] = string(labels)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// HaveNodeConstraint in config
|
// HaveNodeConstraint in config
|
||||||
func (c *ContainerConfig) HaveNodeConstraint() bool {
|
func (c *ContainerConfig) HaveNodeConstraint() bool {
|
||||||
constraints := c.extractExprs("constraints")
|
constraints := c.extractExprs("constraints")
|
||||||
|
|
|
||||||
|
|
@ -90,6 +90,20 @@ func TestAddAffinity(t *testing.T) {
|
||||||
assert.Len(t, config.Affinities(), 1)
|
assert.Len(t, config.Affinities(), 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRemoveAffinity(t *testing.T) {
|
||||||
|
config := BuildContainerConfig(dockerclient.ContainerConfig{})
|
||||||
|
assert.Empty(t, config.Affinities())
|
||||||
|
|
||||||
|
config.AddAffinity("image==~testimage1")
|
||||||
|
config.AddAffinity("image==~testimage2")
|
||||||
|
assert.Len(t, config.Affinities(), 2)
|
||||||
|
|
||||||
|
config.RemoveAffinity("image==~testimage1")
|
||||||
|
assert.Len(t, config.Affinities(), 1)
|
||||||
|
|
||||||
|
assert.Equal(t, config.Affinities()[0], "image==~testimage2")
|
||||||
|
}
|
||||||
|
|
||||||
func TestHaveNodeConstraint(t *testing.T) {
|
func TestHaveNodeConstraint(t *testing.T) {
|
||||||
config := BuildContainerConfig(dockerclient.ContainerConfig{})
|
config := BuildContainerConfig(dockerclient.ContainerConfig{})
|
||||||
assert.False(t, config.HaveNodeConstraint())
|
assert.False(t, config.HaveNodeConstraint())
|
||||||
|
|
|
||||||
|
|
@ -118,12 +118,12 @@ func (c *Cluster) generateUniqueID() string {
|
||||||
func (c *Cluster) CreateContainer(config *cluster.ContainerConfig, name string, authConfig *dockerclient.AuthConfig) (*cluster.Container, error) {
|
func (c *Cluster) CreateContainer(config *cluster.ContainerConfig, name string, authConfig *dockerclient.AuthConfig) (*cluster.Container, error) {
|
||||||
container, err := c.createContainer(config, name, false, authConfig)
|
container, err := c.createContainer(config, name, false, authConfig)
|
||||||
|
|
||||||
// fails with image not found, then try to reschedule with image-affinity
|
// fails with image not found, then try to reschedule with image affinity
|
||||||
if err != nil {
|
if err != nil {
|
||||||
bImageNotFoundError, _ := regexp.MatchString(`image \S* not found`, err.Error())
|
bImageNotFoundError, _ := regexp.MatchString(`image \S* not found`, err.Error())
|
||||||
if bImageNotFoundError && !config.HaveNodeConstraint() {
|
if bImageNotFoundError && !config.HaveNodeConstraint() {
|
||||||
// Check if the image exists in the cluster
|
// Check if the image exists in the cluster
|
||||||
// If exists, retry with a image-affinity
|
// If exists, retry with a image affinity
|
||||||
if c.Image(config.Image) != nil {
|
if c.Image(config.Image) != nil {
|
||||||
container, err = c.createContainer(config, name, true, authConfig)
|
container, err = c.createContainer(config, name, true, authConfig)
|
||||||
}
|
}
|
||||||
|
|
@ -145,12 +145,16 @@ func (c *Cluster) createContainer(config *cluster.ContainerConfig, name string,
|
||||||
swarmID := c.generateUniqueID()
|
swarmID := c.generateUniqueID()
|
||||||
config.SetSwarmID(swarmID)
|
config.SetSwarmID(swarmID)
|
||||||
|
|
||||||
configTemp := config
|
|
||||||
if withImageAffinity {
|
if withImageAffinity {
|
||||||
configTemp.AddAffinity("image==" + config.Image)
|
config.AddAffinity("image==" + config.Image)
|
||||||
|
}
|
||||||
|
|
||||||
|
nodes, err := c.scheduler.SelectNodesForContainer(c.listNodes(), config)
|
||||||
|
|
||||||
|
if withImageAffinity {
|
||||||
|
config.RemoveAffinity("image==" + config.Image)
|
||||||
}
|
}
|
||||||
|
|
||||||
nodes, err := c.scheduler.SelectNodesForContainer(c.listNodes(), configTemp)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.scheduler.Unlock()
|
c.scheduler.Unlock()
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
||||||
|
|
@ -101,7 +101,7 @@ function teardown() {
|
||||||
[[ "${output}" == *"\"StopSignal\": \"SIGKILL\""* ]]
|
[[ "${output}" == *"\"StopSignal\": \"SIGKILL\""* ]]
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "docker run - reschedule with soft-image-affinity" {
|
@test "docker run - reschedule with image affinity" {
|
||||||
start_docker_with_busybox 1
|
start_docker_with_busybox 1
|
||||||
start_docker 1
|
start_docker 1
|
||||||
|
|
||||||
|
|
@ -115,15 +115,19 @@ function teardown() {
|
||||||
|
|
||||||
# try to create container on node-1, node-1 does not have busyboxabcde and will pull it
|
# try to create container on node-1, node-1 does not have busyboxabcde and will pull it
|
||||||
# but can not find busyboxabcde in dockerhub
|
# but can not find busyboxabcde in dockerhub
|
||||||
# then will retry with soft-image-affinity
|
# then will retry with image affinity
|
||||||
docker_swarm run -d --name test_container -e constraint:node==~node-1 busyboxabcde sleep 1000
|
docker_swarm run -d --name test_container -e constraint:node==~node-1 busyboxabcde sleep 1000
|
||||||
|
|
||||||
# check container running on node-0
|
# check container running on node-0
|
||||||
run docker_swarm ps
|
run docker_swarm ps
|
||||||
[[ "${output}" == *"node-0/test_container"* ]]
|
[[ "${output}" == *"node-0/test_container"* ]]
|
||||||
|
|
||||||
|
# check the image affinity wasn't saved
|
||||||
|
run docker_swarm inspect test_container
|
||||||
|
[[ "${output}" != *"image==busyboxabcde"* ]]
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "docker run - reschedule with soft-image-affinity and node constraint" {
|
@test "docker run - reschedule with image affinity and node constraint" {
|
||||||
start_docker_with_busybox 1
|
start_docker_with_busybox 1
|
||||||
start_docker 1
|
start_docker 1
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue