Skip to content

Commit b797dee

Browse files
authored
Add annotate config demo function (#161)
* Add annotate config demo function * Use getAll instead of get(isKubernetesObject)
1 parent e712034 commit b797dee

File tree

4 files changed

+144
-0
lines changed

4 files changed

+144
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
FROM node:lts-alpine as builder
2+
3+
RUN mkdir -p /home/node/app && \
4+
chown -R node:node /home/node/app
5+
6+
USER node
7+
8+
WORKDIR /home/node/app
9+
10+
# Install dependencies and cache them.
11+
COPY --chown=node:node package*.json ./
12+
RUN npm ci --ignore-scripts
13+
14+
# Build the source.
15+
COPY --chown=node:node tsconfig.json .
16+
COPY --chown=node:node src src
17+
RUN npm run build && \
18+
npm prune --production && \
19+
rm -r src tsconfig.json
20+
21+
#############################################
22+
23+
FROM node:lts-alpine
24+
25+
# Run as non-root user as a best-practices:
26+
# https://github.com/nodejs/docker-node/blob/master/docs/BestPractices.md
27+
USER node
28+
29+
WORKDIR /home/node/app
30+
31+
COPY --from=builder /home/node/app /home/node/app
32+
33+
ENTRYPOINT ["node", "/home/node/app/dist/annotate_config_run.js"]
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { Configs, addAnnotation } from 'kpt-functions';
2+
3+
const ANNOTATION_NAME = 'annotation_name';
4+
const ANNOTATION_VALUE = 'annotation_value';
5+
6+
export async function annotateConfig(configs: Configs) {
7+
const annotationName = configs.getFunctionConfigValueOrThrow(ANNOTATION_NAME);
8+
const annotationValue = configs.getFunctionConfigValueOrThrow(
9+
ANNOTATION_VALUE
10+
);
11+
configs
12+
.getAll()
13+
.forEach((n) => addAnnotation(n, annotationName, annotationValue));
14+
}
15+
16+
annotateConfig.usage = `
17+
Adds an annotation to all configuration files.
18+
19+
Configured using a ConfigMap with the following keys:
20+
21+
${ANNOTATION_NAME}: Annotation name to add to configs.
22+
${ANNOTATION_VALUE}: Annotation value to add to configs.
23+
24+
Example:
25+
26+
To add an annotation 'org: sre-supported' to Namespaces:
27+
28+
apiVersion: v1
29+
kind: ConfigMap
30+
data:
31+
${ANNOTATION_NAME}: org
32+
${ANNOTATION_VALUE}: sre-supported
33+
metadata:
34+
name: my-config
35+
`;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { annotateConfig } from './annotate_config';
2+
import { run } from 'kpt-functions';
3+
4+
run(annotateConfig);
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { Configs, TestRunner } from 'kpt-functions';
2+
import { annotateConfig } from './annotate_config';
3+
import { ConfigMap, Namespace } from './gen/io.k8s.api.core.v1';
4+
import { FunctionConfigError } from 'kpt-functions';
5+
6+
const RUNNER = new TestRunner(annotateConfig);
7+
const TEST_NAMESPACE = 'testNamespace';
8+
const TEST_ANNOTATION_NAME = 'configsupport';
9+
const TEST_ANNOTATION_VALUE = 'ops-supported';
10+
const FUNC_CONFIG: ConfigMap = new ConfigMap({
11+
metadata: { name: 'config' },
12+
data: {
13+
annotation_name: TEST_ANNOTATION_NAME,
14+
annotation_value: TEST_ANNOTATION_VALUE,
15+
},
16+
});
17+
18+
describe('annotateConfig', () => {
19+
it(
20+
'empty input ok',
21+
RUNNER.assertCallback(new Configs(undefined, FUNC_CONFIG), 'unchanged')
22+
);
23+
24+
it(
25+
'requires functionConfig',
26+
RUNNER.assertCallback(undefined, undefined, FunctionConfigError)
27+
);
28+
29+
it('adds annotation namespace when metadata.annotations is undefined', async () => {
30+
const input = new Configs(undefined, FUNC_CONFIG);
31+
input.insert(Namespace.named(TEST_NAMESPACE));
32+
33+
const output = new Configs();
34+
output.insert(
35+
new Namespace({
36+
metadata: {
37+
name: TEST_NAMESPACE,
38+
annotations: { [TEST_ANNOTATION_NAME]: TEST_ANNOTATION_VALUE },
39+
},
40+
})
41+
);
42+
43+
await RUNNER.assert(input, output);
44+
});
45+
46+
it('adds annotation to namespace when metadata.annotations is defined', async () => {
47+
const input = new Configs(undefined, FUNC_CONFIG);
48+
input.insert(
49+
new Namespace({
50+
metadata: {
51+
name: TEST_NAMESPACE,
52+
annotations: { a: 'b' },
53+
},
54+
})
55+
);
56+
57+
const output = new Configs();
58+
output.insert(
59+
new Namespace({
60+
metadata: {
61+
name: TEST_NAMESPACE,
62+
annotations: {
63+
a: 'b',
64+
[TEST_ANNOTATION_NAME]: TEST_ANNOTATION_VALUE,
65+
},
66+
},
67+
})
68+
);
69+
70+
await RUNNER.assert(input, output);
71+
});
72+
});

0 commit comments

Comments
 (0)