Skip to content

Commit 0063969

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

File tree

17 files changed

+1125
-0
lines changed

17 files changed

+1125
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
diff --git a/resources.yaml b/resources.yaml
2+
index 1e641f7..bb4be90 100644
3+
--- a/resources.yaml
4+
+++ b/resources.yaml
5+
@@ -2,6 +2,9 @@ apiVersion: v1
6+
kind: ConfigMap
7+
metadata:
8+
name: the-map
9+
+ labels:
10+
+ color: orange
11+
+ fruit: apple
12+
data:
13+
some-key: some-value
14+
---
15+
@@ -10,6 +13,9 @@ kind: MyResource
16+
metadata:
17+
name: the-service
18+
namespace: the-namespace
19+
+ labels:
20+
+ color: orange
21+
+ fruit: apple
22+
spec:
23+
selector:
24+
labels:
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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 your-app
13+
```
14+
15+
We use the following `Kptfile` to configure the function.
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 by running the following commands:
30+
31+
```shell
32+
$ kpt fn render your-app
33+
```
34+
35+
### Expected result
36+
All resources should have `app.kubernetes.io/name: your-app` label added to the meta and label selector fields.
37+
38+
[`set-default-labels`]: https://catalog.kpt.dev/set-labels/v0.1/
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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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: ghost
11+
image: ghost:3-alpine
12+
ports:
13+
- containerPort: 2368
14+
env:
15+
- name: url
16+
value: http://localhost:8080
17+
- name: database__connection__database
18+
value: ghost
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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# app2
2+
3+
## Description
4+
sample description
5+
6+
## Usage
7+
8+
### Fetch the package
9+
`kpt pkg get REPO_URI[.git]/PKG_PATH[@VERSION] app2`
10+
Details: https://kpt.dev/reference/cli/pkg/get/
11+
12+
### View package content
13+
`kpt pkg tree app2`
14+
Details: https://kpt.dev/reference/cli/pkg/tree/
15+
16+
### Apply the package
17+
```
18+
kpt live init app2
19+
kpt live apply app2 --reconcile-timeout=2m --output=table
20+
```
21+
Details: https://kpt.dev/reference/cli/live/
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: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
## Usage
23+
24+
This function should be run in a KPT package. It does not require function config.
25+
26+
### Run the function once
27+
```bash
28+
kpt fn eval --image set-default-labels
29+
```
30+
31+
### Run the function as a Kpt pipeline
32+
```bash
33+
# add the function to Kptfile
34+
kpt fn eval -t mutator -s -i set-default-labels:unstable
35+
36+
# check Kptfile
37+
cat Kptfile
38+
39+
apiVersion: kpt.dev/v1
40+
kind: Kptfile
41+
metadata:
42+
name: set-default-label-example
43+
annotations:
44+
config.kubernetes.io/local-config: "true"
45+
pipeline:
46+
mutators:
47+
- image: gcr.io/kpt-fn/set-default-labels:unstable
48+
49+
# run the function in a CI pipeline
50+
kpt fn render
51+
```
52+
<!--mdtogo-->
53+
54+
[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)