...

Source file src/k8s.io/kubernetes/pkg/volume/iscsi/disk_manager.go

Documentation: k8s.io/kubernetes/pkg/volume/iscsi

     1  /*
     2  Copyright 2015 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 iscsi
    18  
    19  import (
    20  	"os"
    21  
    22  	v1 "k8s.io/api/core/v1"
    23  	"k8s.io/klog/v2"
    24  	"k8s.io/mount-utils"
    25  
    26  	"k8s.io/kubernetes/pkg/volume"
    27  	"k8s.io/kubernetes/pkg/volume/util"
    28  )
    29  
    30  // Abstract interface to disk operations.
    31  type diskManager interface {
    32  	MakeGlobalPDName(disk iscsiDisk) string
    33  	MakeGlobalVDPDName(disk iscsiDisk) string
    34  	// Attaches the disk to the kubelet's host machine.
    35  	AttachDisk(b iscsiDiskMounter) (string, error)
    36  	// Detaches the disk from the kubelet's host machine.
    37  	DetachDisk(disk iscsiDiskUnmounter, mntPath string) error
    38  	// Detaches the block disk from the kubelet's host machine.
    39  	DetachBlockISCSIDisk(disk iscsiDiskUnmapper, mntPath string) error
    40  }
    41  
    42  // utility to mount a disk based filesystem
    43  // globalPDPath: global mount path like, /var/lib/kubelet/plugins/kubernetes.io/iscsi/{ifaceName}/{portal-some_iqn-lun-lun_id}
    44  // volPath: pod volume dir path like, /var/lib/kubelet/pods/{podUID}/volumes/kubernetes.io~iscsi/{volumeName}
    45  func diskSetUp(manager diskManager, b iscsiDiskMounter, volPath string, mounter mount.Interface, fsGroup *int64, fsGroupChangePolicy *v1.PodFSGroupChangePolicy) error {
    46  	notMnt, err := mounter.IsLikelyNotMountPoint(volPath)
    47  	if err != nil && !os.IsNotExist(err) {
    48  		klog.Errorf("cannot validate mountpoint: %s", volPath)
    49  		return err
    50  	}
    51  	if !notMnt {
    52  		return nil
    53  	}
    54  
    55  	if err := os.MkdirAll(volPath, 0750); err != nil {
    56  		klog.Errorf("failed to mkdir:%s", volPath)
    57  		return err
    58  	}
    59  	// Perform a bind mount to the full path to allow duplicate mounts of the same disk.
    60  	options := []string{"bind"}
    61  	if b.readOnly {
    62  		options = append(options, "ro")
    63  	}
    64  	if b.iscsiDisk.InitiatorName != "" {
    65  		// new iface name is <target portal>:<volume name>
    66  		b.iscsiDisk.Iface = b.iscsiDisk.Portals[0] + ":" + b.iscsiDisk.VolName
    67  	}
    68  	globalPDPath := manager.MakeGlobalPDName(*b.iscsiDisk)
    69  	mountOptions := util.JoinMountOptions(b.mountOptions, options)
    70  	err = mounter.MountSensitiveWithoutSystemd(globalPDPath, volPath, "", mountOptions, nil)
    71  	if err != nil {
    72  		klog.Errorf("Failed to bind mount: source:%s, target:%s, err:%v", globalPDPath, volPath, err)
    73  		noMnt, mntErr := b.mounter.IsLikelyNotMountPoint(volPath)
    74  		if mntErr != nil {
    75  			klog.Errorf("IsLikelyNotMountPoint check failed: %v", mntErr)
    76  			return err
    77  		}
    78  		if !noMnt {
    79  			if mntErr = b.mounter.Unmount(volPath); mntErr != nil {
    80  				klog.Errorf("Failed to unmount: %v", mntErr)
    81  				return err
    82  			}
    83  			noMnt, mntErr = b.mounter.IsLikelyNotMountPoint(volPath)
    84  			if mntErr != nil {
    85  				klog.Errorf("IsLikelyNotMountPoint check failed: %v", mntErr)
    86  				return err
    87  			}
    88  			if !noMnt {
    89  				//  will most likely retry on next sync loop.
    90  				klog.Errorf("%s is still mounted, despite call to unmount().  Will try again next sync loop.", volPath)
    91  				return err
    92  			}
    93  		}
    94  		os.Remove(volPath)
    95  		return err
    96  	}
    97  
    98  	if !b.readOnly {
    99  		volume.SetVolumeOwnership(&b, volPath, fsGroup, fsGroupChangePolicy, util.FSGroupCompleteHook(b.plugin, nil))
   100  	}
   101  
   102  	return nil
   103  }
   104  

View as plain text