Skip to content

Commit

Permalink
feat: add helm v2 support
Browse files Browse the repository at this point in the history
  • Loading branch information
vlad-tkachenko committed Sep 6, 2020
1 parent a01f65d commit fc6be81
Show file tree
Hide file tree
Showing 29 changed files with 911 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM fireblink/kind:1.19.0
FROM fireblink/kind:1.19.0-2

RUN npm i -g http-server

Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ pipeline:

## Action Handlers

- [helm update --install](./docs/UpdateOrInstall.md)
- [helm delete](./docs/Delete.md)
- [helm test](./docs/Test.md)
- [helm repo add](./docs/RepoAdd.md)
- Helm V3 only: [helm update --install](./docs/UpdateOrInstall.md)
- Helm V2 only: [helm update --install](./docs/UpdateOrInstall_V2.md)
- Helm V2 and V3: [helm delete](./docs/Delete.md)
- Helm V2 and V3: [helm test](./docs/Test.md)
- Helm V2 and V3: [helm repo add](./docs/RepoAdd.md)
7 changes: 7 additions & 0 deletions docker-run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@ mkdir -p test/assets/helm/repo
helm package test/assets/helm/test --destination test/assets/helm/repo
helm repo index test/assets/helm/repo
http-server -p 9999 test/assets/helm/repo &

mkdir -p test/assets/helm/repo-v2
helm_v2 package test/assets/helm/test-v2 --destination test/assets/helm/repo-v2
helm_v2 repo index test/assets/helm/repo-v2
http-server -p 9988 test/assets/helm/repo-v2 &

sleep 2
helm repo add localhost http://localhost:9999
helm_v2 repo add localhost http://localhost:9988

echo "-> running tests"
cd /usr/app
Expand Down
4 changes: 4 additions & 0 deletions docs/Delete.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ Action handler that allows to delete the release.
```yaml
pipeline:
helm.del:
# [optional] Custom helm binary name/path
# Default value: helm
binary: 'helm_v3'

# [required] Release name.
release: 'string'

Expand Down
4 changes: 4 additions & 0 deletions docs/RepoAdd.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ Action handler that allows to add new repository.
```yaml
pipeline:
helm.repo.add:
# [optional] Custom helm binary name/path
# Default value: helm
binary: 'helm_v3'

# [required] Name of the repository to add
name: repo_name

Expand Down
4 changes: 4 additions & 0 deletions docs/Test.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ Action handler that allows to test the release.
```yaml
pipeline:
helm.test:
# [optional] Custom helm binary name/path
# Default value: helm
binary: 'helm_v3'

# [required] Release name.
release: 'string'

Expand Down
6 changes: 5 additions & 1 deletion docs/UpdateOrInstall.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ Action handler that allows to install new / update existing helm chart release.
```yaml
pipeline:
helm.install:
# [optional] Custom helm binary name/path
# Default value: helm
binary: 'helm_v3'

# [required] Release name.
release: 'string'

Expand All @@ -26,7 +30,7 @@ pipeline:

# [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.'
version: '1.0.0'

# [optional] K8s namespace into which to install the release.
# Default value: `default`
Expand Down
78 changes: 78 additions & 0 deletions docs/UpdateOrInstall_V2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Update or Install Helm Chart (for Helm V2)

Action handler that allows to install new / update existing helm chart release.

**ID:** `com.fireblink.fbl.plugins.k8s.helm.upgrade.v2`

**Aliases:**

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

## Syntax:

```yaml
pipeline:
helm.install:
# [optional] Custom helm binary name/path
# Default value: helm
binary: 'helm_v2'

# [required] Release name.
release: 'string'

# [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,
# the latest version is used (works only for charts referenced from repositories).
version: '1.0.0'

# [optional] K8s namespace into which to install the release.
# Default value: `default`
namespace: 'main'

# [optional] extra variables to override / exted default helm chart values
variables:
# [optional] list of files to use as sources for values
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:
- assets/chart_values.tpl.yml

# [optional] define values inline the flow
# Note: inlive values have a priority over both `files` and `templates`
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`.
# Default value: false
wait: true

# [optional] Еime in seconds to wait for any individual Kubernetes operation (like Jobs for hooks)
# Default value: 300 (5 minutes)
# Max allowed value: 3600 (1 hour)
timeout: 60

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

