diff --git a/nomad/structs/csi.go b/nomad/structs/csi.go index 07e5d46a868d..e0eb5d8e1ef3 100644 --- a/nomad/structs/csi.go +++ b/nomad/structs/csi.go @@ -294,39 +294,24 @@ type CSIVolListStub struct { // NewCSIVolume creates the volume struct. No side-effects func NewCSIVolume(volumeID string, index uint64) *CSIVolume { - out := &CSIVolume{ - ID: volumeID, - CreateIndex: index, - ModifyIndex: index, - } - - out.newStructs() - return out -} + return &CSIVolume{ + ID: volumeID, + Topologies: []*CSITopology{}, + MountOptions: &CSIMountOptions{}, + Secrets: CSISecrets{}, + Parameters: map[string]string{}, + Context: map[string]string{}, -func (v *CSIVolume) newStructs() { - if v.Topologies == nil { - v.Topologies = []*CSITopology{} - } - if v.Context == nil { - v.Context = map[string]string{} - } - if v.Parameters == nil { - v.Parameters = map[string]string{} - } - if v.MountOptions == nil { - v.MountOptions = &CSIMountOptions{} - } - if v.Secrets == nil { - v.Secrets = CSISecrets{} - } + ReadAllocs: map[string]*Allocation{}, + WriteAllocs: map[string]*Allocation{}, - v.ReadAllocs = map[string]*Allocation{} - v.WriteAllocs = map[string]*Allocation{} + ReadClaims: map[string]*CSIVolumeClaim{}, + WriteClaims: map[string]*CSIVolumeClaim{}, + PastClaims: map[string]*CSIVolumeClaim{}, - v.ReadClaims = map[string]*CSIVolumeClaim{} - v.WriteClaims = map[string]*CSIVolumeClaim{} - v.PastClaims = map[string]*CSIVolumeClaim{} + CreateIndex: index, + ModifyIndex: index, + } } func (v *CSIVolume) RemoteID() string { @@ -407,48 +392,57 @@ func (v *CSIVolume) InUse() bool { // Copy returns a copy of the volume, which shares only the Topologies slice func (v *CSIVolume) Copy() *CSIVolume { - copy := *v - out := © - out.newStructs() - for k, v := range v.Parameters { - out.Parameters[k] = v + out := new(CSIVolume) + *out = *v + + out.Topologies = []*CSITopology{} + for _, t := range v.Topologies { + out.Topologies = append(out.Topologies, t.Copy()) } - for k, v := range v.Context { - out.Context[k] = v + + out.MountOptions = new(CSIMountOptions) + if v.MountOptions != nil { + *out.MountOptions = *v.MountOptions } + + out.Secrets = CSISecrets{} for k, v := range v.Secrets { out.Secrets[k] = v } - mo := *v.MountOptions - out.MountOptions = &mo - for k, v := range v.ReadAllocs { - if v != nil { - a := *v - out.ReadAllocs[k] = &a - } else { - out.ReadAllocs[k] = nil - } + out.Parameters = map[string]string{} + for k, v := range v.Parameters { + out.Parameters[k] = v } - for k, v := range v.WriteAllocs { - if v != nil { - a := *v - out.WriteAllocs[k] = &a - } else { - out.WriteAllocs[k] = nil - } + out.Context = map[string]string{} + for k, v := range v.Context { + out.Context[k] = v + } + + out.ReadAllocs = map[string]*Allocation{} + for k, alloc := range v.ReadAllocs { + out.ReadAllocs[k] = alloc.Copy() + } + out.WriteAllocs = map[string]*Allocation{} + for k, alloc := range v.WriteAllocs { + out.WriteAllocs[k] = alloc.Copy() } + out.ReadClaims = map[string]*CSIVolumeClaim{} for k, v := range v.ReadClaims { claim := *v out.ReadClaims[k] = &claim } + + out.WriteClaims = map[string]*CSIVolumeClaim{} for k, v := range v.WriteClaims { claim := *v out.WriteClaims[k] = &claim } + + out.PastClaims = map[string]*CSIVolumeClaim{} for k, v := range v.PastClaims { claim := *v out.PastClaims[k] = &claim @@ -784,19 +778,19 @@ func (p *CSIPlugin) Copy() *CSIPlugin { out.newStructs() for k, v := range p.Controllers { - out.Controllers[k] = v + out.Controllers[k] = v.Copy() } for k, v := range p.Nodes { - out.Nodes[k] = v + out.Nodes[k] = v.Copy() } for k, v := range p.ControllerJobs { - out.ControllerJobs[k] = v + out.ControllerJobs[k] = v.Copy() } for k, v := range p.NodeJobs { - out.NodeJobs[k] = v + out.NodeJobs[k] = v.Copy() } return out @@ -998,6 +992,14 @@ type JobDescription struct { // JobNamespacedDescriptions maps Job.ID to JobDescription type JobNamespacedDescriptions map[string]JobDescription +func (j JobNamespacedDescriptions) Copy() JobNamespacedDescriptions { + copy := JobNamespacedDescriptions{} + for k, v := range j { + copy[k] = v + } + return copy +} + // JobDescriptions maps Namespace to a mapping of Job.ID to JobDescription type JobDescriptions map[string]JobNamespacedDescriptions