utils

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 9, 2023 License: Apache-2.0 Imports: 28 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Cgroupfs CgroupDriverType = "cgroupfs"
	Systemd  CgroupDriverType = "systemd"

	KubeRootNameSystemd       = "kubepods.slice/"
	KubeBurstableNameSystemd  = "kubepods-burstable.slice/"
	KubeBesteffortNameSystemd = "kubepods-besteffort.slice/"

	KubeRootNameCgroupfs       = "kubepods/"
	KubeBurstableNameCgroupfs  = "burstable/"
	KubeBesteffortNameCgroupfs = "besteffort/"
)
View Source
const (
	Gi          uint64 = 1024 * 1024 * 1024
	Mi          uint64 = 1024 * 1024
	DefaultPort int32  = 23000

	StrategyBinpack StrategyType = "binpack"
	StrategySpread  StrategyType = "spread"

	ProvisionerName string = "local.csi.kubeservice.cn"

	// ServiceType tag
	ServiceType = "SERVICE_TYPE"
	// PluginService represents the csi-plugin type.
	PluginService = "local"
	// ProvisionerService represents the csi-provisioner type.
	ProvisionerService = "provisioner"

	EnvLogLevel = "LogLevel"
	LogPanic    = "Panic"
	LogFatal    = "Fatal"
	LogError    = "Error"
	LogWarn     = "Warn"
	LogInfo     = "Info"
	LogDebug    = "Debug"
	LogTrace    = "Trace"

	KubernetesNodeIdentityKey = "kubernetes.io/hostname"
	VolumeTypeKey             = "volumeType"
	VolumeFSTypeKey           = "fsType"
	VolumeMediaType           = "mediaType"
	VolumeFSTypeExt4          = "ext4"
	VolumeFSTypeExt3          = "ext3"
	VolumeFSTypeXFS           = "xfs"
	VolumeIOPS                = "iops"
	VolumeBPS                 = "bps"

	BPSReadFile   = "blkio.throttle.read_bps_device"
	BPSWriteFile  = "blkio.throttle.write_bps_device"
	IOPSReadFile  = "blkio.throttle.read_iops_device"
	IOPSWriteFile = "blkio.throttle.write_iops_device"

	PVName       = "csi.storage.k8s.io/pv/name"
	PVCName      = "csi.storage.k8s.io/pvc/name"
	PVCNameSpace = "csi.storage.k8s.io/pvc/namespace"
	Ephemeral    = "csi.storage.k8s.io/ephemeral"
	VGName       = "vgName"
	MPName       = "MountPoint"
	DeviceName   = "Device"

	// VolumeType MUST BE case sensitive
	VolumeTypeMountPoint VolumeType = "MountPoint"
	VolumeTypeLVM        VolumeType = "LVM"
	VolumeTypeDevice     VolumeType = "Device"
	VolumeTypeQuota      VolumeType = "Quota"
	VolumeTypeUnknown    VolumeType = "Unknown"
	MediaTypeSSD         MediaType  = "ssd"
	MediaTypeHDD         MediaType  = "hdd"
	MediaTypeUnspecified MediaType  = "Unspecified"

	// This annotation is added to a PVC that has been triggered by scheduler to
	// be dynamically provisioned. Its value is the name of the selected node.
	AnnoSelectedNode                     = "volume.kubernetes.io/selected-node"
	LabelReschduleTimestamp              = "pod.oecp.io/reschdule-timestamp"
	EnvExpandSnapInterval                = "Expand_Snapshot_Interval"
	EnvForceCreateVG                     = "Force_Create_VG"
	PendingWithoutScheduledFieldSelector = "status.phase=Pending,spec.nodeName="
	TriggerPendingPodCycle               = time.Second * 300

	ParamSnapshotName            = "yoda.io/snapshot-name"
	ParamSnapshotReadonly        = "csi.kubeservice.cn/readonly"
	ParamSnapshotInitialSize     = "csi.kubeservice.cn/snapshot-initial-size"
	ParamSnapshotThreshold       = "csi.kubeservice.cn/snapshot-expansion-threshold"
	ParamSnapshotExpansionSize   = "csi.kubeservice.cn/snapshot-expansion-size"
	ParamVGName                  = "vgName"
	ParamLVSize                  = "size"
	EnvSnapshotPrefix            = "SNAPSHOT_PREFIX"
	DefaultSnapshotPrefix        = "snap"
	DefaultSnapshotInitialSize   = 4 * 1024 * 1024 * 1024
	DefaultSnapshotThreshold     = 0.5
	DefaultSnapshotExpansionSize = 1 * 1024 * 1024 * 1024

	Separator = "<:SEP:>"

	// lv tags
	Lvm2LVNameTag        = "LVM2_LV_NAME"
	Lvm2LVSizeTag        = "LVM2_LV_SIZE"
	Lvm2LVKernelMajorTag = "LVM2_LV_KERNEL_MAJOR"
	Lvm2LVKernelMinorTag = "LVM2_LV_KERNEL_MINOR"
	Lvm2LVAttrTag        = "LVM2_LV_ATTR"
	Lvm2LVUuidTag        = "LVM2_LV_UUID"
	Lvm2CopyPercentTag   = "LVM2_COPY_PERCENT"
	Lvm2LVTagsTag        = "LVM2_LV_TAGS"

	// vg tags
	Lvm2VGNameTag  = "LVM2_VG_NAME"
	Lvm2VGSizeTag  = "LVM2_VG_SIZE"
	Lvm2VGFreeTag  = "LVM2_VG_FREE"
	Lvm2VGUuidTag  = "LVM2_VG_UUID"
	Lvm2VGTagsTag  = "LVM2_VG_TAGS"
	Lvm2PVCountTag = "LVM2_PV_COUNT"

	// pv tags
	Lvm2PVUuidTag = "LVM2_PV_UUID"
	Lvm2PVNameTag = "LVM2_PV_NAME"
	Lvm2PVSizeTag = "LVM2_PV_SIZE"
	Lvm2PVFreeTag = "LVM2_PV_FREE"
	Lvm2PVTagsTag = "LVM2_PV_TAGS"

	// EVENT
	EventCreateVGFailed = "CreateVGFailed"

	NsenterCmd = "nsenter --mount=/proc/1/ns/mnt --ipc=/proc/1/ns/ipc --net=/proc/1/ns/net --uts=/proc/1/ns/uts "

	/*
		record: PVC->VG mapper
		- update by schedulerFramework prebind
		- read by schedulerFramework eventHandlers onPodAdd/Update
	*/
	AnnotationPodPVCAllocatedNeedMigrateKey = "csi.kubeservice.cn/pod-pvc-migrate"

	/*
		record: vgName to PV
		- update by nsl controller
		- read by csi: nodeServer publishVolume
	*/
	AnnotationPVAllocatedInfoKey = "csi.kubeservice.cn/pv-allocated"
)
View Source
const (
	// RuncRunTimeTag tag
	RuncRunTimeTag = "runc"
	// RunvRunTimeTag tag
	RunvRunTimeTag = "runv"
)

