Skip to content

Commit 5f748b5

Browse files
committed
packages/genpolicy: support GPU containers
This adds the required patches to Genpolicy to allow launching GPU containers on bare-metal Contrast.
1 parent c201276 commit 5f748b5

File tree

4 files changed

+168
-13
lines changed

4 files changed

+168
-13
lines changed

packages/by-name/kata/genpolicy/genpolicy_settings_coordinator.patch

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
diff --git a/genpolicy-settings.json b/genpolicy-settings.json
2-
index 4e9f6481d..7d0356b90 100644
2+
index 50406e8ac..8201465ab 100644
33
--- a/genpolicy-settings.json
44
+++ b/genpolicy-settings.json
5-
@@ -333,7 +333,8 @@
6-
"^AZURE_CLIENT_ID=[A-Fa-f0-9-]*$",
5+
@@ -337,7 +337,8 @@
76
"^AZURE_TENANT_ID=[A-Fa-f0-9-]*$",
87
"^AZURE_FEDERATED_TOKEN_FILE=/var/run/secrets/azure/tokens/azure-identity-token$",
9-
- "^AZURE_AUTHORITY_HOST=https://login\\.microsoftonline\\.com/$"
10-
+ "^AZURE_AUTHORITY_HOST=https://login\\.microsoftonline\\.com/$",
8+
"^AZURE_AUTHORITY_HOST=https://login\\.microsoftonline\\.com/$",
9+
- "^PCI_RESOURCE_NVIDIA_COM.*=[a-fA-F0-9:.-]*$"
10+
+ "^PCI_RESOURCE_NVIDIA_COM.*=[a-fA-F0-9:.-]*$",
1111
+ "^CONTRAST_[A-Z0-9_]*=.*$"
1212
]
1313
},
1414
"CopyFileRequest": [
15-
@@ -343,9 +343,9 @@
16-
"allowed_commands": [],
15+
@@ -348,7 +349,7 @@
1716
"regex": []
1817
},
1918
"CloseStdinRequest": false,

