Skip to content
This repository was archived by the owner on Feb 28, 2023. It is now read-only.

Commit 9c107b9

Browse files
committed
Generate prefixed names that are no longer than 64 characters
Drop API group for names generated for cluster-scoped configs, if `<kind>-<apigroup>-<resourcename>` would be too long, and use `<kind>-<resourcename>` instead to still have some protection against collisions. If all prefixing results in names that are longer than 64 characters, fall back to just using the target resource name.
1 parent 24b4fdb commit 9c107b9

1 file changed

Lines changed: 22 additions & 16 deletions

File tree

lib/resource-locker.libjsonnet

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -98,23 +98,29 @@ local replaceColon(str) =
9898
local rl_obj_name(objdata) =
9999
// Some objects like ClusterRoleBinding can contain colons.
100100
local name = replaceColon(objdata.name);
101-
if objdata.namespace != null then
102-
'%s-%s' % [ objdata.namespace, name ]
103-
else
104-
// Reduce potential for name collisions when generating names for
105-
// cluster-scoped ResourceLocker configurations.
106-
local prefix =
107-
if objdata.apigroup != '' then
108-
'%s-%s' % [
109-
std.asciiLower(objdata.kind),
110-
std.strReplace(objdata.apigroup, '.', '-'),
111-
]
101+
local n =
102+
if objdata.namespace != null then
103+
'%s-%s' % [ objdata.namespace, name ]
104+
else
105+
// Reduce potential for name collisions when generating names for
106+
// cluster-scoped ResourceLocker configurations.
107+
local prefix =
108+
if objdata.apigroup != '' &&
109+
std.length(objdata.apigroup + objdata.kind + name) <= 61
110+
then
111+
'%s-%s' % [
112+
std.asciiLower(objdata.kind),
113+
std.strReplace(objdata.apigroup, '.', '-'),
114+
]
115+
else
116+
std.asciiLower(objdata.kind);
117+
if std.length(prefix + name) >= 63 then
118+
name
112119
else
113-
std.asciiLower(objdata.kind);
114-
'%s-%s' % [
115-
prefix,
116-
name,
117-
];
120+
'%s-%s' % [ prefix, name ];
121+
122+
assert std.length(n) <= 63;
123+
n;
118124

119125
local rbac_objs(objdata, verbs=[ 'create', 'get', 'update', 'patch' ]) =
120126
local dest_ns = objdata.namespace;

0 commit comments

Comments
 (0)