Skip to content

Commit

Permalink
Merge pull request #11744 from hashicorp/b-node-copy
Browse files Browse the repository at this point in the history
Fix Node.Copy()
  • Loading branch information
schmichael authored and lgfa29 committed Jan 18, 2022
1 parent 2ba4892 commit 5d5bb26
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .changelog/11744.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
core: Fix missing fields in Node.Copy()
```
4 changes: 1 addition & 3 deletions nomad/node_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1531,9 +1531,7 @@ func TestClientEndpoint_GetNode(t *testing.T) {
node.StatusUpdatedAt = resp2.Node.StatusUpdatedAt
node.SecretID = ""
node.Events = resp2.Node.Events
if !reflect.DeepEqual(node, resp2.Node) {
t.Fatalf("bad: %#v \n %#v", node, resp2.Node)
}
require.Equal(t, node, resp2.Node)

// assert that the node register event was set correctly
if len(resp2.Node.Events) != 1 {
Expand Down
24 changes: 17 additions & 7 deletions nomad/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1980,19 +1980,19 @@ func (n *Node) Copy() *Node {
nn := new(Node)
*nn = *n
nn.Attributes = helper.CopyMapStringString(nn.Attributes)
nn.Resources = nn.Resources.Copy()
nn.Reserved = nn.Reserved.Copy()
nn.NodeResources = nn.NodeResources.Copy()
nn.ReservedResources = nn.ReservedResources.Copy()
nn.Resources = nn.Resources.Copy()
nn.Reserved = nn.Reserved.Copy()
nn.Links = helper.CopyMapStringString(nn.Links)
nn.Meta = helper.CopyMapStringString(nn.Meta)
nn.Events = copyNodeEvents(n.Events)
nn.DrainStrategy = nn.DrainStrategy.Copy()
nn.LastDrain = nn.LastDrain.Copy()
nn.Events = copyNodeEvents(n.Events)
nn.Drivers = copyNodeDrivers(n.Drivers)
nn.CSIControllerPlugins = copyNodeCSI(nn.CSIControllerPlugins)
nn.CSINodePlugins = copyNodeCSI(nn.CSINodePlugins)
nn.Drivers = copyNodeDrivers(n.Drivers)
nn.HostVolumes = copyNodeHostVolumes(n.HostVolumes)
nn.LastDrain = nn.LastDrain.Copy()
return nn
}

Expand Down Expand Up @@ -2602,6 +2602,7 @@ func (n *NetworkResource) Copy() *NetworkResource {
}
newR := new(NetworkResource)
*newR = *n
newR.DNS = n.DNS.Copy()
if n.ReservedPorts != nil {
newR.ReservedPorts = make([]Port, len(n.ReservedPorts))
copy(newR.ReservedPorts, n.ReservedPorts)
Expand Down Expand Up @@ -2816,8 +2817,7 @@ func (n *NodeResources) Copy() *NodeResources {

newN := new(NodeResources)
*newN = *n

// Copy the networks
newN.Cpu = n.Cpu.Copy()
newN.Networks = n.Networks.Copy()

if n.NodeNetworks != nil {
Expand Down Expand Up @@ -3011,6 +3011,16 @@ type NodeCpuResources struct {
ReservableCpuCores []uint16
}

func (n NodeCpuResources) Copy() NodeCpuResources {
newN := n
if n.ReservableCpuCores != nil {
newN.ReservableCpuCores = make([]uint16, len(n.ReservableCpuCores))
copy(newN.ReservableCpuCores, n.ReservableCpuCores)
}

return newN
}

func (n *NodeCpuResources) Merge(o *NodeCpuResources) {
if o == nil {
return
Expand Down
9 changes: 9 additions & 0 deletions nomad/structs/structs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1672,6 +1672,15 @@ func TestNetworkResource_Copy(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
output := tc.inputNetworkResource.Copy()
assert.Equal(t, tc.inputNetworkResource, output, tc.name)

if output == nil {
return
}

// Assert changes to the copy aren't propagated to the
// original
output.DNS.Servers[1] = "foo"
assert.NotEqual(t, tc.inputNetworkResource, output, tc.name)
})
}
}
Expand Down

0 comments on commit 5d5bb26

Please sign in to comment.