Skip to content

Commit 6a07861

Browse files
committed
Test resourcelocker replacement
1 parent 544123d commit 6a07861

15 files changed

+358
-1
lines changed

class/defaults.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
parameters:
22
kyverno:
3+
_metadata:
4+
replaces: resource-locker
5+
library_aliases:
6+
resource-locker.libjsonnet: kyverno-resource-locker-migrate.libsonnet
7+
38
namespace: syn-kyverno
49
images:
510
kyverno:
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
std.trace(
2+
'importing resource-locker.libjsonnet is deprecated, ' +
3+
"please switch to `import 'patch-operator.libsonnet'`. " +
4+
'See https://hub.syn.tools/patch-operator/how-tos/migrating-from-resource-locker.html for more details.',
5+
import 'kyverno.libsonnet'
6+
)
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* \file Library with public methods provided by component patch-operator.
3+
*/
4+
5+
local kap = import 'lib/kapitan.libjsonnet';
6+
local kube = import 'lib/kube.libjsonnet';
7+
local kyverno = import 'lib/kyverno.libsonnet';
8+
9+
local inv = kap.inventory();
10+
local kyverno_params = inv.parameters.kyverno;
11+
local namespace = kyverno_params.namespace;
12+
local instance = inv.parameters._instance;
13+
14+
local apiVersion = 'redhatcop.redhat.io/v1alpha1';
15+
16+
local render_patch(patch, _, patch_id='patch1') =
17+
{ [patch_id]: patch };
18+
19+
20+
local targetData(obj) =
21+
local apigrp = std.split(obj.apiVersion, '/')[0];
22+
{
23+
apiVersion: obj.apiVersion,
24+
apigroup:: if apigrp == 'v1' then '' else apigrp,
25+
kind: obj.kind,
26+
name: obj.metadata.name,
27+
namespace: if std.objectHas(obj.metadata, 'namespace') then obj.metadata.namespace,
28+
};
29+
30+
local patchName(targetObj) =
31+
local target = targetData(targetObj);
32+
33+
// Some objects like ClusterRoleBinding can contain colons.
34+
local name = std.strReplace(target.name, ':', '-');
35+
local unhashed = '%s-%s-%s-%s-%s' % [ instance, target.kind, target.apigroup, target.namespace, name ];
36+
// Take 15 characters of the md5 hash, to leave room for a human-readable
37+
// prefix.
38+
local hashed = std.substr(std.md5(unhashed), 0, 15);
39+
40+
local prefix =
41+
local p =
42+
if target.namespace != null then
43+
// for namespaced objects, use `<ns>-<name>` as the prefix
44+
'%s-%s' % [ std.asciiLower(target.namespace), name ]
45+
else
46+
// for cluster-scoped objects, use `<kind>-<name>` as the prefix
47+
// We could also add `<apigroup>` in the prefix, but we don't
48+
// need to do this, since the apigroup is part of the hashed string.
49+
'%s-%s' % [ std.asciiLower(target.kind), name ];
50+
// Trim the prefix if it's too long, make sure the kind/namespace part of
51+
// the prefix remains.
52+
if std.length(p) > 31 then
53+
std.substr(p, 0, 31)
54+
else
55+
p;
56+
57+
local n = '%s-%s' % [ prefix, hashed ];
58+
59+
// We generate names with a max length of 47, so there's a few characters
60+
// left for adding `manager` in `clusterRoleName()` and `saname`.
61+
assert
62+
std.length(n) <= 47 :
63+
"name generated by rl_obj_name() is longer than 47 characters, this shouldn't happen";
64+
n;
65+
66+
local Patch(targetobj, patchTemplate, patchStrategy='application/strategic-merge-patch+json') =
67+
local name = patchName(targetobj);
68+
[
69+
kyverno.ClusterPolicy(name) {
70+
spec: {
71+
rules: [ {
72+
name: name,
73+
match: {
74+
all: [ {
75+
resources: {
76+
kinds: [ '%s/%s' % [ targetobj.apiVersion, targetobj.kind ] ],
77+
[if std.objectHas(targetobj.metadata, 'namespace') && targetobj.metadata.namespace != null then 'namespaces']: [ targetobj.metadata.namespace ],
78+
names: [ targetobj.metadata.name ],
79+
},
80+
} ],
81+
},
82+
mutate: {
83+
patchStrategicMerge: patchTemplate,
84+
},
85+
} ],
86+
},
87+
},
88+
];
89+
90+
local Resource(obj) =
91+
error "kyverno doesn't support kind `Resource`, please manage full resources directly in your component";
92+
93+
{
94+
apiVersion: apiVersion,
95+
Resource: Resource,
96+
Patch: Patch,
97+
renderPatch: render_patch,
98+
}

