Skip to content

Commit a360dfe

Browse files
committed
Comment demo functions
Fixes #50
1 parent 2ce4294 commit a360dfe

File tree

5 files changed

+41
-19
lines changed

5 files changed

+41
-19
lines changed

ts/demo-functions/src/expand_team_cr.ts

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,37 @@ import { RoleBinding, Subject } from './gen/io.k8s.api.rbac.v1';
2222
const ENVIRONMENTS = ['dev', 'prod'];
2323

2424
export const expandTeamCr: KptFunc = (configs) => {
25+
// For each 'Team' custom resource in the input:
26+
// 1. Generate a per-enviroment Namespace.
27+
// 2. Generate RoleBindings in each Namespace.
2528
configs.get(isTeam).forEach((team) => {
2629
const name = team.metadata.name;
2730

2831
ENVIRONMENTS.forEach((suffix) => {
2932
const ns = `${name}-${suffix}`;
3033
configs.insert(Namespace.named(ns));
31-
configs.insert(...expandTeam(team, ns));
34+
configs.insert(...createRoleBindings(team, ns));
3235
});
3336
});
3437
};
3538

39+
function createRoleBindings(team: Team, namespace: string): RoleBinding[] {
40+
return (team.spec.roles || []).map((item) => {
41+
return new RoleBinding({
42+
metadata: {
43+
name: item.role,
44+
namespace,
45+
},
46+
subjects: roleSubjects(item),
47+
roleRef: {
48+
kind: 'ClusterRole',
49+
name: item.role,
50+
apiGroup: 'rbac.authorization.k8s.io',
51+
},
52+
});
53+
});
54+
}
55+
3656
function roleSubjects(item: Team.Spec.Item): Subject[] {
3757
const userSubjects: Subject[] = (item.users || []).map(
3858
(user) =>
@@ -51,23 +71,6 @@ function roleSubjects(item: Team.Spec.Item): Subject[] {
5171
return userSubjects.concat(groupSubjects);
5272
}
5373

54-
function expandTeam(team: Team, namespace: string): RoleBinding[] {
55-
return (team.spec.roles || []).map((item) => {
56-
return new RoleBinding({
57-
metadata: {
58-
name: item.role,
59-
namespace,
60-
},
61-
subjects: roleSubjects(item),
62-
roleRef: {
63-
kind: 'ClusterRole',
64-
name: item.role,
65-
apiGroup: 'rbac.authorization.k8s.io',
66-
},
67-
});
68-
});
69-
}
70-
7174
expandTeamCr.usage = `
7275
Generates per-environment Namespaces and RoleBindings from the 'Team' custom resource.
7376

ts/demo-functions/src/mutate_psp.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import { KptFunc } from '@googlecontainertools/kpt-functions';
1818
import { isPodSecurityPolicy } from './gen/io.k8s.api.policy.v1beta1';
1919

2020
export const mutatePsp: KptFunc = (configs) => {
21+
// Iterate over all PodSecurityPolicy objects in the input and if
22+
// the 'allowPrivilegeEscalation' field is not to 'false', set the field to false.
2123
configs
2224
.get(isPodSecurityPolicy)
2325
.filter((psp) => psp.spec && psp.spec.allowPrivilegeEscalation !== false)

ts/demo-functions/src/read_yaml.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,17 @@ export const SOURCE_DIR = 'source_dir';
2424
export const FILTER_IVNALID = 'filter_invalid';
2525

2626
export const readYaml: kpt.KptFunc = (configs) => {
27+
// Get the parameters.
2728
const sourceDir = configs.getFunctionConfigValueOrThrow(SOURCE_DIR);
2829
const ignoreInvalid = configs.getFunctionConfigValue(FILTER_IVNALID) === 'true';
2930

3031
// Discard any input objects since this is a source function.
3132
configs.deleteAll();
3233

34+
// Only read files with YAML extensions. Other file types are ignored.
3335
const files = glob.sync(sourceDir + '/**/*.+(yaml|yml)');
36+
37+
// Parse each file and convert it to a KubernetesObject.
3438
const errors: kpt.ConfigError[] = files
3539
.map((f) => parseFile(configs, sourceDir, f, ignoreInvalid))
3640
.filter((err) => err !== undefined)
@@ -72,6 +76,7 @@ function parseFile(
7276
const contents = readFileOrThrow(file);
7377
let objects = safeLoadAll(contents);
7478

79+
// Filter for objects that are not KubernetesObject. This is conditional on 'ignoreValid' parameter.
7580
const invalidObjects: object[] = objects.filter((o) => !kpt.isKubernetesObject(o));
7681
if (invalidObjects.length) {
7782
if (ignoreInvalid) {
@@ -84,10 +89,13 @@ function parseFile(
8489
}
8590
}
8691

92+
// Add the standard path and index annotations to preserve the filesystem hierarchy
93+
// and ordering within a file.
8794
objects.forEach((o, i) => {
8895
kpt.addAnnotation(o, kpt.SOURCE_PATH_ANNOTATION, path.relative(sourceDir, file));
8996
kpt.addAnnotation(o, kpt.SOURCE_INDEX_ANNOTATION, i.toString());
9097
});
98+
9199
configs.insert(...objects);
92100
return;
93101
}

ts/demo-functions/src/validate_rolebinding.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ import { isRoleBinding } from './gen/io.k8s.api.rbac.v1';
2424
export const SUBJECT_NAME = 'subject_name';
2525

2626
export const validateRolebinding: KptFunc = (configs) => {
27+
// Get the subject name parameter.
2728
const subjectName = configs.getFunctionConfigValueOrThrow(SUBJECT_NAME);
2829

30+
// Iterate over all RoleBinding objects in the input, and filter those that have a subject
31+
// we're looking for.
2932
let errors: KubernetesObjectError[] = configs
3033
.get(isRoleBinding)
3134
.filter((rb) => rb && rb.subjects && rb.subjects.find((s) => s.name === subjectName))

ts/demo-functions/src/write_yaml.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,22 @@ const YAML_STYLE: DumpOptions = {
3333
};
3434

3535
export const writeYaml: kpt.KptFunc = (configs) => {
36+
// Get the paramters.
3637
const sinkDir = configs.getFunctionConfigValueOrThrow(SINK_DIR);
3738
const overwrite = configs.getFunctionConfigValue(OVERWRITE) === 'true';
3839

40+
// If sink diretory is not empty, require 'overwrite' parameter to be set.
3941
const yamls = listYamlFiles(sinkDir);
4042
if (!overwrite && yamls.length > 0) {
4143
throw new Error(`sink dir contains YAML files and overwrite is not set to string 'true'.`);
4244
}
4345

4446
const filesToDelete = new Set(yamls);
4547

48+
// Group objects by the file path and create a multi-object file if required.
4649
configs.groupBy(buildSourcePath).forEach(([p, configsAtPath]) => {
50+
// Preserve the original filesystem hierarchy and object ordering using the annotations
51+
// set by the source function. Remove these annotations before writing files.
4752
const documents = configsAtPath
4853
.sort(compareSourceIndex)
4954
.map((config) => kpt.removeAnnotation(config, kpt.SOURCE_INDEX_ANNOTATION))
@@ -59,7 +64,6 @@ export const writeYaml: kpt.KptFunc = (configs) => {
5964

6065
if (fs.existsSync(file)) {
6166
filesToDelete.delete(file);
62-
// Doesn't handle large files well. Should compare buffered output.
6367
const currentContents = fs.readFileSync(file).toString();
6468
if (contents == currentContents) {
6569
// No changes to make.
@@ -70,6 +74,8 @@ export const writeYaml: kpt.KptFunc = (configs) => {
7074
fs.writeFileSync(file, contents, 'utf8');
7175
});
7276

77+
// Delete YAML files that are missing from the input.
78+
// Other file types are ignored.
7379
filesToDelete.forEach((file) => {
7480
fs.unlinkSync(file);
7581
});

0 commit comments

Comments
 (0)