Skip to content

Commit

Permalink
deploy pod label is back
Browse files Browse the repository at this point in the history
  • Loading branch information
kaovilai committed May 10, 2022
1 parent 4b237d6 commit f026cb5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 12 deletions.
11 changes: 9 additions & 2 deletions controllers/velero.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,11 +431,18 @@ func removeDuplicateValues(slice []string) []string {

func (r *DPAReconciler) customizeVeleroDeployment(dpa *oadpv1alpha1.DataProtectionApplication, veleroDeployment *appsv1.Deployment) error {
//append dpa labels
veleroDeployment.Labels = common.AppendLabels(veleroDeployment.Labels, r.getDpaAppLabels(dpa))
var err error
veleroDeployment.Labels, err = common.AppendUniqueLabels(veleroDeployment.Labels, r.getDpaAppLabels(dpa), )
if err != nil {
return fmt.Errorf("error appending veleroDeployment label: %v", err)
}
veleroDeployment.Spec.Selector = &metav1.LabelSelector{
MatchLabels: veleroDeployment.Labels,
}
veleroDeployment.Spec.Template.Labels = common.AppendLabels(veleroDeployment.Spec.Template.Labels, veleroDeployment.Labels)
veleroDeployment.Spec.Template.Labels, err = common.AppendUniqueLabels(veleroDeployment.Spec.Template.Labels, veleroDeployment.Labels)
if err != nil {
return fmt.Errorf("error appending veleroDeployment template label: %v", err)
}

isSTSNeeded := r.isSTSTokenNeeded(dpa.Spec.BackupLocations, dpa.Namespace)

Expand Down
2 changes: 2 additions & 0 deletions controllers/velero_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,7 @@ func TestDPAReconciler_buildVeleroDeployment(t *testing.T) {
"app.kubernetes.io/component": Server,
oadpv1alpha1.OadpOperatorLabel: "True",
"component": "velero",
"deploy": "velero",
},
Annotations: map[string]string{
"prometheus.io/scrape": "true",
Expand Down Expand Up @@ -919,6 +920,7 @@ func TestDPAReconciler_buildVeleroDeployment(t *testing.T) {
"app.kubernetes.io/component": Server,
oadpv1alpha1.OadpOperatorLabel: "True",
"component": "velero",
"deploy": "velero",
},
Annotations: map[string]string{
"prometheus.io/scrape": "true",
Expand Down
12 changes: 9 additions & 3 deletions pkg/common/common.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package common

import "fmt"

const (
Velero = "velero"
Restic = "restic"
Expand Down Expand Up @@ -44,12 +46,16 @@ const (
)

// append labels together
func AppendLabels(userLabels ...map[string]string) map[string]string {
func AppendUniqueLabels(userLabels ...map[string]string) (map[string]string, error) {
base := map[string]string{}
for _, labels := range userLabels {
for k, v := range labels {
base[k] = v
if base[k] == "" {
base[k] = v
} else if base[k] != v {
return nil, fmt.Errorf("duplicate label %s", k)
}
}
}
return base
return base, nil
}
31 changes: 24 additions & 7 deletions pkg/common/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ import (
"testing"
)

func TestAppendLabels(t *testing.T) {
func TestAppendUniqueLabels(t *testing.T) {
type args struct {
userLabels []map[string]string
}
tests := []struct {
name string
args args
want map[string]string
name string
args args
want map[string]string
wantErr bool
}{
{
name: "append labels together",
name: "append unique labels together",
args: args{
userLabels: []map[string]string{
{"a": "a"},
Expand All @@ -27,11 +28,27 @@ func TestAppendLabels(t *testing.T) {
"b": "b",
},
},
{
name: "should error when append duplicate label keys with different value together",
args: args{
userLabels: []map[string]string{
{"a": "a"},
{"a": "b"},
},
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := AppendLabels(tt.args.userLabels...); !reflect.DeepEqual(got, tt.want) {
t.Errorf("AppendLabels() = %v, want %v", got, tt.want)
got, err := AppendUniqueLabels(tt.args.userLabels...)
if (err != nil) != tt.wantErr {
t.Errorf("AppendUniqueLabels() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("AppendUniqueLabels() = %v, want %v", got, tt.want)
}
})
}
Expand Down

0 comments on commit f026cb5

Please sign in to comment.