mirror of
https://github.com/Azure/k8s-set-context.git
synced 2026-06-26 08:49:27 +08:00
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>
This commit is contained in:
+57
-55
@@ -1,50 +1,52 @@
|
||||
import * as core from "@actions/core";
|
||||
import * as jsyaml from "js-yaml";
|
||||
import { KubeConfig } from "@kubernetes/client-node";
|
||||
import { K8sSecret, parseK8sSecret } from "../types/k8sSecret";
|
||||
import { Method, parseMethod } from "../types/method";
|
||||
import * as core from '@actions/core'
|
||||
import * as jsyaml from 'js-yaml'
|
||||
import {KubeConfig} from '@kubernetes/client-node'
|
||||
import {K8sSecret, parseK8sSecret} from '../types/k8sSecret'
|
||||
import {Method, parseMethod} from '../types/method'
|
||||
|
||||
/**
|
||||
* Gets the kubeconfig based on provided method for a default Kubernetes cluster
|
||||
* @returns The kubeconfig
|
||||
*/
|
||||
export function getDefaultKubeconfig(): string {
|
||||
const method: Method | undefined = parseMethod(
|
||||
core.getInput("method", { required: true })
|
||||
);
|
||||
const method: Method | undefined = parseMethod(
|
||||
core.getInput('method', {required: true})
|
||||
)
|
||||
|
||||
switch (method) {
|
||||
case Method.SERVICE_ACCOUNT: {
|
||||
const clusterUrl = core.getInput("k8s-url", { required: true });
|
||||
core.debug(
|
||||
"Found clusterUrl. Creating kubeconfig using certificate and token"
|
||||
);
|
||||
switch (method) {
|
||||
case Method.SERVICE_ACCOUNT: {
|
||||
const clusterUrl = core.getInput('k8s-url', {required: true})
|
||||
core.debug(
|
||||
'Found clusterUrl. Creating kubeconfig using certificate and token'
|
||||
)
|
||||
|
||||
const k8sSecret: string = core.getInput("k8s-secret", {
|
||||
required: true,
|
||||
});
|
||||
const parsedK8sSecret: K8sSecret = parseK8sSecret(jsyaml.load(k8sSecret));
|
||||
const certAuth: string = parsedK8sSecret.data["ca.crt"];
|
||||
const token: string = Buffer.from(
|
||||
parsedK8sSecret.data.token,
|
||||
"base64"
|
||||
).toString();
|
||||
const k8sSecret: string = core.getInput('k8s-secret', {
|
||||
required: true
|
||||
})
|
||||
const parsedK8sSecret: K8sSecret = parseK8sSecret(
|
||||
jsyaml.load(k8sSecret)
|
||||
)
|
||||
const certAuth: string = parsedK8sSecret.data['ca.crt']
|
||||
const token: string = Buffer.from(
|
||||
parsedK8sSecret.data.token,
|
||||
'base64'
|
||||
).toString()
|
||||
|
||||
return createKubeconfig(certAuth, token, clusterUrl);
|
||||
}
|
||||
case Method.SERVICE_PRINCIPAL: {
|
||||
core.warning(
|
||||
"Service Principal method not supported for default cluster type"
|
||||
);
|
||||
}
|
||||
case undefined: {
|
||||
core.warning("Defaulting to kubeconfig method");
|
||||
}
|
||||
default: {
|
||||
core.debug("Setting context using kubeconfig");
|
||||
return core.getInput("kubeconfig", { required: true });
|
||||
}
|
||||
}
|
||||
return createKubeconfig(certAuth, token, clusterUrl)
|
||||
}
|
||||
case Method.SERVICE_PRINCIPAL: {
|
||||
core.warning(
|
||||
'Service Principal method not supported for default cluster type'
|
||||
)
|
||||
}
|
||||
case undefined: {
|
||||
core.warning('Defaulting to kubeconfig method')
|
||||
}
|
||||
default: {
|
||||
core.debug('Setting context using kubeconfig')
|
||||
return core.getInput('kubeconfig', {required: true})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -55,22 +57,22 @@ export function getDefaultKubeconfig(): string {
|
||||
* @returns The kubeconfig as a string
|
||||
*/
|
||||
export function createKubeconfig(
|
||||
certAuth: string,
|
||||
token: string,
|
||||
clusterUrl: string
|
||||
certAuth: string,
|
||||
token: string,
|
||||
clusterUrl: string
|
||||
): string {
|
||||
const kc = new KubeConfig();
|
||||
kc.loadFromClusterAndUser(
|
||||
{
|
||||
name: "default",
|
||||
server: clusterUrl,
|
||||
caData: certAuth,
|
||||
skipTLSVerify: false,
|
||||
},
|
||||
{
|
||||
name: "default-user",
|
||||
token,
|
||||
}
|
||||
);
|
||||
return kc.exportConfig();
|
||||
const kc = new KubeConfig()
|
||||
kc.loadFromClusterAndUser(
|
||||
{
|
||||
name: 'default',
|
||||
server: clusterUrl,
|
||||
caData: certAuth,
|
||||
skipTLSVerify: false
|
||||
},
|
||||
{
|
||||
name: 'default-user',
|
||||
token
|
||||
}
|
||||
)
|
||||
return kc.exportConfig()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user