-
Notifications
You must be signed in to change notification settings - Fork 72
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
yuwenma
wants to merge
1
commit into
kptdev:master
Choose a base branch
from
yuwenma:set-default-label
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
examples/set-default-labels-nested-package/.expected/diff.patch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.expected |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
8 changes: 8 additions & 0 deletions
8
examples/set-default-labels-nested-package/app1_ghost/Kptfile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
17 changes: 17 additions & 0 deletions
17
examples/set-default-labels-nested-package/app1_ghost/resources.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
8 changes: 8 additions & 0 deletions
8
examples/set-default-labels-nested-package/app2_mysql/Kptfile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
15 changes: 15 additions & 0 deletions
15
examples/set-default-labels-nested-package/app2_mysql/resources.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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--> | ||
yuwenma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
[recommended labels]: https://kubernetes.io/docs/concepts/overview/working-with-objects/common-labels/ |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.