fix an issue in NodeInfo.Clone()

- usedPorts is a map-in-map struct, add fix to ensure it's deep copied
- updated unit test
pull/8/head
Wei Huang 2018-08-14 23:29:18 -07:00
parent 1da4c59e46
commit 976797c0b8
No known key found for this signature in database
GPG Key ID: BE5E9752F8B6E005
2 changed files with 8 additions and 2 deletions

View File

@ -400,8 +400,13 @@ func (n *NodeInfo) Clone() *NodeInfo {
clone.pods = append([]*v1.Pod(nil), n.pods...)
}
if len(n.usedPorts) > 0 {
for k, v := range n.usedPorts {
clone.usedPorts[k] = v
// util.HostPortInfo is a map-in-map struct
// make sure it's deep copied
for ip, portMap := range n.usedPorts {
clone.usedPorts[ip] = make(map[util.ProtocolPort]struct{})
for protocolPort, v := range portMap {
clone.usedPorts[ip][protocolPort] = v
}
}
}
if len(n.podsWithAffinity) > 0 {

View File

@ -495,6 +495,7 @@ func TestNodeInfoClone(t *testing.T) {
ni := test.nodeInfo.Clone()
// Modify the field to check if the result is a clone of the origin one.
test.nodeInfo.generation += 10
test.nodeInfo.usedPorts.Remove("127.0.0.1", "TCP", 80)
if !reflect.DeepEqual(test.expected, ni) {
t.Errorf("expected: %#v, got: %#v", test.expected, ni)
}