Skip to content

Commit

Permalink
Merge pull request #37 from cloud-native-toolkit/feature/add-results-…
Browse files Browse the repository at this point in the history
…to-tasks

feat: Add build results to tasks
  • Loading branch information
nathanagood authored Feb 22, 2024
2 parents 829079f + 2b64358 commit 91fb12a
Show file tree
Hide file tree
Showing 13 changed files with 201 additions and 73 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/build.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions .github/workflows/release.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .github/workflows/upgrade-main.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions .projen/deps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions .projenrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ const project = new cdk8s.ConstructLibraryCdk8s({
defaultReleaseBranch: 'main',
author: 'Nathan Good',
authorAddress: 'nathan.good@ibm.com',
cdk8sVersion: '2.30.0',
jsiiVersion: '~5.0.0',
cdk8sVersion: '2.68.30',
jsiiVersion: '~5.2.0',
workflowNodeVersion: '18.x',
projenrcTs: true,
peerDeps: [
'cdk8s',
Expand Down
29 changes: 27 additions & 2 deletions API.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 31 additions & 3 deletions src/builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import * as fs from 'fs';
import { ApiObject, ApiObjectProps, Yaml } from 'cdk8s';
import { Construct } from 'constructs';
import { buildParam } from './common';
import { usingBuildParameter, usingResultsPath } from './common';
import {
Pipeline,
PipelineParam,
Expand Down Expand Up @@ -235,7 +235,7 @@ export class ParameterBuilder {
this._requiresPipelineParam = true;
this._name = pipelineParamName;
this._defaultValue = defaultValue;
this._value = buildParam(pipelineParamName);
this._value = usingBuildParameter(pipelineParamName);
return this;
}

Expand Down Expand Up @@ -308,6 +308,20 @@ class UrlScriptResolver implements ScriptResolver {
}
}

class UrlScriptToResultsResolver implements ScriptResolver {
readonly _resolver: ScriptResolver;
readonly _resultsName: string;

constructor(url: string, resultsName: string) {
this._resolver = new UrlScriptResolver(url);
this._resultsName = resultsName;
}

public scriptData(): string {
return [this._resolver.scriptData(), `tee ${usingResultsPath(this._resultsName)}`].join(' | ');
}
}

/**
* Gets the content from the static value provided.
*/
Expand Down Expand Up @@ -432,6 +446,20 @@ export class TaskStepBuilder {
return this;
}

/**
* If supplied, uses the content found at the given URL for the `script` value
* of the step and writes its output to the `results`. Use this as an
* alternative to "heredoc", which is embedding hard-coded shell or other
* scripts in the step.
*
* @param url
* @param resultsName
*/
public fromScriptUrlToResults(url: string, resultsName: string): TaskStepBuilder {
this._script = new UrlScriptToResultsResolver(url, resultsName);
return this;
}

/**
* If supplied, uses the cdk8s `ApiObject` supplied as the body of the
* `script` for the `Task`. This is most useful when used with `oc apply` or
Expand Down Expand Up @@ -641,7 +669,7 @@ export class TaskBuilder {
}

/**
* Allows you to add an result to the Task.
* Allows you to add a result to the Task.
*
* @see https://tekton.dev/docs/pipelines/tasks/#emitting-results
*
Expand Down
16 changes: 14 additions & 2 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function secretKeyRef(name: string, key: string): NameKeyPair {
* Convenience method for formatting the value of a working directory.
* @param workspace
*/
export function buildWorkingDir(workspace: string): string {
export function usingWorkspacePath(workspace: string): string {
return `$(workspaces.${workspace}.path)`;
}

Expand All @@ -43,6 +43,18 @@ export function buildWorkingDir(workspace: string): string {
* For example, if the parameter is `foo`, the result will be `$(params.foo)`.
* @param name The name of the parameter.
*/
export function buildParam(name: string): string {
export function usingBuildParameter(name: string): string {
return `$(params.${name})`;
}

/**
* Builds the correct string for building a reference to the file in which the
* result can be written during the execution of the Task. For example, if the
* name of the result is `foo`, this function will return `$(results.foo.path)`.
*
* @see https://tekton.dev/docs/pipelines/tasks/#emitting-results
* @param resultName The name of the result from the task.
*/
export function usingResultsPath(resultName: string): string {
return `$(results.${resultName}.path)`;
}
16 changes: 13 additions & 3 deletions test/__snapshots__/taskbuilder.test.ts.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/files/retrieve-secret.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ export AUTH_RESPONSE_JSON=$(curl -s -X POST \
export ACCESS_TOKEN=$(echo $AUTH_RESPONSE_JSON | grep -o '"access_token":"[^"]*' | grep -o '[^"]*$')
export SECRET_JSON=$(curl -s -X GET --location --header "Authorization: Bearer ${ACCESS_TOKEN}" --header "Accept: application/json" "$(params.SECRETS_MANAGER_ENDPOINT_URL)/api/v2/secrets/$(params.KEY_ID)")
export SECRET=$(echo $SECRET_JSON | grep -o '"payload":"[^"]*' | grep -o '[^"]*$')
printf "${SECRET}" | tee $(results.secret-value.path)
printf "${SECRET}"
20 changes: 10 additions & 10 deletions test/taskbuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as path from 'path';
import { Chart, Testing } from 'cdk8s';
import { ChartProps } from 'cdk8s/lib/chart';
import { Construct } from 'constructs';
import { buildParam, buildWorkingDir, secretKeyRef, TaskBuilder, TaskStepBuilder, valueFrom, WorkspaceBuilder, ParameterBuilder } from '../src';
import { usingBuildParameter, usingWorkspacePath, secretKeyRef, TaskBuilder, TaskStepBuilder, valueFrom, WorkspaceBuilder, ParameterBuilder } from '../src';

/**
* Using "ansible-runner" as the reference task that I want this test builder to
Expand Down Expand Up @@ -35,8 +35,8 @@ class TestBasicTaskBuild extends Chart {
.withName('run-playbook')
.withImage(imageName)
.withCommand(['entrypoint'])
.withArgs(['ansible-runner', 'run', buildParam('args'), buildParam('project-dir')])
.withWorkingDir(buildWorkingDir('runner-dir')))
.withArgs(['ansible-runner', 'run', usingBuildParameter('args'), usingBuildParameter('project-dir')])
.withWorkingDir(usingWorkspacePath('runner-dir')))
.buildTask();
}
}
Expand Down Expand Up @@ -124,10 +124,6 @@ class TestIBMCloudSecretsManagerGet extends Chart {
constructor(scope: Construct, id: string, props?: ChartProps) {
super(scope, id, props);

// Build the task, using the https://github.com/tektoncd/catalog/blob/main/task/pull-request/0.1/pull-request.yaml
// as the example, just like with the `task.test.ts`. At some point, it would
// be nice to compare the snapshots with each other just to make sure that the
// builder does build the exact same object as the longer, non-builder method.
new TaskBuilder(this, 'ibmcloud-secrets-manager-get')
.withName('ibmcloud-secrets-manager-get')
.withLabel('app.kubernetes.io/version', '0.1')
Expand All @@ -140,9 +136,13 @@ class TestIBMCloudSecretsManagerGet extends Chart {
.withStringParam(new ParameterBuilder('KEY_ID')
.withDefaultValue('968d7819-f2c5-7b67-c420-3c6bfd51521e')
.withDescription('An IBM Cloud Secrets Manager key ID'))
.withStringParam(new ParameterBuilder('SECRETS_MANAGER_ENDPOINT_URL')
.withDefaultValue('https://{instance_ID}.us-south.secrets-manager.appdomain.cloud')
.withDescription('An IBM Cloud Secrets Manager instance endpoint URL (https://cloud.ibm.com/apidocs/secrets-manager/secrets-manager-v2#endpoints)'))
.withResult('secret-value', ' A secret value retrieved using the provided KEY_ID')
.withStep(new TaskStepBuilder().withName('retrieve-key')
.withImage('quay.io/openshift/origin-cli:4.7')
.fromScriptUrl('test/files/retrieve-secret.sh'))
.fromScriptUrlToResults('test/files/retrieve-secret.sh', 'secret-value'))
.buildTask();
}
}
Expand Down Expand Up @@ -178,7 +178,7 @@ class TestPullRequestTaskBuild extends Chart {
.withName('pullrequest-init')
.withImage('gcr.io/tekton-releases/github.com/tektoncd/pipeline/cmd/pullrequest-init@sha256:69633ecd0e948f6462c61bb9e008b940a05b143ef51c67e6e4093278a23dac53')
.withCommand(['/ko-app/pullrequest-init'])
.withEnv('AUTH_TOKEN', valueFrom(secretKeyRef(buildParam('secret-key-ref'), 'token')))
.withEnv('AUTH_TOKEN', valueFrom(secretKeyRef(usingBuildParameter('secret-key-ref'), 'token')))
.withArgs([
'-url',
'$(params.url)',
Expand All @@ -203,7 +203,7 @@ describe('TaskBuilderTest', () => {
const results = Testing.synth(chart);
expect(results).toMatchSnapshot();
});
test('TaskBuilderIBMSecretGet', () => {
test('TestIBMCloudSecretsManagerGet', () => {
const app = Testing.app();
const chart = new TestIBMCloudSecretsManagerGet(app, 'test-get-secret');
const results = Testing.synth(chart);
Expand Down
Loading

0 comments on commit 91fb12a

Please sign in to comment.