Variables

View Source
var (
	// Kubeconfig is the path to kubeconfig file
	Kubeconfig string
	// MasterURL is the url of kube-apiserver
	MasterURL string
)
View Source
var (
	ValidProvisionerNames = []string{
		ProvisionerName,
	}
	ValidVolumeType = []VolumeType{
		VolumeTypeMountPoint,
		VolumeTypeLVM,
		VolumeTypeDevice,
		VolumeTypeQuota,
	}
	SupportedFS       = []string{VolumeFSTypeExt3, VolumeFSTypeExt4, VolumeFSTypeXFS}
	SchedulerStrategy = StrategyBinpack
)
View Source
var (
	// NodeAddrMap map for NodeID and its Address
	NodeAddrMap = map[string]string{}
	// NodeAddrMutex Mutex for NodeAddr map
	NodeAddrMutex sync.RWMutex
)
View Source
var CgroupPathFormatter = cgroupPathFormatterInSystemd

default use Systemd cgroup path format

View Source
var KubeletRootDir = "/var/lib/kubelet"

KubeletRootDir kubelet root dir;

View Source
var MountPathWithTLS = "/tls"

MountPathWithTLS tls mount path;

Functions

func AppendJSONData

func AppendJSONData(dataFilePath string, appData map[string]string) error

AppendJSONData append map data to json file.

func CheckParameterValidate

func CheckParameterValidate(inputs []string) bool

CheckParameterValidate is check parameter validating in csi-plugin

func CheckQuotaPathValidate

func CheckQuotaPathValidate(kubeClient *kubernetes.Clientset, path string) error

CheckQuotaPathValidate is check quota path validating in csi-plugin

func ConnectorRun

func ConnectorRun(cmd string) (string, error)

ConnectorRun Run shell command with host connector host connector is daemon running in host.

func CreateDest

func CreateDest(dest string) error

CreateDest create de destination dir

func CreateDestInHost

func CreateDestInHost(dest string) error

CreateDestInHost create host dest directory

func DoMountInHost

func DoMountInHost(mntCmd string) error

func EnsureBlock

func EnsureBlock(target string) error

func Fsync

func Fsync(f *os.File) error

Fsync is a wrapper around file.Sync(). Special handling is needed on darwin platform.

func GetAllocateInfoJSONFromPod

func GetAllocateInfoJSONFromPod(pod *corev1.Pod) string

func GetFileContent