packages/by-name/kata/genpolicy/genpolicy_settings_dev.patch

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
diff --git a/genpolicy-settings.json b/genpolicy-settings.json
2-
index 4e9f6481d..64f16a760 100644
2+
index 50406e8ac..77f13c4d3 100644
33
--- a/genpolicy-settings.json
44
+++ b/genpolicy-settings.json
5-
@@ -333,7 +333,8 @@
6-
"^AZURE_CLIENT_ID=[A-Fa-f0-9-]*$",
5+
@@ -337,7 +337,8 @@
76
"^AZURE_TENANT_ID=[A-Fa-f0-9-]*$",
87
"^AZURE_FEDERATED_TOKEN_FILE=/var/run/secrets/azure/tokens/azure-identity-token$",
9-
- "^AZURE_AUTHORITY_HOST=https://login\\.microsoftonline\\.com/$"
10-
+ "^AZURE_AUTHORITY_HOST=https://login\\.microsoftonline\\.com/$",
8+
"^AZURE_AUTHORITY_HOST=https://login\\.microsoftonline\\.com/$",
9+
- "^PCI_RESOURCE_NVIDIA_COM.*=[a-fA-F0-9:.-]*$"
10+
+ "^PCI_RESOURCE_NVIDIA_COM.*=[a-fA-F0-9:.-]*$",
1111
+ "^CONTRAST_[A-Z0-9_]*=.*$"
1212
]
1313
},
1414
"CopyFileRequest": [
15-
@@ -341,11 +342,13 @@
15+
@@ -345,11 +346,13 @@
1616
],
1717
"ExecProcessRequest": {
1818
"allowed_commands": [],
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2+
From: Moritz Sanft <58110325+msanft@users.noreply.github.com>
3+
Date: Mon, 20 Jan 2025 13:44:00 +0100
4+
Subject: [PATCH] genpolicy: support dynamic annotations
5+
6+
This adds support for handling annotations with dynamic keys to
7+
genpolicy. This is necessary for use-cases like GPU containers, where
8+
in-cluster components (i.e. post policy-generation) instrument
9+
containers with annotations with varying keys, like `cdi.k8s.io/vfioXY`,
10+
where `XY` corresponds to a dynamic ID.
11+
---
12+
src/tools/genpolicy/genpolicy-settings.json | 8 ++-
13+
src/tools/genpolicy/rules.rego | 56 +++++++++++++++++++--
14+
src/tools/genpolicy/src/policy.rs | 6 +++
15+
3 files changed, 64 insertions(+), 6 deletions(-)
16+
17+
diff --git a/src/tools/genpolicy/genpolicy-settings.json b/src/tools/genpolicy/genpolicy-settings.json
18+
index 9b95f9f7462717d04f0b9ce685d97c0455f949da..7dac0e5e0585c25e324a39656d1a2dcfa12e7d96 100644
19+
--- a/src/tools/genpolicy/genpolicy-settings.json
20+
+++ b/src/tools/genpolicy/genpolicy-settings.json
21+
@@ -309,7 +309,10 @@
22+
"CAP_PERFMON",
23+
"CAP_BPF",
24+
"CAP_CHECKPOINT_RESTORE"
25+
- ]
26+
+ ],
27+
+ "dynamic_annotations": {
28+
+ "^cdi\\.k8s\\.io\\/vfio[0-9]{2}$": "^nvidia.com/gpu=[0-9]+$"
29+
+ }
30+
},
31+
"kata_config": {
32+
"confidential_guest": false,
33+
@@ -333,7 +336,8 @@
34+
"^AZURE_CLIENT_ID=[A-Fa-f0-9-]*$",
35+
"^AZURE_TENANT_ID=[A-Fa-f0-9-]*$",
36+
"^AZURE_FEDERATED_TOKEN_FILE=/var/run/secrets/azure/tokens/azure-identity-token$",
37+
- "^AZURE_AUTHORITY_HOST=https://login\\.microsoftonline\\.com/$"
38+
+ "^AZURE_AUTHORITY_HOST=https://login\\.microsoftonline\\.com/$",
39+
+ "^PCI_RESOURCE_NVIDIA_COM.*=[a-fA-F0-9:.-]*$"
40+
]
41+
},
42+
"CopyFileRequest": [
43+
diff --git a/src/tools/genpolicy/rules.rego b/src/tools/genpolicy/rules.rego
44+
index 43cb19a56fe8ea5833708f0639c9e85ddd884cb3..271df2aebe05bd4bbd7aa396be24fb6fee0668bf 100644
45+
--- a/src/tools/genpolicy/rules.rego
46+
+++ b/src/tools/genpolicy/rules.rego
47+
@@ -199,26 +199,31 @@ allow_anno(p_oci, i_oci) {
48+
}
49+
allow_anno(p_oci, i_oci) {
50+
print("allow_anno 2: p Annotations =", p_oci.Annotations)
51+
+ p_dynamic_annotations := policy_data.common.dynamic_annotations
52+
+ print("allow_anno 2: p Dynamic Annotations =", p_dynamic_annotations)
53+
+
54+
+ i_annotations := i_oci.Annotations
55+
print("allow_anno 2: i Annotations =", i_oci.Annotations)
56+
57+
- i_keys := object.keys(i_oci.Annotations)
58+
+ i_keys := object.keys(i_annotations)
59+
print("allow_anno 2: i keys =", i_keys)
60+
61+
every i_key in i_keys {
62+
- allow_anno_key(i_key, p_oci)
63+
+ allow_anno_key(i_key, p_oci, p_dynamic_annotations)
64+
+ allow_dynamic_anno_value(i_key, i_annotations, p_dynamic_annotations)
65+
}
66+
67+
print("allow_anno 2: true")
68+
}
69+
70+
-allow_anno_key(i_key, p_oci) {
71+
+allow_anno_key(i_key, p_oci, p_dynamic_annotations) {
72+
print("allow_anno_key 1: i key =", i_key)
73+
74+
startswith(i_key, "io.kubernetes.cri.")
75+
76+
print("allow_anno_key 1: true")
77+
}
78+
-allow_anno_key(i_key, p_oci) {
79+
+allow_anno_key(i_key, p_oci, p_dynamic_annotations) {
80+
print("allow_anno_key 2: i key =", i_key)
81+
82+
some p_key, _ in p_oci.Annotations
83+
@@ -227,6 +232,49 @@ allow_anno_key(i_key, p_oci) {
84+
print("allow_anno_key 2: true")
85+
}
86+
87+
+allow_anno_key(i_key, p_oci, p_dynamic_annotations) {
88+
+ print("allow_anno_key 3: i key =", i_key)
89+
+
90+
+ some p_key, _ in p_dynamic_annotations
91+
+ regex.match(p_key, i_key)
92+
+
93+
+ print("allow_anno_key 3: true")
94+
+}
95+
+
96+
+
97+
+# Account for containers without dynamic annotations
98+
+# at all..
99+
+allow_dynamic_anno_value(i_key, i_annotations, p_dynamic_annotations) {
100+
+ print("allow_dynamic_anno_value 1: i key =", i_key)
101+
+
102+
+ not p_dynamic_annotations
103+
+
104+
+ print("allow_dynamic_anno_value 1: true")
105+
+}
106+
+# ..for annotations which do not have a corresponding
107+
+# dynamic annotation set in the settings...
108+
+allow_dynamic_anno_value(i_key, i_annotations, p_dynamic_annotations) {
109+
+ print("allow_dynamic_anno_value 2: i key =", i_key)
110+
+
111+
+ every p_key, _ in p_dynamic_annotations {
112+
+ not regex.match(p_key, i_key)
113+
+ }
114+
+
115+
+ print("allow_dynamic_anno_value 2: true")
116+
+}
117+
+# ...and check those which do.
118+
+allow_dynamic_anno_value(i_key, i_annotations, p_dynamic_annotations) {
119+
+ print("allow_dynamic_anno_value 3: i key =", i_key)
120+
+
121+
+ some p_key, p_value in p_dynamic_annotations
122+
+ regex.match(p_key, i_key)
123+
+
124+
+ i_value := i_annotations[i_key]
125+
+ regex.match(p_value, i_value)
126+
+
127+
+ print("allow_dynamic_anno_value 3: true")
128+
+}
129+
+
130+
# Get the value of the S_NAME_KEY annotation and
131+
# correlate it with other annotations and process fields.
132+
allow_by_anno(p_container, i_oci, i_storages) {
133+
diff --git a/src/tools/genpolicy/src/policy.rs b/src/tools/genpolicy/src/policy.rs
134+
index e2012bf6f2d80ffea678a38803d8e85f5369b9dc..80bb6a63b915fa021e60f2b1d60e4bb32b67ba19 100644
135+
--- a/src/tools/genpolicy/src/policy.rs
136+
+++ b/src/tools/genpolicy/src/policy.rs
137+
@@ -392,6 +392,12 @@ pub struct CommonData {
138+
139+
/// Default capabilities for a privileged container.
140+
pub privileged_caps: Vec<String>,
141+
+
142+
+ /// Dynamic annotations contains arbitrary metadata for the container.
143+
+ /// It is different to `KataSpec.Annotations` in that it allows dynamic keys *and*
144+
+ /// values, and that they are checked for *all* keys, whereas `Annotations`
145+
+ /// only allows dynamic values, and only checks them for certain keys at all.
146+
+ pub dynamic_annotations: BTreeMap<String, String>,
147+
}
148+
149+
/// Configuration from "kubectl config".

packages/by-name/kata/kata-runtime/package.nix

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@ buildGoModule rec {
124124
# TODO(msanft): Get native CDI working, which will allow us to drop this patch / undo the revert.
125125
# See https://dev.azure.com/Edgeless/Edgeless/_workitems/edit/5061
126126
./0019-agent-remove-CDI-support.patch
127+
128+
# This adds support for annotations with dynamic keys *and* values to Genpolicy.
129+
# This is required for e.g. GPU containers, which get annotated by an in-cluster
130+
# component (i.e. after policy generation based on the Pod spec) with an annotation
131+
# like `cdi.k8s.io/vfioXY`, where `XY` corresponds to a dynamic ID.
132+
# Upstream issue: https://github.com/kata-containers/kata-containers/issues/10745
133+
./0020-genpolicy-support-dynamic-annotations.patch
127134
];
128135
};
129136

0 commit comments

Comments
 (0)