-
Notifications
You must be signed in to change notification settings - Fork 164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
kubeapi for matching pv/pvc to /dev/sd devices #4378
Open
andrewd-zededa
wants to merge
1
commit into
lf-edge:master
Choose a base branch
from
andrewd-zededa:kubeapi-diskmetric
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
// Copyright (c) 2024 Zededa, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
//go:build kubevirt | ||
|
||
package kubeapi | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"strings" | ||
"syscall" | ||
|
||
"github.com/lf-edge/eve/pkg/pillar/pubsub" | ||
"golang.org/x/sys/unix" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
const ( | ||
longhornDevPath = "/dev/longhorn" | ||
) | ||
|
||
func getMajorMinorStr(stat syscall.Stat_t) string { | ||
major := unix.Major(stat.Rdev) | ||
minor := unix.Minor(stat.Rdev) | ||
return fmt.Sprintf("%d:%d", major, minor) | ||
} | ||
|
||
func isPvAttachedOnThisNode(pv string) bool { | ||
if _, err := os.Stat(longhornDevPath + "/" + pv); err != nil { | ||
return false | ||
} | ||
return true | ||
} | ||
func isBlockDeviceAttachedOnThisNode(dev string) bool { | ||
if _, err := os.Stat("/dev/" + dev); err != nil { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
// CleanupUnmountedDiskMetrics loops over existing DiskMetric | ||
// objects and unpublishes them if the device no longer exists | ||
func CleanupUnmountedDiskMetrics(pubDiskMetric pubsub.Publication, pvcToPvMap map[string]string) { | ||
existingMetrics := pubDiskMetric.GetAll() | ||
|
||
for id := range existingMetrics { | ||
if strings.HasPrefix(id, "pvc-") { | ||
pvName, ok := pvcToPvMap[id] | ||
if ok { | ||
if !isPvAttachedOnThisNode(pvName) { | ||
pubDiskMetric.Unpublish(id) | ||
} | ||
} | ||
} else { | ||
if !isBlockDeviceAttachedOnThisNode(id) { | ||
pubDiskMetric.Unpublish(id) | ||
} | ||
} | ||
} | ||
} | ||
|
||
// LonghornGetMajorMinorMaps builds two maps between | ||
// device major:minor -> kube-pv-name/lh-volume-name | ||
// and kube-pv-name/lh-volume-name -> maj:min to | ||
// help callers find a PV/PVC in /proc/diskstats | ||
// which only shows the sdX path. | ||
func LonghornGetMajorMinorMaps() (map[string]string, map[string]string, error) { | ||
lhMajMinToNameMap := make(map[string]string) // maj:min -> kube-pv-name/lh-volume-name | ||
lhNameToMajMinMap := make(map[string]string) // kube-pv-name/lh-volume-name -> maj:min | ||
|
||
if _, err := os.Stat(longhornDevPath); err != nil { | ||
return lhMajMinToNameMap, lhNameToMajMinMap, fmt.Errorf("longhorn dev path missing") | ||
} | ||
|
||
lhPvcList, err := os.ReadDir(longhornDevPath) | ||
if err != nil { | ||
return lhMajMinToNameMap, lhNameToMajMinMap, fmt.Errorf("unable to read longhorn devs") | ||
} | ||
|
||
for _, lhDirEnt := range lhPvcList { | ||
var lhStat syscall.Stat_t | ||
err := syscall.Stat(longhornDevPath+"/"+lhDirEnt.Name(), &lhStat) | ||
|
||
if err != nil { | ||
continue | ||
} | ||
majMinKey := getMajorMinorStr(lhStat) | ||
lhMajMinToNameMap[majMinKey] = lhDirEnt.Name() | ||
lhNameToMajMinMap[lhDirEnt.Name()] = majMinKey | ||
|
||
} | ||
return lhMajMinToNameMap, lhNameToMajMinMap, nil | ||
} | ||
|
||
// SCSIGetMajMinMaps builds two maps to assist linking with other devices | ||
// First map: maj:min -> sdX | ||
// Second map: sdX -> maj:min | ||
func SCSIGetMajMinMaps() (map[string]string, map[string]string, error) { | ||
sdMajMinToNameMap := make(map[string]string) // maj:min -> sdX | ||
sdNameToMajMinMap := make(map[string]string) // sdX -> maj:min | ||
|
||
blockDevs, err := os.ReadDir("/sys/class/block/") | ||
if err != nil { | ||
return sdMajMinToNameMap, sdNameToMajMinMap, fmt.Errorf("unable to read sd devs") | ||
} | ||
|
||
for _, devEnt := range blockDevs { | ||
if !strings.HasPrefix(devEnt.Name(), "sd") { | ||
continue | ||
} | ||
|
||
var blockStat syscall.Stat_t | ||
err := syscall.Stat("/dev/"+devEnt.Name(), &blockStat) | ||
if err != nil { | ||
continue | ||
} | ||
majMinVal := getMajorMinorStr(blockStat) | ||
sdMajMinToNameMap[majMinVal] = devEnt.Name() | ||
sdNameToMajMinMap[devEnt.Name()] = majMinVal | ||
} | ||
return sdMajMinToNameMap, sdNameToMajMinMap, nil | ||
} | ||
|
||
// PvPvcMaps returns two maps of pv-name/longhorn-name -> pvc-name | ||
// and pvc-name -> pv-name/longhorn-name | ||
func PvPvcMaps() (map[string]string, map[string]string, error) { | ||
pvsMap := make(map[string]string) | ||
pvcsMap := make(map[string]string) | ||
|
||
clientset, err := GetClientSet() | ||
if err != nil { | ||
return pvsMap, pvcsMap, fmt.Errorf("PvToPvc_Map: can't get clientset %v", err) | ||
} | ||
|
||
pvcs, err := clientset.CoreV1().PersistentVolumeClaims(EVEKubeNameSpace).List(context.Background(), metav1.ListOptions{}) | ||
if err != nil { | ||
return pvsMap, pvcsMap, fmt.Errorf("get pvcs:%v", err) | ||
} | ||
for _, pvc := range pvcs.Items { | ||
pvsMap[pvc.Spec.VolumeName] = pvc.ObjectMeta.Name | ||
pvcsMap[pvc.ObjectMeta.Name] = pvc.Spec.VolumeName | ||
} | ||
return pvsMap, pvcsMap, nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@andrewd-zededa , why are you considering only sdX devices? What about devices that have NVMes that will be named into something like nvme0n1, nvme0n1p1, nvme0n1p2, nvme0n1p3?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And also eMMCs, that in this case will be named as something like mmcblk0p1, mmcblk0p2, etc...