Skip to content

Commit

Permalink
Add volumecontext package for accessing volume context from CSI
Browse files Browse the repository at this point in the history
Signed-off-by: Burak Varlı <[email protected]>
  • Loading branch information
unexge committed Jan 14, 2025
1 parent f1880c3 commit cb6e302
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
27 changes: 10 additions & 17 deletions pkg/driver/node/mounter/credential_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
k8sv1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/klog/v2"
k8sstrings "k8s.io/utils/strings"

"github.com/awslabs/aws-s3-csi-driver/pkg/driver/node/volumecontext"
)

const hostPluginDirEnv = "HOST_PLUGIN_DIR"
Expand All @@ -34,15 +36,6 @@ const (
AuthenticationSourcePod AuthenticationSource = "pod"
)

const (
VolumeCtxAuthenticationSource = "authenticationSource"
VolumeCtxSTSRegion = "stsRegion"
VolumeCtxServiceAccountName = "csi.storage.k8s.io/serviceAccount.name"
VolumeCtxServiceAccountTokens = "csi.storage.k8s.io/serviceAccount.tokens"
VolumeCtxPodNamespace = "csi.storage.k8s.io/pod.namespace"
VolumeCtxPodUID = "csi.storage.k8s.io/pod.uid"
)

const (
// This is to ensure only owner/group can read the file and no one else.
serviceAccountTokenPerm = 0440
Expand Down Expand Up @@ -96,7 +89,7 @@ func (c *CredentialProvider) Provide(ctx context.Context, volumeID string, volum
return nil, status.Error(codes.InvalidArgument, "Missing volume context")
}

authenticationSource := volumeContext[VolumeCtxAuthenticationSource]
authenticationSource := volumeContext[volumecontext.AuthenticationSource]
switch authenticationSource {
case AuthenticationSourcePod:
return c.provideFromPod(ctx, volumeID, volumeContext, mountpointArgs)
Expand Down Expand Up @@ -129,7 +122,7 @@ func (c *CredentialProvider) provideFromDriver() (*MountCredentials, error) {
func (c *CredentialProvider) provideFromPod(ctx context.Context, volumeID string, volumeContext map[string]string, mountpointArgs []string) (*MountCredentials, error) {
klog.V(4).Infof("NodePublishVolume: Using pod identity")

tokensJson := volumeContext[VolumeCtxServiceAccountTokens]
tokensJson := volumeContext[volumecontext.CSIServiceAccountTokens]
if tokensJson == "" {
klog.Error("`authenticationSource` configured to `pod` but no service account tokens are received. Please make sure to enable `podInfoOnMountCompat`, see " + podLevelCredentialsDocsPage)
return nil, status.Error(codes.InvalidArgument, "Missing service account tokens")
Expand Down Expand Up @@ -161,7 +154,7 @@ func (c *CredentialProvider) provideFromPod(ctx context.Context, volumeID string
defaultRegion = region
}

podID := volumeContext[VolumeCtxPodUID]
podID := volumeContext[volumecontext.CSIPodUID]
if podID == "" {
return nil, status.Error(codes.InvalidArgument, "Missing Pod info. Please make sure to enable `podInfoOnMountCompat`, see "+podLevelCredentialsDocsPage)
}
Expand All @@ -174,8 +167,8 @@ func (c *CredentialProvider) provideFromPod(ctx context.Context, volumeID string
hostPluginDir := hostPluginDirWithDefault()
hostTokenPath := path.Join(hostPluginDir, c.tokenFilename(podID, volumeID))

podNamespace := volumeContext[VolumeCtxPodNamespace]
podServiceAccount := volumeContext[VolumeCtxServiceAccountName]
podNamespace := volumeContext[volumecontext.CSIPodNamespace]
podServiceAccount := volumeContext[volumecontext.CSIServiceAccountName]
cacheKey := podNamespace + "/" + podServiceAccount

return &MountCredentials{
Expand Down Expand Up @@ -222,8 +215,8 @@ func (c *CredentialProvider) tokenFilename(podID string, volumeID string) string
}

func (c *CredentialProvider) findPodServiceAccountRole(ctx context.Context, volumeContext map[string]string) (string, error) {
podNamespace := volumeContext[VolumeCtxPodNamespace]
podServiceAccount := volumeContext[VolumeCtxServiceAccountName]
podNamespace := volumeContext[volumecontext.CSIPodNamespace]
podServiceAccount := volumeContext[volumecontext.CSIServiceAccountName]
if podNamespace == "" || podServiceAccount == "" {
klog.Error("`authenticationSource` configured to `pod` but no pod info found. Please make sure to enable `podInfoOnMountCompat`, see " + podLevelCredentialsDocsPage)
return "", status.Error(codes.InvalidArgument, "Missing Pod info. Please make sure to enable `podInfoOnMountCompat`, see "+podLevelCredentialsDocsPage)
Expand Down Expand Up @@ -253,7 +246,7 @@ func (c *CredentialProvider) findPodServiceAccountRole(ctx context.Context, volu
//
// It returns an error if all of them fails.
func (c *CredentialProvider) stsRegion(volumeContext map[string]string, mountpointArgs []string) (string, error) {
region := volumeContext[VolumeCtxSTSRegion]
region := volumeContext[volumecontext.STSRegion]
if region != "" {
klog.V(5).Infof("NodePublishVolume: Pod-level: Detected STS region %s from volume context", region)
return region, nil
Expand Down
12 changes: 6 additions & 6 deletions pkg/driver/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ import (

"github.com/awslabs/aws-s3-csi-driver/pkg/driver/node/mounter"
"github.com/awslabs/aws-s3-csi-driver/pkg/driver/node/targetpath"
"github.com/awslabs/aws-s3-csi-driver/pkg/driver/node/volumecontext"
)

const (
volumeCtxBucketName = "bucketName"
defaultKubeletPath = "/var/lib/kubelet"
defaultKubeletPath = "/var/lib/kubelet"
)

var kubeletPath = getKubeletPath()
Expand Down Expand Up @@ -68,8 +68,8 @@ func NewS3NodeServer(nodeID string, mounter mounter.Mounter, credentialProvider

func (ns *S3NodeServer) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRequest) (*csi.NodeStageVolumeResponse, error) {
volumeContext := req.GetVolumeContext()
if volumeContext[mounter.VolumeCtxAuthenticationSource] == mounter.AuthenticationSourcePod {
podID := volumeContext[mounter.VolumeCtxPodUID]
if volumeContext[volumecontext.AuthenticationSource] == mounter.AuthenticationSourcePod {
podID := volumeContext[volumecontext.CSIPodUID]
volumeID := req.GetVolumeId()
if podID != "" && volumeID != "" {
err := ns.credentialProvider.CleanupToken(volumeID, podID)
Expand All @@ -96,7 +96,7 @@ func (ns *S3NodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePubl

volumeContext := req.GetVolumeContext()

bucket, ok := volumeContext[volumeCtxBucketName]
bucket, ok := volumeContext[volumecontext.BucketName]
if !ok {
return nil, status.Error(codes.InvalidArgument, "Bucket name not provided")
}
Expand Down Expand Up @@ -295,7 +295,7 @@ func (ns *S3NodeServer) isValidVolumeCapabilities(volCaps []*csi.VolumeCapabilit
// with sensitive fields removed.
func logSafeNodePublishVolumeRequest(req *csi.NodePublishVolumeRequest) *csi.NodePublishVolumeRequest {
safeVolumeContext := maps.Clone(req.VolumeContext)
delete(safeVolumeContext, mounter.VolumeCtxServiceAccountTokens)
delete(safeVolumeContext, volumecontext.CSIServiceAccountTokens)

return &csi.NodePublishVolumeRequest{
VolumeId: req.VolumeId,
Expand Down
13 changes: 13 additions & 0 deletions pkg/driver/node/volumecontext/volume_context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Package volumecontext provides utilities for accessing volume context passed via CSI RPC.
package volumecontext

const (
BucketName = "bucketName"
AuthenticationSource = "authenticationSource"
STSRegion = "stsRegion"

CSIServiceAccountName = "csi.storage.k8s.io/serviceAccount.name"
CSIServiceAccountTokens = "csi.storage.k8s.io/serviceAccount.tokens"
CSIPodNamespace = "csi.storage.k8s.io/pod.namespace"
CSIPodUID = "csi.storage.k8s.io/pod.uid"
)

0 comments on commit cb6e302

Please sign in to comment.