1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
|
From 140166e0f385a2f23502efeeba4113536736c3c8 Mon Sep 17 00:00:00 2001
From: zhongtao <zhongtao17@huawei.com>
Date: Thu, 14 Nov 2024 16:45:08 +0800
Subject: [PATCH 150/156] add missing con linux info for nri module
Signed-off-by: zhongtao <zhongtao17@huawei.com>
---
src/daemon/common/nri/nri_convert.cc | 482 ++++++++++++++++++++++++++-
1 file changed, 480 insertions(+), 2 deletions(-)
diff --git a/src/daemon/common/nri/nri_convert.cc b/src/daemon/common/nri/nri_convert.cc
index 30caf1dd..d862d992 100644
--- a/src/daemon/common/nri/nri_convert.cc
+++ b/src/daemon/common/nri/nri_convert.cc
@@ -15,12 +15,16 @@
#include "nri_convert.h"
+#include <sys/stat.h>
+#include <sys/sysmacros.h>
+
#include "container_api.h"
#include "v1_cri_helpers.h"
#include "path.h"
#include "transform.h"
#include "nri_utils.h"
#include "cstruct_wrapper.h"
+#include "specs_api.h"
static int64_t DefaultOOMScoreAdj = 0;
@@ -385,6 +389,462 @@ error_out:
return false;
}
+static int ConvertDevice(const char *host_path, const char *container_path, const char *permissions,
+ nri_linux_device &device, nri_linux_device_cgroup &deviceCgroup)
+{
+ int ret = 0;
+ struct stat st;
+ const char *dev_type = NULL;
+ unsigned int file_mode = 0;
+
+ if (host_path == NULL) {
+ return -1;
+ }
+
+ ret = stat(host_path, &st);
+ if (ret < 0) {
+ ERROR("device %s no exists", host_path);
+ return -1;
+ }
+
+ file_mode = st.st_mode & 0777;
+
+ /* check device type first */
+ if (S_ISBLK(st.st_mode)) {
+ file_mode |= S_IFBLK;
+ dev_type = "b";
+ } else if (S_ISCHR(st.st_mode)) {
+ file_mode |= S_IFCHR;
+ dev_type = "c";
+ } else {
+ ERROR("Cannot determine the device number for device %s", host_path);
+ return -1;
+ }
+
+ /* fill spec dev */
+ device.major = (int64_t)major(st.st_rdev);
+ device.minor = (int64_t)minor(st.st_rdev);
+ device.uid = (uint32_t *)util_common_calloc_s(sizeof(uint32_t*));
+ if (device.uid == nullptr) {
+ ERROR("Out of memory");
+ return -1;
+ }
+ *(device.uid) = st.st_uid;
+ device.gid = (uint32_t *)util_common_calloc_s(sizeof(uint32_t*));
+ if (device.gid == nullptr) {
+ ERROR("Out of memory");
+ return -1;
+ }
+ *(device.gid) = st.st_gid;
+ device.file_mode = (uint32_t *)util_common_calloc_s(sizeof(uint32_t));
+ if (device.file_mode == nullptr) {
+ ERROR("Out of memory");
+ return -1;
+ }
+ *(device.file_mode) = (int)file_mode;
+ device.type = util_strdup_s(dev_type);
+ device.path = util_strdup_s(container_path);
+
+ /* fill spec cgroup dev */
+ deviceCgroup.allow = true;
+ deviceCgroup.access = util_strdup_s(permissions);
+ deviceCgroup.type = util_strdup_s(dev_type);
+ deviceCgroup.major = (int64_t *)util_common_calloc_s(sizeof(int64_t));
+ if (deviceCgroup.major == nullptr) {
+ ERROR("Out of memory");
+ return -1;
+ }
+ *(deviceCgroup.major) = (int64_t)major(st.st_rdev);
+ deviceCgroup.minor = (int64_t *)util_common_calloc_s(sizeof(int64_t));
+ if (deviceCgroup.minor == nullptr) {
+ ERROR("Out of memory");
+ return -1;
+ }
+ *(deviceCgroup.minor) = (int64_t)minor(st.st_rdev);
+
+ return 0;
+}
+
+static int ConvertHostConfigDevices(const host_config_devices_element *dev_map, nri_linux_device &device,
+ nri_linux_device_cgroup &deviceCgroup)
+{
+ return ConvertDevice(dev_map->path_on_host, dev_map->path_in_container,
+ dev_map->cgroup_permissions, device, deviceCgroup);
+}
+
+static int ConLinuxDeviceToNRI(const host_config *config, nri_container &con)
+{
+ size_t i;
+
+ if (config->devices_len == 0 && config->nri_devices_len == 0) {
+ return 0;
+ }
+ con.linux->devices = (nri_linux_device **)util_smart_calloc_s(sizeof(nri_linux_device *),
+ config->devices_len + config->nri_devices_len);
+ if (con.linux->devices == nullptr) {
+ ERROR("Out of memory");
+ return -1;
+ }
+
+ con.linux->resources->devices = (nri_linux_device_cgroup **)util_smart_calloc_s(sizeof(nri_linux_device_cgroup *),
+ config->devices_len);
+ if (con.linux->resources->devices == nullptr) {
+ ERROR("Out of memory");
+ return -1;
+ }
+
+ for (i = 0; i < config->devices_len; i++) {
+ nri_linux_device *device = (nri_linux_device *)util_common_calloc_s(sizeof(nri_linux_device));
+ if (device == nullptr) {
+ ERROR("Out of memory");
+ return -1;
+ }
+
+ nri_linux_device_cgroup *deviceCgroup = (nri_linux_device_cgroup *)util_common_calloc_s(sizeof(
+ nri_linux_device_cgroup));
+ if (deviceCgroup == nullptr) {
+ ERROR("Out of memory");
+ free_nri_linux_device(device);
+ return -1;
+ }
+
+ if (ConvertHostConfigDevices(config->devices[i], *device, *deviceCgroup) != 0) {
+ ERROR("Failed to convert host config devices");
+ free_nri_linux_device(device);
+ free_nri_linux_device_cgroup(deviceCgroup);
+ return -1;
+ }
+
+ con.linux->devices[i] = device;
+ con.linux->resources->devices[i] = deviceCgroup;
+ con.linux->devices_len++;
+ con.linux->resources->devices_len++;
+ }
+
+ for (i = 0; i < config->nri_devices_len; i++) {
+ nri_linux_device *device = (nri_linux_device *)util_common_calloc_s(sizeof(nri_linux_device));
+ if (device == nullptr) {
+ ERROR("Out of memory");
+ return -1;
+ }
+
+ device->file_mode = (uint32_t *)util_common_calloc_s(sizeof(uint32_t));
+ if (device->file_mode == nullptr) {
+ ERROR("Out of memory");
+ free_nri_linux_device(device);
+ return -1;
+ }
+ *(device->file_mode) = config->nri_devices[i]->file_mode;
+
+ device->path = util_strdup_s(config->nri_devices[i]->path);
+ device->type = util_strdup_s(config->nri_devices[i]->type);
+ device->major = config->nri_devices[i]->major;
+ device->minor = config->nri_devices[i]->minor;
+
+ device->uid = (uint32_t *)util_common_calloc_s(sizeof(uint32_t));
+ if (device->uid == nullptr) {
+ ERROR("Out of memory");
+ free_nri_linux_device(device);
+ return -1;
+ }
+ *(device->uid) = config->nri_devices[i]->uid;
+
+ device->gid = (uint32_t *)util_common_calloc_s(sizeof(uint32_t));
+ if (device->gid == nullptr) {
+ ERROR("Out of memory");
+ free_nri_linux_device(device);
+ return -1;
+ }
+ *(device->gid) = config->nri_devices[i]->gid;
+ con.linux->devices[i + config->devices_len] = device;
+ con.linux->devices_len++;
+ }
+
+ return 0;
+}
+
+static int ConvertCRIV1Devices(const ::runtime::v1::Device &dev_map, nri_linux_device &device,
+ nri_linux_device_cgroup &deviceCgroup)
+{
+ return ConvertDevice(dev_map.host_path().c_str(), dev_map.container_path().c_str(),
+ dev_map.permissions().c_str(), device, deviceCgroup);
+}
+
+static bool ConLinuxResourcesCpuToNRI(const host_config *config, nri_linux_cpu &cpu)
+{
+ cpu.shares = (uint64_t *)util_common_calloc_s(sizeof(uint64_t));
+ if (cpu.shares == nullptr) {
+ ERROR("Out of memory");
+ return false;
+ }
+ *(cpu.shares) = config->cpu_shares;
+
+ cpu.quota = (int64_t *)util_common_calloc_s(sizeof(int64_t));
+ if (cpu.quota == nullptr) {
+ ERROR("Out of memory");
+ return false;
+ }
+ *(cpu.quota) = config->cpu_quota;
+
+ cpu.period = (uint64_t *)util_common_calloc_s(sizeof(uint64_t));
+ if (cpu.period == nullptr) {
+ ERROR("Out of memory");
+ return false;
+ }
+ *(cpu.period) = config->cpu_period;
+
+ cpu.cpus = util_strdup_s(config->cpuset_cpus);
+ cpu.mems = util_strdup_s(config->cpuset_mems);
+
+ return true;
+}
+
+static bool ConLinuxResourcesMemoryToNRI(const host_config *config, nri_linux_memory &memory)
+{
+ memory.limit = (int64_t *)util_common_calloc_s(sizeof(int64_t));
+ if (memory.limit == nullptr) {
+ ERROR("Out of memory");
+ return false;
+ }
+ *(memory.limit) = config->memory;
+
+ memory.reservation = (int64_t *)util_common_calloc_s(sizeof(int64_t));
+ if (memory.reservation == nullptr) {
+ ERROR("Out of memory");
+ return false;
+ }
+
+ *(memory.reservation) = config->memory_reservation;
+
+ memory.swap = (int64_t *)util_common_calloc_s(sizeof(int64_t));
+ if (memory.swap == nullptr) {
+ ERROR("Out of memory");
+ return false;
+ }
+ *(memory.swap) = config->memory_swap;
+
+ memory.kernel = (int64_t *)util_common_calloc_s(sizeof(int64_t));
+ if (memory.kernel == nullptr) {
+ ERROR("Out of memory");
+ return false;
+ }
+ *(memory.kernel) = config->kernel_memory;
+
+ // isulad has not set kernel_tcp
+ memory.kernel_tcp = nullptr;
+
+ if (config->memory_swappiness != nullptr) {
+ memory.swappiness = (uint64_t *)util_common_calloc_s(sizeof(uint64_t));
+ if (memory.swappiness == nullptr) {
+ ERROR("Out of memory");
+ return false;
+ }
+ *(memory.swappiness) = *(config->memory_swappiness);
+ }
+
+ memory.disable_oom_killer = (uint8_t *)util_common_calloc_s(sizeof(uint8_t));
+ if (memory.disable_oom_killer == nullptr) {
+ ERROR("Out of memory");
+ return false;
+ }
+ *(memory.disable_oom_killer) = config->oom_kill_disable;
+
+ // isulad has not set use_hierarchy
+ memory.use_hierarchy = (uint8_t *)util_common_calloc_s(sizeof(uint8_t));
+ if (memory.use_hierarchy == nullptr) {
+ ERROR("Out of memory");
+ return false;
+ }
+ *(memory.use_hierarchy) = false;
+ return true;
+}
+
+auto ConLinuxResourcesToNRI(const host_config *config) -> nri_linux_resources *
+{
+ nri_linux_resources *resources = nullptr;
+ size_t i;
+
+ resources = init_nri_linux_resources();
+ if (resources == nullptr) {
+ ERROR("Failed to init nri linux resources");
+ return nullptr;
+ }
+
+ if (!ConLinuxResourcesCpuToNRI(config, *resources->cpu)) {
+ ERROR("Failed to transform cpu to nri");
+ goto error_out;
+ }
+
+ if (!ConLinuxResourcesMemoryToNRI(config, *resources->memory)) {
+ ERROR("Failed to transform memory to nri");
+ goto error_out;
+ }
+
+ resources->hugepage_limits = (nri_hugepage_limit **)util_smart_calloc_s(sizeof(nri_hugepage_limit *),
+ config->hugetlbs_len);
+ if (resources->hugepage_limits == nullptr) {
+ ERROR("Out of memory");
+ goto error_out;
+ }
+
+ for (i = 0; i < config->hugetlbs_len; i++) {
+ resources->hugepage_limits[i] = (nri_hugepage_limit *)util_common_calloc_s(sizeof(nri_hugepage_limit));
+ if (resources->hugepage_limits[i] == nullptr) {
+ ERROR("Out of memory");
+ goto error_out;
+ }
+ resources->hugepage_limits[i]->page_size = util_strdup_s(config->hugetlbs[i]->page_size);
+ resources->hugepage_limits[i]->limit = config->hugetlbs[i]->limit;
+ resources->hugepage_limits_len++;
+ }
+
+ // resources.blockio_class is not support
+ // resources.rdt_class is not support
+ // They are not standard fields in oci spec
+
+ if (dup_json_map_string_string(config->unified, resources->unified) != 0) {
+ ERROR("Failed to copy unified map");
+ goto error_out;
+ }
+
+ // resources.devices is set in ConLinuxDeviceToNRI
+
+ return resources;
+
+error_out:
+ free_nri_linux_resources(resources);
+ resources = nullptr;
+ return resources;
+}
+
+static bool ConLinuxToNRI(const char *id, const host_config *config, nri_container &con)
+{
+ con.linux = (nri_linux_container *)util_common_calloc_s(sizeof(nri_linux_container));
+ if (con.linux == nullptr) {
+ ERROR("Out of memory");
+ return false;
+ }
+
+ con.linux->resources = ConLinuxResourcesToNRI(config);
+ if (con.linux->resources == nullptr) {
+ ERROR("Failed to transform resources to nri for con : %s", id);
+ return false;
+ }
+
+ if (ConLinuxDeviceToNRI(config, con) != 0) {
+ ERROR("Failed to transform devices to nri for con : %s", id);
+ return false;
+ }
+
+ con.linux->oom_score_adj = (int64_t *)util_common_calloc_s(sizeof(int64_t));
+ if (con.linux->oom_score_adj == nullptr) {
+ ERROR("Out of memory");
+ return false;
+ }
+
+ *(con.linux->oom_score_adj) = config->oom_score_adj;
+
+ con.linux->cgroups_path = merge_container_cgroups_path(id, config);
+ if (con.linux->cgroups_path == NULL) {
+ WARN("nri container cgroups path is NULL");
+ }
+ return true;
+}
+
+static int ConConfigLinuxDeviceToNRI(const runtime::v1::ContainerConfig &containerConfig, nri_container &con)
+{
+ int i;
+ int conConfigDevicesSize = containerConfig.devices_size();
+
+ if (conConfigDevicesSize == 0) {
+ return 0;
+ }
+ con.linux->devices = (nri_linux_device **)util_smart_calloc_s(sizeof(nri_linux_device *), conConfigDevicesSize);
+ if (con.linux->devices == nullptr) {
+ ERROR("Out of memory");
+ return -1;
+ }
+
+ if (con.linux->resources == nullptr) {
+ con.linux->resources = init_nri_linux_resources();
+ if (con.linux->resources == nullptr) {
+ ERROR("Failed to init nri linux resources");
+ return -1;
+ }
+ }
+
+ con.linux->resources->devices = (nri_linux_device_cgroup **)util_smart_calloc_s(sizeof(nri_linux_device_cgroup *),
+ conConfigDevicesSize);
+ if (con.linux->resources->devices == nullptr) {
+ ERROR("Out of memory");
+ return -1;
+ }
+
+ for (i = 0; i < conConfigDevicesSize; i++) {
+ nri_linux_device *device = (nri_linux_device *)util_common_calloc_s(sizeof(nri_linux_device));
+ if (device == nullptr) {
+ ERROR("Out of memory");
+ return -1;
+ }
+
+ nri_linux_device_cgroup *deviceCgroup = (nri_linux_device_cgroup *)util_common_calloc_s(sizeof(
+ nri_linux_device_cgroup));
+ if (deviceCgroup == nullptr) {
+ ERROR("Out of memory");
+ free_nri_linux_device(device);
+ return -1;
+ }
+
+ if (ConvertCRIV1Devices(containerConfig.devices(i), *device, *deviceCgroup) != 0) {
+ ERROR("Failed to convert CRI v1 devices");
+ free_nri_linux_device(device);
+ free_nri_linux_device_cgroup(deviceCgroup);
+ return -1;
+ }
+
+ con.linux->devices[i] = device;
+ con.linux->resources->devices[i] = deviceCgroup;
+ con.linux->devices_len++;
+ con.linux->resources->devices_len++;
+ }
+
+ return 0;
+}
+
+static bool ConConfigLinuxToNRI(const runtime::v1::ContainerConfig &containerConfig, nri_container &con)
+{
+ const char *name = containerConfig.metadata().name().c_str();
+ con.linux = (nri_linux_container *)util_common_calloc_s(sizeof(nri_linux_container));
+ if (con.linux == nullptr) {
+ ERROR("Out of memory");
+ return false;
+ }
+
+ if (containerConfig.has_linux() && containerConfig.linux().has_resources()) {
+ con.linux->resources = LinuxResourcesToNRI(containerConfig.linux().resources());
+ if (con.linux->resources == nullptr) {
+ ERROR("Failed to transform resources to nri for con : %s", name);
+ return false;
+ }
+
+ con.linux->oom_score_adj = (int64_t *)util_common_calloc_s(sizeof(int64_t));
+ if (con.linux->oom_score_adj == nullptr) {
+ ERROR("Out of memory");
+ return false;
+ }
+ *(con.linux->oom_score_adj) = containerConfig.linux().resources().oom_score_adj();
+ }
+
+ if (ConConfigLinuxDeviceToNRI(containerConfig, con) != 0) {
+ ERROR("Failed to convert devices to nri for con : %s", name);
+ return false;
+ }
+
+ // ContainerToNRIByConfig is called when CreateContainer, and cannot get pid at this time
+ con.linux->cgroups_path = NULL;
+ return true;
+}
+
// container info is incomplete because container in excution is not created
auto ContainerToNRIByConConfig(const runtime::v1::ContainerConfig &containerConfig, nri_container &con) -> bool
{
@@ -395,6 +855,9 @@ auto ContainerToNRIByConConfig(const runtime::v1::ContainerConfig &containerConf
Errors tmpError;
+ // ContainerToNRIByConfig is called when CreateConatiner, and the status is 0(CONTAINER_UNKNOWN) at this time
+ con.state = 0;
+
con.labels = Transform::ProtobufMapToJsonMapForString(containerConfig.labels(), tmpError);
if (con.labels == nullptr) {
ERROR("Failed to transform labels to nri for con : %s, : %s", con.name, tmpError.GetMessage().c_str());
@@ -426,9 +889,18 @@ auto ContainerToNRIByConConfig(const runtime::v1::ContainerConfig &containerConf
ERROR("Failed to transform mounts to nri for con : %s", con.name);
return false;
}
- return true;
- // todo: can not get container hooks and pid from containerConfig
+ if (!ConConfigLinuxToNRI(containerConfig, con)) {
+ ERROR("Failed to convert conatiner linux info to nri for con : %s", con.name);
+ return false;
+ }
+
+ // todo: CRI module can not get container hooks from containerConfig
+ // ContainerToNRIByConfig is called when CreateConatiner, and cannot get pid at this time
+
+ // rlimit not support in containerd
+
+ return true;
}
// container info is incomplete because container in excution is not created
@@ -486,6 +958,11 @@ auto ContainerToNRIByID(const std::string &id, nri_container &con) -> bool
goto out;
}
+ if (!ConLinuxToNRI(cont->common_config->id, cont->hostconfig, con)) {
+ ERROR("Failed to transform conatiner linux info to nri for con : %s", con.name);
+ goto out;
+ }
+
// todo: can convert hostconfig's hook_spec to nri spec
con.pid = container_state_get_pid(cont->state);
@@ -644,6 +1121,7 @@ auto ContainersToNRI(std::vector<std::unique_ptr<runtime::v1::Container>> &conta
}
if (!ContainerToNRIByID(containers[i].get()->id(), *con)) {
ERROR("Failed to transform container to nri for container : %s", containers[i]->metadata().name().c_str());
+ free_nri_container(con);
return false;
}
cons.push_back(con);
--
2.34.1
|