A container (`path` ends in containers[], initContainers[] or ephemeralContainers[], any depth: pod templates, CronJobs, the pod templates of custom resources): its literal image, and the environment variables it sets -- each env[] name, with where its value comes from, never the value. Both carry the container's name, so a variable joins the program of its own container's image.
| 1472 | // never the value. Both carry the container's name, so a variable joins the |
| 1473 | // program of its own container's image. |
| 1474 | static void k8s_container(k8s_walk_t *w, const char *path, TSNode container) { |
| 1475 | static const char *const ITEMS[] = {"containers[]", "initContainers[]", |
| 1476 | "ephemeralContainers[]"}; |
| 1477 | size_t pl = strlen(path); |
| 1478 | bool at = false; |
| 1479 | for (size_t i = 0; i < sizeof(ITEMS) / sizeof(ITEMS[0]) && !at; i++) { |
| 1480 | size_t il = strlen(ITEMS[i]); |
| 1481 | at = pl >= il && strcmp(path + pl - il, ITEMS[i]) == 0 && |
| 1482 | (pl == il || path[pl - il - SKIP_ONE] == '.'); |
| 1483 | } |
| 1484 | if (!at) { |
| 1485 | return; |
| 1486 | } |
| 1487 | const char *cname = k8s_text(w, container, "name"); |
| 1488 | cname = cname && cname[0] && !strstr(cname, "{{") ? cname : NULL; |
| 1489 | const char *image = k8s_text(w, container, "image"); |
| 1490 | if (k8s_image_literal(image)) { |
| 1491 | CBMK8sFact f = {.fact = CBM_K8S_FACT_IMAGE, |
| 1492 | .def_index = w->def_index, |
| 1493 | .name = image, |
| 1494 | .container = cname, |
| 1495 | .via = "container"}; |
| 1496 | cbm_k8sfact_push(&w->ctx->result->k8s_facts, w->ctx->arena, f); |
| 1497 | } |
| 1498 | k8s_iter_t env = k8s_iter(k8s_child(w, container, "env")); |
| 1499 | TSNode item; |
| 1500 | while (k8s_next(&env, &item)) { |
| 1501 | const char *key = k8s_is_mapping(item) ? k8s_text(w, item, "name") : NULL; |
| 1502 | if (!key || !key[0] || strstr(key, "{{")) { |
| 1503 | continue; |
| 1504 | } |
| 1505 | CBMK8sFact f = {.fact = CBM_K8S_FACT_ENV, |
| 1506 | .def_index = w->def_index, |
| 1507 | .name = key, |
| 1508 | .container = cname, |
| 1509 | .via = k8s_env_source(w, item), |
| 1510 | .line = (int)ts_node_start_point(item).row + SKIP_ONE}; |
| 1511 | cbm_k8sfact_push(&w->ctx->result->k8s_facts, w->ctx->arena, f); |
| 1512 | k8s_env_address(w, item, key, cname); |
| 1513 | } |
| 1514 | } |
| 1515 | |
| 1516 | // A mapping at `path` that holds a reference in its kind/name/namespace. |
| 1517 | static void k8s_mapping(k8s_walk_t *w, const char *path, TSNode mapping) { |
no test coverage detected