Back to / pkg / volume / util

package util

Latest Go to latest
Published: Jun 26, 2020 | License: Apache-2.0 | Module:


Package util contains utility code for use by volume plugins.



const (
	// EBSVolumeLimitKey resource name that will store volume limits for EBS
	EBSVolumeLimitKey = "attachable-volumes-aws-ebs"
	// EBSNitroLimitRegex finds nitro instance types with different limit than EBS defaults
	EBSNitroLimitRegex = "^[cmr]5.*|t3|z1d"
	// DefaultMaxEBSVolumes is the limit for volumes attached to an instance.
	// Amazon recommends no more than 40; the system root volume uses at least one.
	// See
	DefaultMaxEBSVolumes = 39
	// DefaultMaxEBSNitroVolumeLimit is default EBS volume limit on m5 and c5 instances
	DefaultMaxEBSNitroVolumeLimit = 25
	// AzureVolumeLimitKey stores resource name that will store volume limits for Azure
	AzureVolumeLimitKey = "attachable-volumes-azure-disk"
	// GCEVolumeLimitKey stores resource name that will store volume limits for GCE node
	GCEVolumeLimitKey = "attachable-volumes-gce-pd"

	// CinderVolumeLimitKey contains Volume limit key for Cinder
	CinderVolumeLimitKey = "attachable-volumes-cinder"
	// DefaultMaxCinderVolumes defines the maximum number of PD Volumes for Cinder
	// For Openstack we are keeping this to a high enough value so as depending on backend
	// cluster admins can configure it.
	DefaultMaxCinderVolumes = 256

	// CSIAttachLimitPrefix defines prefix used for CSI volumes
	CSIAttachLimitPrefix = "attachable-volumes-csi-"

	// ResourceNameLengthLimit stores maximum allowed Length for a ResourceName
	ResourceNameLengthLimit = 63
const (
	// PVCProtectionFinalizer is the name of finalizer on PVCs that have a running pod.
	PVCProtectionFinalizer = ""

	// PVProtectionFinalizer is the name of finalizer on PVs that are bound by PVCs
	PVProtectionFinalizer = ""
const (

	// ControllerManagedAttachAnnotation is the key of the annotation on Node
	// objects that indicates attach/detach operations for the node should be
	// managed by the attach/detach controller
	ControllerManagedAttachAnnotation string = ""

	// KeepTerminatedPodVolumesAnnotation is the key of the annotation on Node
	// that decides if pod volumes are unmounted when pod is terminated
	KeepTerminatedPodVolumesAnnotation string = ""

	// MountsInGlobalPDPath is name of the directory appended to a volume plugin
	// name to create the place for volume mounts in the global PD path.
	MountsInGlobalPDPath = "mounts"

	// VolumeGidAnnotationKey is the of the annotation on the PersistentVolume
	// object that specifies a supplemental GID.
	VolumeGidAnnotationKey = ""

	// VolumeDynamicallyCreatedByKey is the key of the annotation on PersistentVolume
	// object created dynamically
	VolumeDynamicallyCreatedByKey = ""

func AccessModesContainedInAll

func AccessModesContainedInAll(indexedModes []v1.PersistentVolumeAccessMode, requestedModes []v1.PersistentVolumeAccessMode) bool

AccessModesContainedInAll returns whether all of the requested modes are contained by modes

func AccessModesContains

func AccessModesContains(modes []v1.PersistentVolumeAccessMode, mode v1.PersistentVolumeAccessMode) bool

AccessModesContains returns whether the requested mode is contained by modes

func CalculateTimeoutForVolume

func CalculateTimeoutForVolume(minimumTimeout, timeoutIncrement int, pv *v1.PersistentVolume) int64

CalculateTimeoutForVolume calculates time for a Recycler pod to complete a recycle operation. The calculation and return value is either the minimumTimeout or the timeoutIncrement per Gi of storage size, whichever is greater.

func CheckNodeAffinity

func CheckNodeAffinity(pv *v1.PersistentVolume, nodeLabels map[string]string) error

CheckNodeAffinity looks at the PV node affinity, and checks if the node has the same corresponding labels This ensures that we don't mount a volume that doesn't belong to this node

func CheckPersistentVolumeClaimModeBlock

func CheckPersistentVolumeClaimModeBlock(pvc *v1.PersistentVolumeClaim) bool

CheckPersistentVolumeClaimModeBlock checks VolumeMode. If the mode is Block, return true otherwise return false.

func CheckVolumeModeFilesystem

func CheckVolumeModeFilesystem(volumeSpec *volume.Spec) (bool, error)

CheckVolumeModeFilesystem checks VolumeMode. If the mode is Filesystem, return true otherwise return false.

func ClaimToClaimKey

func ClaimToClaimKey(claim *v1.PersistentVolumeClaim) string

ClaimToClaimKey return namespace/name string for pvc

func GenerateVolumeName

func GenerateVolumeName(clusterName, pvName string, maxLength int) string

GenerateVolumeName returns a PV name with clusterName prefix. The function should be used to generate a name of GCE PD or Cinder volume. It basically adds "<clusterName>-dynamic-" before the PV name, making sure the resulting string fits given length and cuts "dynamic" if not.

func GenericResizeFS

func GenericResizeFS(host volume.VolumeHost, pluginName, devicePath, deviceMountPath string) (bool, error)

GenericResizeFS : call generic filesystem resizer for plugins that don't have any special filesystem resize requirements

func GetCSIAttachLimitKey

func GetCSIAttachLimitKey(driverName string) string

GetCSIAttachLimitKey returns limit key used for CSI volumes

func GetClassForVolume

func GetClassForVolume(kubeClient clientset.Interface, pv *v1.PersistentVolume) (*storage.StorageClass, error)

GetClassForVolume locates storage class by persistent volume

func GetFullQualifiedPluginNameForVolume

func GetFullQualifiedPluginNameForVolume(pluginName string, spec *volume.Spec) string

GetFullQualifiedPluginNameForVolume returns full qualified plugin name for given volume. For CSI plugin, it appends plugin driver name at the end of plugin name, e.g. It helps to distinguish between metrics emitted for CSI volumes which may be handled by different CSI plugin drivers.

func GetPath

func GetPath(mounter volume.Mounter) (string, error)

GetPath checks if the path from the mounter is empty.

func GetPersistentVolumeClaimQualifiedName

func GetPersistentVolumeClaimQualifiedName(claim *v1.PersistentVolumeClaim) string

GetPersistentVolumeClaimQualifiedName returns a qualified name for pvc.

func GetPluginMountDir

func GetPluginMountDir(host volume.VolumeHost, name string) string

GetPluginMountDir returns the global mount directory name appended to the given plugin name's plugin directory

func GetPodVolumeNames

func GetPodVolumeNames(pod *v1.Pod) (mounts sets.String, devices sets.String)

GetPodVolumeNames returns names of volumes that are used in a pod, either as filesystem mount or raw block device.

func GetSecretForPV

func GetSecretForPV(secretNamespace, secretName, volumePluginName string, kubeClient clientset.Interface) (map[string]string, error)

GetSecretForPV locates secret by name and namespace, verifies the secret type, and returns secret map

func GetSecretForPod

func GetSecretForPod(pod *v1.Pod, secretName string, kubeClient clientset.Interface) (map[string]string, error)

GetSecretForPod locates secret by name in the pod's namespace and returns secret map

func GetUniquePodName

func GetUniquePodName(pod *v1.Pod) types.UniquePodName

GetUniquePodName returns a unique identifier to reference a pod by

func GetUniqueVolumeName

func GetUniqueVolumeName(pluginName, volumeName string) v1.UniqueVolumeName

GetUniqueVolumeName returns a unique name representing the volume/plugin. Caller should ensure that volumeName is a name/ID uniquely identifying the actual backing device, directory, path, etc. for a particular volume. The returned name can be used to uniquely reference the volume, for example, to prevent operations (attach/detach or mount/unmount) from being triggered on the same volume.

func GetUniqueVolumeNameFromSpec

func GetUniqueVolumeNameFromSpec(
	volumePlugin volume.VolumePlugin,
	volumeSpec *volume.Spec) (v1.UniqueVolumeName, error)

GetUniqueVolumeNameFromSpec uses the given VolumePlugin to generate a unique name representing the volume defined in the specified volume spec. This returned name can be used to uniquely reference the actual backing device, directory, path, etc. referenced by the given volumeSpec. If the given plugin does not support the volume spec, this returns an error.

func GetUniqueVolumeNameFromSpecWithPod

func GetUniqueVolumeNameFromSpecWithPod(
	podName types.UniquePodName, volumePlugin volume.VolumePlugin, volumeSpec *volume.Spec) v1.UniqueVolumeName

GetUniqueVolumeNameFromSpecWithPod returns a unique volume name with pod name included. This is useful to generate different names for different pods on same volume.

func GetVolumeMode

func GetVolumeMode(volumeSpec *volume.Spec) (v1.PersistentVolumeMode, error)

GetVolumeMode retrieves VolumeMode from pv. If the volume doesn't have PersistentVolume, it's an inline volume, should return volumeMode as filesystem to keep existing behavior.

func GetWindowsPath

func GetWindowsPath(path string) string

GetWindowsPath get a windows path

func HasMountRefs

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

HasMountRefs checks if the given mountPath has mountRefs. TODO: this is a workaround for the unmount device issue caused by gci mounter. In GCI cluster, if gci mounter is used for mounting, the container started by mounter script will cause additional mounts created in the container. Since these mounts are irrelevant to the original mounts, they should be not considered when checking the mount references. Current solution is to filter out those mount paths that contain the string of original mount path. Plan to work on better approach to solve this issue.

func IsLocalEphemeralVolume

func IsLocalEphemeralVolume(volume v1.Volume) bool

IsLocalEphemeralVolume determines whether the argument is a local ephemeral volume vs. some other type

func IsMultiAttachAllowed

func IsMultiAttachAllowed(volumeSpec *volume.Spec) bool

IsMultiAttachAllowed checks if attaching this volume to multiple nodes is definitely not allowed/possible. In its current form, this function can only reliably say for which volumes it's definitely forbidden. If it returns false, it is not guaranteed that multi-attach is actually supported by the volume type and we must rely on the attacher to fail fast in such cases. Please see and

func IsPodTerminated

func IsPodTerminated(pod *v1.Pod, podStatus v1.PodStatus) bool

IsPodTerminated checks if pod is terminated

func IsReady

func IsReady(dir string) bool

IsReady checks for the existence of a regular file called 'ready' in the given directory and returns true if that file exists.

func IsWindowsLocalPath

func IsWindowsLocalPath(goos, path string) bool

IsWindowsLocalPath checks if path is a local path prefixed with "/" or "\" like "/foo/bar" or "\foo\bar"

func IsWindowsUNCPath

func IsWindowsUNCPath(goos, path string) bool

IsWindowsUNCPath checks if path is prefixed with \\ This can be used to skip any processing of paths that point to SMB shares, local named pipes and local UNC path

func JoinMountOptions

func JoinMountOptions(userOptions []string, systemOptions []string) []string

JoinMountOptions joins mount options eliminating duplicates

func LoadPodFromFile

func LoadPodFromFile(filePath string) (*v1.Pod, error)

LoadPodFromFile will read, decode, and return a Pod from a file.

func MakeAbsolutePath

func MakeAbsolutePath(goos, path string) string

MakeAbsolutePath convert path to absolute path according to GOOS

func MakeNestedMountpoints

func MakeNestedMountpoints(name, baseDir string, pod v1.Pod) error

MakeNestedMountpoints creates mount points in baseDir for volumes mounted beneath name

func MapBlockVolume

func MapBlockVolume(
	blkUtil volumepathhandler.BlockVolumePathHandler,
	volumeMapName string,
	podUID utypes.UID,
) error

MapBlockVolume is a utility function to provide a common way of mapping block device path for a specified volume and pod. This function should be called by volume plugins that implements volume.BlockVolumeMapper.Map() method.

func MarkFSResizeFinished

func MarkFSResizeFinished(
	pvc *v1.PersistentVolumeClaim,
	newSize resource.Quantity,
	kubeClient clientset.Interface) error

MarkFSResizeFinished marks file system resizing as done

func MarkForFSResize

func MarkForFSResize(
	pvc *v1.PersistentVolumeClaim,
	kubeClient clientset.Interface) error

MarkForFSResize marks file system resizing as pending

func MarkResizeFinished

func MarkResizeFinished(
	pvc *v1.PersistentVolumeClaim,
	newSize resource.Quantity,
	kubeClient clientset.Interface) error

MarkResizeFinished marks all resizing as done

func MarkResizeInProgressWithResizer

func MarkResizeInProgressWithResizer(
	pvc *v1.PersistentVolumeClaim,
	resizerName string,
	kubeClient clientset.Interface) (*v1.PersistentVolumeClaim, error)

MarkResizeInProgressWithResizer marks cloudprovider resizing as in progress and also annotates the PVC with the name of the resizer.

func MergeResizeConditionOnPVC

func MergeResizeConditionOnPVC(
	pvc *v1.PersistentVolumeClaim,
	resizeConditions []v1.PersistentVolumeClaimCondition) *v1.PersistentVolumeClaim

MergeResizeConditionOnPVC updates pvc with requested resize conditions leaving other conditions untouched.

func MountOptionFromSpec

func MountOptionFromSpec(spec *volume.Spec, options ...string) []string

MountOptionFromSpec extracts and joins mount options from volume spec with supplied options

func NewSafeFormatAndMountFromHost

func NewSafeFormatAndMountFromHost(pluginName string, host volume.VolumeHost) *mount.SafeFormatAndMount

NewSafeFormatAndMountFromHost creates a new SafeFormatAndMount with Mounter and Exec taken from given VolumeHost.

func OperationCompleteHook

func OperationCompleteHook(plugin, operationName string) func(*error)

OperationCompleteHook returns a hook to call when an operation is completed

func PatchPVCStatus

func PatchPVCStatus(
	oldPVC *v1.PersistentVolumeClaim,
	newPVC *v1.PersistentVolumeClaim,
	kubeClient clientset.Interface) (*v1.PersistentVolumeClaim, error)

PatchPVCStatus updates PVC status using PATCH verb Don't use Update because this can be called from kubelet and if kubelet has an older client its Updates will overwrite new fields. And to avoid writing to a stale object, add ResourceVersion to the patch so that Patch will fail if the patch's RV != actual up-to-date RV like Update would

func RecordOperationLatencyMetric

func RecordOperationLatencyMetric(plugin, operationName string, secondsTaken float64)

RecordOperationLatencyMetric records the end to end latency for certain operation into metric volume_operation_total_seconds

func SetClaimResizer

func SetClaimResizer(
	pvc *v1.PersistentVolumeClaim,
	resizerName string,
	kubeClient clientset.Interface) (*v1.PersistentVolumeClaim, error)

SetClaimResizer sets resizer annotation on PVC

func SetReady

func SetReady(dir string)

SetReady creates a file called 'ready' in the given directory. It logs an error if the file cannot be created.

func SplitUniqueName

func SplitUniqueName(uniqueName v1.UniqueVolumeName) (string, string, error)

SplitUniqueName splits the unique name to plugin name and volume name strings. It expects the uniqueName to follow the format plugin_name/volume_name and the plugin name must be namespaced as described by the plugin interface, i.e. namespace/plugin containing exactly one '/'. This means the unique name will always be in the form of plugin_namespace/plugin/volume_name, see VolumePlugin interface description and pkg/volume/util/volumehelper/volumehelper.go GetUniqueVolumeNameFromSpec that constructs the unique volume names.

func UnmapBlockVolume

func UnmapBlockVolume(
	blkUtil volumepathhandler.BlockVolumePathHandler,
	volumeMapName string,
	podUID utypes.UID,
) error

UnmapBlockVolume is a utility function to provide a common way of unmapping block device path for a specified volume and pod. This function should be called by volume plugins that implements volume.BlockVolumeMapper.Map() method.

func UnmountViaEmptyDir

func UnmountViaEmptyDir(dir string, host volume.VolumeHost, volName string, volSpec volume.Spec, podUID utypes.UID) error

UnmountViaEmptyDir delegates the tear down operation for secret, configmap, git_repo and downwardapi to empty_dir

func UpdatePVSize

func UpdatePVSize(
	pv *v1.PersistentVolume,
	newSize resource.Quantity,
	kubeClient clientset.Interface) error

UpdatePVSize updates just pv size after cloudprovider resizing is successful

func WriteVolumeCache

func WriteVolumeCache(deviceMountPath string, exec utilexec.Interface) error

WriteVolumeCache flush disk data given the spcified mount path

type AtomicWriter

type AtomicWriter struct {
	// contains filtered or unexported fields

AtomicWriter handles atomically projecting content for a set of files into a target directory.


1. AtomicWriter reserves the set of pathnames starting with `..`. 2. AtomicWriter offers no concurrency guarantees and must be synchronized

by the caller.

The visible files in this volume are symlinks to files in the writer's data directory. Actual files are stored in a hidden timestamped directory which is symlinked to by the data directory. The timestamped directory and data directory symlink are created in the writer's target dir.  This scheme allows the files to be atomically updated by changing the target of the data directory symlink.

Consumers of the target directory can monitor the symlink using inotify or fanotify to receive events when the content in the volume is updated.

func NewAtomicWriter

func NewAtomicWriter(targetDir string, logContext string) (*AtomicWriter, error)

NewAtomicWriter creates a new AtomicWriter configured to write to the given target directory, or returns an error if the target directory does not exist.

func (*AtomicWriter) Write

func (w *AtomicWriter) Write(payload map[string]FileProjection) error

Write does an atomic projection of the given payload into the writer's target directory. Input paths must not begin with '..'.

The Write algorithm is:

1.  The payload is validated; if the payload is invalid, the function returns
2.  The current timestamped directory is detected by reading the data directory
3.  The old version of the volume is walked to determine whether any
    portion of the payload was deleted and is still present on disk.
4.  The data in the current timestamped directory is compared to the projected
    data to determine if an update is required.
5.  A new timestamped dir is created
6.  The payload is written to the new timestamped directory
7.  Symlinks and directory for new user-visible files are created (if needed).

    For example, consider the files:

    The user visible files are symbolic links into the internal data directory:
      <target-dir>/podName         ->
      <target-dir>/usr ->
      <target-dir>/k8s ->

    The data directory itself is a link to a timestamped directory with
    the real data:
      <target-dir>/          -> ..2016_02_01_15_04_05.12345678/
8.  A symlink to the new timestamped directory ..data_tmp is created that will
    become the new data directory
9.  The new data directory symlink is renamed to the data directory; rename is atomic

10. Old paths are removed from the user-visible portion of the target directory 11.  The previous timestamped directory is removed, if it exists

type DeviceUtil

type DeviceUtil interface {
	FindMultipathDeviceForDevice(disk string) string
	FindSlaveDevicesOnMultipath(disk string) []string
	GetISCSIPortalHostMapForTarget(targetIqn string) (map[string]int, error)
	FindDevicesForISCSILun(targetIqn string, lun int) ([]string, error)

DeviceUtil is a util for common device methods

func NewDeviceHandler

func NewDeviceHandler(io IoUtil) DeviceUtil

NewDeviceHandler Create a new IoHandler implementation

type FileProjection

type FileProjection struct {
	Data []byte
	Mode int32

FileProjection contains file Data and access Mode

type IoUtil

type IoUtil interface {
	ReadFile(filename string) ([]byte, error)
	ReadDir(dirname string) ([]os.FileInfo, error)
	Lstat(name string) (os.FileInfo, error)
	EvalSymlinks(path string) (string, error)

IoUtil is a mockable util for common IO operations

func NewIOHandler

func NewIOHandler() IoUtil

NewIOHandler Create a new IoHandler implementation

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier