Skip to content

Commit

Permalink
Merge pull request #20 from marcofranssen/add-plugin-install
Browse files Browse the repository at this point in the history
Add feature to install krew plugins
  • Loading branch information
marcofranssen authored Oct 7, 2022
2 parents cfacca9 + 3720d33 commit 3b268f0
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 7 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/test-kubectl-action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
matrix:
# os: [ubuntu-22.04, macos-12, windows-2022]
os: [ubuntu-22.04, macos-12]
version: [v1.24.4, latest, stable]
version: [v1.23.12, v1.24.6, stable, latest]
enablePlugins: [true, false]

runs-on: ${{ matrix.os }}
Expand Down Expand Up @@ -71,6 +71,7 @@ jobs:
with:
kubectlVersion: ${{ matrix.version }}
enablePlugins: ${{ matrix.enablePlugins }}
plugins: aws-auth grep

- name: Check output and installPath
run: |
Expand All @@ -80,7 +81,12 @@ jobs:
${{ matrix.enablePlugins }} && {
hash kubectl-krew 2>/dev/null || { echo >&2 "kubectl-krew can not be found in the PATH."; exit 1; }
echo "::notice::Installed krew ${KREW_VERSION} at $(which kubectl-krew)"
ls -ahl /$HOME/.krew/bin
echo "::notice::Installed following plugins $KREW_PLUGINS"
hash kubectl-aws_auth 2>/dev/null || { echo >&2 "aws-auth plugin can not be found in PATH."; exit 1; }
hash kubectl-grep 2>/dev/null || { echo >&2 "grep plugin can not be found in PATH."; exit 1; }
} || true
env:
KUBECTL_VERSION: ${{ steps.setup-kubectl.outputs.kubectl-version }}
KREW_VERSION: ${{ steps.setup-kubectl.outputs.krew-version }}
KREW_PLUGINS: ${{ steps.setup-kubectl.outputs.krew-plugins }}
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ steps:
- run: kubectl krew install aws-auth
```

### Install plugins

Install `krew` allong with `kubectl` and some plugins.

```yaml
steps:
- uses: marcofranssen/setup-kubectl@v0.1.0
with:
enable-plugins: true
plugins: aws-auth,grep
- run: echo ${{ steps.kubectl.output.krew-version }}
- run: echo ${{ fromJson(steps.kubectl.output.krew-plugins) }}
- run: kubectl aws-auth version
```

[kubectl]: https://kubernetes.io/docs/tasks/tools/install-kubectl-linux/ "The Kubernetes CLI"
[krew]: https://krew.sigs.k8s.io/ "Krew is the plugin manager for kubectl command-line tool."
Expand Down
7 changes: 7 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,18 @@ inputs:
Install krew to manage kubectl plugins (defaults to false).
required: false
default: 'false'
plugins:
description: >-
Install the given plugins (comma or space separated).
required: false
default: ''
outputs:
kubectl-version:
description: 'The kubectl version that was installed.'
krew-version:
description: 'The krew version that was installed.'
krew-plugins:
description: 'The list of installed plugins (json serialized object).'
runs:
using: 'node16'
main: 'dist/index.js'
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

44 changes: 42 additions & 2 deletions lib/krew.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as tc from "@actions/tool-cache";
import { mapOS, mapArch, download } from "./utils";
import { exec } from "child_process";

export async function setupKrew() {
export async function setupKrew(plugins: string[]) {
const osPlatform = platform();
const osArch = arch();
const p = mapOS(osPlatform);
Expand All @@ -27,8 +27,10 @@ export async function setupKrew() {
}

krewVersion = await installKrew(cachedPath, binary);

core.setOutput("krew-version", krewVersion);

const installedPlugins = await installPlugins(cachedPath, binary, plugins);
core.setOutput("krew-plugins", JSON.stringify(installedPlugins));
}

function installKrew(cachedPath: string, binary: string): Promise<string> {
Expand All @@ -53,3 +55,41 @@ function installKrew(cachedPath: string, binary: string): Promise<string> {
});
});
}

async function installPlugins(
cachedPath: string,
binary: string,
plugins: string[]
): Promise<Record<string, string>[]> {
const krewBin = `${homedir()}/.krew/bin`;
// parallel installs will not work, so we do have to await within the loop
const installed: Record<string, string>[] = [];
for (const plugin of plugins) {
installed.push(await installPlugin(krewBin, plugin));
}
return installed;
}

function installPlugin(
krewBin: string,
plugin: string
): Promise<Record<string, string>> {
return new Promise((resolve, reject) => {
core.info(`Attempting to install plugin: ${plugin}…`);
exec(`${krewBin}/kubectl-krew install ${plugin}`, (e) => {
if (e) {
return reject(e);
}

const pluginBin = `kubectl-${plugin.replace("-", "_")}`;
exec(`${krewBin}/${pluginBin} version`, (e, stdout) => {
if (e) {
resolve({ [plugin]: "unknown" });
}

core.debug(stdout);
resolve({ [plugin]: stdout });
});
});
});
}
9 changes: 7 additions & 2 deletions lib/setup-kubectl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { setupKrew } from "./krew";
export async function setupKubectl() {
try {
const kubectlVersion = await getVersion(core.getInput("kubectlVersion"));
const krewPlugins = getPlugins(core.getInput("plugins"));

core.debug(`Installing kubectl ${kubectlVersion}…`);

Expand Down Expand Up @@ -44,15 +45,19 @@ export async function setupKubectl() {
core.setOutput("kubectl-version", kubectlVersion);

if (core.getInput("enablePlugins")) {
await setupKrew();
await setupKrew(krewPlugins);
}
} catch (e) {
core.error(e as Error);
throw e;
}
}

async function getVersion(version: string) {
function getPlugins(plugins: string): string[] {
return plugins.split(/,|\s/).filter((p) => p);
}

async function getVersion(version: string): Promise<string> {
const semVerRegx =
/^v(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+)?$/;
switch (version) {
Expand Down

0 comments on commit 3b268f0

Please sign in to comment.