From 67bb7e5928dd15b0ca9edc2385e2d9a3034b3ae8 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Tue, 13 Feb 2024 14:34:59 +0100 Subject: [PATCH] confd: simplify container network naming The interface name is already a unique qualifier, so we can simplify CNI network naming to ease the burden when debugging. Signed-off-by: Joachim Wiberg --- src/confd/src/ietf-interfaces.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/confd/src/ietf-interfaces.c b/src/confd/src/ietf-interfaces.c index 14810d10..ced6d025 100644 --- a/src/confd/src/ietf-interfaces.c +++ b/src/confd/src/ietf-interfaces.c @@ -14,6 +14,8 @@ #include "core.h" #include "dagger.h" +#define CNI_NAME "/etc/cni/net.d/%s.conflist" + #define ERR_IFACE(_iface, _err, _fmt, ...) \ ({ \ ERROR("%s: " _fmt, lydx_get_cattr(_iface, "name"), \ @@ -26,12 +28,15 @@ #define IF_XPATH "/ietf-interfaces:interfaces/interface" -static bool iface_is_cni(const char *ifname, struct lyd_node *cif) +static bool iface_is_cni(const char *ifname, struct lyd_node *node, const char **type) { - struct lyd_node *net = lydx_get_child(cif, "container-network"); + struct lyd_node *net = lydx_get_child(node, "container-network"); - if (net) + if (net) { + if (type) + *type = lydx_get_cattr(net, "type"); return true; + } return false; } @@ -115,9 +120,11 @@ static int cni_bridge(struct lyd_node *net, const char *ifname) int first = 1; FILE *fp; - fp = fopenf("w", "/etc/cni/net.d/90-%s-bridge.conflist", ifname); - if (!fp) + fp = fopenf("w", CNI_NAME, ifname); + if (!fp) { + ERRNO("Failed creating container bridge " CNI_NAME, ifname); return -EIO; + } fprintf(fp, "{\n" " \"cniVersion\": \"1.0.0\",\n" @@ -205,14 +212,15 @@ static int cni_bridge(struct lyd_node *net, const char *ifname) static int cni_host(struct lyd_node *net, const char *ifname) { - const char *fn = "/etc/cni/net.d/90-%s-host.conflist"; struct lyd_node *node, *ip; int addr = 0, route = 0; FILE *fp; - fp = fopenf("w", fn, ifname); - if (!fp) + fp = fopenf("w", CNI_NAME, ifname); + if (!fp) { + ERRNO("Failed creating container interface " CNI_NAME, ifname); return -EIO; + } fprintf(fp, "{\n" " \"cniVersion\": \"1.0.0\",\n"