func GetFileContent(fileName string) string

GetFileContent get file content

func GetNodeAddr

func GetNodeAddr(client kubernetes.Interface, node string, port string) (string, error)

GetNodeAddr get node address

func GetNodeIP

func GetNodeIP(client kubernetes.Interface, nodeID string) (net.IP, error)

GetNodeIP get node address

func GetPodRunTime

func GetPodRunTime(req *csi.NodePublishVolumeRequest, clientSet *kubernetes.Clientset) (string, error)

GetPodRunTime Get Pod runtimeclass config Default as runc.

func GetPvNameFormPodMnt

func GetPvNameFormPodMnt(mntPath string) string

GetPvNameFormPodMnt get pv name

func GetRegionIDAndInstanceID

func GetRegionIDAndInstanceID(nodeName string) (string, string, error)

GetRegionIDAndInstanceID get regionID and instanceID object

func GetVGNameFromCsiPV

func GetVGNameFromCsiPV(pv *corev1.PersistentVolume) string

func IsBlockDevice

func IsBlockDevice(fullPath string) (bool, error)

IsBlockDevice checks if the given path is a block device

func IsDir

func IsDir(path string) bool

IsDir check file is directory

func IsDirEmpty

func IsDirEmpty(name string) (bool, error)

IsDirEmpty return status of dir empty or not

func IsDirTmpfs

func IsDirTmpfs(path string) bool

IsDirTmpfs check path is tmpfs mounted or not

func IsFileExisting

func IsFileExisting(filename string) bool

IsFileExisting check file exist in volume driver or not

func IsHostFileExist

func IsHostFileExist(path string) bool

IsHostFileExist is check host file is existing in lvm

func IsKataInstall

func IsKataInstall() bool

IsKataInstall check kata daemon installed

func IsLikelyNotMountPoint

func IsLikelyNotMountPoint(file string) (bool, error)

IsLikelyNotMountPoint return status of mount point,this function fix IsMounted return 0 bug IsLikelyNotMountPoint determines if a directory is not a mountpoint. It is fast but not necessarily ALWAYS correct. If the path is in fact a bind mount from one part of a mount to another it will not be detected. It also can not distinguish between mountpoints and symbolic links. mkdir /tmp/a /tmp/b; mount --bind /tmp/a /tmp/b; IsLikelyNotMountPoint("/tmp/b") will return true. When in fact /tmp/b is a mount point. If this situation is of interest to you, don't use this function...

func IsMounted

func IsMounted(mountPath string) bool

IsMounted return status of mount operation

func IsMountedInHost

func IsMountedInHost(mountPath string) bool

IsMountedInHost return status of host mounted or not

func IsPathAvailiable

func IsPathAvailiable(path string) error

IsPathAvailiable

func LoadJSONData

func LoadJSONData(dataFileName string) (map[string]string, error)

LoadJSONData loads json info from specified json file

func MkdirAll

func MkdirAll(path string, mode fs.FileMode) error

func MountBlock

func MountBlock(source, target string, opts ...string) error

func Ping

func Ping(ipAddress string) (*ping.Statistics, error)

Ping check network like shell ping command

func ReadJSONFile

func ReadJSONFile(file string) (map[string]string, error)

ReadJSONFile return a json object

func RemoveAll

func RemoveAll(deletePath string) error

func Run

func Run(cmd string) (string, error)

Run run shell command

func RunTimeout

func RunTimeout(cmd string, timeout int) error

RunTimeout tag

func SetNodeAddrMap

func SetNodeAddrMap(key string, value string)

SetNodeAddrMap set map with mutex

func SetupCgroupPathFormatter

func SetupCgroupPathFormatter(driver CgroupDriverType)

func Umount

func Umount(mountPath string) error

Umount do an unmount operation

func UmountInHost

func UmountInHost(mountPath string) error

UmountInHost do an unmount operation

func WriteAndSyncFile

func WriteAndSyncFile(filename string, data []byte, perm os.FileMode) error

WriteAndSyncFile behaves just like ioutil.WriteFile in the standard library, but calls Sync before closing the file. WriteAndSyncFile guarantees the data is synced if there is no error returned.

func WriteJSONFile

func WriteJSONFile(obj interface{}, file string) error

WriteJSONFile save json data to file

func WriteMetricsInfo

func WriteMetricsInfo(metricsPathPrefix string, req *csi.NodePublishVolumeRequest, metricsTop string, clientName string, storageBackendName string, fsName string)

Types

type BindingInfo

