Skip to content

Commit c7b7e86

Browse files
andudoAndrey Udovenko
andauthored
Add set-project-id function. (#518)
* Add set-project-id function. * Addressed comments. * Update go functions Makefile. * Add processing of Kptfiles in sub-packages. * Seatch for Kptfile by kind. * Update set-project-id-simple example. * Fix golangci-lint error. * Add advanced example with sub-package. * Update test diffs. Co-authored-by: Andrey Udovenko <andu@google.com>
1 parent 3cd99b1 commit c7b7e86

File tree

25 files changed

+826
-0
lines changed

25 files changed

+826
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
testType: eval
2+
includeMetaResources: true
3+
image: gcr.io/kpt-fn/set-project-id:unstable
4+
args:
5+
project-id: foo
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
diff --git a/resources.yaml b/resources.yaml
2+
index 9e4fea2..ff21ea3 100644
3+
--- a/resources.yaml
4+
+++ b/resources.yaml
5+
@@ -19,6 +19,7 @@ metadata:
6+
namespace: config-control # kpt-set: ${namespace}
7+
annotations:
8+
cnrm.cloud.google.com/force-destroy: "false"
9+
+ cnrm.cloud.google.com/project-id: foo
10+
spec:
11+
storageClass: standard # kpt-set: ${storage-class}
12+
uniformBucketLevelAccess: true
13+
diff --git a/setters.yaml b/setters.yaml
14+
index 1702bac..06515a7 100644
15+
--- a/setters.yaml
16+
+++ b/setters.yaml
17+
@@ -5,3 +5,4 @@ metadata:
18+
data:
19+
namespace: config-control
20+
storage-class: standard
21+
+ project-id: foo
22+
diff --git a/subpkg/Kptfile b/subpkg/Kptfile
23+
index c96b2cd..2643351 100644
24+
--- a/subpkg/Kptfile
25+
+++ b/subpkg/Kptfile
26+
@@ -2,3 +2,8 @@ apiVersion: kpt.dev/v1
27+
kind: Kptfile
28+
metadata:
29+
name: subpkg
30+
+pipeline:
31+
+ mutators:
32+
+ - image: gcr.io/kpt-fn/apply-setters:v0.1
33+
+ configMap:
34+
+ project-id: foo
35+
diff --git a/subpkg/resources.yaml b/subpkg/resources.yaml
36+
index 84a6d6a..d568a8f 100644
37+
--- a/subpkg/resources.yaml
38+
+++ b/subpkg/resources.yaml
39+
@@ -3,5 +3,7 @@ kind: IAMServiceAccount
40+
metadata:
41+
name: iamserviceaccount-sample
42+
namespace: config-control # kpt-set: ${namespace}
43+
+ annotations:
44+
+ cnrm.cloud.google.com/project-id: foo
45+
spec:
46+
displayName: Example Service Account
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
skip:
2+
- Kptfile
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.expected
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
apiVersion: kpt.dev/v1
2+
kind: Kptfile
3+
metadata:
4+
name: test-blueprint
5+
pipeline:
6+
mutators:
7+
- image: gcr.io/kpt-fn/apply-setters:v0.1
8+
configPath: setters.yaml
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# set-project-id: Advanced Example
2+
3+
### Overview
4+
5+
This example shows how [`set-project-id`] function works on packages with
6+
sub-packages.
7+
8+
Running `set-project-id` function on the example packed will:
9+
10+
1. Set `project-id`
11+
[setter](https://catalog.kpt.dev/apply-setters/v0.1/?id=definitions).
12+
2. Add `cnrm.cloud.google.com/project-id` annotation on
13+
[Config Connector resources](https://cloud.google.com/config-connector/docs/reference/overview)
14+
that don't have it.
15+
16+
### Fetch the example package
17+
18+
Get the example package by running the following commands:
19+
20+
```shell
21+
kpt pkg get https://github.com/GoogleContainerTools/kpt-functions-catalog.git/examples/set-project-id-advanced
22+
```
23+
24+
### Function invocation
25+
26+
Invoke the function with the following command:
27+
28+
```shell
29+
kpt fn eval set-project-id-advanced --include-meta-resources --image gcr.io/kpt-fn/set-project-id:unstable -- 'project-id=foo'
30+
```
31+
32+
### Expected result
33+
34+
1. File setters.yaml will include `project-id: foo` setter.
35+
2. In resources.yaml `my-test-project-second-bucket` StorageBucket resource
36+
will include `cnrm.cloud.google.com/project-id: foo` annotation.
37+
3. Kptfile in subpkg folder will include `apply-setters` mutator with
38+
`project-id: foo` setter.
39+
4. In resources.yaml in subpkg folder `iamserviceaccount-sample`
40+
IAMServiceAccount resource will include `cnrm.cloud.google.com/project-id:
41+
foo` annotation.
42+
43+
[`set-project-id`]: https://catalog.kpt.dev/set-project-id/v0.1/
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
apiVersion: storage.cnrm.cloud.google.com/v1beta1
2+
kind: StorageBucket
3+
metadata:
4+
name: my-test-project-first-bucket # kpt-set: ${project-id}-first-bucket
5+
namespace: config-control # kpt-set: ${namespace}
6+
annotations:
7+
cnrm.cloud.google.com/force-destroy: "false"
8+
cnrm.cloud.google.com/project-id: my-test-project # kpt-set: ${project-id}
9+
spec:
10+
storageClass: standard # kpt-set: ${storage-class}
11+
uniformBucketLevelAccess: true
12+
versioning:
13+
enabled: false
14+
---
15+
apiVersion: storage.cnrm.cloud.google.com/v1beta1
16+
kind: StorageBucket
17+
metadata:
18+
name: my-test-project-second-bucket # kpt-set: ${project-id}-second-bucket
19+
namespace: config-control # kpt-set: ${namespace}
20+
annotations:
21+
cnrm.cloud.google.com/force-destroy: "false"
22+
spec:
23+
storageClass: standard # kpt-set: ${storage-class}
24+
uniformBucketLevelAccess: true
25+
versioning:
26+
enabled: false
27+
---
28+
apiVersion: v1
29+
kind: Namespace
30+
metadata:
31+
name: newnamespace
32+
---
33+
apiVersion: core.cnrm.cloud.google.com/v1beta1
34+
kind: ConfigConnectorContext
35+
metadata:
36+
name: configconnectorcontext.core.cnrm.cloud.google.com
37+
namespace: newnamespace
38+
spec:
39+
googleServiceAccount: new-namespace-sa@project-id.iam.gserviceaccount.com # kpt-set: new-namespace-sa@${project-id}.iam.gserviceaccount.com
40+
---
41+
apiVersion: v1
42+
kind: ConfigMap
43+
metadata:
44+
name: test-map
45+
data:
46+
some-key: some-value
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
name: setters
5+
data:
6+
namespace: config-control
7+
storage-class: standard
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
apiVersion: kpt.dev/v1
2+
kind: Kptfile
3+
metadata:
4+
name: subpkg
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
apiVersion: iam.cnrm.cloud.google.com/v1beta1
2+
kind: IAMServiceAccount
3+
metadata:
4+
name: iamserviceaccount-sample
5+
namespace: config-control # kpt-set: ${namespace}
6+
spec:
7+
displayName: Example Service Account

0 commit comments

Comments
 (0)