diff --git a/nomad/structs/csi.go b/nomad/structs/csi.go index 07e5d46a868d..4bcc5e4cb696 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, - } + return &CSIVolume{ + ID: volumeID, + Topologies: []*CSITopology{}, + MountOptions: &CSIMountOptions{}, + Secrets: CSISecrets{}, + Parameters: map[string]string{}, + Context: map[string]string{}, - out.newStructs() - return out -} - -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,55 @@ 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() + out := new(CSIVolume) + *out = *v + + out.Topologies = []*CSITopology{} + for _, t := range v.Topologies { + out.Topologies = append(out.Topologies, t.Copy()) + } + + out.MountOptions = new(CSIMountOptions) + *out.MountOptions = *v.MountOptions + + out.Secrets = CSISecrets{} + for k, v := range v.Secrets { + out.Secrets[k] = v + } + + out.Parameters = map[string]string{} for k, v := range v.Parameters { out.Parameters[k] = v } + + out.Context = map[string]string{} for k, v := range v.Context { out.Context[k] = v } - 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.ReadAllocs = map[string]*Allocation{} + for k, alloc := range v.ReadAllocs { + out.ReadAllocs[k] = alloc.Copy() } - for k, v := range v.WriteAllocs { - if v != nil { - a := *v - out.WriteAllocs[k] = &a - } else { - out.WriteAllocs[k] = nil - } - + 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