Skip to content

Commit f7e0cc1

Browse files
authored
Merge pull request #3213 from whitewindmills/automated-cherry-pick-of-#3201-upstream-release-1.2
Automated cherry pick of #3201: fix the bug that the work object may contain uppercase
2 parents ae5e32c + a50672e commit f7e0cc1

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

pkg/util/names/names.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@ func GenerateBindingReferenceKey(namespace, name string) string {
7373

7474
// GenerateWorkName will generate work name by its name and the hash of its namespace, kind and name.
7575
func GenerateWorkName(kind, name, namespace string) string {
76+
// The name of resources, like 'Role'/'ClusterRole'/'RoleBinding'/'ClusterRoleBinding',
77+
// may contain symbols(like ':' or uppercase upper case) that are not allowed by CRD resources which require the
78+
// name can be used as a DNS subdomain name. So, we need to replace it.
79+
// These resources may also allow for other characters(like '&','$') that are not allowed
80+
// by CRD resources, we only handle the most common ones now for performance concerns.
81+
// For more information about the DNS subdomain name, please refer to
82+
// https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#dns-subdomain-names.
83+
if strings.Contains(name, ":") {
84+
name = strings.ReplaceAll(name, ":", ".")
85+
}
86+
name = strings.ToLower(name)
87+
7688
var workName string
7789
if len(namespace) == 0 {
7890
workName = strings.ToLower(name + "-" + kind)

pkg/util/names/names_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package names
33
import (
44
"fmt"
55
"hash/fnv"
6+
"strings"
67
"testing"
78

89
"k8s.io/apimachinery/pkg/util/rand"
@@ -177,7 +178,7 @@ func TestGenerateWorkName(t *testing.T) {
177178

178179
hash := fnv.New32a()
179180
hashutil.DeepHashObject(hash, test.workname)
180-
if result := fmt.Sprintf("%s-%s", test.name, rand.SafeEncodeString(fmt.Sprint(hash.Sum32()))); result != got {
181+
if result := fmt.Sprintf("%s-%s", strings.ToLower(test.name), rand.SafeEncodeString(fmt.Sprint(hash.Sum32()))); result != got {
181182
t.Errorf("Test %s failed: expected %v, but got %v", test.testCase, result, got)
182183
}
183184
}

0 commit comments

Comments
 (0)