Skip to content

Commit cccd4dc

Browse files
committed
Use Configs.addResults() instead of throwing exceptions.
1 parent 661c289 commit cccd4dc

30 files changed

+540
-637
lines changed

ts/demo-functions/package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ts/demo-functions/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"dependencies": {
2525
"glob": "^7.1.3",
2626
"js-yaml": "^3.13.1",
27-
"kpt-functions": "^0.13.0"
27+
"kpt-functions": "^0.14.0-rc.1"
2828
},
2929
"devDependencies": {
3030
"create-kpt-functions": "^0.16.0",

ts/demo-functions/src/expand_team_test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,17 @@ import { expandTeamCr } from './expand_team_cr';
2323
const RUNNER = new TestRunner(expandTeamCr);
2424

2525
describe(expandTeamCr.name, () => {
26-
it('does nothing to empty repos', RUNNER.assertCallback());
26+
it(
27+
'does nothing to empty repos',
28+
RUNNER.assertCallback(undefined, 'unchanged')
29+
);
2730

2831
it(
2932
'does nothing to non-Team objects',
30-
RUNNER.assertCallback(new Configs([Namespace.named('backend')]))
33+
RUNNER.assertCallback(
34+
new Configs([Namespace.named('backend')]),
35+
'unchanged'
36+
)
3137
);
3238

3339
it(

ts/demo-functions/src/label_namespace_test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const FUNC_CONFIG: ConfigMap = new ConfigMap({
3030
describe('labelNamespace', () => {
3131
it(
3232
'empty input ok',
33-
RUNNER.assertCallback(new Configs(undefined, FUNC_CONFIG))
33+
RUNNER.assertCallback(new Configs(undefined, FUNC_CONFIG), 'unchanged')
3434
);
3535

3636
it(

ts/demo-functions/src/mutate_psp_test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { mutatePsp } from './mutate_psp';
2121
const RUNNER = new TestRunner(mutatePsp);
2222

2323
describe('mutatePsp', () => {
24-
it('empty configs is noop', RUNNER.assertCallback());
24+
it('empty configs is noop', RUNNER.assertCallback(undefined, 'unchanged'));
2525

2626
it(
2727
'modifies PSP with allowPrivilegeEscalation = true to false',
@@ -30,7 +30,7 @@ describe('mutatePsp', () => {
3030

3131
it(
3232
'leaves PSP with allowPrivilegeEscalation = false alone',
33-
RUNNER.assertCallback(new Configs([psp(false)]))
33+
RUNNER.assertCallback(new Configs([psp(false)]), 'unchanged')
3434
);
3535
});
3636

ts/demo-functions/src/no_op_test.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,10 @@ import { Role } from './gen/io.k8s.api.rbac.v1';
2121
const RUNNER = new TestRunner(noOp);
2222

2323
describe('noOp', () => {
24-
it('empty', RUNNER.assertCallback());
24+
it('empty passes', RUNNER.assertCallback(undefined, 'unchanged'));
2525

2626
it(
2727
'pass through',
28-
RUNNER.assertCallback(
29-
new Configs([Role.named('alice')]),
30-
new Configs([Role.named('alice')])
31-
)
28+
RUNNER.assertCallback(new Configs([Role.named('alice')]), 'unchanged')
3229
);
3330
});

ts/demo-functions/src/read_yaml.ts

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,22 @@ import { safeLoadAll } from 'js-yaml';
2020
import * as path from 'path';
2121
import {
2222
Configs,
23-
ConfigError,
24-
MultiConfigError,
2523
isKubernetesObject,
26-
ConfigFileError,
2724
addAnnotation,
25+
configFileResult,
2826
SOURCE_PATH_ANNOTATION,
2927
SOURCE_INDEX_ANNOTATION,
28+
Result,
3029
} from 'kpt-functions';
3130

3231
export const SOURCE_DIR = 'source_dir';
33-
export const FILTER_IVNALID = 'filter_invalid';
32+
export const FILTER_INVALID = 'filter_invalid';
3433

3534
export async function readYaml(configs: Configs) {
3635
// Get the parameters.
3736
const sourceDir = configs.getFunctionConfigValueOrThrow(SOURCE_DIR);
3837
const ignoreInvalid =
39-
configs.getFunctionConfigValue(FILTER_IVNALID) === 'true';
38+
configs.getFunctionConfigValue(FILTER_INVALID) === 'true';
4039

4140
// Discard any input objects since this is a source function.
4241
configs.deleteAll();
@@ -45,17 +44,10 @@ export async function readYaml(configs: Configs) {
4544
const files = glob.sync(sourceDir + '/**/*.+(yaml|yml)');
4645

4746
// Parse each file and convert it to a KubernetesObject.
48-
const errors: ConfigError[] = files
47+
files
4948
.map(f => parseFile(configs, sourceDir, f, ignoreInvalid))
50-
.filter(err => err !== undefined)
51-
.map(err => err as ConfigError);
52-
53-
if (errors.length) {
54-
throw new MultiConfigError(
55-
`Found files containing invalid objects. To filter invalid objects set ${FILTER_IVNALID} to 'true'.`,
56-
errors
57-
);
58-
}
49+
.filter(result => result !== undefined)
50+
.forEach(result => configs.addResults(result as Result));
5951
}
6052

6153
readYaml.usage = `
@@ -64,7 +56,7 @@ Reads a directory of kubernetes YAML configs recursively.
6456
Configured using a ConfigMap with the following keys:
6557
6658
${SOURCE_DIR}: Path to the config directory to read.
67-
${FILTER_IVNALID}: [Optional] If 'true', ignores invalid Kubernetes objects instead of failing.
59+
${FILTER_INVALID}: [Optional] If 'true', ignores invalid Kubernetes objects instead of failing.
6860
6961
Example:
7062
@@ -81,7 +73,7 @@ function parseFile(
8173
sourceDir: string,
8274
file: string,
8375
ignoreInvalid: boolean
84-
): ConfigError | undefined {
76+
): Result | undefined {
8577
const contents = readFileOrThrow(file);
8678
let objects = safeLoadAll(contents);
8779

@@ -91,7 +83,7 @@ function parseFile(
9183
if (ignoreInvalid) {
9284
objects = objects.filter(o => isKubernetesObject(o));
9385
} else {
94-
return new ConfigFileError(
86+
return configFileResult(
9587
`File contains invalid Kubernetes objects '${JSON.stringify(
9688
invalidObjects
9789
)}'`,

ts/demo-functions/src/read_yaml_test.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,19 @@ import {
2222
Configs,
2323
readConfigs,
2424
FileFormat,
25-
MultiConfigError,
25+
configFileResult,
2626
} from 'kpt-functions';
2727

2828
const RUNNER = new TestRunner(readYaml);
2929

3030
describe('readYaml', () => {
3131
const functionConfig = ConfigMap.named('config');
32-
functionConfig.data = {};
3332

3433
it('works on empty dir', async () => {
3534
const sourceDir = path.resolve(__dirname, '../test-data/source/empty');
36-
functionConfig.data![SOURCE_DIR] = sourceDir;
35+
functionConfig.data = {
36+
[SOURCE_DIR]: sourceDir,
37+
};
3738
const configs = new Configs(undefined, functionConfig);
3839

3940
await readYaml(configs);
@@ -51,17 +52,27 @@ describe('readYaml', () => {
5152
expectedIntermediateFile,
5253
FileFormat.YAML
5354
);
54-
functionConfig.data![SOURCE_DIR] = sourceDir;
55+
functionConfig.data = {
56+
[SOURCE_DIR]: sourceDir,
57+
};
5558
const actualConfigs = new Configs(undefined, functionConfig);
5659

5760
await RUNNER.assert(actualConfigs, expectedConfigs);
5861
});
5962

6063
it('fails for invalid KubernetesObjects', async () => {
6164
const sourceDir = path.resolve(__dirname, '../test-data/source/invalid');
62-
functionConfig.data![SOURCE_DIR] = sourceDir;
65+
functionConfig.data = {
66+
[SOURCE_DIR]: sourceDir,
67+
};
6368
const actualConfigs = new Configs(undefined, functionConfig);
69+
const expectedConfigs = new Configs(undefined, functionConfig, [
70+
configFileResult(
71+
`File contains invalid Kubernetes objects '[{"kind":"I don't have apiVersion"}]'`,
72+
path.join(sourceDir, 'invalid.yaml')
73+
),
74+
]);
6475

65-
await RUNNER.assert(actualConfigs, undefined, MultiConfigError);
76+
await RUNNER.assert(actualConfigs, expectedConfigs);
6677
});
6778
});

ts/demo-functions/src/suggest_psp.ts

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,23 @@
1-
import {
2-
Configs,
3-
KubernetesObjectError,
4-
MultiConfigError,
5-
} from 'kpt-functions';
1+
import { Configs, kubernetesObjectResult } from 'kpt-functions';
62
import { isPodSecurityPolicy } from './gen/io.k8s.api.policy.v1beta1';
73

84
export async function suggestPsp(configs: Configs) {
95
// Iterate over all PodSecurityPolicy objects in the input and flag any
106
// that do not have 'allowPrivilegeEscalation' field set to true.
11-
const errors = configs
7+
const results = configs
128
.get(isPodSecurityPolicy)
139
.filter(psp => psp.spec && psp.spec.allowPrivilegeEscalation !== false)
14-
.map(
15-
psp =>
16-
new KubernetesObjectError(
17-
`Suggest explicitly disabling privilege escalation`,
18-
psp,
19-
{ path: `spec.allowPrivilegeEscalation`, suggestedValue: false },
20-
'warn',
21-
{ category: 'security' }
22-
)
10+
.map(psp =>
11+
kubernetesObjectResult(
12+
'Suggest explicitly disabling privilege escalation',
13+
psp,
14+
{ path: 'spec.allowPrivilegeEscalation', suggestedValue: false },
15+
'warn',
16+
{ category: 'security' }
17+
)
2318
);
2419

25-
if (errors.length) {
26-
throw new MultiConfigError(undefined, errors);
27-
}
20+
configs.addResults(...results);
2821
}
2922

3023
suggestPsp.usage = `

ts/demo-functions/src/suggest_psp_test.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,34 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { Configs, TestRunner, ConfigError } from 'kpt-functions';
17+
import { Configs, TestRunner, kubernetesObjectResult } from 'kpt-functions';
1818
import { PodSecurityPolicy } from './gen/io.k8s.api.policy.v1beta1';
1919
import { suggestPsp } from './suggest_psp';
2020

2121
const RUNNER = new TestRunner(suggestPsp);
2222

2323
describe('suggestPsp', () => {
24-
it('empty configs is noop', RUNNER.assertCallback());
24+
it('empty configs is noop', RUNNER.assertCallback(undefined, 'unchanged'));
2525

2626
it(
2727
'suggest PSP with allowPrivilegeEscalation = true to false',
2828
RUNNER.assertCallback(
2929
new Configs([psp(true)]),
30-
new Configs([psp(true)]),
31-
ConfigError
30+
new Configs([psp(true)], undefined, [
31+
kubernetesObjectResult(
32+
'Suggest explicitly disabling privilege escalation',
33+
psp(true),
34+
{ path: 'spec.allowPrivilegeEscalation', suggestedValue: false },
35+
'warn',
36+
{ category: 'security' }
37+
),
38+
])
3239
)
3340
);
3441

3542
it(
3643
'leaves PSP with allowPrivilegeEscalation = false alone',
37-
RUNNER.assertCallback(new Configs([psp(false)]))
44+
RUNNER.assertCallback(new Configs([psp(false)]), 'unchanged')
3845
);
3946
});
4047

0 commit comments

Comments
 (0)