Skip to content

Commit

Permalink
Merge pull request docker-archive#2202 from allencloud/change-the-way…
Browse files Browse the repository at this point in the history
…-of-producing-engine-label

change the way of producing engine label
  • Loading branch information
dongluochen authored Jul 7, 2016
2 parents b9de046 + c1100ab commit 6302424
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
27 changes: 20 additions & 7 deletions cluster/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,11 +493,19 @@ func (e *Engine) updateSpecs() error {
e.Name = info.Name
e.Cpus = int64(info.NCPU)
e.Memory = info.MemTotal
e.Labels = map[string]string{
"storagedriver": info.Driver,
"executiondriver": info.ExecutionDriver,
"kernelversion": info.KernelVersion,
"operatingsystem": info.OperatingSystem,

e.Labels = map[string]string{}
if info.Driver != "" {
e.Labels["storagedriver"] = info.Driver
}
if info.ExecutionDriver != "" {
e.Labels["executiondriver"] = info.ExecutionDriver
}
if info.KernelVersion != "" {
e.Labels["kernelversion"] = info.KernelVersion
}
if info.OperatingSystem != "" {
e.Labels["operatingsystem"] = info.OperatingSystem
}
for _, label := range info.Labels {
kv := strings.SplitN(label, "=", 2)
Expand All @@ -512,10 +520,15 @@ func (e *Engine) updateSpecs() error {
// since "node" in constraint will match node.Name instead of label.
// Log warn message in this case.
if kv[0] == "node" {
log.Warnf("Engine (ID: %s, Addr: %s) containers a label (%s) with key of \"node\" which cannot be used in Swarm.", e.ID, e.Addr, label)
log.Warnf("Engine (ID: %s, Addr: %s) contains a label (%s) with key of \"node\" which cannot be used in Swarm.", e.ID, e.Addr, label)
continue
}

e.Labels[kv[0]] = kv[1]
if value, exist := e.Labels[kv[0]]; exist {
log.Warnf("Node (ID: %s, Addr: %s) already contains a label (%s) with key (%s), and Engine's label (%s) cannot override it.", e.ID, e.Addr, value, kv[0], kv[1])
} else {
e.Labels[kv[0]] = kv[1]
}
}
return nil
}
Expand Down
21 changes: 14 additions & 7 deletions cluster/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func TestEngineFailureCount(t *testing.T) {
assert.True(t, engine.failureCount == 0)
}

func TestHealthINdicator(t *testing.T) {
func TestHealthIndicator(t *testing.T) {
engine := NewEngine("test", 0, engOpts)
assert.True(t, engine.state == statePending)
assert.True(t, engine.HealthIndicator() == 0)
Expand Down Expand Up @@ -222,14 +222,21 @@ func TestEngineSpecs(t *testing.T) {
assert.True(t, engine.isConnected())
assert.True(t, engine.IsHealthy())

assert.Equal(t, engine.Cpus, int64(mockInfo.NCPU))
assert.Equal(t, engine.Memory, mockInfo.MemTotal)
assert.Equal(t, engine.Labels["storagedriver"], mockInfo.Driver)
assert.Equal(t, engine.Labels["executiondriver"], mockInfo.ExecutionDriver)
assert.Equal(t, engine.Labels["kernelversion"], mockInfo.KernelVersion)
assert.Equal(t, engine.Labels["operatingsystem"], mockInfo.OperatingSystem)
mockInfo2 := mockInfo
mockInfo2.Labels = []string{"foo=bar", "executiondriver=newdriver", "node=node1"}

assert.Equal(t, engine.Cpus, int64(mockInfo2.NCPU))
assert.Equal(t, engine.Memory, mockInfo2.MemTotal)
assert.Equal(t, engine.Labels["storagedriver"], mockInfo2.Driver)

assert.Equal(t, engine.Labels["executiondriver"], mockInfo2.ExecutionDriver)

assert.Equal(t, engine.Labels["kernelversion"], mockInfo2.KernelVersion)
assert.Equal(t, engine.Labels["operatingsystem"], mockInfo2.OperatingSystem)
assert.Equal(t, engine.Labels["foo"], "bar")

assert.NotEqual(t, engine.Labels["node"], "node1")

client.Mock.AssertExpectations(t)
apiClient.Mock.AssertExpectations(t)
}
Expand Down

0 comments on commit 6302424

Please sign in to comment.