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

feat: add pod labels/annotations in the workload patcher #1123

Merged
merged 1 commit into from
May 16, 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
8 changes: 6 additions & 2 deletions pkg/apis/api.kusion.io/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,10 +402,14 @@ type AppConfiguration struct {
type Patcher struct {
// Environments represent the environment variables patched to all containers in the workload.
Environments []v1.EnvVar `json:"environments" yaml:"environments"`
// Labels represent the labels patched to both the workload and pod.
// Labels represent the labels patched to the workload.
Labels map[string]string `json:"labels" yaml:"labels"`
// Annotations represent the annotations patched to both the workload and pod.
// PodLabels represent the labels patched to the pods.
PodLabels map[string]string `json:"podLabels" yaml:"podLabels"`
// Annotations represent the annotations patched to the workload.
Annotations map[string]string `json:"annotations" yaml:"annotations"`
// PodAnnotations represent the annotations patched to the pods.
PodAnnotations map[string]string `json:"podAnnotations" yaml:"podAnnotations"`
}

const ConfigBackends = "backends"
Expand Down
27 changes: 20 additions & 7 deletions pkg/modules/generators/app_configurations_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,15 @@ func PatchWorkload(workload *v1.Resource, patcher *v1.Patcher) error {
if objLabels == nil {
objLabels = make(map[string]string)
}
// merge labels
for k, v := range patcher.Labels {
objLabels[k] = v
}
un.SetLabels(objLabels)
}

// patch pod labels
if patcher.PodLabels != nil {
podLabels, b, err := unstructured.NestedStringMap(un.Object, "spec", "template", "metadata", "labels")
if err != nil {
return fmt.Errorf("failed to get pod labels from workload:%s. %w", workload.ID, err)
Expand All @@ -171,11 +180,9 @@ func PatchWorkload(workload *v1.Resource, patcher *v1.Patcher) error {
podLabels = make(map[string]string)
}
// merge labels
for k, v := range patcher.Labels {
objLabels[k] = v
for k, v := range patcher.PodLabels {
podLabels[k] = v
}
un.SetLabels(objLabels)
err = unstructured.SetNestedStringMap(un.Object, podLabels, "spec", "template", "metadata", "labels")
if err != nil {
return err
Expand All @@ -188,6 +195,15 @@ func PatchWorkload(workload *v1.Resource, patcher *v1.Patcher) error {
if objAnnotations == nil {
objAnnotations = make(map[string]string)
}
// merge annotations
for k, v := range patcher.Annotations {
objAnnotations[k] = v
}
un.SetAnnotations(objAnnotations)
}

// patch pod annotations
if patcher.PodAnnotations != nil {
podAnnotations, b, err := unstructured.NestedStringMap(un.Object, "spec", "template", "metadata", "annotations")
if err != nil {
return fmt.Errorf("failed to get pod annotations from workload:%s. %w", workload.ID, err)
Expand All @@ -196,12 +212,9 @@ func PatchWorkload(workload *v1.Resource, patcher *v1.Patcher) error {
podAnnotations = make(map[string]string)
}
// merge annotations
for k, v := range patcher.Annotations {
objAnnotations[k] = v
for k, v := range patcher.PodAnnotations {
podAnnotations[k] = v
}

un.SetAnnotations(objAnnotations)
err = unstructured.SetNestedStringMap(un.Object, podAnnotations, "spec", "template", "metadata", "annotations")
if err != nil {
return err
Expand Down
10 changes: 6 additions & 4 deletions pkg/modules/generators/app_configurations_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,10 @@ func Test_patchWorkload(t *testing.T) {

t.Run("Patch labels and annotations", func(t *testing.T) {
patcher := &v1.Patcher{
Labels: map[string]string{"newLabel": "newValue"},
Annotations: map[string]string{"newAnnotation": "newValue"},
Labels: map[string]string{"newLabel": "newValue"},
Annotations: map[string]string{"newAnnotation": "newValue"},
PodLabels: map[string]string{"newPodLabel": "newValue"},
PodAnnotations: map[string]string{"newPodAnnotation": "newValue"},
}

err := PatchWorkload(res, patcher)
Expand All @@ -305,7 +307,7 @@ func Test_patchWorkload(t *testing.T) {
assert.Equal(t, "newValue", workloadLabels["newLabel"])
assert.Equal(t, "oldValue", workloadLabels["oldLabel"])
// assert pod labels
assert.Equal(t, "newValue", podLabels["newLabel"])
assert.Equal(t, "newValue", podLabels["newPodLabel"])
assert.Equal(t, "oldValue", podLabels["oldLabel"])

annotations := res.Attributes["metadata"].(map[string]interface{})["annotations"].(map[string]interface{})
Expand All @@ -314,7 +316,7 @@ func Test_patchWorkload(t *testing.T) {
// assert deployment annotations
assert.Equal(t, "newValue", annotations["newAnnotation"])
// assert pod annotations
assert.Equal(t, "newValue", podAnnotations["newAnnotation"])
assert.Equal(t, "newValue", podAnnotations["newPodAnnotation"])
assert.Equal(t, "oldValue", podLabels["oldLabel"])
})

Expand Down
Loading