Skip to content

Commit

Permalink
csi: refactor CSIVolume Copy for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
tgross committed Nov 2, 2020
1 parent 849754c commit 8915dd0
Showing 1 changed file with 34 additions and 44 deletions.
78 changes: 34 additions & 44 deletions nomad/structs/csi.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,25 +305,14 @@ func NewCSIVolume(volumeID string, index uint64) *CSIVolume {
}

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{}
}
v.Topologies = []*CSITopology{}
v.MountOptions = new(CSIMountOptions)
v.Secrets = CSISecrets{}
v.Parameters = map[string]string{}
v.Context = map[string]string{}

v.ReadAllocs = map[string]*Allocation{}
v.WriteAllocs = map[string]*Allocation{}

v.ReadClaims = map[string]*CSIVolumeClaim{}
v.WriteClaims = map[string]*CSIVolumeClaim{}
v.PastClaims = map[string]*CSIVolumeClaim{}
Expand Down Expand Up @@ -407,38 +396,31 @@ 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 := &copy
out.newStructs()
out := new(CSIVolume)
*out = *v
out.newStructs() // zero-out the non-primitive structs

for _, t := range v.Topologies {
out.Topologies = append(out.Topologies, t.Copy())
}
if v.MountOptions != nil {
*out.MountOptions = *v.MountOptions
}
for k, v := range v.Secrets {
out.Secrets[k] = v
}
for k, v := range v.Parameters {
out.Parameters[k] = v
}
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
}
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
}

for k, alloc := range v.WriteAllocs {
out.WriteAllocs[k] = alloc.Copy()
}

for k, v := range v.ReadClaims {
Expand Down Expand Up @@ -784,19 +766,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
Expand Down Expand Up @@ -998,6 +980,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

Expand Down

0 comments on commit 8915dd0

Please sign in to comment.