lib/kyverno.libsonnet

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,5 @@ local GenerateRequest(name) = kube._Object('kyverno.io/v1alpha1', 'GenerateReque
5858
ClusterReportChangeRequest: ClusterReportChangeRequest,
5959
GenerateRequest: GenerateRequest,
6060
}
61+
+
62+
import 'kyverno-resource-locker.libsonnet'

tests/golden/lib/kyverno/apps/kyverno.yaml

Whitespace-only changes.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
apiVersion: kyverno.io/v1
2+
kind: ClusterPolicy
3+
metadata:
4+
annotations: {}
5+
labels:
6+
name: foo-test-5f124f8af545124
7+
name: foo-test-5f124f8af545124
8+
spec:
9+
rules:
10+
- match:
11+
all:
12+
- resources:
13+
kinds:
14+
- rbac.authorization.k8s.io/v1/ClusterRoleBinding
15+
names:
16+
- test
17+
namespaces:
18+
- foo
19+
mutate:
20+
patchStrategicMerge:
21+
metadata:
22+
annotations:
23+
patched: ''
24+
name: foo-test-5f124f8af545124
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
apiVersion: kyverno.io/v1
2+
kind: ClusterPolicy
3+
metadata:
4+
annotations: {}
5+
labels:
6+
name: foo-test-2647e277de7e28f
7+
name: foo-test-2647e277de7e28f
8+
spec:
9+
rules:
10+
- match:
11+
all:
12+
- resources:
13+
kinds:
14+
- rbac.authorization.k8s.io/v1/ClusterRole
15+
names:
16+
- test
17+
namespaces:
18+
- foo
19+
mutate:
20+
patchStrategicMerge:
21+
metadata:
22+
annotations:
23+
patched: ''
24+
name: foo-test-2647e277de7e28f
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
apiVersion: kyverno.io/v1
2+
kind: ClusterPolicy
3+
metadata:
4+
annotations: {}
5+
labels:
6+
name: foo-test-748008595ba4d68
7+
name: foo-test-748008595ba4d68
8+
spec:
9+
rules:
10+
- match:
11+
all:
12+
- resources:
13+
kinds:
14+
- rbac.authorization.k8s.io/v1/RoleBinding
15+
names:
16+
- test
17+
namespaces:
18+
- foo
19+
mutate:
20+
patchStrategicMerge:
21+
metadata:
22+
annotations:
23+
patched: ''
24+
name: foo-test-748008595ba4d68
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
apiVersion: kyverno.io/v1
2+
kind: ClusterPolicy
3+
metadata:
4+
annotations: {}
5+
labels:
6+
name: foo-test-124e2e3e489a95e
7+
name: foo-test-124e2e3e489a95e
8+
spec:
9+
rules:
10+
- match:
11+
all:
12+
- resources:
13+
kinds:
14+
- rbac.authorization.k8s.io/v1/Role
15+
names:
16+
- test
17+
namespaces:
18+
- foo
19+
mutate:
20+
patchStrategicMerge:
21+
metadata:
22+
annotations:
23+
patched: ''
24+
name: foo-test-124e2e3e489a95e
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
apiVersion: kyverno.io/v1
2+
kind: ClusterPolicy
3+
metadata:
4+
annotations: {}
5+
labels:
6+
name: clusterrolebinding-system-build-7d296671e024f88
7+
name: clusterrolebinding-system-build-7d296671e024f88
8+
spec:
9+
rules:
10+
- match:
11+
all:
12+
- resources:
13+
kinds:
14+
- rbac.authorization.k8s.io/v1/ClusterRoleBinding
15+
names:
16+
- system:build-strategy-docker-binding
17+
mutate:
18+
patchStrategicMerge:
19+
annotations:
20+
rbac.authorization.kubernetes.io/autoupdate: 'false'
21+
name: clusterrolebinding-system-build-7d296671e024f88

0 commit comments

Comments
 (0)