Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add set-project-id function. #518

Merged
merged 9 commits into from
Aug 10, 2021
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions examples/set-project-id-advanced/.expected/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
testType: eval
includeMetaResources: true
image: gcr.io/kpt-fn/set-project-id:unstable
args:
project-id: foo
47 changes: 47 additions & 0 deletions examples/set-project-id-advanced/.expected/diff.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
diff --git a/resources.yaml b/resources.yaml
index 9e4fea2..ff21ea3 100644
--- a/resources.yaml
+++ b/resources.yaml
@@ -19,6 +19,7 @@ metadata:
namespace: config-control # kpt-set: ${namespace}
annotations:
cnrm.cloud.google.com/force-destroy: "false"
+ cnrm.cloud.google.com/project-id: foo
spec:
storageClass: standard # kpt-set: ${storage-class}
uniformBucketLevelAccess: true
diff --git a/setters.yaml b/setters.yaml
index 1702bac..b0ef261 100644
--- a/setters.yaml
+++ b/setters.yaml
@@ -4,4 +4,5 @@ metadata:
name: setters
data:
namespace: config-control
+ project-id: foo
storage-class: standard
diff --git a/subpkg/Kptfile b/subpkg/Kptfile
index c96b2cd..9adb21b 100644
--- a/subpkg/Kptfile
+++ b/subpkg/Kptfile
@@ -2,3 +2,8 @@ apiVersion: kpt.dev/v1
kind: Kptfile
metadata:
name: subpkg
+pipeline:
+ mutators:
+ - image: gcr.io/kpt-fn/apply-setters:v0.1
+ configMap:
+ project-id: foo
diff --git a/subpkg/resources.yaml b/subpkg/resources.yaml
index 84a6d6a..d568a8f 100644
--- a/subpkg/resources.yaml
+++ b/subpkg/resources.yaml
@@ -3,5 +3,7 @@ kind: IAMServiceAccount
metadata:
name: iamserviceaccount-sample
namespace: config-control # kpt-set: ${namespace}
+ annotations:
+ cnrm.cloud.google.com/project-id: foo
spec:
displayName: Example Service Account
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
skip:
- Kptfile
1 change: 1 addition & 0 deletions examples/set-project-id-advanced/.krmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.expected
8 changes: 8 additions & 0 deletions examples/set-project-id-advanced/Kptfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: kpt.dev/v1
kind: Kptfile
metadata:
name: test-blueprint
pipeline:
mutators:
- image: gcr.io/kpt-fn/apply-setters:v0.1
configPath: setters.yaml
43 changes: 43 additions & 0 deletions examples/set-project-id-advanced/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# set-project-id: Advanced Example

### Overview

This example shows how [`set-project-id`] function works on packages with
sub-packages.

Running `set-project-id` function on the example packed will:

