diff --git a/cmd/gke-gcloud-auth-plugin/cred.go b/cmd/gke-gcloud-auth-plugin/cred.go index 378ceee0df..7afa50aa42 100644 --- a/cmd/gke-gcloud-auth-plugin/cred.go +++ b/cmd/gke-gcloud-auth-plugin/cred.go @@ -348,6 +348,10 @@ func writeCacheFile(content string) error { } func getCacheFilePath() string { + if override := os.Getenv("GKE_GCLOUD_AUTH_PLUGIN_CACHE_OVERRIDE"); override != "" { + return override + } + po := clientcmd.NewDefaultPathOptions() kubeconfig := po.GetDefaultFilename() dir := filepath.Dir(kubeconfig) diff --git a/cmd/gke-gcloud-auth-plugin/cred_test.go b/cmd/gke-gcloud-auth-plugin/cred_test.go index 6ce4b5eef7..ce296e1064 100644 --- a/cmd/gke-gcloud-auth-plugin/cred_test.go +++ b/cmd/gke-gcloud-auth-plugin/cred_test.go @@ -3,6 +3,7 @@ package main import ( "context" "fmt" + "os" "path" "testing" "time" @@ -720,6 +721,17 @@ func TestCloudsdkBasedGcloudAccessToken(t *testing.T) { } } +func TestGetCacheFilePathOverride(t *testing.T) { + overridePath := "/tmp/my_custom_cache_path" + os.Setenv("GKE_GCLOUD_AUTH_PLUGIN_CACHE_OVERRIDE", overridePath) + defer os.Setenv("GKE_GCLOUD_AUTH_PLUGIN_CACHE_OVERRIDE", "") + + path := getCacheFilePath() + if path != overridePath { + t.Errorf("getCacheFilePath() = %v, want %v", path, overridePath) + } +} + func fakeDefaultTokenSource(ctx context.Context, scope ...string) (oauth2.TokenSource, error) { return &mockTokenSource{}, nil }