diff --git a/pkg/volume/csi/csi_mounter.go b/pkg/volume/csi/csi_mounter.go index 4d57e4f6fa3..1062b3ad319 100644 --- a/pkg/volume/csi/csi_mounter.go +++ b/pkg/volume/csi/csi_mounter.go @@ -18,6 +18,7 @@ package csi import ( "context" + "errors" "fmt" "os" "path" @@ -25,8 +26,11 @@ import ( "github.com/golang/glog" api "k8s.io/api/core/v1" + apierrs "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/types" + utilfeature "k8s.io/apiserver/pkg/util/feature" "k8s.io/client-go/kubernetes" + "k8s.io/kubernetes/pkg/features" kstrings "k8s.io/kubernetes/pkg/util/strings" "k8s.io/kubernetes/pkg/volume" "k8s.io/kubernetes/pkg/volume/util" @@ -162,6 +166,22 @@ func (c *csiMountMgr) SetUpAt(dir string, fsGroup *int64) error { accessMode = c.spec.PersistentVolume.Spec.AccessModes[0] } + // Inject pod information into volume_attributes + podAttrs, err := c.podAttributes() + if err != nil { + glog.Error(log("mouter.SetUpAt failed to assemble volume attributes: %v", err)) + return err + } + if podAttrs != nil { + if attribs == nil { + attribs = podAttrs + } else { + for k, v := range podAttrs { + attribs[k] = v + } + } + } + fsType := csiSource.FSType err = csi.NodePublishVolume( ctx, @@ -216,6 +236,38 @@ func (c *csiMountMgr) SetUpAt(dir string, fsGroup *int64) error { return nil } +func (c *csiMountMgr) podAttributes() (map[string]string, error) { + if !utilfeature.DefaultFeatureGate.Enabled(features.CSIPodInfo) { + return nil, nil + } + if c.plugin.csiDriverLister == nil { + return nil, errors.New("CSIDriver lister does not exist") + } + + csiDriver, err := c.plugin.csiDriverLister.Get(c.driverName) + if err != nil { + if apierrs.IsNotFound(err) { + glog.V(4).Infof(log("CSIDriver %q not found, not adding pod information", c.driverName)) + return nil, nil + } + return nil, err + } + + if csiDriver.Spec.PodInfoRequiredOnMount == nil || *csiDriver.Spec.PodInfoRequiredOnMount == false { + glog.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, + } + glog.V(4).Infof(log("CSIDriver %q requires pod information", c.driverName)) + return attrs, nil +} + func (c *csiMountMgr) GetAttributes() volume.Attributes { mounter := c.plugin.host.GetMounter(c.plugin.GetPluginName()) path := c.GetPath()