Skip to content

Commit

Permalink
csi: nil-check allocs for VolumeDenormalize and claim methods (#7760)
Browse files Browse the repository at this point in the history
  • Loading branch information
tgross authored Apr 21, 2020
1 parent 2c1dcc8 commit 9f5156a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
10 changes: 7 additions & 3 deletions nomad/state/state_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2143,22 +2143,26 @@ func (s *StateStore) CSIVolumeDenormalizePlugins(ws memdb.WatchSet, vol *structs
return vol, nil
}

// csiVolumeDenormalizeAllocs returns a CSIVolume with allocations
// CSIVolumeDenormalize returns a CSIVolume with allocations
func (s *StateStore) CSIVolumeDenormalize(ws memdb.WatchSet, vol *structs.CSIVolume) (*structs.CSIVolume, error) {
for id := range vol.ReadAllocs {
a, err := s.AllocByID(ws, id)
if err != nil {
return nil, err
}
vol.ReadAllocs[id] = a
if a != nil {
vol.ReadAllocs[id] = a
}
}

for id := range vol.WriteAllocs {
a, err := s.AllocByID(ws, id)
if err != nil {
return nil, err
}
vol.WriteAllocs[id] = a
if a != nil {
vol.WriteAllocs[id] = a
}
}

return vol, nil
Expand Down
9 changes: 9 additions & 0 deletions nomad/structs/csi.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,9 @@ func (v *CSIVolume) Claim(claim CSIVolumeClaimMode, alloc *Allocation) error {

// ClaimRead marks an allocation as using a volume read-only
func (v *CSIVolume) ClaimRead(alloc *Allocation) error {
if alloc == nil {
return fmt.Errorf("allocation missing")
}
if _, ok := v.ReadAllocs[alloc.ID]; ok {
return nil
}
Expand All @@ -385,6 +388,9 @@ func (v *CSIVolume) ClaimRead(alloc *Allocation) error {

// ClaimWrite marks an allocation as using a volume as a writer
func (v *CSIVolume) ClaimWrite(alloc *Allocation) error {
if alloc == nil {
return fmt.Errorf("allocation missing")
}
if _, ok := v.WriteAllocs[alloc.ID]; ok {
return nil
}
Expand All @@ -411,6 +417,9 @@ func (v *CSIVolume) ClaimWrite(alloc *Allocation) error {

// ClaimRelease is called when the allocation has terminated and already stopped using the volume
func (v *CSIVolume) ClaimRelease(alloc *Allocation) error {
if alloc == nil {
return fmt.Errorf("allocation missing")
}
delete(v.ReadAllocs, alloc.ID)
delete(v.WriteAllocs, alloc.ID)
return nil
Expand Down

0 comments on commit 9f5156a

Please sign in to comment.