github.com/mirantis/virtlet@v1.5.2-0.20191204181327-1659b8a48e9b/pkg/libvirttools/flexvolume_volumesource.go (about) 1 /* 2 Copyright 2017 Mirantis 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 libvirttools 18 19 import ( 20 "encoding/json" 21 "fmt" 22 "io/ioutil" 23 "os" 24 "path/filepath" 25 26 "github.com/golang/glog" 27 28 "github.com/Mirantis/virtlet/pkg/metadata/types" 29 ) 30 31 const ( 32 flexvolumeSubdir = "volumes/virtlet~flexvolume_driver" 33 flexvolumeDataFile = "virtlet-flexvolume.json" 34 ) 35 36 type flexvolumeSource func(volumeName, configPath string, config *types.VMConfig, owner volumeOwner) (VMVolume, error) 37 38 var flexvolumeTypeMap = map[string]flexvolumeSource{} 39 40 func addFlexvolumeSource(fvType string, source flexvolumeSource) { 41 flexvolumeTypeMap[fvType] = source 42 } 43 44 // ScanFlexVolumes returns VMVolume objects for Virtlet flexvolumes 45 // mounted into the pod. 46 func ScanFlexVolumes(config *types.VMConfig, owner volumeOwner) ([]VMVolume, error) { 47 dir := filepath.Join(owner.KubeletRootDir(), config.PodSandboxID, flexvolumeSubdir) 48 if _, err := os.Stat(dir); os.IsNotExist(err) { 49 glog.V(2).Infof("No flexvolumes to process for %q with uuid %q", config.Name, config.DomainUUID) 50 return nil, nil 51 } else if err != nil { 52 return nil, err 53 } 54 55 glog.V(2).Info("Processing flexvolumes for flexvolume_driver") 56 volDirItems, err := ioutil.ReadDir(dir) 57 if err != nil { 58 return nil, err 59 } 60 61 glog.V(2).Infof("Found flexvolumes definitions at %s:\n%#v", dir, volDirItems) 62 var vols []VMVolume 63 for _, fi := range volDirItems { 64 if !fi.IsDir() { 65 continue 66 } 67 dataFilePath := filepath.Join(dir, fi.Name(), flexvolumeDataFile) 68 content, err := ioutil.ReadFile(dataFilePath) 69 if err != nil { 70 if os.IsNotExist(err) { 71 continue 72 } 73 return nil, fmt.Errorf("error reading flexvolume config %q: %v", dataFilePath, err) 74 } 75 var msi map[string]interface{} 76 if err = json.Unmarshal(content, &msi); err != nil { 77 return nil, fmt.Errorf("error unmarshal flexvolume config %q: %v", dataFilePath, err) 78 } 79 fvType, _ := msi["type"].(string) 80 if fvType == "" { 81 return nil, fmt.Errorf("flexvolume config %q: need to specify 'type' (a string)", dataFilePath) 82 } 83 fvSource, found := flexvolumeTypeMap[fvType] 84 if !found { 85 return nil, fmt.Errorf("bad flexvolume config %q: bad type %q", dataFilePath, fvType) 86 } 87 vol, err := fvSource(fi.Name(), dataFilePath, config, owner) 88 if err != nil { 89 return nil, err 90 } 91 glog.V(3).Infof("Found flexvolume: %s", string(content)) 92 vols = append(vols, vol) 93 } 94 return vols, nil 95 }