From 445642718fd3dd7867aad9fc8e6eb5b7dafadcb6 Mon Sep 17 00:00:00 2001
From: liuxu <liuxu156@huawei.com>
Date: Fri, 1 Nov 2024 12:56:38 +0800
Subject: [PATCH 145/156] cni:change error info

Signed-off-by: liuxu <liuxu156@huawei.com>
---
 .../modules/network/cni_operator/cni_operate.c     |  8 ++++----
 .../network/cni_operator/libcni/libcni_api.c       | 14 +++++++++++++-
 .../network/cni_operator/libcni/libcni_api.h       |  2 +-
 3 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/src/daemon/modules/network/cni_operator/cni_operate.c b/src/daemon/modules/network/cni_operator/cni_operate.c
index 6db6db51..80a2642d 100644
--- a/src/daemon/modules/network/cni_operator/cni_operate.c
+++ b/src/daemon/modules/network/cni_operator/cni_operate.c
@@ -731,13 +731,13 @@ static int update_runtime_conf_cni_args_by_cached(cni_cached_info *info, struct
     return 0;
 }
 
-static int get_configs_from_cached(const char *network, struct runtime_conf *rc, char **conf_list)
+static int get_configs_from_cached(const char *network, const char *cni_version, struct runtime_conf *rc, char **conf_list)
 {
     int ret = 0;
     size_t i;
     cni_cached_info *info = NULL;
 
-    info = cni_get_network_list_cached_info(network, rc);
+    info = cni_get_network_list_cached_info(network, cni_version, rc);
     if (info == NULL) {
         return 0;
     }
@@ -857,7 +857,7 @@ int check_network_plane(const struct cni_manager *manager, const struct cni_netw
         goto out;
     }
 
-    ret = get_configs_from_cached(list->list->name, rc, NULL);
+    ret = get_configs_from_cached(list->list->name, list->list->cni_version, rc, NULL);
     if (ret != 0) {
         ERROR("Get cached info failed");
         ret = -1;
@@ -901,7 +901,7 @@ int detach_network_plane(const struct cni_manager *manager, const struct cni_net
         goto out;
     }
 
-    ret = get_configs_from_cached(list->list->name, rc, NULL);
+    ret = get_configs_from_cached(list->list->name, list->list->cni_version, rc, NULL);
     if (ret != 0) {
         ERROR("Get cached info failed");
         ret = -1;
diff --git a/src/daemon/modules/network/cni_operator/libcni/libcni_api.c b/src/daemon/modules/network/cni_operator/libcni/libcni_api.c
index 7ba983af..c55c8e9d 100644
--- a/src/daemon/modules/network/cni_operator/libcni/libcni_api.c
+++ b/src/daemon/modules/network/cni_operator/libcni/libcni_api.c
@@ -81,13 +81,25 @@ struct cni_opt_result *cni_get_network_list_cached_result(const struct cni_netwo
     return result;
 }
 
-cni_cached_info *cni_get_network_list_cached_info(const char *network, const struct runtime_conf *rc)
+cni_cached_info *cni_get_network_list_cached_info(const char *network, const char *cni_version, const struct runtime_conf *rc)
 {
+    bool greater = false;
     if (network == NULL) {
         ERROR("Empty network");
         return NULL;
     }
 
+    if (util_version_greater_than_or_equal_to(cni_version, SUPPORT_CACHE_AND_CHECK_VERSION, &greater) != 0) {
+        ERROR("Invalid cni version %s", cni_version);
+        return NULL;
+    }
+
+    // CACHE was added in CNI spec version 0.4.0 and higher
+    if (!greater) {
+        WARN("result version: %s is too old, do not save this cache", cni_version);
+        return NULL;
+    }
+
     return cni_cache_read(g_module_conf.cache_dir, network, rc);
 }
 
diff --git a/src/daemon/modules/network/cni_operator/libcni/libcni_api.h b/src/daemon/modules/network/cni_operator/libcni/libcni_api.h
index 2f10d6e9..bf16f168 100644
--- a/src/daemon/modules/network/cni_operator/libcni/libcni_api.h
+++ b/src/daemon/modules/network/cni_operator/libcni/libcni_api.h
@@ -75,7 +75,7 @@ bool cni_module_init(const char *cache_dir, const char * const *paths, size_t pa
 struct cni_opt_result *cni_get_network_list_cached_result(const struct cni_network_list_conf *list,
                                                           const struct runtime_conf *rc);
 
-cni_cached_info *cni_get_network_list_cached_info(const char *network, const struct runtime_conf *rc);
+cni_cached_info *cni_get_network_list_cached_info(const char *network, const char *cni_version, const struct runtime_conf *rc);
 
 int cni_add_network_list(const struct cni_network_list_conf *list, const struct runtime_conf *rc,
                          struct cni_opt_result **pret);
-- 
2.34.1

