|
| 1 | +/* |
| 2 | +Copyright 2017 The Kubernetes Authors. |
| 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 cinder |
| 18 | + |
| 19 | +import ( |
| 20 | + "github.com/container-storage-interface/spec/lib/go/csi" |
| 21 | + "github.com/golang/glog" |
| 22 | + "github.com/kubernetes-csi/drivers/pkg/cinder/openstack" |
| 23 | + csicommon "github.com/kubernetes-csi/drivers/pkg/csi-common" |
| 24 | + "github.com/pborman/uuid" |
| 25 | + "golang.org/x/net/context" |
| 26 | + "k8s.io/kubernetes/pkg/volume" |
| 27 | +) |
| 28 | + |
| 29 | +type controllerServer struct { |
| 30 | + *csicommon.DefaultControllerServer |
| 31 | +} |
| 32 | + |
| 33 | +func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest) (*csi.CreateVolumeResponse, error) { |
| 34 | + |
| 35 | + // Volume Name |
| 36 | + volName := req.GetName() |
| 37 | + if len(volName) == 0 { |
| 38 | + volName = uuid.NewUUID().String() |
| 39 | + } |
| 40 | + |
| 41 | + // Volume Size - Default is 1 GiB |
| 42 | + volSizeBytes := int64(1 * 1024 * 1024 * 1024) |
| 43 | + if req.GetCapacityRange() != nil { |
| 44 | + volSizeBytes = int64(req.GetCapacityRange().GetRequiredBytes()) |
| 45 | + } |
| 46 | + volSizeGB := int(volume.RoundUpSize(volSizeBytes, 1024*1024*1024)) |
| 47 | + |
| 48 | + // Volume Type |
| 49 | + volType := req.GetParameters()["type"] |
| 50 | + if len(volType) == 0 { |
| 51 | + volType = "lvmdriver-1" |
| 52 | + } |
| 53 | + |
| 54 | + // Volume Availability - Default is nova |
| 55 | + volAvailability := req.GetParameters()["availability"] |
| 56 | + if len(volType) == 0 { |
| 57 | + volAvailability = "nova" |
| 58 | + } |
| 59 | + |
| 60 | + // Get OpenStack Provider |
| 61 | + cloud, err := openstack.GetOpenStackProvider() |
| 62 | + if err != nil { |
| 63 | + glog.V(3).Infof("Failed to GetOpenStackProvider: %v", err) |
| 64 | + return nil, err |
| 65 | + } |
| 66 | + |
| 67 | + // Volume Create |
| 68 | + resID, resAvailability, err := cloud.CreateVolume(volName, volSizeGB, volType, volAvailability, nil) |
| 69 | + if err != nil { |
| 70 | + glog.V(3).Infof("Failed to CreateVolume: %v", err) |
| 71 | + return nil, err |
| 72 | + } |
| 73 | + |
| 74 | + glog.V(4).Infof("Create volume %s in Availability Zone: %s", resID, resAvailability) |
| 75 | + |
| 76 | + return &csi.CreateVolumeResponse{ |
| 77 | + VolumeInfo: &csi.VolumeInfo{ |
| 78 | + Id: resID, |
| 79 | + Attributes: map[string]string{ |
| 80 | + "availability": resAvailability, |
| 81 | + }, |
| 82 | + }, |
| 83 | + }, nil |
| 84 | +} |
| 85 | + |
| 86 | +func (cs *controllerServer) DeleteVolume(ctx context.Context, req *csi.DeleteVolumeRequest) (*csi.DeleteVolumeResponse, error) { |
| 87 | + |
| 88 | + // Get OpenStack Provider |
| 89 | + cloud, err := openstack.GetOpenStackProvider() |
| 90 | + if err != nil { |
| 91 | + glog.V(3).Infof("Failed to GetOpenStackProvider: %v", err) |
| 92 | + return nil, err |
| 93 | + } |
| 94 | + |
| 95 | + // Volume Delete |
| 96 | + volID := req.GetVolumeId() |
| 97 | + err = cloud.DeleteVolume(volID) |
| 98 | + if err != nil { |
| 99 | + glog.V(3).Infof("Failed to CreateVolume: %v", err) |
| 100 | + return nil, err |
| 101 | + } |
| 102 | + |
| 103 | + glog.V(4).Infof("Delete volume %s", volID) |
| 104 | + |
| 105 | + return &csi.DeleteVolumeResponse{}, nil |
| 106 | +} |
| 107 | + |
| 108 | +func (cs *controllerServer) ControllerPublishVolume(ctx context.Context, req *csi.ControllerPublishVolumeRequest) (*csi.ControllerPublishVolumeResponse, error) { |
| 109 | + |
| 110 | + // Get OpenStack Provider |
| 111 | + cloud, err := openstack.GetOpenStackProvider() |
| 112 | + if err != nil { |
| 113 | + glog.V(3).Infof("Failed to GetOpenStackProvider: %v", err) |
| 114 | + return nil, err |
| 115 | + } |
| 116 | + |
| 117 | + // Volume Attach |
| 118 | + instanceID := req.GetNodeId() |
| 119 | + volumeID := req.GetVolumeId() |
| 120 | + |
| 121 | + _, err = cloud.AttachVolume(instanceID, volumeID) |
| 122 | + if err != nil { |
| 123 | + glog.V(3).Infof("Failed to AttachVolume: %v", err) |
| 124 | + return nil, err |
| 125 | + } |
| 126 | + |
| 127 | + err = cloud.WaitDiskAttached(instanceID, volumeID) |
| 128 | + if err != nil { |
| 129 | + glog.V(3).Infof("Failed to WaitDiskAttached: %v", err) |
| 130 | + return nil, err |
| 131 | + } |
| 132 | + |
| 133 | + devicePath, err := cloud.GetAttachmentDiskPath(instanceID, volumeID) |
| 134 | + if err != nil { |
| 135 | + glog.V(3).Infof("Failed to GetAttachmentDiskPath: %v", err) |
| 136 | + return nil, err |
| 137 | + } |
| 138 | + |
| 139 | + glog.V(4).Infof("ControllerPublishVolume %s on %s", volumeID, instanceID) |
| 140 | + |
| 141 | + // Publish Volume Info |
| 142 | + pvInfo := map[string]string{} |
| 143 | + pvInfo["DevicePath"] = devicePath |
| 144 | + |
| 145 | + return &csi.ControllerPublishVolumeResponse{ |
| 146 | + PublishVolumeInfo: pvInfo, |
| 147 | + }, nil |
| 148 | +} |
| 149 | + |
| 150 | +func (cs *controllerServer) ControllerUnpublishVolume(ctx context.Context, req *csi.ControllerUnpublishVolumeRequest) (*csi.ControllerUnpublishVolumeResponse, error) { |
| 151 | + |
| 152 | + // Get OpenStack Provider |
| 153 | + cloud, err := openstack.GetOpenStackProvider() |
| 154 | + if err != nil { |
| 155 | + glog.V(3).Infof("Failed to GetOpenStackProvider: %v", err) |
| 156 | + return nil, err |
| 157 | + } |
| 158 | + |
| 159 | + // Volume Detach |
| 160 | + instanceID := req.GetNodeId() |
| 161 | + volumeID := req.GetVolumeId() |
| 162 | + |
| 163 | + err = cloud.DetachVolume(instanceID, volumeID) |
| 164 | + if err != nil { |
| 165 | + glog.V(3).Infof("Failed to DetachVolume: %v", err) |
| 166 | + return nil, err |
| 167 | + } |
| 168 | + |
| 169 | + err = cloud.WaitDiskDetached(instanceID, volumeID) |
| 170 | + if err != nil { |
| 171 | + glog.V(3).Infof("Failed to WaitDiskDetached: %v", err) |
| 172 | + return nil, err |
| 173 | + } |
| 174 | + |
| 175 | + glog.V(4).Infof("ControllerUnpublishVolume %s on %s", volumeID, instanceID) |
| 176 | + |
| 177 | + return &csi.ControllerUnpublishVolumeResponse{}, nil |
| 178 | +} |
0 commit comments