Skip to content

Commit

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

### Global installation

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

### Register plugin to be accessible by fbl

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

```yaml
requires:
fbl: '>=1.8.0'
fbl: '>=1.12.0 <2.0.0'
plugins:
'@fbl-plguins/k8s-helm': '>=1.0.1'
'@fbl-plugins/k8s-helm': '>=1.1.1'

pipeline:
# your flow goes here
```

## Action Handlers

- [helm update --install](./docs/UpdateOrInstall.md)
- [helm delete](./docs/Delete.md)
- [helm test](./docs/Test.md)
- [helm update --install](./docs/UpdateOrInstall.md)
- [helm repo add](./docs/RepoAdd.md)
5 changes: 3 additions & 2 deletions docs/Delete.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pipeline:

# [optional] List of extra arguments to append to the command.
# Refer to `helm help delete` for all available options
extra: --tiller-namespace
kube-system
extra:
- --tiller-namespace
- kube-system
```
43 changes: 43 additions & 0 deletions docs/RepoAdd.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Add Helm Repository

Action handler that allows to add new repository.

**ID:** `com.fireblink.fbl.plugins.k8s.helm.repo.add`

**Aliases:**

- `fbl.plugins.k8s.helm.repo.add`
- `k8s.helm.repo.add`
- `helm.repo.add`

## Syntax:

```yaml
pipeline:
helm.repo.add:
# [required] Name of the repository to add
name: repo_name

# [required] URL address of the repository
url: http://localhost:8888

# [optional] Enable verbose output.
# Default value: false
debug: true

# [optional] Raise error if repo is already registered
# Default value: false
noUpdate: true

# [optional] Chart repository username
username: root

# [optional] Chart repository password
password: toor

# [optional] Verify certificates of HTTPS-enabled servers using this CA bundle
ca: server.ca

# [optional] Identify HTTPS client using this SSL certificate file
cert: server.cert
```
5 changes: 3 additions & 2 deletions docs/Test.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pipeline:

# [optional] List of extra arguments to append to the command.
# Refer to `helm help delete` for all available options
extra: --tiller-namespace
kube-system
extra:
- --tiller-namespace
- kube-system
```
35 changes: 18 additions & 17 deletions docs/UpdateOrInstall.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ Action handler that allows to install new / update existing helm chart release.
**ID:** `com.fireblink.fbl.plugins.k8s.helm.upgrade`

**Aliases:**
* `fbl.plugins.k8s.helm.upgrade`
* `fbl.plugins.k8s.helm.install`
* `k8s.helm.upgrade`
* `k8s.helm.install`
* `helm.upgrade`
* `helm.install`

- `fbl.plugins.k8s.helm.upgrade`
- `fbl.plugins.k8s.helm.install`
- `k8s.helm.upgrade`
- `k8s.helm.install`
- `helm.upgrade`
- `helm.install`

## Syntax:

Expand All @@ -23,7 +24,7 @@ pipeline:
# [required} Chart name or local path (absolute or relative to the flow file).
chart: 'repo/chart'

# [optional] Specify the exact chart version to use. If this is not specified,
# [optional] Specify the exact chart version to use. If this is not specified,
# the latest version is used (works only for charts referenced from repositories).
version: '1.0.0.'

Expand All @@ -32,28 +33,28 @@ pipeline:
namespace: 'main'

# [optional] extra variables to override / exted default helm chart values
variables:
variables:
# [optional] list of files to use as sources for values
files:
files:
- assets/chart_values.yml

# [optional] list of files that are EJS templates (both FBL global and local delimiters are supported)
# Note: template have a priority over `files`
templates:
templates:
- assets/chart_values.tpl.yml

# [optional] define values inline the flow
# Note: inlive values have a priority over both `files` and `templates`
inline:
inline:
ImagePullPolicy: Always

# [optional] Force resource update through delete/recreate if needed.
# Default value: false
force: true

# [optional] If set, will wait until all Pods, PVCs, Services, and minimum number of Pods
# of a Deployment are in a ready state before marking the release as successful. It will
# wait for as long as `timeout`.
# [optional] If set, will wait until all Pods, PVCs, Services, and minimum number of Pods
# of a Deployment are in a ready state before marking the release as successful. It will
# wait for as long as `timeout`.
# Default value: false
wait: true

Expand All @@ -68,6 +69,6 @@ pipeline:

# [optional] List of extra arguments to append to the command.
# Refer to `helm help upgrade` for all available options
extra:
--dry-run
```
extra:
- --dry-run
```
9 changes: 7 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IPlugin } from 'fbl';
import { UpgradeOrInstallActionHandler, DeleteActionHandler, TestActionHandler } from './src/handlers';
import * as handlers from './src/handlers';

const packageJson = require('../package.json');

