|
| 1 | +/* |
| 2 | +Copyright 2018 Intel Corporation |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package sanity |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "log" |
| 22 | + |
| 23 | + "github.com/container-storage-interface/spec/lib/go/csi/v0" |
| 24 | + |
| 25 | + . "github.com/onsi/ginkgo" |
| 26 | +) |
| 27 | + |
| 28 | +// VolumeInfo keeps track of the information needed to delete a volume. |
| 29 | +type VolumeInfo struct { |
| 30 | + // Node on which the volume was published, empty if none |
| 31 | + // or publishing is not supported. |
| 32 | + NodeID string |
| 33 | + |
| 34 | + // Volume ID assigned by CreateVolume. |
| 35 | + VolumeID string |
| 36 | +} |
| 37 | + |
| 38 | +// Cleanup keeps track of resources, in particular volumes, which need |
| 39 | +// to be freed when testing is done. |
| 40 | +type Cleanup struct { |
| 41 | + Context *SanityContext |
| 42 | + ControllerClient csi.ControllerClient |
| 43 | + NodeClient csi.NodeClient |
| 44 | + ControllerPublishSupported bool |
| 45 | + NodeStageSupported bool |
| 46 | + |
| 47 | + // Maps from volume name to the node ID for which the volume |
| 48 | + // is published and the volume ID. |
| 49 | + volumes map[string]VolumeInfo |
| 50 | +} |
| 51 | + |
| 52 | +// RegisterVolume adds or updates an entry for the volume with the |
| 53 | +// given name. |
| 54 | +func (cl *Cleanup) RegisterVolume(name string, info VolumeInfo) { |
| 55 | + if cl.volumes == nil { |
| 56 | + cl.volumes = make(map[string]VolumeInfo) |
| 57 | + } |
| 58 | + cl.volumes[name] = info |
| 59 | +} |
| 60 | + |
| 61 | +// MaybeRegisterVolume adds or updates an entry for the volume with |
| 62 | +// the given name if CreateVolume was successful. |
| 63 | +func (cl *Cleanup) MaybeRegisterVolume(name string, vol *csi.CreateVolumeResponse, err error) { |
| 64 | + if err == nil && vol.GetVolume().GetId() != "" { |
| 65 | + cl.RegisterVolume(name, VolumeInfo{VolumeID: vol.GetVolume().GetId()}) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +// UnregisterVolume removes the entry for the volume with the |
| 70 | +// given name, thus preventing all cleanup operations for it. |
| 71 | +func (cl *Cleanup) UnregisterVolume(name string) { |
| 72 | + if cl.volumes != nil { |
| 73 | + delete(cl.volumes, name) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +// DeleteVolumes stops using the registered volumes and tries to delete all of them. |
| 78 | +func (cl *Cleanup) DeleteVolumes() { |
| 79 | + if cl.volumes == nil { |
| 80 | + return |
| 81 | + } |
| 82 | + logger := log.New(GinkgoWriter, "cleanup: ", 0) |
| 83 | + ctx := context.Background() |
| 84 | + |
| 85 | + for name, info := range cl.volumes { |
| 86 | + logger.Printf("deleting %s = %s", name, info.VolumeID) |
| 87 | + if _, err := cl.NodeClient.NodeUnpublishVolume( |
| 88 | + ctx, |
| 89 | + &csi.NodeUnpublishVolumeRequest{ |
| 90 | + VolumeId: info.VolumeID, |
| 91 | + TargetPath: cl.Context.Config.TargetPath, |
| 92 | + }, |
| 93 | + ); err != nil { |
| 94 | + logger.Printf("warning: NodeUnpublishVolume: %s", err) |
| 95 | + } |
| 96 | + |
| 97 | + if cl.NodeStageSupported { |
| 98 | + if _, err := cl.NodeClient.NodeUnstageVolume( |
| 99 | + ctx, |
| 100 | + &csi.NodeUnstageVolumeRequest{ |
| 101 | + VolumeId: info.VolumeID, |
| 102 | + StagingTargetPath: cl.Context.Config.StagingPath, |
| 103 | + }, |
| 104 | + ); err != nil { |
| 105 | + logger.Printf("warning: NodeUnstageVolume: %s", err) |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + if cl.ControllerPublishSupported && info.NodeID != "" { |
| 110 | + if _, err := cl.ControllerClient.ControllerUnpublishVolume( |
| 111 | + ctx, |
| 112 | + &csi.ControllerUnpublishVolumeRequest{ |
| 113 | + VolumeId: info.VolumeID, |
| 114 | + NodeId: info.NodeID, |
| 115 | + ControllerUnpublishSecrets: cl.Context.Secrets.ControllerUnpublishVolumeSecret, |
| 116 | + }, |
| 117 | + ); err != nil { |
| 118 | + logger.Printf("warning: ControllerUnpublishVolume: %s", err) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + if _, err := cl.ControllerClient.DeleteVolume( |
| 123 | + ctx, |
| 124 | + &csi.DeleteVolumeRequest{ |
| 125 | + VolumeId: info.VolumeID, |
| 126 | + ControllerDeleteSecrets: cl.Context.Secrets.DeleteVolumeSecret, |
| 127 | + }, |
| 128 | + ); err != nil { |
| 129 | + logger.Printf("error: DeleteVolume: %s", err) |
| 130 | + } |
| 131 | + |
| 132 | + cl.UnregisterVolume(name) |
| 133 | + } |
| 134 | +} |
0 commit comments