Files
k8s-set-context/src/kubeconfigs/arc.ts
Vidya Reddy b6c5bf067a Vidya reddy pretty code (#53)
* updated action file with node16

* Code consistency using prettier and its workflow

* Enforce Prettier

* code fix

* code fix

* code fix

Co-authored-by: Vidya Reddy <vidyareddy@microsoft.com>
2022-06-24 16:08:48 -07:00

69 lines
2.1 KiB
TypeScript

import * as core from '@actions/core'
import * as io from '@actions/io'
import {Method, parseMethod} from '../types/method'
import * as path from 'path'
import {runAzCliCommand, runAzKubeconfigCommandBlocking} from './azCommands'
const RUNNER_TEMP: string = process.env['RUNNER_TEMP'] || ''
export const KUBECONFIG_LOCATION: string = path.join(
RUNNER_TEMP,
`arc_kubeconfig_${Date.now()}`
)
/**
* Gets the kubeconfig based on provided method for an Arc Kubernetes cluster
* @returns The kubeconfig wrapped in a Promise
*/
export async function getArcKubeconfig(): Promise<string> {
const resourceGroupName = core.getInput('resource-group', {required: true})
const clusterName = core.getInput('cluster-name', {required: true})
const azPath = await io.which('az', true)
const method: Method | undefined = parseMethod(
core.getInput('method', {required: true})
)
await runAzCliCommand(azPath, ['extension', 'add', '-n', 'connectedk8s'])
switch (method) {
case Method.SERVICE_ACCOUNT:
const saToken = core.getInput('token', {required: true})
return await runAzKubeconfigCommandBlocking(
azPath,
[
'connectedk8s',
'proxy',
'-n',
clusterName,
'-g',
resourceGroupName,
'--token',
saToken,
'-f',
KUBECONFIG_LOCATION
],
KUBECONFIG_LOCATION
)
case Method.SERVICE_PRINCIPAL:
return await runAzKubeconfigCommandBlocking(
azPath,
[
'connectedk8s',
'proxy',
'-n',
clusterName,
'-g',
resourceGroupName,
'-f',
KUBECONFIG_LOCATION
],
KUBECONFIG_LOCATION
)
case undefined:
core.warning('Defaulting to kubeconfig method')
case Method.KUBECONFIG:
default:
throw Error('Kubeconfig method not supported for Arc cluster')
}
}