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 7 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-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.
andudo marked this conversation as resolved.
Show resolved Hide resolved

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

1. Set `project-id` setter.
andudo marked this conversation as resolved.
Show resolved Hide resolved
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. Kptfile will include `apply-setters` mutator with `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"]

82 changes: 82 additions & 0 deletions functions/go/set-project-id/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# set-project-id
mengqiy marked this conversation as resolved.
Show resolved Hide resolved

## Overview

<!--mdtogo:Short-->

The `set-project-id` function sets 'project-id' setter and
andudo marked this conversation as resolved.
Show resolved Hide resolved
`cnrm.cloud.google.com/project-id` annotation to the provided project ID, only
if they are not already set.

<!--mdtogo-->

<!--mdtogo:Long-->

## Usage

set-project-id function is expected to be executed imperatively like:

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

The `set-project-id` function does the following:

1. Sets the the 'project-id' setter to the provided project ID.
* If an 'apply-setters' function is present in the Kptfile which does not
andudo marked this conversation as resolved.
Show resolved Hide resolved
have the 'project-id' setter or sets it to an empty value, update that
config to set the “project-id” setter to the provided project ID value.
* If no 'apply-setters' function is present in the Kptfile, add one with
mengqiy marked this conversation as resolved.
Show resolved Hide resolved
just the 'project-id' setter set to the provided project ID value.
* If no pipeline is declared in the Kptfile, declare it and add
'apply-setters' function as mutator with the 'project-id' setter set to
the provided project ID value.
2. For all
[Config Connector resources](https://cloud.google.com/config-connector/docs/reference/overview),
check if they have the `cnrm.cloud.google.com/project-id` annotation set. If
the annotation is not present or is set to an empty value, set it to the
provided project ID value.

### FunctionConfig

This function supports `ConfigMap` `functionConfig` and expects the 'project-id'
value to be present in the map.

<!--mdtogo-->

## Examples

<!--mdtogo:Examples-->

Setting the `project-id` setter on the package without setters.

Let's start with the Kptfile of an example package.

```yaml
apiVersion: kpt.dev/v1
kind: Kptfile
metadata:
name: example-package
```

Invoke the function:

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

Kptfile will be updated to the following:

```yaml
apiVersion: kpt.dev/v1
kind: Kptfile
metadata:
name: example-package
pipeline:
mutators:
- image: gcr.io/kpt-fn/apply-setters:v0.1
configMap:
project-id: foo
```

<!--mdtogo-->
8 changes: 8 additions & 0 deletions functions/go/set-project-id/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/set-project-id

go 1.16

require (
github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/fix v0.0.0-20210723002714-5e92d58c7664
andudo marked this conversation as resolved.
Show resolved Hide resolved
sigs.k8s.io/kustomize/kyaml v0.11.0
)
Loading