Skip to content

Commit bd9b765

Browse files
committed
Add set-default-labels function
1 parent f05f540 commit bd9b765

File tree

16 files changed

+1139
-0
lines changed

16 files changed

+1139
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
diff --git a/app1_ghost/resources.yaml b/app1_ghost/resources.yaml
2+
index 2e6dcbd..935a47b 100644
3+
--- a/app1_ghost/resources.yaml
4+
+++ b/app1_ghost/resources.yaml
5+
@@ -2,6 +2,8 @@ apiVersion: apps/v1
6+
kind: Deployment
7+
metadata:
8+
name: ghost
9+
+ labels:
10+
+ app.kubernetes.io/name: root-app
11+
spec:
12+
replicas: 2
13+
template:
14+
@@ -14,4 +16,10 @@ spec:
15+
- containerPort: 2368
16+
env:
17+
- name: url
18+
- value: http://my-blog.com
19+
\ No newline at end of file
20+
+ value: http://my-blog.com
21+
+ metadata:
22+
+ labels:
23+
+ app.kubernetes.io/name: root-app
24+
+ selector:
25+
+ matchLabels:
26+
+ app.kubernetes.io/name: root-app
27+
diff --git a/app2_mysql/resources.yaml b/app2_mysql/resources.yaml
28+
index 88d0df7..be45a8d 100644
29+
--- a/app2_mysql/resources.yaml
30+
+++ b/app2_mysql/resources.yaml
31+
@@ -2,6 +2,8 @@ apiVersion: apps/v1
32+
kind: Deployment
33+
metadata:
34+
name: mysql
35+
+ labels:
36+
+ app.kubernetes.io/name: root-app
37+
spec:
38+
template:
39+
spec:
40+
@@ -11,5 +13,11 @@ spec:
41+
ports:
42+
- name: mysql
43+
containerPort: 3306
44+
+ metadata:
45+
+ labels:
46+
+ app.kubernetes.io/name: root-app
47+
strategy:
48+
type: Recreate
49+
+ selector:
50+
+ matchLabels:
51+
+ app.kubernetes.io/name: root-app
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.expected
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
apiVersion: kpt.dev/v1
2+
kind: Kptfile
3+
metadata:
4+
name: root-app
5+
annotations:
6+
config.kubernetes.io/local-config: "true"
7+
pipeline:
8+
mutators:
9+
- image: gcr.io/kpt-fn/set-default-labels:unstable
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# set-default-labels: Nested Package Example
2+
3+
### Overview
4+
5+
This example shows how to run [`set-default-labels`] in a nested KPT package.
6+
7+
### Fetch the example package
8+
9+
Get the example package by running the following commands:
10+
11+
```shell
12+
$ kpt pkg get https://github.com/GoogleContainerTools/kpt-functions-catalog.git/examples/set-default-labels-nested-package
13+
```
14+
15+
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`.
16+
17+
```yaml
18+
apiVersion: kpt.dev/v1
19+
kind: Kptfile
20+
metadata:
21+
name: your-app
22+
pipeline:
23+
mutators:
24+
- image: gcr.io/kpt-fn/set-default-labels:unstable
25+
```
26+
27+
### Function invocation
28+
29+
Invoke the function:
30+
31+
```shell
32+
$ kpt fn render root-app
33+
```
34+
35+
### Expected result
36+
A [recommended label] `app.kubernetes.io/name: your-app` using the root pacakge name is added to all the resources `labels` and `matchLabels` fields.
37+
38+
[`set-default-labels`]: https://catalog.kpt.dev/set-default-labels/v0.1/
39+
[recommended label]: https://kubernetes.io/docs/concepts/overview/working-with-objects/common-labels/
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: app1_ghost
5+
annotations:
6+
config.kubernetes.io/local-config: "true"
7+
info:
8+
description: sample description
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: ghost
5+
spec:
6+
replicas: 2
7+
template:
8+
spec:
9+
containers:
10+
- name: blog
11+
image: ghost:2.6-alpine
12+
imagePullPolicy: Always
13+
ports:
14+
- containerPort: 2368
15+
env:
16+
- name: url
17+
value: http://my-blog.com
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: app2_mysql
5+
annotations:
6+
config.kubernetes.io/local-config: "true"
7+
info:
8+
description: sample description
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: mysql
5+
spec:
6+
template:
7+
spec:
8+
containers:
9+
- name: mysql
10+
image: mysql:5.7
11+
ports:
12+
- name: mysql
13+
containerPort: 3306
14+
strategy:
15+
type: Recreate

functions/go/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ FUNCTIONS := \
3737
set-enforcement-action \
3838
set-image \
3939
set-labels \
40+
set-default-labels \
4041
set-namespace \
4142
set-project-id \
4243
source-gcloud-config \
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# set-default-labels
2+
3+
## Overview
4+
5+
<!--mdtogo:Short-->
6+
7+
The `set-default-labels` function applies KPT label convention to your KPT package.
8+
9+
10+
### `app.kubernetes.io/name`
11+
12+
Running the function can add this label using the value from the root `Kptfile` name.
13+
14+
This convention believes that all KRM resources under the same kpt package should be served for a specific application.
15+
An application (or KPT package) can be composed by other applications (nested sub KPT packages).
16+
So the function can accept multiple Kptfile and use the root Kptfile to set the app name.
17+
18+
<!--mdtogo-->
19+
20+
You can learn more about recommended labels [here][recommended labels].
21+
22+
<!--mdtogo:Long-->
23+
24+
## Usage
25+
26+
This function should be run in a KPT package. It does not require function config.
27+
28+
### Run the function once
29+
```shell
30+
$ kpt fn eval --image set-default-labels:unstable
31+
```
32+
33+
### Run the function in a Kpt pipeline
34+
35+
```shell
36+
# add the function to Kptfile
37+
$ kpt fn eval -t mutator -s -i set-default-labels:unstable
38+
```
39+
40+
```shell
41+
# check Kptfile
42+
$ cat Kptfile
43+
44+
apiVersion: kpt.dev/v1
45+
kind: Kptfile
46+
metadata:
47+
name: set-default-label-example
48+
annotations:
49+
config.kubernetes.io/local-config: "true"
50+
pipeline:
51+
mutators:
52+
- image: gcr.io/kpt-fn/set-default-labels:unstable
53+
```
54+
55+
```shell
56+
# run the function in a CI pipeline
57+
$ kpt fn render
58+
```
59+
<!--mdtogo-->
60+
61+
62+
[recommended labels]: https://kubernetes.io/docs/concepts/overview/working-with-objects/common-labels/

functions/go/set-default-labels/generated/docs.go

Lines changed: 69 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/set-default-labels
2+
3+
go 1.17
4+
5+
require (
6+
github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/set-labels v0.0.0-20220720210558-f05f54028534
7+
github.com/GoogleContainerTools/kpt-functions-sdk/go/fn v0.0.0-20220723081830-33aee7fe8a2e
8+
)
9+
10+
require (
11+
github.com/GoogleContainerTools/kpt-functions-sdk/go/api v0.0.0-20220720212527-133180134b93 // indirect
12+
github.com/davecgh/go-spew v1.1.1 // indirect
13+
github.com/go-errors/errors v1.4.2 // indirect
14+
github.com/go-logr/logr v1.2.3 // indirect
15+
github.com/go-openapi/jsonpointer v0.19.5 // indirect
16+
github.com/go-openapi/jsonreference v0.20.0 // indirect
17+
github.com/go-openapi/swag v0.21.1 // indirect
18+
github.com/golang/protobuf v1.5.2 // indirect
19+
github.com/google/gnostic v0.6.9 // indirect
20+
github.com/josharian/intern v1.0.0 // indirect
21+
github.com/mailru/easyjson v0.7.7 // indirect
22+
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
23+
github.com/pkg/errors v0.9.1 // indirect
24+
github.com/stretchr/testify v1.8.0 // indirect
25+
github.com/xlab/treeprint v1.1.0 // indirect
26+
google.golang.org/protobuf v1.28.0 // indirect
27+
gopkg.in/yaml.v2 v2.4.0 // indirect
28+
gopkg.in/yaml.v3 v3.0.1 // indirect
29+
k8s.io/klog/v2 v2.70.1 // indirect
30+
k8s.io/kube-openapi v0.0.0-20220627174259-011e075b9cb8 // indirect
31+
sigs.k8s.io/kustomize/kyaml v0.13.7 // indirect
32+
)

0 commit comments

Comments
 (0)