diff --git a/nomad/structs/structs.go b/nomad/structs/structs.go index 910520df2..ef1f6c04b 100644 --- a/nomad/structs/structs.go +++ b/nomad/structs/structs.go @@ -539,6 +539,18 @@ type Resources struct { Networks []*NetworkResource } +// Copy returns a deep copy of the resources +func (r *Resources) Copy() *Resources { + newR := new(Resources) + *newR = *r + n := len(r.Networks) + newR.Networks = make([]*NetworkResource, n) + for i := 0; i < n; i++ { + newR.Networks[i] = r.Networks[i].Copy() + } + return newR +} + // NetIndex finds the matching net index either using IP or // CIDR block lookup func (r *Resources) NetIndex(n *NetworkResource) int { @@ -664,6 +676,15 @@ type NetworkResource struct { DynamicPorts int // Dynamically assigned ports } +// Copy returns a deep copy of the network resource +func (n *NetworkResource) Copy() *NetworkResource { + newR := new(NetworkResource) + *newR = *n + newR.ReservedPorts = make([]int, len(n.ReservedPorts)) + copy(newR.ReservedPorts, n.ReservedPorts) + return newR +} + // Add adds the resources of the delta to this, potentially // returning an error if not possible. func (n *NetworkResource) Add(delta *NetworkResource) {