Skip to content

Add set-default-label function #890

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
51 changes: 51 additions & 0 deletions examples/set-default-labels-nested-package/.expected/diff.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
diff --git a/app1_ghost/resources.yaml b/app1_ghost/resources.yaml
index 2e6dcbd..935a47b 100644
--- a/app1_ghost/resources.yaml
+++ b/app1_ghost/resources.yaml
@@ -2,6 +2,8 @@ apiVersion: apps/v1
kind: Deployment
metadata:
name: ghost
+ labels:
+ app.kubernetes.io/name: root-app
spec:
replicas: 2
template:
@@ -14,4 +16,10 @@ spec:
- containerPort: 2368
env:
- name: url
- value: http://my-blog.com
\ No newline at end of file
+ value: http://my-blog.com
+ metadata:
+ labels:
+ app.kubernetes.io/name: root-app
+ selector:
+ matchLabels:
+ app.kubernetes.io/name: root-app
diff --git a/app2_mysql/resources.yaml b/app2_mysql/resources.yaml
index 88d0df7..be45a8d 100644
--- a/app2_mysql/resources.yaml
+++ b/app2_mysql/resources.yaml
@@ -2,6 +2,8 @@ apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
+ labels:
+ app.kubernetes.io/name: root-app
spec:
template:
spec:
@@ -11,5 +13,11 @@ spec:
ports:
- name: mysql
containerPort: 3306
+ metadata:
+ labels:
+ app.kubernetes.io/name: root-app
strategy:
type: Recreate
+ selector:
+ matchLabels:
+ app.kubernetes.io/name: root-app
1 change: 1 addition & 0 deletions examples/set-default-labels-nested-package/.krmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.expected
9 changes: 9 additions & 0 deletions examples/set-default-labels-nested-package/Kptfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: kpt.dev/v1
kind: Kptfile
metadata:
name: root-app
annotations:
config.kubernetes.io/local-config: "true"
pipeline:
mutators:
- image: gcr.io/kpt-fn/set-default-labels:unstable
39 changes: 39 additions & 0 deletions examples/set-default-labels-nested-package/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# set-default-labels: Nested Package Example

### Overview

This example shows how to run [`set-default-labels`] in a nested KPT package.

### 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-default-labels-nested-package root-app
```

Take a look at the `Kptfile`, it has the `set-default-labels` configured. Note: you do not need to specify the function config via `configPath` or `configMap`.

```yaml
apiVersion: kpt.dev/v1
kind: Kptfile
metadata:
name: root-app
pipeline:
mutators:
- image: gcr.io/kpt-fn/set-default-labels:unstable
```

### Function invocation

Invoke the function:

```shell
$ kpt fn render root-app
```

### Expected result
A [recommended label] `app.kubernetes.io/name: root-app` using the root package name is added to all the resources `labels` and `matchLabels` fields.

[`set-default-labels`]: https://catalog.kpt.dev/set-default-labels/v0.1/
[recommended label]: https://kubernetes.io/docs/concepts/overview/working-with-objects/common-labels/
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: kpt.dev/v1
kind: Kptfile
metadata:
name: app1_ghost
annotations:
config.kubernetes.io/local-config: "true"
info:
description: sample description
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: ghost
spec:
replicas: 2
template:
spec:
containers:
- name: blog
image: ghost:2.6-alpine
imagePullPolicy: Always
ports:
- containerPort: 2368
env:
- name: url
value: http://my-blog.com
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: kpt.dev/v1
kind: Kptfile
metadata:
name: app2_mysql
annotations:
config.kubernetes.io/local-config: "true"
info:
description: sample description
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
spec:
template:
spec:
containers:
- name: mysql
image: mysql:5.7
ports:
- name: mysql
containerPort: 3306
strategy:
type: Recreate
1 change: 1 addition & 0 deletions functions/go/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ FUNCTIONS := \
set-enforcement-action \
set-image \
set-labels \
set-default-labels \
set-namespace \
set-project-id \
source-gcloud-config \
Expand Down
61 changes: 61 additions & 0 deletions functions/go/set-default-labels/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# set-default-labels

## Overview

<!--mdtogo:Short-->

The `set-default-labels` function applies KPT label convention to your KPT package.


### `app.kubernetes.io/name`

Running the function can add this label using the value from the root `Kptfile` name.

This convention believes that all KRM resources under the same kpt package should be served for a specific application.
An application (or KPT package) can be composed by other applications (nested sub KPT packages).
So the function can accept multiple Kptfile and use the root Kptfile to set the app name.

<!--mdtogo-->

You can learn more about the recommended labels [here][recommended labels].

<!--mdtogo:Long-->

## Usage

This function should be run in a KPT package. It does not require function config.

### Run the function once
```shell
$ kpt fn eval --image set-default-labels:unstable
```

### Run the function in a Kpt pipeline

Execute the `set-default-labels` function and save the config to Kptfile pipeline if the function passes.
```shell
$ kpt fn eval -t mutator -s -i set-default-labels:unstable
```

Check the Kptfile file, it now contains the `set-default-labels` function in its pipeline.
```shell
$ cat Kptfile

apiVersion: kpt.dev/v1
kind: Kptfile
metadata:
name: set-default-label-example
annotations:
config.kubernetes.io/local-config: "true"
pipeline:
mutators:
- image: gcr.io/kpt-fn/set-default-labels:unstable
```

```shell
$ kpt fn render
```
<!--mdtogo-->


[recommended labels]: https://kubernetes.io/docs/concepts/overview/working-with-objects/common-labels/
69 changes: 69 additions & 0 deletions functions/go/set-default-labels/generated/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions functions/go/set-default-labels/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/set-default-labels

go 1.17

require (
github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/set-labels v0.0.0-20220720210558-f05f54028534
github.com/GoogleContainerTools/kpt-functions-sdk/go/fn v0.0.0-20220723081830-33aee7fe8a2e
)

require (
github.com/GoogleContainerTools/kpt-functions-sdk/go/api v0.0.0-20220720212527-133180134b93 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-errors/errors v1.4.2 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.20.0 // indirect
github.com/go-openapi/swag v0.21.1 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/gnostic v0.6.9 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/stretchr/testify v1.8.0 // indirect
github.com/xlab/treeprint v1.1.0 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/klog/v2 v2.70.1 // indirect
k8s.io/kube-openapi v0.0.0-20220627174259-011e075b9cb8 // indirect
sigs.k8s.io/kustomize/kyaml v0.13.7 // indirect
)
Loading