Skip to content

Commit

Permalink
fix(controller): make ControllerPublishVolume idempotent again
Browse files Browse the repository at this point in the history
  • Loading branch information
plaffitt committed Jan 25, 2021
1 parent 9d55234 commit e1eade8
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions pkg/controller/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,30 @@ import (
"k8s.io/klog"
)

func CountVolumeMaps(client *dothill.Client, name string) (int, *dothill.ResponseStatus, error) {
func getVolumeMapsHostNames(client *dothill.Client, name string) ([]string, *dothill.ResponseStatus, error) {
if name != "" {
name = fmt.Sprintf("\"%s\"", name)
}
res, status, err := client.Request(fmt.Sprintf("/show/volume-maps/%s", name))
if err != nil {
return 0, status, err
return []string{}, status, err
}

count := 0
hostNames := []string{}
for _, rootObj := range res.Objects {
if rootObj.Name != "volume-view" {
continue
}

for _, object := range rootObj.Objects {
if object.Name == "host-view" && object.PropertiesMap["identifier"].Data != "all other hosts" {
count++
hostName := object.PropertiesMap["identifier"].Data
if object.Name == "host-view" && hostName != "all other hosts" {
hostNames = append(hostNames, hostName)
}
}
}

return count, status, err
return hostNames, status, err
}

// ControllerPublishVolume attaches the given volume to the node
Expand All @@ -55,12 +56,14 @@ func (driver *Driver) ControllerPublishVolume(ctx context.Context, req *csi.Cont
initiatorName := req.GetNodeId()
klog.Infof("attach request for initiator %s, volume id: %s", initiatorName, req.GetVolumeId())

count, _, err := CountVolumeMaps(driver.dothillClient, req.GetVolumeId())
hostNames, _, err := getVolumeMapsHostNames(driver.dothillClient, req.GetVolumeId())
if err != nil {
return nil, err
}
if count > 0 {
return nil, status.Errorf(codes.FailedPrecondition, "volume %s is already attached to another node", req.GetVolumeId())
for _, hostName := range hostNames {
if hostName != initiatorName {
return nil, status.Errorf(codes.FailedPrecondition, "volume %s is already attached to another node", req.GetVolumeId())
}
}

lun, err := driver.chooseLUN(initiatorName)
Expand Down

0 comments on commit e1eade8

Please sign in to comment.