type BindingInfo struct {
	// node is the name of selected node
	Node string `json:"node"`
	// path for mount point
	Disk string `json:"disk"`
	// VgName is the name of selected volume group
	VgName string `json:"vgName"`
	// Device is the name for raw block device: /dev/vdb
	Device string `json:"device"`
	// [lvm] or [disk] or [device] or [quota]
	VolumeType string `json:"volumeType"`

	// PersistentVolumeClaim is the metakey for pvc: {namespace}/{name}
	PersistentVolumeClaim string `json:"persistentVolumeClaim"`
}

BindingInfo represents the pvc and disk/lvm mapping

type CgroupDriverType

type CgroupDriverType string

func (CgroupDriverType) Validate

func (c CgroupDriverType) Validate() bool

type CommandRunFunc

type CommandRunFunc func(cmd string) (string, error)

CommandRunFunc define the run function in utils for ut

type MediaType

type MediaType string

type MountPoint

type MountPoint struct {
	Device string
	Path   string
	Type   string
	Opts   []string // Opts may contain sensitive mount options (like passwords) and MUST be treated as such (e.g. not logged).
	Freq   int
	Pass   int
}

MountPoint represents a single line in /proc/mounts or /etc/fstab.

func ListProcMounts

func ListProcMounts() ([]MountPoint, error)

ListProcMounts is shared with NsEnterMounter

type Mounter

type Mounter interface {
	// If the folder doesn't exist, it will call 'mkdir -p'
	EnsureFolder(target string) error
	// If the block doesn't exist, create it
	EnsureBlock(target string) error
	// Format formats the source with the given filesystem type
	Format(source, fsType string) error

	// Mount mounts source to target with the given fstype and options.
	Mount(source, target, fsType string, options ...string) error

	// Mount mounts source to target for block file.
	MountBlock(source, target string, options ...string) error
	// Unmount unmounts the given target
	Unmount(target string) error

	// IsFormatted checks whether the source device is formatted or not. It
	// returns true if the source device is already formatted.
	IsFormatted(source string) (bool, error)

	// IsMounted checks whether the target path is a correct mount (i.e:
	// propagated). It returns true if it's mounted. An error is returned in
	// case of system errors or if it's mounted incorrectly.
	IsMounted(target string) (bool, error)

	IsNotMountPoint(file string) (bool, error)

	SafePathRemove(target string) error

	HasMountRefs(mountPath string, mountRefs []string) bool
}

Mounter is responsible for formatting and mounting volumes

func NewMounter

func NewMounter() Mounter

NewMounter returns a new mounter instance

type NodeAntiAffinityWeight

type NodeAntiAffinityWeight struct {
	// contains filtered or unexported fields
}

func NewNodeAntiAffinityWeight

func NewNodeAntiAffinityWeight() *NodeAntiAffinityWeight

func (*NodeAntiAffinityWeight) Get

func (w *NodeAntiAffinityWeight) Get(volumeType VolumeType) int

func (*NodeAntiAffinityWeight) Items

func (w *NodeAntiAffinityWeight) Items(copy bool) map[VolumeType]int

func (*NodeAntiAffinityWeight) Put

func (w *NodeAntiAffinityWeight) Put(volumeType VolumeType, weight int)

type PVAllocatedInfo

type PVAllocatedInfo struct {
	VGName     string `json:"vgName"`
	DeviceName string `json:"deviceName"`
	VolumeType string `json:"volumeType"`
}

func GetAllocatedInfoFromPVAnnotation

func GetAllocatedInfoFromPVAnnotation(pv *corev1.PersistentVolume) (*PVAllocatedInfo, error)

type PVCAllocateInfo

type PVCAllocateInfo struct {
	PVCName         string `json:"pvcName"`
	PVCNameSpace    string `json:"pvcNameSpace"`
	PVAllocatedInfo `json:",inline"`
}

type PodPVCAllocateInfo

type PodPVCAllocateInfo struct {
	PvcAllocates map[string]PVCAllocateInfo `json:"pvcAllocates"`
}

func GetAllocateInfoFromPod

func GetAllocateInfoFromPod(pod *corev1.Pod) (*PodPVCAllocateInfo, error)

type Result

type Result struct {
	Status  string `json:"status"`
	Message string `json:"message,omitempty"`
	Device  string `json:"device,omitempty"`
}

Result struct definition

func Fail

func Fail(a ...interface{}) Result

Fail return a Fail Result

func NotSupport

func NotSupport(a ...interface{}) Result

NotSupport return a NotSupport Result

func Succeed

func Succeed(a ...interface{}) Result

Succeed return a Succeed Result

type StrategyType

type StrategyType string

type UpdateStatus

type UpdateStatus string
var (
	UpdateStatusPending  UpdateStatus = "pending"
	UpdateStatusAccepted UpdateStatus = "accepted"
	UpdateStatusFailed   UpdateStatus = "failed"
)

type VolumeType

type VolumeType string

func VolumeTypeFromString

func VolumeTypeFromString(s string) (VolumeType, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL