github.com/jingruilea/kubeedge@v1.2.0-beta.0.0.20200410162146-4bb8902b3879/edge/pkg/edged/volume/csi/csi_controller.go (about) 1 /* 2 Copyright 2019 The KubeEdge 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 csi 18 19 import ( 20 "context" 21 22 "github.com/container-storage-interface/spec/lib/go/csi" 23 "k8s.io/klog" 24 25 "github.com/kubeedge/kubeedge/edge/pkg/metamanager/client" 26 ) 27 28 type Controller struct { 29 metaClient client.CoreInterface 30 csiClient csiClient 31 } 32 33 func (c *Controller) CreateVolume(req *csi.CreateVolumeRequest) (*csi.CreateVolumeResponse, error) { 34 if c.csiClient == nil { 35 var err error 36 c.csiClient, err = newCsiDriverClient(csiDriverName("csi-hostpath")) 37 if err != nil { 38 klog.Errorf("failed to create newCsiDriverClient: %v", err) 39 return nil, err 40 } 41 } 42 client := c.csiClient 43 44 ctx, cancel := context.WithTimeout(context.Background(), csiTimeout) 45 defer cancel() 46 47 res, err := client.ControllerGetCapabilities(ctx, &csi.ControllerGetCapabilitiesRequest{}) 48 if err != nil { 49 klog.Errorf("failed to ControllerGetCapabilities: %v", err) 50 return nil, err 51 } 52 for _, cap := range res.Capabilities { 53 if csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME == cap.GetRpc().GetType() { 54 return client.CreateVolume(ctx, req) 55 } 56 } 57 58 return &csi.CreateVolumeResponse{}, nil 59 } 60 61 func (c *Controller) DeleteVolume(req *csi.DeleteVolumeRequest) (*csi.DeleteVolumeResponse, error) { 62 if c.csiClient == nil { 63 var err error 64 c.csiClient, err = newCsiDriverClient(csiDriverName("csi-hostpath")) 65 if err != nil { 66 klog.Errorf("failed to create newCsiDriverClient: %v", err) 67 return nil, err 68 } 69 } 70 client := c.csiClient 71 72 ctx, cancel := context.WithTimeout(context.Background(), csiTimeout) 73 defer cancel() 74 75 res, err := client.ControllerGetCapabilities(ctx, &csi.ControllerGetCapabilitiesRequest{}) 76 if err != nil { 77 klog.Errorf("failed to ControllerGetCapabilities: %v", err) 78 return nil, err 79 } 80 for _, cap := range res.Capabilities { 81 if csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME == cap.GetRpc().GetType() { 82 return client.DeleteVolume(ctx, req) 83 } 84 } 85 86 return &csi.DeleteVolumeResponse{}, nil 87 } 88 89 func (c *Controller) ControllerPublishVolume(req *csi.ControllerPublishVolumeRequest) (*csi.ControllerPublishVolumeResponse, error) { 90 if c.csiClient == nil { 91 var err error 92 c.csiClient, err = newCsiDriverClient(csiDriverName("csi-hostpath")) 93 if err != nil { 94 klog.Errorf("failed to create newCsiDriverClient: %v", err) 95 return nil, err 96 } 97 } 98 client := c.csiClient 99 100 ctx, cancel := context.WithTimeout(context.Background(), csiTimeout) 101 defer cancel() 102 103 res, err := client.ControllerGetCapabilities(ctx, &csi.ControllerGetCapabilitiesRequest{}) 104 if err != nil { 105 klog.Errorf("failed to ControllerGetCapabilities: %v", err) 106 return nil, err 107 } 108 for _, cap := range res.Capabilities { 109 if csi.ControllerServiceCapability_RPC_PUBLISH_UNPUBLISH_VOLUME == cap.GetRpc().GetType() { 110 return client.ControllerPublishVolume(ctx, req) 111 } 112 } 113 114 return &csi.ControllerPublishVolumeResponse{}, nil 115 } 116 117 func (c *Controller) ControllerUnpublishVolume(req *csi.ControllerUnpublishVolumeRequest) (*csi.ControllerUnpublishVolumeResponse, error) { 118 if c.csiClient == nil { 119 var err error 120 c.csiClient, err = newCsiDriverClient(csiDriverName("csi-hostpath")) 121 if err != nil { 122 klog.Errorf("failed to create newCsiDriverClient: %v", err) 123 return nil, err 124 } 125 } 126 client := c.csiClient 127 128 ctx, cancel := context.WithTimeout(context.Background(), csiTimeout) 129 defer cancel() 130 131 res, err := client.ControllerGetCapabilities(ctx, &csi.ControllerGetCapabilitiesRequest{}) 132 if err != nil { 133 klog.Errorf("failed to ControllerGetCapabilities: %v", err) 134 return nil, err 135 } 136 for _, cap := range res.Capabilities { 137 if csi.ControllerServiceCapability_RPC_PUBLISH_UNPUBLISH_VOLUME == cap.GetRpc().GetType() { 138 return client.ControllerUnpublishVolume(ctx, req) 139 } 140 } 141 142 return &csi.ControllerUnpublishVolumeResponse{}, nil 143 }