Skip to content
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

Fix WithLabels and WithPodLabels to append to eventually existing labels #765

Merged
merged 2 commits into from
Oct 10, 2024
Merged
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
24 changes: 11 additions & 13 deletions pkg/manifest/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,34 +66,32 @@ func WithPodAnnotations(additional map[string]interface{}) CfgFn {
// WithPodLabels appends pod labels (usually used by types where pod template is embedded)
func WithPodLabels(additional map[string]string) CfgFn {
return func(cfg map[string]interface{}) {
if ann, ok := cfg["podlabels"]; ok {
m := make(map[string]interface{}, len(additional))
for k, v := range additional {
m[k] = v
}
appendToOriginal(ann, m)
if labels, ok := cfg["podlabels"]; ok {
appendToOriginal(labels, additional)
return
}
cfg["podlabels"] = additional
}
}

func appendToOriginal(original interface{}, additional map[string]interface{}) {
annotations := original.(map[string]interface{})
func appendToOriginal[T any](original interface{}, additional map[string]T) {
orig := original.(map[string]T)
for k, v := range additional {
// Only add the unspecified ones
if _, ok := annotations[k]; !ok {
annotations[k] = v
if _, ok := orig[k]; !ok {
orig[k] = v
}
}
}

// WithLabels returns a function for configuring labels of the resource
func WithLabels(labels map[string]string) CfgFn {
func WithLabels(additional map[string]string) CfgFn {
return func(cfg map[string]interface{}) {
if labels != nil {
cfg["labels"] = labels
if labels, ok := cfg["labels"]; ok {
appendToOriginal(labels, additional)
return
}
cfg["labels"] = additional
}
}

Expand Down
6 changes: 4 additions & 2 deletions pkg/resources/cronjob/cronjob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,14 @@ func Example_full() {
"name": "foo",
"namespace": "bar",
"image": "baz",
"labels": map[string]string{
"app": "foo",
},
}

opts := []manifest.CfgFn{
job.WithLabels(map[string]string{
job.WithLabels(map[string]string{ //should get appended to cfg.labels
"color": "green",
"app": "foo",
"bar": "true",
}),
job.WithAnnotations(map[string]interface{}{
Expand Down
8 changes: 8 additions & 0 deletions pkg/resources/deployment/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ func Example_full() {
"name": "foo",
"namespace": "bar",
"image": "baz",
"podlabels": map[string]string{
"existing-pod-label": "foo",
},
}

opts := []manifest.CfgFn{
Expand All @@ -172,6 +175,9 @@ func Example_full() {
deployment.WithPodAnnotations(map[string]interface{}{
"pod-annotation": "foo",
}),
deployment.WithPodLabels(map[string]string{
"pod-label": "bar",
}),
deployment.WithReplicas(6),
deployment.WithImagePullPolicy(v1.PullNever),
deployment.WithEnvs(map[string]string{
Expand Down Expand Up @@ -214,6 +220,8 @@ func Example_full() {
// pod-annotation: "foo"
// labels:
// app: "my-app"
// existing-pod-label: "foo"
// pod-label: "bar"
// spec:
// containers:
// - name: user-container
Expand Down
1 change: 1 addition & 0 deletions pkg/resources/deployment/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var (
WithAnnotations = manifest.WithAnnotations
WithLabels = manifest.WithLabels
WithPodAnnotations = manifest.WithPodAnnotations
WithPodLabels = manifest.WithPodLabels
)

func WithSelectors(selectors map[string]string) manifest.CfgFn {
Expand Down
9 changes: 1 addition & 8 deletions pkg/resources/job/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

var (
WithLabels = manifest.WithLabels
WithPodLabels = manifest.WithPodLabels
WithAnnotations = manifest.WithAnnotations
WithPodAnnotations = manifest.WithPodAnnotations
)
Expand All @@ -36,14 +37,6 @@ func WithEnvs(envs map[string]string) manifest.CfgFn {
}
}

func WithPodLabels(labels map[string]string) manifest.CfgFn {
return func(cfg map[string]interface{}) {
if labels != nil {
cfg["podlabels"] = labels
}
}
}

func WithImagePullPolicy(ipp corev1.PullPolicy) manifest.CfgFn {
return func(cfg map[string]interface{}) {
cfg["imagePullPolicy"] = ipp
Expand Down
Loading