k8s.io/kubernetes@v1.29.3/pkg/volume/flexvolume/mounter.go (about) 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 flexvolume 18 19 import ( 20 "os" 21 "strconv" 22 23 "k8s.io/kubernetes/pkg/volume" 24 "k8s.io/kubernetes/pkg/volume/util" 25 "k8s.io/utils/exec" 26 ) 27 28 // FlexVolumeMounter is the disk that will be exposed by this plugin. 29 type flexVolumeMounter struct { 30 *flexVolume 31 // Runner used to setup the volume. 32 runner exec.Interface 33 // the considered volume spec 34 spec *volume.Spec 35 readOnly bool 36 } 37 38 var _ volume.Mounter = &flexVolumeMounter{} 39 40 // Mounter interface 41 42 // SetUp creates new directory. 43 func (f *flexVolumeMounter) SetUp(mounterArgs volume.MounterArgs) error { 44 return f.SetUpAt(f.GetPath(), mounterArgs) 45 } 46 47 // SetUpAt creates new directory. 48 func (f *flexVolumeMounter) SetUpAt(dir string, mounterArgs volume.MounterArgs) error { 49 // Mount only once. 50 alreadyMounted, err := prepareForMount(f.mounter, dir) 51 if err != nil { 52 return err 53 } 54 if alreadyMounted { 55 return nil 56 } 57 58 call := f.plugin.NewDriverCall(mountCmd) 59 60 // Interface parameters 61 call.Append(dir) 62 63 extraOptions := make(map[string]string) 64 65 // pod metadata 66 extraOptions[optionKeyPodName] = f.podName 67 extraOptions[optionKeyPodNamespace] = f.podNamespace 68 extraOptions[optionKeyPodUID] = string(f.podUID) 69 // service account metadata 70 extraOptions[optionKeyServiceAccountName] = f.podServiceAccountName 71 72 // Extract secret and pass it as options. 73 if err := addSecretsToOptions(extraOptions, f.spec, f.podNamespace, f.driverName, f.plugin.host); err != nil { 74 os.Remove(dir) 75 return err 76 } 77 78 // Implicit parameters 79 if mounterArgs.FsGroup != nil { 80 extraOptions[optionFSGroup] = strconv.FormatInt(int64(*mounterArgs.FsGroup), 10) 81 } 82 83 call.AppendSpec(f.spec, f.plugin.host, extraOptions) 84 85 _, err = call.Run() 86 if isCmdNotSupportedErr(err) { 87 err = (*mounterDefaults)(f).SetUpAt(dir, mounterArgs) 88 } 89 90 if err != nil { 91 os.Remove(dir) 92 return err 93 } 94 95 if !f.readOnly { 96 if f.plugin.capabilities.FSGroup { 97 // fullPluginName helps to distinguish different driver from flex volume plugin 98 volume.SetVolumeOwnership(f, dir, mounterArgs.FsGroup, mounterArgs.FSGroupChangePolicy, util.FSGroupCompleteHook(f.plugin, f.spec)) 99 } 100 } 101 102 return nil 103 } 104 105 // GetAttributes get the flex volume attributes. The attributes will be queried 106 // using plugin callout after we finalize the callout syntax. 107 func (f *flexVolumeMounter) GetAttributes() volume.Attributes { 108 return (*mounterDefaults)(f).GetAttributes() 109 }