# [optional] List of extra arguments to append to the command.
# Refer to `helm help upgrade` for all available options
extra:
- --dry-run
```
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module.exports = <IPlugin>{
new handlers.DeleteActionHandler(),
new handlers.TestActionHandler(),
new handlers.UpgradeOrInstallActionHandler(),
new handlers.UpgradeOrInstallV2ActionHandler(),
new handlers.AddRepoActionHandler(),
],

Expand Down
43 changes: 43 additions & 0 deletions src/handlers/UpgradeOrInstallV2ActionHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {
ActionHandler,
ActionProcessor,
IActionHandlerMetadata,
IContext,
ActionSnapshot,
IDelegatedParameters,
} from 'fbl';
import { UpgradeOrInstallV2ActionProcessor } from '../processors';

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

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

/**
* @inheritdoc
*/
getProcessor(
options: any,
context: IContext,
snapshot: ActionSnapshot,
parameters: IDelegatedParameters,
): ActionProcessor {
return new UpgradeOrInstallV2ActionProcessor(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 @@ -2,3 +2,4 @@ export * from './repo';
export * from './DeleteActionHandler';
export * from './TestActionHandler';
export * from './UpgradeOrInstallActionHandler';
export * from './UpgradeOrInstallV2ActionHandler';
7 changes: 5 additions & 2 deletions src/processors/BaseActionProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,31 @@ import Container from 'typedi';
export abstract class BaseActionProcessor extends ActionProcessor {
/**
* Execute "helm" command
* @param binary
* @param {string[]} args
* @param {string} wd
* @return {Promise<IExecOutput>}
*/
async execHelmCommand(
args: string[],
debug: boolean,
binary?: string,
): Promise<{
code: number;
stdout: string;
stderr: string;
}> {
binary = binary || 'helm';
const childProcessService = Container.get(ChildProcessService);

const stdout: string[] = [];
const stderr: string[] = [];

if (debug) {
this.snapshot.log(`Running command "helm ${args.join(' ')}"`);
this.snapshot.log(`Running command "${binary} ${args.join(' ')}"`);
}

const code = await childProcessService.exec('helm', args, this.snapshot.wd, {
const code = await childProcessService.exec(binary, args, this.snapshot.wd, {
stdout: (chunk: any) => {
stdout.push(chunk.toString().trim());
},
Expand Down
5 changes: 4 additions & 1 deletion src/processors/DeleteActionProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { BaseActionProcessor } from './BaseActionProcessor';

export class DeleteActionProcessor extends BaseActionProcessor {
private static validationSchema = Joi.object({
// custom helm v2 binary name or path
binary: Joi.string().optional(),

// release name
release: Joi.string().required(),

Expand Down Expand Up @@ -37,7 +40,7 @@ export class DeleteActionProcessor extends BaseActionProcessor {
this.snapshot.log(`Deleting release ${this.options.release}`);

const args = this.prepareCLIArgs();
await this.execHelmCommand(args, this.options.debug);
await this.execHelmCommand(args, this.options.debug, this.options.binary);
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/processors/TestActionProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { BaseActionProcessor } from './BaseActionProcessor';

export class TestActionProcessor extends BaseActionProcessor {
private static validationSchema = Joi.object({
// custom helm v2 binary name or path
binary: Joi.string().optional(),

// release name
release: Joi.string().required(),

Expand Down Expand Up @@ -37,7 +40,7 @@ export class TestActionProcessor extends BaseActionProcessor {
this.snapshot.log(`Testing release ${this.options.release}`);

const args = this.prepareCLIArgs();
await this.execHelmCommand(args, this.options.debug);
await this.execHelmCommand(args, this.options.debug, this.options.binary);
}

/**
Expand Down
7 changes: 5 additions & 2 deletions src/processors/UpgradeOrInstallActionProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const existsAsync = promisify(exists);

export class UpgradeOrInstallActionProcessor extends BaseActionProcessor {
private static validationSchema = Joi.object({
// custom helm v2 binary name or path
binary: Joi.string().optional(),

// release name
release: Joi.string().required(),

Expand Down Expand Up @@ -66,13 +69,13 @@ export class UpgradeOrInstallActionProcessor extends BaseActionProcessor {
*/
async execute(): Promise<void> {
this.snapshot.log(
`Ugrading or installing release ${this.options.release} of helm chart ${this.options.chart}@${
`Upgrading or installing release ${this.options.release} of helm chart ${this.options.chart}@${
this.options.version || 'latest'
}`,
);

const args = await this.prepareCLIArgs();
await this.execHelmCommand(args, this.options.debug);
await this.execHelmCommand(args, this.options.debug, this.options.binary);
}

/**
Expand Down
Loading

0 comments on commit fc6be81

Please sign in to comment.