|
| 1 | +package hostpath |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/golang/glog" |
| 12 | + fs "k8s.io/kubernetes/pkg/volume/util/fs" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + podVolumeTargetPath = "/var/lib/kubelet/pods" |
| 17 | + csiSignOfVolumeTargetPath = "kubernetes.io~csi/pvc" |
| 18 | +) |
| 19 | + |
| 20 | +type MountPointInfo struct { |
| 21 | + Target string `json:"target"` |
| 22 | + Source string `json:"source"` |
| 23 | + FsType string `json:"fstype"` |
| 24 | + Options string `json:"options"` |
| 25 | + ContainerFileSystem []MountPointInfo `json:"children,omitempty"` |
| 26 | +} |
| 27 | + |
| 28 | +type ContainerFileSystem struct { |
| 29 | + Children []MountPointInfo `json:"children"` |
| 30 | +} |
| 31 | + |
| 32 | +type FileSystems struct { |
| 33 | + Filsystem []ContainerFileSystem `json:"filesystems"` |
| 34 | +} |
| 35 | + |
| 36 | +func locateCommandPath(commandName string) string { |
| 37 | + // default to root |
| 38 | + binary := filepath.Join("/", commandName) |
| 39 | + for _, path := range []string{"/bin", "/usr/sbin", "/usr/bin"} { |
| 40 | + binPath := filepath.Join(path, binary) |
| 41 | + if _, err := os.Stat(binPath); err != nil { |
| 42 | + continue |
| 43 | + } |
| 44 | + |
| 45 | + return binPath |
| 46 | + } |
| 47 | + |
| 48 | + return "" |
| 49 | +} |
| 50 | + |
| 51 | +func getSourcePath(volumeHandle string) string { |
| 52 | + return fmt.Sprintf("%s/%s", dataRoot, volumeHandle) |
| 53 | +} |
| 54 | + |
| 55 | +func checkSourcePathExist(volumeHandle string) (bool, error) { |
| 56 | + sourcePath := getSourcePath(volumeHandle) |
| 57 | + glog.V(3).Infof("Volume: %s Source path is: %s", volumeHandle, sourcePath) |
| 58 | + _, err := os.Stat(sourcePath) |
| 59 | + if err != nil { |
| 60 | + if os.IsNotExist(err) { |
| 61 | + return false, nil |
| 62 | + } |
| 63 | + |
| 64 | + return false, err |
| 65 | + } |
| 66 | + |
| 67 | + return true, nil |
| 68 | +} |
| 69 | + |
| 70 | +func parseMountInfo(originalMountInfo []byte) ([]MountPointInfo, error) { |
| 71 | + fs := FileSystems{ |
| 72 | + Filsystem: make([]ContainerFileSystem, 0), |
| 73 | + } |
| 74 | + |
| 75 | + if err := json.Unmarshal(originalMountInfo, &fs); err != nil { |
| 76 | + return nil, err |
| 77 | + } |
| 78 | + |
| 79 | + if len(fs.Filsystem) <= 0 { |
| 80 | + return nil, fmt.Errorf("failed to get mount info") |
| 81 | + } |
| 82 | + |
| 83 | + return fs.Filsystem[0].Children, nil |
| 84 | +} |
| 85 | + |
| 86 | +func checkMountPointExist(sourcePath string) (bool, error) { |
| 87 | + cmdPath := locateCommandPath("findmnt") |
| 88 | + out, err := exec.Command(cmdPath, "--json").CombinedOutput() |
| 89 | + if err != nil { |
| 90 | + glog.V(3).Infof("failed to execute command: %+v", cmdPath) |
| 91 | + return false, err |
| 92 | + } |
| 93 | + |
| 94 | + if len(out) < 1 { |
| 95 | + return false, fmt.Errorf("mount point info is nil") |
| 96 | + } |
| 97 | + |
| 98 | + mountInfos, err := parseMountInfo([]byte(out)) |
| 99 | + if err != nil { |
| 100 | + return false, fmt.Errorf("failed to parse the mount infos: %+v", err) |
| 101 | + } |
| 102 | + |
| 103 | + mountInfosOfPod := MountPointInfo{} |
| 104 | + for _, mountInfo := range mountInfos { |
| 105 | + if mountInfo.Target == podVolumeTargetPath { |
| 106 | + mountInfosOfPod = mountInfo |
| 107 | + break |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + for _, mountInfo := range mountInfosOfPod.ContainerFileSystem { |
| 112 | + if !strings.Contains(mountInfo.Source, sourcePath) { |
| 113 | + continue |
| 114 | + } |
| 115 | + |
| 116 | + _, err = os.Stat(mountInfo.Target) |
| 117 | + if err != nil { |
| 118 | + if os.IsNotExist(err) { |
| 119 | + return false, nil |
| 120 | + } |
| 121 | + |
| 122 | + return false, err |
| 123 | + } |
| 124 | + |
| 125 | + return true, nil |
| 126 | + } |
| 127 | + |
| 128 | + return false, nil |
| 129 | +} |
| 130 | + |
| 131 | +func checkPVCapacityValid(volumeHandle string) (bool, error) { |
| 132 | + sourcePath := getSourcePath(volumeHandle) |
| 133 | + _, fscapacity, _, _, _, _, err := fs.FsInfo(sourcePath) |
| 134 | + if err != nil { |
| 135 | + return false, fmt.Errorf("failed to get capacity info: %+v", err) |
| 136 | + } |
| 137 | + |
| 138 | + volumeCapacity := hostPathVolumes[volumeHandle].VolSize |
| 139 | + glog.V(3).Infof("volume capacity: %+v fs capacity:%+v", volumeCapacity, fscapacity) |
| 140 | + return fscapacity >= volumeCapacity, nil |
| 141 | +} |
| 142 | + |
| 143 | +func getPVCapacity(volumeHandle string) (int64, int64, int64, error) { |
| 144 | + sourcePath := getSourcePath(volumeHandle) |
| 145 | + fsavailable, fscapacity, fsused, _, _, _, err := fs.FsInfo(sourcePath) |
| 146 | + return fscapacity, fsused, fsavailable, err |
| 147 | +} |
| 148 | + |
| 149 | +func checkPVUsage(volumeHandle string) (bool, error) { |
| 150 | + sourcePath := getSourcePath(volumeHandle) |
| 151 | + fsavailable, _, _, _, _, _, err := fs.FsInfo(sourcePath) |
| 152 | + if err != nil { |
| 153 | + return false, err |
| 154 | + } |
| 155 | + |
| 156 | + glog.V(3).Infof("fs available: %+v", fsavailable) |
| 157 | + return fsavailable > 0, nil |
| 158 | +} |
| 159 | + |
| 160 | +func doHealthCheckInControllerSide(volumeHandle string) (bool, string) { |
| 161 | + spExist, err := checkSourcePathExist(volumeHandle) |
| 162 | + if err != nil { |
| 163 | + return false, err.Error() |
| 164 | + } |
| 165 | + |
| 166 | + if !spExist { |
| 167 | + return false, "The source path of the volume doesn't exist" |
| 168 | + } |
| 169 | + |
| 170 | + capValid, err := checkPVCapacityValid(volumeHandle) |
| 171 | + if err != nil { |
| 172 | + return false, err.Error() |
| 173 | + } |
| 174 | + |
| 175 | + if !capValid { |
| 176 | + return false, "The capacity of volume is greater than actual storage" |
| 177 | + } |
| 178 | + |
| 179 | + available, err := checkPVUsage(volumeHandle) |
| 180 | + if err != nil { |
| 181 | + return false, err.Error() |
| 182 | + } |
| 183 | + |
| 184 | + if !available { |
| 185 | + return false, "The free space of the volume is insufficient" |
| 186 | + } |
| 187 | + |
| 188 | + return true, "" |
| 189 | +} |
| 190 | + |
| 191 | +func doHealthCheckInNodeSide(volumeHandle string) (bool, string) { |
| 192 | + mpExist, err := checkMountPointExist(volumeHandle) |
| 193 | + if err != nil { |
| 194 | + return false, err.Error() |
| 195 | + } |
| 196 | + |
| 197 | + if !mpExist { |
| 198 | + return false, "The volume isn't mounted" |
| 199 | + } |
| 200 | + |
| 201 | + return true, "" |
| 202 | +} |
0 commit comments