diff --git a/pkg/volume/csi/csi_block.go b/pkg/volume/csi/csi_block.go index 2f1d542237a..1b595b70c67 100644 --- a/pkg/volume/csi/csi_block.go +++ b/pkg/volume/csi/csi_block.go @@ -93,6 +93,7 @@ type csiBlockMapper struct { volumeID string readOnly bool spec *volume.Spec + pod *v1.Pod podUID types.UID } @@ -210,8 +211,21 @@ func (m *csiBlockMapper) publishVolumeForBlock( publishVolumeInfo = attachment.Status.AttachmentMetadata } + // Inject pod information into volume_attributes + volAttribs := csiSource.VolumeAttributes + podInfoEnabled, err := m.plugin.podInfoEnabled(string(m.driverName)) + if err != nil { + return "", errors.New(log("blockMapper.publishVolumeForBlock failed to assemble volume attributes: %v", err)) + } + volumeLifecycleMode, err := m.plugin.getVolumeLifecycleMode(m.spec) + if err != nil { + return "", errors.New(log("blockMapper.publishVolumeForBlock failed to get VolumeLifecycleMode: %v", err)) + } + if podInfoEnabled { + volAttribs = mergeMap(volAttribs, GetPodInfoAttrs(m.pod, volumeLifecycleMode)) + } + nodePublishSecrets := map[string]string{} - var err error if csiSource.NodePublishSecretRef != nil { nodePublishSecrets, err = getCredentialsFromSecret(m.k8s, csiSource.NodePublishSecretRef) if err != nil { @@ -241,7 +255,7 @@ func (m *csiBlockMapper) publishVolumeForBlock( publishPath, accessMode, publishVolumeInfo, - csiSource.VolumeAttributes, + volAttribs, nodePublishSecrets, fsTypeBlockName, []string{}, diff --git a/pkg/volume/csi/csi_mounter.go b/pkg/volume/csi/csi_mounter.go index d632ffd1433..654133fe40e 100644 --- a/pkg/volume/csi/csi_mounter.go +++ b/pkg/volume/csi/csi_mounter.go @@ -23,7 +23,6 @@ import ( "fmt" "os" "path/filepath" - "strconv" "k8s.io/klog/v2" @@ -221,11 +220,13 @@ func (c *csiMountMgr) SetUpAt(dir string, mounterArgs volume.MounterArgs) error } // Inject pod information into volume_attributes - podAttrs, err := c.podAttributes() + podInfoEnabled, err := c.plugin.podInfoEnabled(string(c.driverName)) if err != nil { return volumetypes.NewTransientOperationFailure(log("mounter.SetUpAt failed to assemble volume attributes: %v", err)) } - volAttribs = mergeMap(volAttribs, podAttrs) + if podInfoEnabled { + volAttribs = mergeMap(volAttribs, GetPodInfoAttrs(c.pod, c.volumeLifecycleMode)) + } // Inject pod service account token into volume attributes if utilfeature.DefaultFeatureGate.Enabled(features.CSIServiceAccountToken) { @@ -282,45 +283,6 @@ func (c *csiMountMgr) SetUpAt(dir string, mounterArgs volume.MounterArgs) error return nil } -func (c *csiMountMgr) podAttributes() (map[string]string, error) { - kletHost, ok := c.plugin.host.(volume.KubeletVolumeHost) - if ok { - kletHost.WaitForCacheSync() - } - - if c.plugin.csiDriverLister == nil { - return nil, fmt.Errorf("CSIDriverLister not found") - } - - csiDriver, err := c.plugin.csiDriverLister.Get(string(c.driverName)) - if err != nil { - if apierrors.IsNotFound(err) { - klog.V(4).Infof(log("CSIDriver %q not found, not adding pod information", c.driverName)) - return nil, nil - } - return nil, err - } - - // if PodInfoOnMount is not set or false we do not set pod attributes - if csiDriver.Spec.PodInfoOnMount == nil || *csiDriver.Spec.PodInfoOnMount == false { - klog.V(4).Infof(log("CSIDriver %q does not require pod information", c.driverName)) - return nil, nil - } - - attrs := map[string]string{ - "csi.storage.k8s.io/pod.name": c.pod.Name, - "csi.storage.k8s.io/pod.namespace": c.pod.Namespace, - "csi.storage.k8s.io/pod.uid": string(c.pod.UID), - "csi.storage.k8s.io/serviceAccount.name": c.pod.Spec.ServiceAccountName, - } - if utilfeature.DefaultFeatureGate.Enabled(features.CSIInlineVolume) { - attrs["csi.storage.k8s.io/ephemeral"] = strconv.FormatBool(c.volumeLifecycleMode == storage.VolumeLifecycleEphemeral) - } - - klog.V(4).Infof(log("CSIDriver %q requires pod information", c.driverName)) - return attrs, nil -} - func (c *csiMountMgr) podServiceAccountTokenAttrs() (map[string]string, error) { if c.plugin.serviceAccountTokenGetter == nil { return nil, errors.New("ServiceAccountTokenGetter is nil") diff --git a/pkg/volume/csi/csi_plugin.go b/pkg/volume/csi/csi_plugin.go index 85c1c1f3db1..7609d0a3c47 100644 --- a/pkg/volume/csi/csi_plugin.go +++ b/pkg/volume/csi/csi_plugin.go @@ -685,6 +685,7 @@ func (p *csiPlugin) NewBlockVolumeMapper(spec *volume.Spec, podRef *api.Pod, opt readOnly: readOnly, spec: spec, specName: spec.Name(), + pod: podRef, podUID: podRef.UID, } mapper.csiClientGetter.driverName = csiDriverName(pvSource.Driver) @@ -959,6 +960,34 @@ func (p *csiPlugin) newAttacherDetacher() (*csiAttacher, error) { }, nil } +// podInfoEnabled check CSIDriver enabled pod info flag +func (p *csiPlugin) podInfoEnabled(driverName string) (bool, error) { + kletHost, ok := p.host.(volume.KubeletVolumeHost) + if ok { + kletHost.WaitForCacheSync() + } + + if p.csiDriverLister == nil { + return false, fmt.Errorf("CSIDriverLister not found") + } + + csiDriver, err := p.csiDriverLister.Get(driverName) + if err != nil { + if apierrors.IsNotFound(err) { + klog.V(4).Infof(log("CSIDriver %q not found, not adding pod information", driverName)) + return false, nil + } + return false, err + } + + // if PodInfoOnMount is not set or false we do not set pod attributes + if csiDriver.Spec.PodInfoOnMount == nil || *csiDriver.Spec.PodInfoOnMount == false { + klog.V(4).Infof(log("CSIDriver %q does not require pod information", driverName)) + return false, nil + } + return true, nil +} + func unregisterDriver(driverName string) error { csiDrivers.Delete(driverName) diff --git a/pkg/volume/csi/csi_util.go b/pkg/volume/csi/csi_util.go index dec72a46bad..2f71ffd12b4 100644 --- a/pkg/volume/csi/csi_util.go +++ b/pkg/volume/csi/csi_util.go @@ -27,6 +27,7 @@ import ( "time" api "k8s.io/api/core/v1" + storage "k8s.io/api/storage/v1" meta "k8s.io/apimachinery/pkg/apis/meta/v1" utilfeature "k8s.io/apiserver/pkg/util/feature" "k8s.io/client-go/kubernetes" @@ -203,3 +204,17 @@ func createCSIOperationContext(volumeSpec *volume.Spec, timeout time.Duration) ( ctx := context.WithValue(context.Background(), additionalInfoKey, additionalInfo{Migrated: strconv.FormatBool(migrated)}) return context.WithTimeout(ctx, timeout) } + +// GetPodInfoAttrs returns pod info for NodePublish +func GetPodInfoAttrs(pod *api.Pod, volumeMode storage.VolumeLifecycleMode) map[string]string { + attrs := map[string]string{ + "csi.storage.k8s.io/pod.name": pod.Name, + "csi.storage.k8s.io/pod.namespace": pod.Namespace, + "csi.storage.k8s.io/pod.uid": string(pod.UID), + "csi.storage.k8s.io/serviceAccount.name": pod.Spec.ServiceAccountName, + } + if utilfeature.DefaultFeatureGate.Enabled(features.CSIInlineVolume) { + attrs["csi.storage.k8s.io/ephemeral"] = strconv.FormatBool(volumeMode == storage.VolumeLifecycleEphemeral) + } + return attrs +}