Skip to content

Commit

Permalink
feat: add new "kubectl wait" action handler
Browse files Browse the repository at this point in the history
  • Loading branch information
vlad-tkachenko committed Feb 20, 2020
1 parent b34e948 commit e4fd15b
Show file tree
Hide file tree
Showing 15 changed files with 436 additions and 122 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ This is the most recommended way. Create `package.json` next to your flow file w
},
"license": "UNLICENSED",
"dependencies": {
"@fbl-plguins/k8s-kubectl": "1.0.0",
"@fbl-plugins/k8s-kubectl": "1.0.0",
"fbl": "1.7.0"
}
}
Expand All @@ -59,18 +59,18 @@ After that you can use `yarn fbl <args>` to execute your flow or even register a

### Global installation

`npm i -g @fbl-plguins/k8s-kubectl`
`npm i -g @fbl-plugins/k8s-kubectl`

### Register plugin to be accessible by fbl

- via cli: `fbl -p @fbl-plguins/k8s-kubectl <args>`
- via cli: `fbl -p @fbl-plugins/k8s-kubectl <args>`
- via flow:

```yaml
requires:
fbl: '>=1.8.0 <2.0.0'
plugins:
'@fbl-plguins/k8s-kubectl': '>=1.1.0'
'@fbl-plugins/k8s-kubectl': '>=1.1.0'

pipeline:
# your flow goes here
Expand All @@ -84,3 +84,4 @@ pipeline:
- [kubectl create](docs/Create.md)
- [kubectl apply docker secret](docs/secrets/ApplyDockerSecret.md)
- [kubectl delete](docs/Delete.md)
- [kubectl wait](docs/Wait.md)
46 changes: 46 additions & 0 deletions docs/Wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Kubectl Wait

`kubectl wait` command wrapper.

**ID:** `com.fireblink.fbl.plugins.k8s.kubectl.wait`

**Aliases:**

- `fbl.plugins.k8s.kubectl.wait`
- `k8s.kubectl.wait`
- `kubectl.wait`

## Usage

```yaml
kubectl.wait:
# [required] K8s resource type name
resource: pod

# [required] Resource name
name: busybox1

# [required] condition
for:
# [optional] whether should wait for the resource to be deleted
# Note: either "delete" or "condition" should be specified, but not both at the same time
delete: true

# [optional] condition to waif for
# Note: either "delete" or "condition" should be specified, but not both at the same time
condition: Ready

# [optional] timeout to wait
timeout: '60s'

# [optional] K8s namespace.
# Default value: default
namespace: nondefault

# [optional] Enable verbose output.
debug: true

