Skip to content

Commit

Permalink
fix: parse useAuthProvider and useInternalIP as booleans (#157)
Browse files Browse the repository at this point in the history
Also change the signature on createClusterConfig to take options to allow for easier expansion.
  • Loading branch information
sethvargo committed Dec 14, 2021
1 parent 7d394eb commit 413f8df
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 33 deletions.
25 changes: 17 additions & 8 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

name: Get GKE Credentials
description: |-
Use this action to authenticate to a GKE cluster by generating
Use this action to authenticate to a GKE cluster by generating
an emphemeral kubeconfig to use with kubectl, helm etc.
author: Google LLC

Expand All @@ -25,7 +25,7 @@ inputs:
formatted private key which can be exported from the Cloud Console. The
value can be raw or base64-encoded.
required: false

cluster_name:
description: |-
Name of the cluster to get credentials for.
Expand All @@ -38,19 +38,28 @@ inputs:

project_id:
description: |-
Project ID where the cluster is deployed. If provided, this will override the project configured by gcloud.
Project ID where the cluster is deployed. If provided, this will override
the project configured by previous steps or environment variables.
required: false

use_auth_provider:
description: |-
Flag to use GCP auth plugin in kubectl instead of a short lived token. Defaults to false.
Flag to use GCP auth plugin in kubectl instead of a short lived token.
Defaults to false.
default: false
required: false

use_internal_ip:
description: |-
Flag to use the internal IP address of the cluster endpoint (for use with Private GKE clusters).
Flag to use the internal IP address of the cluster endpoint (for use with
Private GKE clusters).
default: false
required: false

branding:
icon: 'lock'
color: 'blue'

runs:
using: "node12"
main: "dist/index.js"
using: 'node12'
main: 'dist/index.js'
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

38 changes: 22 additions & 16 deletions src/gkeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,23 +162,16 @@ export class ClusterClient {
/**
* Create kubeconfig for cluster.
*
* @param authProvider boolean to use short lived OIDC token or GCP auth plugin in kubectl.
* @param useInternalIp boolean to use the internal IP address of the cluster endpoint.
* @returns kubeconfig
* @param opts Input options. See CreateKubeConfigOptions.
*/
async createKubeConfig(
authProvider: string,
useInternalIp: string,
cluster: ClusterResponse,
): Promise<string> {
const endpoint =
String(useInternalIp).toLowerCase() === 'true'
? cluster.data.privateClusterConfig.privateEndpoint
: cluster.data.endpoint;
const auth =
String(authProvider).toLowerCase() === 'true'
? { user: { 'auth-provider': { name: 'gcp' } } }
: { user: { token: await this.getToken() } };
async createKubeConfig(opts: CreateKubeConfigOptions): Promise<string> {
const cluster = opts.clusterData;
const endpoint = opts.useInternalIP
? cluster.data.privateClusterConfig.privateEndpoint
: cluster.data.endpoint;
const auth = opts.useAuthProvider
? { user: { 'auth-provider': { name: 'gcp' } } }
: { user: { token: await this.getToken() } };
const kubeConfig: KubeConfig = {
'apiVersion': 'v1',
'clusters': [
Expand Down Expand Up @@ -223,6 +216,19 @@ type context = {
name: string;
};

export type CreateKubeConfigOptions = {
// useAuthProvider is a boolean to use short lived OIDC token or GCP auth
// plugin in kubectl.
useAuthProvider: boolean;

// useInternalIP is a boolean to use the internal IP address of the cluster
// endpoint.
useInternalIP: boolean;

// clusterData is the cluster response data.
clusterData: ClusterResponse;
};

export type KubeConfig = {
'apiVersion': string;
'clusters': cluster[];
Expand Down
13 changes: 9 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import {
exportVariable,
getBooleanInput,
getInput,
info as logInfo,
setFailed,
Expand All @@ -31,8 +32,8 @@ async function run(): Promise<void> {
const location = getInput('location', { required: true });
const credentials = getInput('credentials');
const projectId = getInput('project_id');
const authProvider = getInput('use_auth_provider');
const useInternalIp = getInput('use_internal_ip');
const useAuthProvider = getBooleanInput('use_auth_provider');
const useInternalIP = getBooleanInput('use_internal_ip');

// Add warning if using credentials
if (credentials) {
Expand All @@ -46,10 +47,14 @@ async function run(): Promise<void> {
const client = new ClusterClient(location, { projectId, credentials });

// Get Cluster object
const cluster = await client.getCluster(name);
const clusterData = await client.getCluster(name);

// Create KubeConfig
const kubeConfig = await client.createKubeConfig(authProvider, useInternalIp, cluster);
const kubeConfig = await client.createKubeConfig({
useAuthProvider: useAuthProvider,
useInternalIP: useInternalIP,
clusterData: clusterData,
});

// Write kubeconfig to disk
const kubeConfigPath = await writeFile(kubeConfig);
Expand Down
32 changes: 28 additions & 4 deletions tests/clusterClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,13 @@ describe('Cluster', function () {
credentials: credentials,
projectId: project,
});
const kubeconfig = YAML.parse(await client.createKubeConfig('false', 'false', publicCluster));
const kubeconfig = YAML.parse(
await client.createKubeConfig({
useAuthProvider: false,
useInternalIP: false,
clusterData: publicCluster,
}),
);

expect(kubeconfig.clusters[0].name).to.eql(publicCluster.data.name);
expect(kubeconfig.clusters[0].cluster['certificate-authority-data']).to.eql(
Expand All @@ -136,7 +142,13 @@ describe('Cluster', function () {
credentials: credentials,
projectId: project,
});
const kubeconfig = YAML.parse(await client.createKubeConfig('true', 'false', publicCluster));
const kubeconfig = YAML.parse(
await client.createKubeConfig({
useAuthProvider: true,
useInternalIP: false,
clusterData: publicCluster,
}),
);

expect(kubeconfig.clusters[0].name).to.eql(publicCluster.data.name);
expect(kubeconfig.clusters[0].cluster['certificate-authority-data']).to.eql(
Expand All @@ -157,7 +169,13 @@ describe('Cluster', function () {
credentials: credentials,
projectId: project,
});
const kubeconfig = YAML.parse(await client.createKubeConfig('false', 'true', privateCluster));
const kubeconfig = YAML.parse(
await client.createKubeConfig({
useAuthProvider: false,
useInternalIP: true,
clusterData: privateCluster,
}),
);

expect(kubeconfig.clusters[0].name).to.eql(privateCluster.data.name);
expect(kubeconfig.clusters[0].cluster['certificate-authority-data']).to.eql(
Expand All @@ -180,7 +198,13 @@ describe('Cluster', function () {
credentials: credentials,
projectId: project,
});
const kubeconfig = YAML.parse(await client.createKubeConfig('true', 'true', privateCluster));
const kubeconfig = YAML.parse(
await client.createKubeConfig({
useAuthProvider: true,
useInternalIP: true,
clusterData: privateCluster,
}),
);

expect(kubeconfig.clusters[0].name).to.eql(privateCluster.data.name);
expect(kubeconfig.clusters[0].cluster['certificate-authority-data']).to.eql(
Expand Down

0 comments on commit 413f8df

Please sign in to comment.