Skip to content

Commit c27cecd

Browse files
author
Mengqi Yu
committed
add test
1 parent 4488cca commit c27cecd

File tree

5 files changed

+76
-0
lines changed

5 files changed

+76
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
diff --git a/app.yaml b/app.yaml
2+
index f821124..4cf1245 100644
3+
--- a/app.yaml
4+
+++ b/app.yaml
5+
@@ -2,6 +2,7 @@ apiVersion: v1
6+
kind: ConfigMap
7+
metadata:
8+
name: my-cm
9+
+ namespace: prod
10+
annotations:
11+
config.k8s.io/id: "1"
12+
data:
13+
@@ -11,6 +12,7 @@ apiVersion: foo.com/v1
14+
kind: Bar
15+
metadata:
16+
name: my-bar
17+
+ namespace: prod
18+
annotations:
19+
config.k8s.io/id: "2"
20+
spec:
21+
diff --git a/fn-config.yaml b/fn-config.yaml
22+
index 5b8d869..eddef45 100644
23+
--- a/fn-config.yaml
24+
+++ b/fn-config.yaml
25+
@@ -2,6 +2,7 @@ apiVersion: fn.kpt.dev/v1alpha1
26+
kind: StarlarkRun
27+
metadata:
28+
name: set-namespace-to-prod
29+
+ namespace: prod
30+
annotations:
31+
config.k8s.io/id: "3"
32+
source: |
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#! /bin/bash
2+
3+
set -euo pipefail
4+
5+
rm -rf new-dir
6+
7+
# We are not using eval here, since eval will also touch id annotation.
8+
# We want to ensure the starlark function doesn't touch id annotations.
9+
# fn sink doesn't allow us to write to existing directory, so we write a
10+
# different directory and then copy the files.
11+
kpt fn source --fn-config fn-config.yaml | docker run -i gcr.io/kpt-fn/starlark:unstable | docker run -i gcr.io/kpt-fn/format:v0.1 | kpt fn sink new-dir
12+
mv new-dir/* .
13+
rm -rf new-dir
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.expected
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
name: my-cm
5+
annotations:
6+
config.k8s.io/id: "1"
7+
data:
8+
some-key: some-value
9+
---
10+
apiVersion: foo.com/v1
11+
kind: Bar
12+
metadata:
13+
name: my-bar
14+
annotations:
15+
config.k8s.io/id: "2"
16+
spec:
17+
featureA: true
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apiVersion: fn.kpt.dev/v1alpha1
2+
kind: StarlarkRun
3+
metadata:
4+
name: set-namespace-to-prod
5+
annotations:
6+
config.k8s.io/id: "3"
7+
source: |
8+
# set the namespace on all resources
9+
def setnamespace(resources, namespace):
10+
for resource in resources:
11+
# mutate the resource
12+
resource["metadata"]["namespace"] = namespace
13+
setnamespace(ctx.resource_list["items"], "prod")

0 commit comments

Comments
 (0)