Skip to content

[Feature] Set labels conditional spec selector #1064

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 7 commits 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
19 changes: 19 additions & 0 deletions functions/go/set-labels/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,25 @@ labels:
fruit: apple
```

The `set-labels` function adds the desired labels to more fields than just the one
found in `.metadata`. For example, the `Deployment` resource has a `spec.selector`,
which gets updated by the `set-labels` function by default. This is not always desired.

To disable updating of `.spec.selectors`, define a `SetLabels` custom resource, and set
`.options.setSelectorLabels` to `false` like so:

```yaml
apiVersion: fn.kpt.dev/v1alpha1
kind: SetLabels
metadata:
name: my-config
labels:
color: orange
fruit: apple
options:
setSelectorLabels: false # default is true
```

<!--mdtogo-->

[labels]: https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/
Expand Down
17 changes: 17 additions & 0 deletions functions/go/set-labels/generated/docs.go

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

13 changes: 12 additions & 1 deletion functions/go/set-labels/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,18 @@ import (
)

func NewTransformer() fn.ResourceListProcessor {
return fn.WithContext(fn.Context{Context: context.Background()}, &setlabels.SetLabels{})
// Create a new SetLabels struct
labels := make(map[string]string)
options := map[string]bool{
"setSelectorLabels": true,
}
setLabels := &setlabels.SetLabels{
Labels: labels,
Options: options,
}

// Return the SetLabels struct as a fn.ResourceListProcessor
return fn.WithContext(fn.Context{Context: context.Background()}, setLabels)
}

func run() error {
Expand Down
13 changes: 11 additions & 2 deletions functions/go/set-labels/setlabels/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ var _ fn.Runner = &SetLabels{}
// SetLabels supports the set-labels workflow, it uses Config to parse functionConfig, Transform to change the labels
type SetLabels struct {
// labels is the desired labels
Labels map[string]string `json:"labels,omitempty"`
count int
Labels map[string]string `json:"labels,omitempty"`

// options is the configurable and optional options
Options map[string]bool `json:"options,omitempty"`

count int
}

// EmptyfnConfig is a workaround since kpt creates a FunctionConfig placeholder if users don't provide the functionConfig.
Expand Down Expand Up @@ -161,6 +165,11 @@ func hasSpecSelector(o *fn.KubeObject) bool {

// setLabelsInSelector set labels for all selectors, including spec selector map, spec selector LabelSelector, LabelSelector in JobTemplate, and podSelector in NetworkPolicy, and
func (p *SetLabels) setLabelsInSelector(o *fn.KubeObject) error {
// setSelectorLabels is true by default
if !p.Options["setSelectorLabels"] {
return nil
}

if hasSpecSelector(o) {
fieldPath := FieldPath{"spec", "selector"}
if err := p.updateLabels(&o.SubObject, fieldPath, p.Labels, true); err != nil {
Expand Down
78 changes: 78 additions & 0 deletions tests/set-labels/deployment/.expected/diff.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
diff --git a/Kptfile b/Kptfile
index ad493d3..917ae02 100644
--- a/Kptfile
+++ b/Kptfile
@@ -17,4 +17,4 @@ pipeline:
- image: gcr.io/kpt-fn/set-labels:unstable
configPath: fn-config-selector-labels-undefined.yaml
selectors:
- - name: the-deployment-selector-labels-undefined
\ No newline at end of file
+ - name: the-deployment-selector-labels-undefined
diff --git a/deployment.yaml b/deployment.yaml
index 11be507..b03ae0a 100644
--- a/deployment.yaml
+++ b/deployment.yaml
@@ -4,6 +4,8 @@ metadata:
name: the-deployment-selector-labels-false
labels:
app: the-deployment
+ color: orange
+ fruit: apple
spec:
selector:
matchLabels:
@@ -12,6 +14,8 @@ spec:
metadata:
labels:
app: the-deployment
+ color: orange
+ fruit: apple
spec:
containers:
- name: the-container
@@ -23,14 +27,20 @@ metadata:
name: the-deployment-selector-labels-true
labels:
app: the-deployment
+ color: orange
+ fruit: apple
spec:
selector:
matchLabels:
app: the-deployment
+ color: orange
+ fruit: apple
template:
metadata:
labels:
app: the-deployment
+ color: orange
+ fruit: apple
spec:
containers:
- name: the-container
@@ -42,15 +52,21 @@ metadata:
name: the-deployment-selector-labels-undefined
labels:
app: the-deployment
+ color: orange
+ fruit: apple
spec:
selector:
matchLabels:
app: the-deployment
+ color: orange
+ fruit: apple
template:
metadata:
labels:
app: the-deployment
+ color: orange
+ fruit: apple
spec:
containers:
- name: the-container
- image: the-image
\ No newline at end of file
+ image: the-image
21 changes: 21 additions & 0 deletions tests/set-labels/deployment/.expected/results.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: kpt.dev/v1
kind: FunctionResultList
metadata:
name: fnresults
exitCode: 0
items:
- image: gcr.io/kpt-fn/set-labels:unstable
exitCode: 0
results:
- message: set 4 labels in total
severity: info
- image: gcr.io/kpt-fn/set-labels:unstable
exitCode: 0
results:
- message: set 6 labels in total
severity: info
- image: gcr.io/kpt-fn/set-labels:unstable
exitCode: 0
results:
- message: set 6 labels in total
severity: info
1 change: 1 addition & 0 deletions tests/set-labels/deployment/.krmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.expected
20 changes: 20 additions & 0 deletions tests/set-labels/deployment/Kptfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: kpt.dev/v1
kind: Kptfile
metadata:
name: example
annotations:
config.kubernetes.io/local-config: "true"
pipeline:
mutators:
- image: gcr.io/kpt-fn/set-labels:unstable
configPath: fn-config-selector-labels-false.yaml
selectors:
- name: the-deployment-selector-labels-false
- image: gcr.io/kpt-fn/set-labels:unstable
configPath: fn-config-selector-labels-true.yaml
selectors:
- name: the-deployment-selector-labels-true
- image: gcr.io/kpt-fn/set-labels:unstable
configPath: fn-config-selector-labels-undefined.yaml
selectors:
- name: the-deployment-selector-labels-undefined
56 changes: 56 additions & 0 deletions tests/set-labels/deployment/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: the-deployment-selector-labels-false
labels:
app: the-deployment
spec:
selector:
matchLabels:
app: the-deployment
template:
metadata:
labels:
app: the-deployment
spec:
containers:
- name: the-container
image: the-image
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: the-deployment-selector-labels-true
labels:
app: the-deployment
spec:
selector:
matchLabels:
app: the-deployment
template:
metadata:
labels:
app: the-deployment
spec:
containers:
- name: the-container
image: the-image
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: the-deployment-selector-labels-undefined
labels:
app: the-deployment
spec:
selector:
matchLabels:
app: the-deployment
template:
metadata:
labels:
app: the-deployment
spec:
containers:
- name: the-container
image: the-image
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: fn.kpt.dev/v1alpha1
kind: SetLabels
metadata:
name: my-config
annotations:
config.kubernetes.io/local-config: "true"
labels:
color: orange
fruit: apple
options:
setSelectorLabels: false
11 changes: 11 additions & 0 deletions tests/set-labels/deployment/fn-config-selector-labels-true.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: fn.kpt.dev/v1alpha1
kind: SetLabels
metadata:
name: my-config
annotations:
config.kubernetes.io/local-config: "true"
labels:
color: orange
fruit: apple
options:
setSelectorLabels: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: fn.kpt.dev/v1alpha1
kind: SetLabels
metadata:
name: my-config
annotations:
config.kubernetes.io/local-config: "true"
labels:
color: orange
fruit: apple