1. Set `project-id`
[setter](https://catalog.kpt.dev/apply-setters/v0.1/?id=definitions).
2. Add `cnrm.cloud.google.com/project-id` annotation on
[Config Connector resources](https://cloud.google.com/config-connector/docs/reference/overview)
that don't have it.

### Fetch the example package

Get the example package by running the following commands:

```shell
kpt pkg get https://github.com/GoogleContainerTools/kpt-functions-catalog.git/examples/set-project-id-advanced
```

### Function invocation

Invoke the function with the following command:

```shell
kpt fn eval set-project-id-advanced --include-meta-resources --image gcr.io/kpt-fn/set-project-id:unstable -- 'project-id=foo'
```

### Expected result

1. File setters.yaml will include `project-id: foo` setter.
2. In resources.yaml `my-test-project-second-bucket` StorageBucket resource
will include `cnrm.cloud.google.com/project-id: foo` annotation.
3. Kptfile in subpkg folder will include `apply-setters` mutator with
`project-id: foo` setter.
4. In resources.yaml in subpkg folder `iamserviceaccount-sample`
IAMServiceAccount resource will include `cnrm.cloud.google.com/project-id:
foo` annotation.

[`set-project-id`]: https://catalog.kpt.dev/set-project-id/v0.1/
46 changes: 46 additions & 0 deletions examples/set-project-id-advanced/resources.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
apiVersion: storage.cnrm.cloud.google.com/v1beta1
kind: StorageBucket
metadata:
name: my-test-project-first-bucket # kpt-set: ${project-id}-first-bucket
namespace: config-control # kpt-set: ${namespace}
annotations:
cnrm.cloud.google.com/force-destroy: "false"
cnrm.cloud.google.com/project-id: my-test-project # kpt-set: ${project-id}
spec:
storageClass: standard # kpt-set: ${storage-class}
uniformBucketLevelAccess: true
versioning:
enabled: false
---
apiVersion: storage.cnrm.cloud.google.com/v1beta1
kind: StorageBucket
metadata:
name: my-test-project-second-bucket # kpt-set: ${project-id}-second-bucket
namespace: config-control # kpt-set: ${namespace}
annotations:
cnrm.cloud.google.com/force-destroy: "false"
spec:
storageClass: standard # kpt-set: ${storage-class}
uniformBucketLevelAccess: true
versioning:
enabled: false
---
apiVersion: v1
kind: Namespace
metadata:
name: newnamespace
---
apiVersion: core.cnrm.cloud.google.com/v1beta1
kind: ConfigConnectorContext
metadata:
name: configconnectorcontext.core.cnrm.cloud.google.com
namespace: newnamespace
spec:
googleServiceAccount: new-namespace-sa@project-id.iam.gserviceaccount.com # kpt-set: new-namespace-sa@${project-id}.iam.gserviceaccount.com
---
apiVersion: v1
kind: ConfigMap
metadata:
name: test-map
data:
some-key: some-value
7 changes: 7 additions & 0 deletions examples/set-project-id-advanced/setters.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: setters
data:
namespace: config-control
storage-class: standard
4 changes: 4 additions & 0 deletions examples/set-project-id-advanced/subpkg/Kptfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: kpt.dev/v1
kind: Kptfile
metadata:
name: subpkg
7 changes: 7 additions & 0 deletions examples/set-project-id-advanced/subpkg/resources.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: iam.cnrm.cloud.google.com/v1beta1
kind: IAMServiceAccount
metadata:
name: iamserviceaccount-sample
namespace: config-control # kpt-set: ${namespace}
spec:
displayName: Example Service Account
5 changes: 5 additions & 0 deletions examples/set-project-id-simple/.expected/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
testType: eval
includeMetaResources: true
image: gcr.io/kpt-fn/set-project-id:unstable
args:
project-id: foo
22 changes: 22 additions & 0 deletions examples/set-project-id-simple/.expected/diff.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
diff --git a/resources.yaml b/resources.yaml
index 9e4fea2..ff21ea3 100644
--- a/resources.yaml
+++ b/resources.yaml
@@ -19,6 +19,7 @@ metadata:
namespace: config-control # kpt-set: ${namespace}
annotations:
cnrm.cloud.google.com/force-destroy: "false"
+ cnrm.cloud.google.com/project-id: foo
spec:
storageClass: standard # kpt-set: ${storage-class}
uniformBucketLevelAccess: true
diff --git a/setters.yaml b/setters.yaml
index 1702bac..b0ef261 100644
--- a/setters.yaml
+++ b/setters.yaml
@@ -4,4 +4,5 @@ metadata:
name: setters
data:
namespace: config-control
+ project-id: foo
storage-class: standard
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
skip:
- Kptfile
2 changes: 2 additions & 0 deletions examples/set-project-id-simple/.krmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.expected

8 changes: 8 additions & 0 deletions examples/set-project-id-simple/Kptfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: kpt.dev/v1
kind: Kptfile
metadata:
name: test-blueprint
pipeline:
mutators:
- image: gcr.io/kpt-fn/apply-setters:v0.1
configPath: setters.yaml
36 changes: 36 additions & 0 deletions examples/set-project-id-simple/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# set-project-id: Simple Example

### Overview

This example shows how [`set-project-id`] function works.

Running `set-project-id` function on the example packed will:

1. Set `project-id` [setter](https://catalog.kpt.dev/apply-setters/v0.1/?id=definitions).
2. Add `cnrm.cloud.google.com/project-id` annotation on
[Config Connector resources](https://cloud.google.com/config-connector/docs/reference/overview)
that don't have it.

### Fetch the example package

Get the example package by running the following commands:

```shell
kpt pkg get https://github.com/GoogleContainerTools/kpt-functions-catalog.git/examples/set-project-id-simple
```

### Function invocation

Invoke the function with the following command:

```shell
kpt fn eval set-project-id-simple --include-meta-resources --image gcr.io/kpt-fn/set-project-id:unstable -- 'project-id=foo'
```

### Expected result

1. File setters.yaml will include `project-id: foo` setter.
2. In resources.yaml `my-test-project-second-bucket` StorageBucket resource
will include `cnrm.cloud.google.com/project-id: foo` annotation.

[`set-project-id`]: https://catalog.kpt.dev/set-project-id/v0.1/
46 changes: 46 additions & 0 deletions examples/set-project-id-simple/resources.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
apiVersion: storage.cnrm.cloud.google.com/v1beta1
kind: StorageBucket
metadata:
name: my-test-project-first-bucket # kpt-set: ${project-id}-first-bucket
namespace: config-control # kpt-set: ${namespace}
annotations:
cnrm.cloud.google.com/force-destroy: "false"
cnrm.cloud.google.com/project-id: my-test-project # kpt-set: ${project-id}
spec:
storageClass: standard # kpt-set: ${storage-class}
uniformBucketLevelAccess: true
versioning:
enabled: false
---
apiVersion: storage.cnrm.cloud.google.com/v1beta1
kind: StorageBucket
metadata:
name: my-test-project-second-bucket # kpt-set: ${project-id}-second-bucket
namespace: config-control # kpt-set: ${namespace}
annotations:
cnrm.cloud.google.com/force-destroy: "false"
spec:
storageClass: standard # kpt-set: ${storage-class}
uniformBucketLevelAccess: true
versioning:
enabled: false
---
apiVersion: v1
kind: Namespace
metadata:
name: newnamespace
---
apiVersion: core.cnrm.cloud.google.com/v1beta1
kind: ConfigConnectorContext
metadata:
name: configconnectorcontext.core.cnrm.cloud.google.com
namespace: newnamespace
spec:
googleServiceAccount: new-namespace-sa@project-id.iam.gserviceaccount.com # kpt-set: new-namespace-sa@${project-id}.iam.gserviceaccount.com
---
apiVersion: v1
kind: ConfigMap
metadata:
name: test-map
data:
some-key: some-value
7 changes: 7 additions & 0 deletions examples/set-project-id-simple/setters.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: setters
data:
namespace: config-control
storage-class: standard
1 change: 1 addition & 0 deletions functions/go/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ FUNCTIONS := \
set-annotations \
set-labels \
set-namespace \
set-project-id \
starlark \
upsert-resource

Expand Down
16 changes: 16 additions & 0 deletions functions/go/set-project-id/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM golang:1.16-alpine3.13
ENV CGO_ENABLED=0
WORKDIR /go/src/

COPY go.mod go.sum ./
RUN go mod download

COPY . .
RUN go build -o /usr/local/bin/function ./

#############################################

FROM alpine:3.13
COPY --from=0 /usr/local/bin/function /usr/local/bin/function
ENTRYPOINT ["function"]

Loading