k8s.io/kubernetes@v1.29.3/pkg/volume/flexvolume/util.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 "encoding/base64" 21 "fmt" 22 "os" 23 24 "k8s.io/klog/v2" 25 "k8s.io/mount-utils" 26 27 "k8s.io/kubernetes/pkg/volume" 28 "k8s.io/kubernetes/pkg/volume/util" 29 ) 30 31 func addSecretsToOptions(options map[string]string, spec *volume.Spec, namespace string, driverName string, host volume.VolumeHost) error { 32 secretName, secretNamespace, err := getSecretNameAndNamespace(spec, namespace) 33 if err != nil { 34 return err 35 } 36 37 if len(secretName) == 0 || len(secretNamespace) == 0 { 38 return nil 39 } 40 41 kubeClient := host.GetKubeClient() 42 if kubeClient == nil { 43 return fmt.Errorf("cannot get kube client") 44 } 45 46 secrets, err := util.GetSecretForPV(secretNamespace, secretName, driverName, host.GetKubeClient()) 47 if err != nil { 48 err = fmt.Errorf("couldn't get secret %v/%v err: %w", secretNamespace, secretName, err) 49 return err 50 } 51 for name, data := range secrets { 52 options[optionKeySecret+"/"+name] = base64.StdEncoding.EncodeToString([]byte(data)) 53 klog.V(1).Infof("found flex volume secret info: %s", name) 54 } 55 56 return nil 57 } 58 59 var errNotFlexVolume = fmt.Errorf("not a flex volume") 60 61 func getDriver(spec *volume.Spec) (string, error) { 62 if spec.Volume != nil && spec.Volume.FlexVolume != nil { 63 return spec.Volume.FlexVolume.Driver, nil 64 } 65 if spec.PersistentVolume != nil && spec.PersistentVolume.Spec.FlexVolume != nil { 66 return spec.PersistentVolume.Spec.FlexVolume.Driver, nil 67 } 68 return "", errNotFlexVolume 69 } 70 71 func getFSType(spec *volume.Spec) (string, error) { 72 if spec.Volume != nil && spec.Volume.FlexVolume != nil { 73 return spec.Volume.FlexVolume.FSType, nil 74 } 75 if spec.PersistentVolume != nil && spec.PersistentVolume.Spec.FlexVolume != nil { 76 return spec.PersistentVolume.Spec.FlexVolume.FSType, nil 77 } 78 return "", errNotFlexVolume 79 } 80 81 func getSecretNameAndNamespace(spec *volume.Spec, podNamespace string) (string, string, error) { 82 if spec.Volume != nil && spec.Volume.FlexVolume != nil { 83 if spec.Volume.FlexVolume.SecretRef == nil { 84 return "", "", nil 85 } 86 return spec.Volume.FlexVolume.SecretRef.Name, podNamespace, nil 87 } 88 if spec.PersistentVolume != nil && spec.PersistentVolume.Spec.FlexVolume != nil { 89 if spec.PersistentVolume.Spec.FlexVolume.SecretRef == nil { 90 return "", "", nil 91 } 92 secretName := spec.PersistentVolume.Spec.FlexVolume.SecretRef.Name 93 secretNamespace := spec.PersistentVolume.Spec.FlexVolume.SecretRef.Namespace 94 if len(secretNamespace) == 0 { 95 secretNamespace = podNamespace 96 } 97 return secretName, secretNamespace, nil 98 } 99 return "", "", errNotFlexVolume 100 } 101 102 func getReadOnly(spec *volume.Spec) (bool, error) { 103 if spec.Volume != nil && spec.Volume.FlexVolume != nil { 104 return spec.Volume.FlexVolume.ReadOnly, nil 105 } 106 if spec.PersistentVolume != nil && spec.PersistentVolume.Spec.FlexVolume != nil { 107 // ReadOnly is specified at the PV level 108 return spec.ReadOnly, nil 109 } 110 return false, errNotFlexVolume 111 } 112 113 func getOptions(spec *volume.Spec) (map[string]string, error) { 114 if spec.Volume != nil && spec.Volume.FlexVolume != nil { 115 return spec.Volume.FlexVolume.Options, nil 116 } 117 if spec.PersistentVolume != nil && spec.PersistentVolume.Spec.FlexVolume != nil { 118 return spec.PersistentVolume.Spec.FlexVolume.Options, nil 119 } 120 return nil, errNotFlexVolume 121 } 122 123 func prepareForMount(mounter mount.Interface, deviceMountPath string) (bool, error) { 124 125 notMnt, err := mounter.IsLikelyNotMountPoint(deviceMountPath) 126 if err != nil { 127 if os.IsNotExist(err) { 128 if err := os.MkdirAll(deviceMountPath, 0750); err != nil { 129 return false, err 130 } 131 notMnt = true 132 } else { 133 return false, err 134 } 135 } 136 137 return !notMnt, nil 138 } 139 140 // Mounts the device at the given path. 141 // It is expected that prepareForMount has been called before. 142 func doMount(mounter mount.Interface, devicePath, deviceMountPath, fsType string, options []string) error { 143 err := mounter.MountSensitiveWithoutSystemd(devicePath, deviceMountPath, fsType, options, nil) 144 if err != nil { 145 klog.Errorf("Failed to mount the volume at %s, device: %s, error: %s", deviceMountPath, devicePath, err.Error()) 146 return err 147 } 148 return nil 149 } 150 151 func isNotMounted(mounter mount.Interface, deviceMountPath string) (bool, error) { 152 notmnt, err := mounter.IsLikelyNotMountPoint(deviceMountPath) 153 if err != nil { 154 klog.Errorf("Error checking mount point %s, error: %v", deviceMountPath, err) 155 return false, err 156 } 157 return notmnt, nil 158 }