Skip to content

Commit cb30984

Browse files
butler54claude
andcommitted
fix: switch initdata injection from MutatingPolicy to ClusterPolicy
The policies.kyverno.io/v1 MutatingPolicy API doesn't support ConfigMap lookups (namespaceObject.get is not available). Switch to kyverno.io/v1 ClusterPolicy which supports configMap context lookups and has autogen for Deployments/StatefulSets/DaemonSets/Jobs. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent ea2d8fd commit cb30984

1 file changed

Lines changed: 38 additions & 44 deletions

File tree

Lines changed: 38 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,47 @@
1-
apiVersion: policies.kyverno.io/v1
2-
kind: MutatingPolicy
1+
apiVersion: kyverno.io/v1
2+
kind: ClusterPolicy
33
metadata:
44
name: inject-coco-initdata
55
annotations:
66
policies.kyverno.io/title: Inject CoCo InitData
77
policies.kyverno.io/category: Confidential Computing
88
policies.kyverno.io/severity: medium
9-
policies.kyverno.io/subject: Deployment
9+
policies.kyverno.io/subject: Pod,Deployment
1010
policies.kyverno.io/description: >-
11-
Injects cc_init_data annotation into Deployment pod templates with a kata
12-
runtime class by reading from a ConfigMap specified via the
13-
coco.io/initdata-configmap annotation on the pod template.
14-
Adapted from upstream kyverno inject-coco-initdata policy.
11+
Injects cc_init_data annotation into pods with a kata runtime class
12+
by reading from a ConfigMap specified via the coco.io/initdata-configmap
13+
annotation. Kyverno autogen extends this to Deployments, StatefulSets,
14+
DaemonSets, and Jobs automatically.
1515
argocd.argoproj.io/sync-wave: "1"
16+
pod-policies.kyverno.io/autogen-controllers: Deployment,StatefulSet,DaemonSet,Job
1617
spec:
17-
matchConstraints:
18-
resourceRules:
19-
- apiGroups: ["apps"]
20-
apiVersions: ["v1"]
21-
operations: ["CREATE", "UPDATE"]
22-
resources: ["deployments"]
23-
matchConditions:
24-
- name: has-kata-runtime
25-
expression: >-
26-
has(object.spec.template.spec.runtimeClassName) &&
27-
(object.spec.template.spec.runtimeClassName == "kata" || object.spec.template.spec.runtimeClassName == "kata-cc")
28-
- name: has-initdata-configmap-annotation
29-
expression: >-
30-
has(object.spec.template.metadata.annotations) &&
31-
'coco.io/initdata-configmap' in object.spec.template.metadata.annotations &&
32-
object.spec.template.metadata.annotations['coco.io/initdata-configmap'] != ''
33-
- name: no-existing-cc-init-data
34-
expression: >-
35-
!has(object.spec.template.metadata.annotations) ||
36-
!('io.katacontainers.config.hypervisor.cc_init_data' in object.spec.template.metadata.annotations)
37-
variables:
38-
- name: configMapName
39-
expression: "object.spec.template.metadata.annotations['coco.io/initdata-configmap']"
40-
- name: configMap
41-
expression: >-
42-
namespaceObject.get('configmaps', variables.configMapName)
43-
mutations:
44-
- patchType: JSONPatch
45-
jsonPatch:
46-
expression: >-
47-
[
48-
JSONPatch{
49-
op: "add",
50-
path: "/spec/template/metadata/annotations/io.katacontainers.config.hypervisor.cc_init_data",
51-
value: variables.configMap.data['INITDATA']
52-
}
53-
]
18+
rules:
19+
- name: inject-initdata
20+
match:
21+
any:
22+
- resources:
23+
kinds:
24+
- Pod
25+
operations:
26+
- CREATE
27+
preconditions:
28+
all:
29+
- key: "{{ "{{" }}request.object.spec.runtimeClassName || '' {{ "}}" }}"
30+
operator: AnyIn
31+
value: ["kata", "kata-cc"]
32+
- key: "{{ "{{" }}request.object.metadata.annotations.\"coco.io/initdata-configmap\" || '' {{ "}}" }}"
33+
operator: NotEquals
34+
value: ""
35+
- key: "{{ "{{" }}request.object.metadata.annotations.\"io.katacontainers.config.hypervisor.cc_init_data\" || '' {{ "}}" }}"
36+
operator: Equals
37+
value: ""
38+
context:
39+
- name: initdata
40+
configMap:
41+
name: "{{ "{{" }}request.object.metadata.annotations.\"coco.io/initdata-configmap\"{{ "}}" }}"
42+
namespace: "{{ "{{" }}request.namespace{{ "}}" }}"
43+
mutate:
44+
patchStrategicMerge:
45+
metadata:
46+
annotations:
47+
io.katacontainers.config.hypervisor.cc_init_data: "{{ "{{" }}initdata.data.INITDATA{{ "}}" }}"

0 commit comments

Comments
 (0)