Skip to content
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
4 changes: 3 additions & 1 deletion util/reconciliation_utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,9 @@ func GetOwnerLabel(completeResourceName string) map[string]string {
lenCompleteResourceName = lenCompleteResourceName - 63
j = 63 * (i + 1)
}
label[LabelManagedBy+"-"+fmt.Sprint(i)] = completeResourceName[j:]
if j < len(completeResourceName) {
label[LabelManagedBy+"-"+fmt.Sprint(i)] = completeResourceName[j:]
}
} else {
label[LabelManagedBy] = completeResourceName
}
Expand Down
72 changes: 72 additions & 0 deletions util/reconciliation_utility_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package util

import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetOwnerLabel(t *testing.T) {
tests := []struct {
name string
input string
expected map[string]string
}{
{
name: "name shorter than 63 chars fits in one label",
input: strings.Repeat("a", 50),
expected: map[string]string{
LabelResourceOwner: LabelValueResourceOwner,
LabelManagedBy: strings.Repeat("a", 50),
},
},
{
name: "name exactly 63 chars fits in one label",
input: strings.Repeat("a", 63),
expected: map[string]string{
LabelResourceOwner: LabelValueResourceOwner,
LabelManagedBy: strings.Repeat("a", 63),
},
},
{
name: "name 64 chars splits across two labels",
input: strings.Repeat("a", 63) + "b",
expected: map[string]string{
LabelResourceOwner: LabelValueResourceOwner,
LabelManagedBy: strings.Repeat("a", 63),
LabelManagedBy + "-1": "b",
},
},
{
// 126 = 2 × 63: post-loop would produce an empty-value label without the fix
name: "name exactly 126 chars produces exactly two name labels with no empty value",
input: strings.Repeat("a", 63) + strings.Repeat("b", 63),
expected: map[string]string{
LabelResourceOwner: LabelValueResourceOwner,
LabelManagedBy: strings.Repeat("a", 63),
LabelManagedBy + "-1": strings.Repeat("b", 63),
},
},
{
name: "name 127 chars splits across three labels",
input: strings.Repeat("a", 63) + strings.Repeat("b", 63) + "c",
expected: map[string]string{
LabelResourceOwner: LabelValueResourceOwner,
LabelManagedBy: strings.Repeat("a", 63),
LabelManagedBy + "-1": strings.Repeat("b", 63),
LabelManagedBy + "-2": "c",
},
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result := GetOwnerLabel(tc.input)
assert.Equal(t, tc.expected, result)
for k, v := range result {
assert.NotEmpty(t, v, "label key %q has empty value", k)
}
})
}
}