Skip to content

Commit

Permalink
feat: detect mode automatically (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
rangoo94 authored Oct 12, 2023
1 parent 2c765fd commit 6194d70
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 27 deletions.
7 changes: 1 addition & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ Then, pass the `organization` and `environment` IDs for the test, along with the
uses: kubeshop/setup-testkube@v1
with:
# Instance
mode: cloud
organization: tkcorg_0123456789abcdef
environment: tkcenv_fedcba9876543210
token: tkcapi_0123456789abcdef0123456789abcd
Expand All @@ -68,7 +67,6 @@ It will be probably unsafe to keep directly in the workflow's YAML configuration
uses: kubeshop/setup-testkube@v1
with:
# Instance
mode: cloud
organization: ${{ secrets.TkOrganization }}
environment: ${{ secrets.TkEnvironment }}
token: ${{ secrets.TkToken }}
Expand Down Expand Up @@ -102,7 +100,6 @@ steps:
# Setup Testkube
- uses: kubeshop/setup-testkube@v1
with:
mode: cloud
organization: ${{ secrets.TkOrganization }}
environment: ${{ secrets.TkEnvironment }}
token: ${{ secrets.TkToken }}
Expand All @@ -119,7 +116,6 @@ steps:
# Setup Testkube
- uses: kubeshop/setup-testkube@v1
with:
mode: cloud
organization: ${{ secrets.TkOrganization }}
environment: ${{ secrets.TkEnvironment }}
token: ${{ secrets.TkToken }}
Expand All @@ -131,15 +127,14 @@ steps:

## Inputs

Besides common inputs, there are some different for `kubectl` and `cloud` mode configuration.
Besides common inputs, there are some different for kubectl and Cloud connection.

### Common

| Required | Name | Description |
|:--------:|-------------------|------------------------------------------------------------------------------------------------------------------------------|
| ✗ | `channel` | Distribution channel to install the latest application from - one of `stable` or `beta` (default: `stable`) |
| ✗ | `version` | Static Testkube CLI version to force its installation instead of the latest |
| ✗ | `mode` | Configuration mode for CLI, either `kubectl` (default) or `cloud`. Additional params may be required, as per other sections. |

### Kubernetes (`kubectl`)

Expand Down
6 changes: 0 additions & 6 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ inputs:
description: "Specific Testkube CLI version to install. Defaults to latest."
required: false

# Configuration
mode:
description: "Testkube context - `kubectl` for kubectl configuration (default), `cloud` for connecting with Cloud/Enterprise."
required: false
default: kubectl

# kubectl configuration
namespace:
description: "Kubernetes namespace where Testkube is located"
Expand Down
17 changes: 10 additions & 7 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15231,18 +15231,21 @@ __nccwpck_require__.r(__webpack_exports__);
const params = {
version: (0,_actions_core__WEBPACK_IMPORTED_MODULE_4__.getInput)('version'),
channel: (0,_actions_core__WEBPACK_IMPORTED_MODULE_4__.getInput)('channel') || 'stable',
mode: (0,_actions_core__WEBPACK_IMPORTED_MODULE_4__.getInput)('mode') || 'kubectl',
namespace: (0,_actions_core__WEBPACK_IMPORTED_MODULE_4__.getInput)('namespace') || 'testkube',
url: (0,_actions_core__WEBPACK_IMPORTED_MODULE_4__.getInput)('url') || 'testkube.io',
organization: (0,_actions_core__WEBPACK_IMPORTED_MODULE_4__.getInput)('organization'),
environment: (0,_actions_core__WEBPACK_IMPORTED_MODULE_4__.getInput)('environment'),
token: (0,_actions_core__WEBPACK_IMPORTED_MODULE_4__.getInput)('token'),
};
// Check params
if (!['kubectl', 'cloud'].includes(params.mode)) {
throw new Error('Invalid `mode` passed - only "kubectl" or "cloud" is allowed.');
const mode = (params.organization || params.environment || params.token) ? 'cloud' : 'kubectl';
if (mode === 'cloud') {
process.stdout.write(`Detected mode: cloud connection.\n`);
}
else {
process.stdout.write(`Detected mode: kubectl connection. To use Cloud connection instead, provide your 'organization', 'environment' and 'token'.\n`);
}
if (params.mode === 'cloud') {
// Check params
if (mode === 'cloud') {
if (!params.organization || !params.environment || !params.token) {
throw new Error('You need to pass `organization`, `environment` and `token` for Cloud connection.');
}
Expand Down Expand Up @@ -15284,7 +15287,7 @@ if (!binaryDirPath) {
throw new Error('Could not find a writable path that is exposed in PATH to put the binary.');
}
// Detect if there is kubectl installed
if (params.mode === 'kubectl') {
if (mode === 'kubectl') {
const hasKubectl = await which__WEBPACK_IMPORTED_MODULE_6___default()('kubectl', { nothrow: true });
process.stdout.write(`kubectl: ${hasKubectl ? 'detected' : 'not available'}.\n`);
if (!hasKubectl) {
Expand Down Expand Up @@ -15337,7 +15340,7 @@ else {
process.stdout.write(`Linked CLI as ${binaryDirPath}/tk.\n`);
}
// Configure the Testkube context
const contextArgs = params.mode === 'kubectl'
const contextArgs = mode === 'kubectl'
? [
'--kubeconfig',
'--namespace', params.namespace,
Expand Down
18 changes: 10 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import which from 'which';
interface Params {
version?: string | null;
channel: string;
mode?: string | null;
namespace?: string | null;
url: string;
organization?: string | null;
Expand All @@ -21,19 +20,22 @@ interface Params {
const params: Params = {
version: getInput('version'),
channel: getInput('channel') || 'stable',
mode: getInput('mode') || 'kubectl',
namespace: getInput('namespace') || 'testkube',
url: getInput('url') || 'testkube.io',
organization: getInput('organization'),
environment: getInput('environment'),
token: getInput('token'),
};

// Check params
if (!['kubectl', 'cloud'].includes(params.mode!)) {
throw new Error('Invalid `mode` passed - only "kubectl" or "cloud" is allowed.');
const mode = (params.organization || params.environment || params.token) ? 'cloud' : 'kubectl';
if (mode === 'cloud') {
process.stdout.write(`Detected mode: cloud connection.\n`);
} else {
process.stdout.write(`Detected mode: kubectl connection. To use Cloud connection instead, provide your 'organization', 'environment' and 'token'.\n`);
}
if (params.mode === 'cloud') {

// Check params
if (mode === 'cloud') {
if (!params.organization || !params.environment || !params.token) {
throw new Error('You need to pass `organization`, `environment` and `token` for Cloud connection.');
}
Expand Down Expand Up @@ -79,7 +81,7 @@ if (!binaryDirPath) {
}

// Detect if there is kubectl installed
if (params.mode === 'kubectl') {
if (mode === 'kubectl') {
const hasKubectl = await which('kubectl', {nothrow: true});
process.stdout.write(`kubectl: ${hasKubectl ? 'detected' : 'not available'}.\n`);
if (!hasKubectl) {
Expand Down Expand Up @@ -137,7 +139,7 @@ if (await which('kubectl-testkube', {nothrow: true})) {
}

// Configure the Testkube context
const contextArgs = params.mode === 'kubectl'
const contextArgs = mode === 'kubectl'
? [
'--kubeconfig',
'--namespace', params.namespace!,
Expand Down

0 comments on commit 6194d70

Please sign in to comment.