Skip to content

Commit 4c3dc10

Browse files
committed
krun: parse annotations for krun.{cpus, ram_mib}
Support the use of OCI annotations as a way for the container engine to configure the microVM's CPU count and RAM amount via `krun.cpus` and `krun.ram_mib` respectively. Signed-off-by: Jake Correnti <[email protected]>
1 parent a58c849 commit 4c3dc10

File tree

1 file changed

+41
-11
lines changed

1 file changed

+41
-11
lines changed

src/libcrun/handlers/krun.c

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,47 @@ libkrun_read_vm_config (yajl_val *config_tree, libcrun_error_t *err)
186186
}
187187

188188
static int
189-
libkrun_configure_vm (uint32_t ctx_id, void *handle, bool *configured, yajl_val *config_tree, libcrun_error_t *err)
189+
libkrun_parse_resource_configuration (yajl_val *config_tree, libcrun_container_t *container, char *annotation, const char *path[], int default_val)
190+
{
191+
char *annotation_val;
192+
char *endptr;
193+
int resource_val = default_val;
194+
yajl_val krun_file_val = NULL;
195+
196+
annotation_val = (char *) find_annotation (container, annotation);
197+
if (annotation_val != NULL)
198+
{
199+
errno = 0;
200+
resource_val = strtol (annotation_val, &endptr, 10);
201+
if (errno != 0 || endptr == annotation_val || *endptr != '\0')
202+
{
203+
fprintf (stderr, "failed to convert %s value to an integer: falling back to the default value %d\n", annotation, default_val);
204+
return default_val;
205+
}
206+
}
207+
else
208+
{
209+
krun_file_val = yajl_tree_get (*config_tree, path, yajl_t_number);
210+
if (! YAJL_IS_INTEGER (krun_file_val))
211+
{
212+
fprintf (stderr, "failed to convert %s value in krun_vm.json to an integer: falling back to the default value %d\n", path[0], default_val);
213+
return default_val;
214+
}
215+
216+
if (krun_file_val != NULL)
217+
resource_val = YAJL_GET_INTEGER (krun_file_val);
218+
}
219+
220+
return resource_val;
221+
}
222+
223+
static int
224+
libkrun_configure_vm (uint32_t ctx_id, void *handle, bool *configured, yajl_val *config_tree, libcrun_container_t *container, libcrun_error_t *err)
190225
{
191226
int32_t (*krun_set_vm_config) (uint32_t ctx_id, uint8_t num_vcpus, uint32_t ram_mib);
192-
yajl_val cpus = NULL;
193-
yajl_val ram_mib = NULL;
227+
int cpus, ram_mib, ret;
194228
const char *path_cpus[] = { "cpus", (const char *) 0 };
195229
const char *path_ram_mib[] = { "ram_mib", (const char *) 0 };
196-
int ret;
197230

198231
if (*config_tree == NULL)
199232
return 0;
@@ -206,18 +239,15 @@ libkrun_configure_vm (uint32_t ctx_id, void *handle, bool *configured, yajl_val
206239
if (UNLIKELY (ret))
207240
return ret;
208241

209-
cpus = yajl_tree_get (*config_tree, path_cpus, yajl_t_number);
210-
ram_mib = yajl_tree_get (*config_tree, path_ram_mib, yajl_t_number);
211-
/* Both cpus and ram_mib must be present at the same time */
212-
if (cpus == NULL || ram_mib == NULL || ! YAJL_IS_INTEGER (cpus) || ! YAJL_IS_INTEGER (ram_mib))
213-
return 0;
242+
cpus = libkrun_parse_resource_configuration (config_tree, container, "krun.cpus", path_cpus, 1);
243+
ram_mib = libkrun_parse_resource_configuration (config_tree, container, "krun.ram_mib", path_ram_mib, 1024);
214244

215245
krun_set_vm_config = dlsym (handle, "krun_set_vm_config");
216246

217247
if (krun_set_vm_config == NULL)
218248
return crun_make_error (err, 0, "could not find symbol in the krun library");
219249

220-
ret = krun_set_vm_config (ctx_id, YAJL_GET_INTEGER (cpus), YAJL_GET_INTEGER (ram_mib));
250+
ret = krun_set_vm_config (ctx_id, cpus, ram_mib);
221251
if (UNLIKELY (ret < 0))
222252
return crun_make_error (err, -ret, "could not set krun vm configuration");
223253

@@ -396,7 +426,7 @@ libkrun_exec (void *cookie, libcrun_container_t *container, const char *pathname
396426
error (EXIT_FAILURE, -ret, "could not set enclave execution arguments");
397427
}
398428

399-
ret = libkrun_configure_vm (ctx_id, handle, &configured, &config_tree, &err);
429+
ret = libkrun_configure_vm (ctx_id, handle, &configured, &config_tree, container, &err);
400430
if (UNLIKELY (ret))
401431
{
402432
libcrun_error_t *tmp_err = &err;

0 commit comments

Comments
 (0)