Skip to content

Commit

Permalink
v2 new release (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Mar 21, 2022
1 parent 90b8d62 commit c7265b1
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 4 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ You must run [Azure/login](https://github.com/Azure/login) before this action.
<td><code>cluster-name</code><br/>(Required)</td>
<td>Name of the AKS cluster</td>
</tr>
<tr>
<td><code>subscription</code></td>
<td>Subscription tied to AKS cluster</td>
</tr>
<tr>
<td><code>admin</code></td>
<td>Get cluster admin credentials. Values: true or false</td>
</tr>
</table>

## Example
Expand Down
7 changes: 7 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ inputs:
cluster-name:
description: "AKS Cluster Name"
required: true
subscription:
description: "AKS Cluster Subscription"
required: false
admin:
description: "Get cluster admin credentials. Values: true or false"
default: false
required: false
branding:
color: "green"
runs:
Expand Down
12 changes: 10 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1680,6 +1680,9 @@ function run() {
// get inputs
const resourceGroupName = core.getInput("resource-group", { required: true });
const clusterName = core.getInput("cluster-name", { required: true });
const subscription = core.getInput("subscription") || "";
const adminInput = core.getInput("admin") || "";
const admin = adminInput.toLowerCase() === "true";
// check az tools
const azPath = yield io.which(AZ_TOOL_NAME, false);
if (!azPath)
Expand All @@ -1688,7 +1691,7 @@ function run() {
const runnerTempDirectory = process.env["RUNNER_TEMP"]; // use process.env until the core libs are updated
const kubeconfigPath = path.join(runnerTempDirectory, `kubeconfig_${Date.now()}`);
core.debug(`Writing kubeconfig to ${kubeconfigPath}`);
const exitCode = yield exec.exec(AZ_TOOL_NAME, [
const cmd = [
"aks",
"get-credentials",
"--resource-group",
Expand All @@ -1697,7 +1700,12 @@ function run() {
clusterName,
"-f",
kubeconfigPath,
]);
];
if (subscription)
cmd.push("--subscription", subscription);
if (admin)
cmd.push("--admin");
const exitCode = yield exec.exec(AZ_TOOL_NAME, cmd);
if (exitCode !== 0)
throw Error("Az cli exited with error code " + exitCode);
fs.chmodSync(kubeconfigPath, "600");
Expand Down
64 changes: 64 additions & 0 deletions src/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as fs from "fs";

const resourceGroup = "sample-rg";
const clusterName = "sample-cluster";
const subscription = "subscription-example";
const azPath = "path";
const runnerTemp = "temp";
const date = 1644272184664;
Expand Down Expand Up @@ -58,4 +59,67 @@ describe("Set context", () => {
expect(fs.chmodSync).toBeCalledWith(kubeconfigPath, "600");
expect(core.exportVariable).toBeCalledWith("KUBECONFIG", kubeconfigPath);
});

it("gets the kubeconfig and sets the context with subscription", async () => {
jest.spyOn(core, "getInput").mockImplementation((inputName, options) => {
if (inputName == "resource-group") return resourceGroup;
if (inputName == "cluster-name") return clusterName;
if (inputName == "subscription") return subscription;
});
jest.spyOn(io, "which").mockImplementation(async () => azPath);
process.env["RUNNER_TEMP"] = runnerTemp;
jest.spyOn(Date, "now").mockImplementation(() => date);
jest.spyOn(exec, "exec").mockImplementation(async () => 0);
jest.spyOn(fs, "chmodSync").mockImplementation();
jest.spyOn(core, "exportVariable").mockImplementation();
jest.spyOn(core, "debug").mockImplementation();

await expect(run()).resolves.not.toThrowError();
const kubeconfigPath = `${runnerTemp}/kubeconfig_${date}`;
expect(exec.exec).toBeCalledWith("az", [
"aks",
"get-credentials",
"--resource-group",
resourceGroup,
"--name",
clusterName,
"-f",
kubeconfigPath,
"--subscription",
subscription,
]);
expect(fs.chmodSync).toBeCalledWith(kubeconfigPath, "600");
expect(core.exportVariable).toBeCalledWith("KUBECONFIG", kubeconfigPath);
});

it("gets the kubeconfig and sets the context with admin", async () => {
jest.spyOn(core, "getInput").mockImplementation((inputName, options) => {
if (inputName == "resource-group") return resourceGroup;
if (inputName == "cluster-name") return clusterName;
if (inputName == "admin") return "true";
});
jest.spyOn(io, "which").mockImplementation(async () => azPath);
process.env["RUNNER_TEMP"] = runnerTemp;
jest.spyOn(Date, "now").mockImplementation(() => date);
jest.spyOn(exec, "exec").mockImplementation(async () => 0);
jest.spyOn(fs, "chmodSync").mockImplementation();
jest.spyOn(core, "exportVariable").mockImplementation();
jest.spyOn(core, "debug").mockImplementation();

await expect(run()).resolves.not.toThrowError();
const kubeconfigPath = `${runnerTemp}/kubeconfig_${date}`;
expect(exec.exec).toBeCalledWith("az", [
"aks",
"get-credentials",
"--resource-group",
resourceGroup,
"--name",
clusterName,
"-f",
kubeconfigPath,
"--admin",
]);
expect(fs.chmodSync).toBeCalledWith(kubeconfigPath, "600");
expect(core.exportVariable).toBeCalledWith("KUBECONFIG", kubeconfigPath);
});
});
11 changes: 9 additions & 2 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export async function run() {
// get inputs
const resourceGroupName = core.getInput("resource-group", { required: true });
const clusterName = core.getInput("cluster-name", { required: true });
const subscription = core.getInput("subscription") || "";
const adminInput = core.getInput("admin") || "";
const admin = adminInput.toLowerCase() === "true";

// check az tools
const azPath = await io.which(AZ_TOOL_NAME, false);
Expand All @@ -25,7 +28,7 @@ export async function run() {
`kubeconfig_${Date.now()}`
);
core.debug(`Writing kubeconfig to ${kubeconfigPath}`);
const exitCode = await exec.exec(AZ_TOOL_NAME, [
const cmd = [
"aks",
"get-credentials",
"--resource-group",
Expand All @@ -34,7 +37,11 @@ export async function run() {
clusterName,
"-f",
kubeconfigPath,
]);
];
if (subscription) cmd.push("--subscription", subscription);
if (admin) cmd.push("--admin");

const exitCode = await exec.exec(AZ_TOOL_NAME, cmd);
if (exitCode !== 0) throw Error("Az cli exited with error code " + exitCode);
fs.chmodSync(kubeconfigPath, "600");

Expand Down

0 comments on commit c7265b1

Please sign in to comment.