Skip to content

Commit

Permalink
Merge pull request #2931 from Poor12/automated-cherry-pick-of-#2919-u…
Browse files Browse the repository at this point in the history
…pstream-release-1.4

Automated cherry pick of #2919: replace colon with point in the rbac resource name
  • Loading branch information
karmada-bot authored Dec 9, 2022
2 parents 0e6f6eb + 5b7a976 commit 1a61b30
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
10 changes: 10 additions & 0 deletions pkg/util/names/names.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,5 +154,15 @@ func GeneratePolicyName(namespace, name, gvk string) string {
hash := fnv.New32a()
hashutil.DeepHashObject(hash, namespace+gvk)

// The name of resources, like 'Role'/'ClusterRole'/'RoleBinding'/'ClusterRoleBinding',
// may contain symbols(like ':') that are not allowed by CRD resources which require the
// name can be used as a DNS subdomain name. So, we need to replace it.
// These resources may also allow for other characters(like '&','$') that are not allowed
// by CRD resources, we only handle the most common ones now for performance concerns.
// For more information about the DNS subdomain name, please refer to
// https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#dns-subdomain-names.
if strings.Contains(name, ":") {
name = strings.ReplaceAll(name, ":", ".")
}
return strings.ToLower(fmt.Sprintf("%s-%s", name, rand.SafeEncodeString(fmt.Sprint(hash.Sum32()))))
}
7 changes: 7 additions & 0 deletions pkg/util/names/names_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,13 @@ func TestGeneratePolicyName(t *testing.T) {
gvk: "rand",
expected: "foo-b4978784",
},
{
name: "generate policy name with :",
namespace: "ns-foo",
resourcename: "system:foo",
gvk: "rand",
expected: "system.foo-b4978784",
},
}
for _, test := range tests {
got := GeneratePolicyName(test.namespace, test.resourcename, test.gvk)
Expand Down

0 comments on commit 1a61b30

Please sign in to comment.