Expand All @@ -19,7 +19,12 @@ module.exports = <IPlugin>{

reporters: [],

actionHandlers: [new DeleteActionHandler(), new TestActionHandler(), new UpgradeOrInstallActionHandler()],
actionHandlers: [
new handlers.DeleteActionHandler(),
new handlers.TestActionHandler(),
new handlers.UpgradeOrInstallActionHandler(),
new handlers.AddRepoActionHandler(),
],

templateUtils: [],
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fbl-plugins/k8s-helm",
"version": "1.1.0",
"version": "1.1.1",
"description": "FBL plugin for K8s Helm CLI",
"main": "dist/index.js",
"scripts": {
Expand Down
1 change: 1 addition & 0 deletions src/handlers/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './repo';
export * from './DeleteActionHandler';
export * from './TestActionHandler';
export * from './UpgradeOrInstallActionHandler';
36 changes: 36 additions & 0 deletions src/handlers/repo/AddRepoActionHandler.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 { AddRepoActionProcessor } from '../../processors';

export class AddRepoActionHandler extends ActionHandler {
private static metadata = <IActionHandlerMetadata>{
id: 'com.fireblink.fbl.plugins.k8s.helm.repo.add',
aliases: ['fbl.plugins.k8s.helm.repo.add', 'k8s.helm.repo.add', 'helm.repo.add'],
};

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

/**
* @inheritdoc
*/
getProcessor(
options: any,
context: IContext,
snapshot: ActionSnapshot,
parameters: IDelegatedParameters,
): ActionProcessor {
return new AddRepoActionProcessor(options, context, snapshot, parameters);
}
}
1 change: 1 addition & 0 deletions src/handlers/repo/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './AddRepoActionHandler';
20 changes: 19 additions & 1 deletion src/processors/BaseActionProcessor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ActionProcessor, ChildProcessService } from 'fbl';
import { ActionProcessor, ChildProcessService, FSUtil } from 'fbl';
import Container from 'typedi';

export abstract class BaseActionProcessor extends ActionProcessor {
Expand Down Expand Up @@ -68,6 +68,15 @@ export abstract class BaseActionProcessor extends ActionProcessor {
}
}

/**
* Resolve path value and push it to args if provided
*/
protected pushPathValue(args: string[], name: string, value: any): void {
if (value !== undefined) {
args.push(name, FSUtil.getAbsolutePath(value.toString(), this.snapshot.wd));
}
}

/**
* Push argument only if value is true
*/
Expand All @@ -76,4 +85,13 @@ export abstract class BaseActionProcessor extends ActionProcessor {
args.push(name);
}
}

/**
* Push all values to args if provided
*/
protected pushAll(args: string[], values?: string[]): void {
if (values) {
args.push(...values);
}
}
}
6 changes: 2 additions & 4 deletions src/processors/DeleteActionProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class DeleteActionProcessor extends BaseActionProcessor {
extra: Joi.array().items(Joi.string()),
})
.required()
.options({ abortEarly: true });
.options({ abortEarly: true, allowUnknown: false });

/**
* @inheritdoc
Expand Down Expand Up @@ -49,9 +49,7 @@ export class DeleteActionProcessor extends BaseActionProcessor {
this.pushWithValue(args, '--timeout', this.options.timeout);
this.pushWithoutValue(args, '--debug', this.options.debug);

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

args.push(this.options.release);

Expand Down
6 changes: 2 additions & 4 deletions src/processors/TestActionProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class TestActionProcessor extends BaseActionProcessor {
extra: Joi.array().items(Joi.string()),
})
.required()
.options({ abortEarly: true });
.options({ abortEarly: true, allowUnknown: false });

/**
* @inheritdoc
Expand Down Expand Up @@ -49,9 +49,7 @@ export class TestActionProcessor extends BaseActionProcessor {
this.pushWithValue(args, '--timeout', this.options.timeout);
this.pushWithoutValue(args, '--debug', this.options.debug);

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

args.push(this.options.release);

Expand Down
12 changes: 5 additions & 7 deletions src/processors/UpgradeOrInstallActionProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class UpgradeOrInstallActionProcessor extends BaseActionProcessor {
extra: Joi.array().items(Joi.string()),
})
.required()
.options({ abortEarly: true });
.options({ abortEarly: true, allowUnknown: false });

/**
* @inheritdoc
Expand Down Expand Up @@ -94,7 +94,7 @@ export class UpgradeOrInstallActionProcessor extends BaseActionProcessor {
if (this.options.variables) {
if (this.options.variables.files) {
this.options.variables.files.forEach((f: string) => {
args.push('-f', FSUtil.getAbsolutePath(f, this.snapshot.wd));
this.pushPathValue(args, '-f', f);
});
}

Expand Down Expand Up @@ -129,21 +129,19 @@ export class UpgradeOrInstallActionProcessor extends BaseActionProcessor {

const filePath = await tempPathsRegistry.createTempFile();
await writeFileAsync(filePath, dump(fileContentObject), 'utf8');
args.push('-f', filePath);
this.pushPathValue(args, '-f', filePath);
}
}

if (this.options.variables.inline) {
const tmpFile = await tempPathsRegistry.createTempFile(false, '.yml');
const yml = dump(this.options.variables.inline);
await writeFileAsync(tmpFile, yml, 'utf8');
args.push('-f', tmpFile);
this.pushPathValue(args, '-f', tmpFile);
}
}

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

args.push(this.options.release);

Expand Down
1 change: 1 addition & 0 deletions src/processors/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './repo';
export * from './DeleteActionProcessor';
export * from './TestActionProcessor';
export * from './UpgradeOrInstallActionProcessor';
Loading

0 comments on commit a01390d

Please sign in to comment.