@@ -26,10 +26,8 @@ import (
2626 "io"
2727 "net"
2828 "net/http"
29- "net/url"
3029 "os"
3130 "os/exec"
32- "path"
3331 "path/filepath"
3432 "strings"
3533 "sync"
@@ -64,10 +62,10 @@ func New(mounterPath string) *Mounter {
6462
6563func (m * Mounter ) Mount (ctx context.Context , mc * MountConfig ) error {
6664 // Start the token server for HostNetwork enabled pods.
67- if mc .PodShouldUseTokenServer {
65+ if mc .TokenServerIdentityProvider != "" {
6866 tp := filepath .Join (mc .TempDir , TokenFileName )
6967 klog .Infof ("Pod has hostNetwork enabled and token server feature is turned on. Starting Token Server on %s." , tp )
70- go StartTokenServer (ctx , tp )
68+ go StartTokenServer (ctx , tp , mc . TokenServerIdentityProvider )
7169 }
7270
7371 klog .Infof ("start to mount bucket %q for volume %q" , mc .BucketName , mc .VolumeName )
@@ -302,13 +300,13 @@ func getK8sTokenFromFile(tokenPath string) (string, error) {
302300 return strings .TrimSpace (string (token )), nil
303301}
304302
305- func fetchIdentityBindingToken (ctx context.Context , k8sSAToken string ) (* oauth2.Token , error ) {
303+ func fetchIdentityBindingToken (ctx context.Context , k8sSAToken string , identityProvider string ) (* oauth2.Token , error ) {
306304 stsService , err := sts .NewService (ctx , option .WithHTTPClient (& http.Client {}))
307305 if err != nil {
308306 return nil , fmt .Errorf ("new STS service error: %w" , err )
309307 }
310308
311- audience , err := getAudienceFromContext (ctx )
309+ audience , err := getAudienceFromContextAndIdentityProvider (ctx , identityProvider )
312310 if err != nil {
313311 return nil , fmt .Errorf ("failed to get audience from the context: %w" , err )
314312 }
@@ -334,39 +332,23 @@ func fetchIdentityBindingToken(ctx context.Context, k8sSAToken string) (*oauth2.
334332 }, nil
335333}
336334
337- func getAudienceFromContext (ctx context.Context ) (string , error ) {
335+ func getAudienceFromContextAndIdentityProvider (ctx context.Context , identityProvider string ) (string , error ) {
338336 projectID , err := metadata .ProjectIDWithContext (ctx )
339337 if err != nil {
340338 return "" , fmt .Errorf ("failed to get project ID: %w" , err )
341339 }
342- // Get all instance metadata attributes
343- clusterLocation , err := metadata .InstanceAttributeValueWithContext (ctx , "cluster-location" )
344- if err != nil {
345- return "" , fmt .Errorf ("failed to get clusterLocation: %w" , err )
346- }
347- clusterName , err := metadata .InstanceAttributeValueWithContext (ctx , "cluster-name" )
348- if err != nil {
349- return "" , fmt .Errorf ("failed to get clusterName: %w" , err )
350- }
351-
352- klog .Infof ("projectID: %s, clusterName: %s, clusterLocation: %s" , projectID , clusterName , clusterLocation )
353- onePlatformClusterResourceURL := & url.URL {
354- Scheme : "https" ,
355- Host : "container.googleapis.com" ,
356- Path : path .Join ("v1" , "projects" , projectID , "locations" , clusterLocation , "clusters" , clusterName ),
357- }
358340
359341 audience := fmt .Sprintf (
360342 "identitynamespace:%s.svc.id.goog:%s" ,
361343 projectID ,
362- onePlatformClusterResourceURL ,
344+ identityProvider ,
363345 )
364346 klog .Infof ("audience: %s" , audience )
365347
366348 return audience , nil
367349}
368350
369- func StartTokenServer (ctx context.Context , tokenURLSocketPath string ) {
351+ func StartTokenServer (ctx context.Context , tokenURLSocketPath string , identityProvider string ) {
370352 // Create a unix domain socket and listen for incoming connections.
371353 tokenSocketListener , err := net .Listen ("unix" , tokenURLSocketPath )
372354 if err != nil {
@@ -388,7 +370,7 @@ func StartTokenServer(ctx context.Context, tokenURLSocketPath string) {
388370
389371 return
390372 }
391- stsToken , err = fetchIdentityBindingToken (ctx , k8stoken )
373+ stsToken , err = fetchIdentityBindingToken (ctx , k8stoken , identityProvider )
392374 if err != nil {
393375 klog .Errorf ("failed to get sts token from path %v" , err )
394376 w .WriteHeader (http .StatusInternalServerError )
0 commit comments