# [optional] Extra arguments to append to the command.
# Refer to `kubectl wait --help` for all available options.
extra: []
```
2 changes: 2 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
GetAllActionHandler,
ApplyDockerSecretActionHandler,
GetOneActionHandler,
WaitActionHandler,
} from './src/handlers';

const packageJson = require('../package.json');
Expand Down Expand Up @@ -33,6 +34,7 @@ module.exports = <IPlugin>{
new DeleteActionHandler(),
new GetAllActionHandler(),
new GetOneActionHandler(),
new WaitActionHandler(),
],

templateUtils: [],
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fbl-plugins/k8s-kubectl",
"version": "1.2.1",
"version": "1.2.2",
"description": "FBL plugin for K8s Kubectl CLI",
"main": "dist/index.js",
"scripts": {
Expand Down Expand Up @@ -56,7 +56,7 @@
"rimraf": "3.0.2",
"source-map-support": "0.5.16",
"ts-node": "8.6.2",
"tslint": "5.20.1",
"tslint": "6.0.0",
"tslint-config-prettier": "1.18.0",
"typescript": "3.7.5",
"yarn-check": "0.0.3"
Expand Down
36 changes: 36 additions & 0 deletions src/handlers/WaitActionHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {
ActionHandler,
ActionProcessor,
IActionHandlerMetadata,
IContext,
ActionSnapshot,
IDelegatedParameters,
} from 'fbl';
import { WaitActionProcessor } from '../processors';

export class WaitActionHandler extends ActionHandler {
private static metadata = <IActionHandlerMetadata>{
id: 'com.fireblink.fbl.plugins.k8s.kubectl.wait',
aliases: ['fbl.plugins.k8s.kubectl.wait', 'k8s.kubectl.wait', 'kubectl.wait'],
};

/* istanbul ignore next */
/**
* @inheritdoc
*/
getMetadata(): IActionHandlerMetadata {
return WaitActionHandler.metadata;
}

/**
* @inheritdoc
*/
getProcessor(
options: any,
context: IContext,
snapshot: ActionSnapshot,
parameters: IDelegatedParameters,
): ActionProcessor {
return new WaitActionProcessor(options, context, snapshot, parameters);
}
}
1 change: 1 addition & 0 deletions src/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './CreateActionHandler';
export * from './DeleteActionHandler';
export * from './GetAllActionHandler';
export * from './GetOneActionHandler';
export * from './WaitActionHandler';
2 changes: 1 addition & 1 deletion src/processors/ApplyActionProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class ApplyActionProcessor extends BaseActionProcessor {
})
.or('paths', 'inline')
.required()
.options({ abortEarly: true });
.options({ abortEarly: true, allowUnknown: false });

/**
* @inheritdoc
Expand Down
2 changes: 1 addition & 1 deletion src/processors/CreateActionProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class CreateActionProcessor extends BaseActionProcessor {
})
.or('paths', 'inline')
.required()
.options({ abortEarly: true });
.options({ abortEarly: true, allowUnknown: false });

/**
* @inheritdoc
Expand Down
2 changes: 1 addition & 1 deletion src/processors/DeleteActionProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class DeleteActionProcessor extends BaseActionProcessor {
.without('labels', 'names')
.or('names', 'paths', 'inline', 'labels', 'all')
.required()
.options({ abortEarly: true });
.options({ abortEarly: true, allowUnknown: false });

/**
* @inheritdoc
Expand Down
2 changes: 1 addition & 1 deletion src/processors/GetAllActionProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class GetAllActionProcessor extends BaseActionProcessor {
.without('labels', 'names')
.or('resources', 'names', 'paths', 'inline', 'labels')
.required()
.options({ abortEarly: true });
.options({ abortEarly: true, allowUnknown: false });

/**
* @inheritdoc
Expand Down
2 changes: 1 addition & 1 deletion src/processors/GetOneActionProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class GetOneActionProcessor extends BaseActionProcessor {
.without('labels', 'name')
.or('resource', 'name', 'path', 'inline', 'labels')
.required()
.options({ abortEarly: true });
.options({ abortEarly: true, allowUnknown: false });

/**
* @inheritdoc
Expand Down
81 changes: 81 additions & 0 deletions src/processors/WaitActionProcessor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import * as Joi from 'joi';
import { BaseActionProcessor } from './BaseActionProcessor';
import { K8sObjectJoiValidationSchema } from '../joi/K8sObjectJoiValidationSchema';
import { TempPathsRegistry, FBL_ASSIGN_TO_SCHEMA, FBL_PUSH_TO_SCHEMA, ContextUtil, FSUtil } from 'fbl';
import Container from 'typedi';
import { join, basename } from 'path';

export class WaitActionProcessor extends BaseActionProcessor {
private static validationSchema = Joi.object({
resource: Joi.string()
.min(1)
.required(),

name: Joi.string()
.min(1)
.required(),

for: Joi.object({
delete: Joi.boolean(),
condition: Joi.string().min(1),
})
.xor('delete', 'condition')
.required()
.options({ abortEarly: true, allowUnknown: false }),

// enable verbose output
debug: Joi.boolean(),

// Namespace
namespace: Joi.string(),

timeout: Joi.string().min(1),

// extra arguments to append to the command
// refer to `kubectl wait --help` for all available options
extra: Joi.array().items(Joi.string()),
})
.required()
.options({ abortEarly: true, allowUnknown: false });

/**
* @inheritdoc
*/
getValidationSchema(): Joi.SchemaLike | null {
return WaitActionProcessor.validationSchema;
}

/**
* @inheritdoc
*/
async execute(): Promise<void> {
const args = await this.prepareCLIArgs();
await this.execKubectlCommand(args, this.options.debug);
}

/**
* Prepare CLI args
*/
private async prepareCLIArgs(): Promise<string[]> {
const args: string[] = ['wait'];

this.pushWithValue(args, '--namespace', this.options.namespace);
this.pushWithValue(args, '--timeout', this.options.timeout);

if (this.options.extra) {
args.push(...this.options.extra);
}

if (this.options.for.delete) {
args.push('--for=delete');
}

if (this.options.for.condition) {
args.push(`--for=condition=${this.options.for.condition}`);
}

args.push(`${this.options.resource}/${this.options.name}`);

return args;
}
}
1 change: 1 addition & 0 deletions src/processors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './CreateActionProcessor';
export * from './DeleteActionProcessor';
export * from './GetAllActionProcessor';
export * from './GetOneActionProcessor';
export * from './WaitActionProcessor';
Loading

0 comments on commit e4fd15b

Please sign in to comment.