Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/gke-gcloud-auth-plugin/cred.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ var (
location = pflag.String("location", "", fmt.Sprintf("Location of the Cluster %s.", applicableOnlyForEdgeCloud))
cluster = pflag.String("cluster", "", fmt.Sprintf("Name of the Cluster %s.", applicableOnlyForEdgeCloud))
impersonateServiceAccount = pflag.String("impersonate_service_account", "", "Impersonate a service account to retrieve tokens for the Cluster.")
configuration = pflag.String("configuration", "", "Optional named configuration to use for gcloud command")
)

func main() {
Expand Down Expand Up @@ -138,6 +139,7 @@ func main() {
account: *account,
project: *project,
impersonateServiceAccount: *impersonateServiceAccount,
configuration: *configuration,
}
}

Expand Down
29 changes: 29 additions & 0 deletions cmd/gke-gcloud-auth-plugin/cred_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ var (
"extra_args": "--project=` + fakeProject + ` --account=` + fakeAccount + `"
}`

wantCacheFileWithConfiguration = `{
"current_context": "gke_user-gke-dev_us-east1-b_cluster-1",
"access_token": "ya29.gcloud_t0k3n",
"token_expiry": "2022-01-01T00:00:00Z",
"extra_args": "--configuration=my-config"
}`

wantCacheFileImpersonateServiceAccount = `{
"current_context": "gke_user-gke-dev_us-east1-b_cluster-1",
"access_token": "ya29.gcloud_t0k3n",
Expand Down Expand Up @@ -225,6 +232,28 @@ func TestExecCredential(t *testing.T) {
"--account=" + fakeAccount,
},
},
{
testName: "NewGcloudAccessTokenWithConfiguration",
p: &plugin{
k8sStartingConfig: fakeK8sStartingConfig,
getCacheFilePath: fakeGetCacheFilePath,
readFile: fakeReadFile,
timeNow: fakeTimeNow,
tokenProvider: &gcloudTokenProvider{
readGcloudConfigRaw: fakeGcloudConfigOutput,
readFile: fakeReadFile,
configuration: "my-config",
},
},
wantToken: fakeExecCredential("ya29.gcloud_t0k3n", &metav1.Time{Time: newYears}),
wantCacheFile: wantCacheFileWithConfiguration,
wantGcloudArgs: []string{
"config",
"config-helper",
"--format=json",
"--configuration=my-config",
},
},
{
testName: "NewGcloudAccessTokenWithImpersonateServiceAccount",
p: &plugin{
Expand Down
6 changes: 5 additions & 1 deletion cmd/gke-gcloud-auth-plugin/gcloud_token_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type gcloudTokenProvider struct {
account string
project string
impersonateServiceAccount string
configuration string
}

// readGcloudConfig returns an object which represents gcloud config output
Expand Down Expand Up @@ -88,7 +89,7 @@ func (p *gcloudTokenProvider) useCache() bool {
}

func (p *gcloudTokenProvider) getExtraArgs() []string {
extraArgs := make([]string, 0, 3)
extraArgs := make([]string, 0, 4)
if p.project != "" {
extraArgs = append(extraArgs, fmt.Sprintf("--project=%s", p.project))
}
Expand All @@ -98,6 +99,9 @@ func (p *gcloudTokenProvider) getExtraArgs() []string {
if p.impersonateServiceAccount != "" {
extraArgs = append(extraArgs, fmt.Sprintf("--impersonate-service-account=%s", p.impersonateServiceAccount))
}
if p.configuration != "" {
extraArgs = append(extraArgs, fmt.Sprintf("--configuration=%s", p.configuration))
}
return extraArgs
}

Expand Down
Loading