From 628f4ceb329e16991ed33d3a460bcf8f5542ba99 Mon Sep 17 00:00:00 2001
From: jikai <jikai11@huawei.com>
Date: Mon, 8 Apr 2024 09:30:04 +0000
Subject: [PATCH 59/69] modify some grpc status codes of cri in case of error

Signed-off-by: jikai <jikai11@huawei.com>
---
 .../cri/v1/cri_v1_runtime_runtime_service.cc  | 41 +++++++++++++------
 .../cri/v1/cri_v1_runtime_runtime_service.h   |  3 ++
 .../v1alpha/cri_runtime_runtime_service.cc    | 41 +++++++++++++------
 .../cri/v1alpha/cri_runtime_runtime_service.h |  2 +
 4 files changed, 63 insertions(+), 24 deletions(-)

diff --git a/src/daemon/entry/connect/grpc/cri/v1/cri_v1_runtime_runtime_service.cc b/src/daemon/entry/connect/grpc/cri/v1/cri_v1_runtime_runtime_service.cc
index e2591ce0..fb5aad3c 100644
--- a/src/daemon/entry/connect/grpc/cri/v1/cri_v1_runtime_runtime_service.cc
+++ b/src/daemon/entry/connect/grpc/cri/v1/cri_v1_runtime_runtime_service.cc
@@ -50,6 +50,23 @@ static void cri_container_topic_release(void *arg)
     delete resp;
 }
 
+grpc::Status RuntimeV1RuntimeServiceImpl::ToGRPCStatus(Errors &error)
+{
+    if (error.Empty()) {
+        return grpc::Status::OK;
+    }
+    if (error.GetMessage().find("Failed to find") != std::string::npos) {
+        return grpc::Status(grpc::StatusCode::NOT_FOUND, error.GetMessage());
+    }
+
+    // Attach exceeded timeout for lxc and Exec container error;exec timeout for runc
+    if (error.GetMessage().find("Attach exceeded timeout") != std::string::npos
+        || error.GetMessage().find("Exec container error;exec timeout") != std::string::npos) {
+        return grpc::Status(grpc::StatusCode::DEADLINE_EXCEEDED, error.GetMessage());
+    }
+    return grpc::Status(grpc::StatusCode::UNKNOWN, error.GetMessage());
+}
+
 void RuntimeV1RuntimeServiceImpl::Init(std::string &podSandboxImage,
                                        std::shared_ptr<Network::PluginManager> networkPlugin,
                                        bool enablePodEvents, Errors &err)
@@ -167,7 +184,7 @@ grpc::Status RuntimeV1RuntimeServiceImpl::CreateContainer(grpc::ServerContext *c
         m_rService->CreateContainer(request->pod_sandbox_id(), request->config(), request->sandbox_config(), error);
     if (!error.Empty() || responseID.empty()) {
         ERROR("Object: CRI, Type: Failed to create container");
-        return grpc::Status(grpc::StatusCode::UNKNOWN, error.GetMessage());
+        return ToGRPCStatus(error);
     }
     reply->set_container_id(responseID);
 
@@ -192,7 +209,7 @@ grpc::Status RuntimeV1RuntimeServiceImpl::StartContainer(grpc::ServerContext *co
     m_rService->StartContainer(request->container_id(), error);
     if (!error.Empty()) {
         ERROR("Object: CRI, Type: Failed to start container %s", request->container_id().c_str());
-        return grpc::Status(grpc::StatusCode::UNKNOWN, error.GetMessage());
+        return ToGRPCStatus(error);
     }
 
     EVENT("Event: {Object: CRI, Type: Started Container: %s}", request->container_id().c_str());
@@ -216,7 +233,7 @@ grpc::Status RuntimeV1RuntimeServiceImpl::StopContainer(grpc::ServerContext *con
     m_rService->StopContainer(request->container_id(), (int64_t)request->timeout(), error);
     if (!error.Empty()) {
         ERROR("Object: CRI, Type: Failed to stop container %s", request->container_id().c_str());
-        return grpc::Status(grpc::StatusCode::UNKNOWN, error.GetMessage());
+        return ToGRPCStatus(error);
     }
 
     EVENT("Event: {Object: CRI, Type: Stopped Container: %s}", request->container_id().c_str());
@@ -240,7 +257,7 @@ grpc::Status RuntimeV1RuntimeServiceImpl::RemoveContainer(grpc::ServerContext *c
     m_rService->RemoveContainer(request->container_id(), error);
     if (!error.Empty()) {
         ERROR("Object: CRI, Type: Failed to remove container %s", request->container_id().c_str());
-        return grpc::Status(grpc::StatusCode::UNKNOWN, error.GetMessage());
+        return ToGRPCStatus(error);
     }
 
     EVENT("Event: {Object: CRI, Type: Removed Container: %s}", request->container_id().c_str());
@@ -359,7 +376,7 @@ grpc::Status RuntimeV1RuntimeServiceImpl::ContainerStatus(grpc::ServerContext *c
         m_rService->ContainerStatus(request->container_id(), error);
     if (!error.Empty() || !contStatus) {
         ERROR("Object: CRI, Type: Failed to get container status %s", request->container_id().c_str());
-        return grpc::Status(grpc::StatusCode::UNKNOWN, error.GetMessage());
+        return ToGRPCStatus(error);
     }
     *(reply->mutable_status()) = *contStatus;
 
@@ -384,7 +401,7 @@ grpc::Status RuntimeV1RuntimeServiceImpl::ExecSync(grpc::ServerContext *context,
     m_rService->ExecSync(request->container_id(), request->cmd(), request->timeout(), reply, error);
     if (!error.Empty()) {
         ERROR("Object: CRI, Type: Failed to sync exec container: %s", request->container_id().c_str());
-        return grpc::Status(grpc::StatusCode::UNKNOWN, error.GetMessage());
+        return ToGRPCStatus(error);
     }
 
     WARN("Event: {Object: CRI, Type: sync execed Container: %s}", request->container_id().c_str());
@@ -437,7 +454,7 @@ grpc::Status RuntimeV1RuntimeServiceImpl::StopPodSandbox(grpc::ServerContext *co
     if (!error.Empty()) {
         ERROR("Object: CRI, Type: Failed to stop pod:%s due to %s", request->pod_sandbox_id().c_str(),
               error.GetMessage().c_str());
-        return grpc::Status(grpc::StatusCode::UNKNOWN, error.GetMessage());
+        return ToGRPCStatus(error);
     }
 
     EVENT("Event: {Object: CRI, Type: Stopped Pod: %s}", request->pod_sandbox_id().c_str());
@@ -462,7 +479,7 @@ grpc::Status RuntimeV1RuntimeServiceImpl::RemovePodSandbox(grpc::ServerContext *
     if (!error.Empty()) {
         ERROR("Object: CRI, Type: Failed to remove pod:%s due to %s", request->pod_sandbox_id().c_str(),
               error.GetMessage().c_str());
-        return grpc::Status(grpc::StatusCode::UNKNOWN, error.GetMessage());
+        return ToGRPCStatus(error);
     }
 
     EVENT("Event: {Object: CRI, Type: Removed Pod: %s}", request->pod_sandbox_id().c_str());
@@ -487,7 +504,7 @@ grpc::Status RuntimeV1RuntimeServiceImpl::PodSandboxStatus(grpc::ServerContext *
     if (!error.Empty()) {
         ERROR("Object: CRI, Type: Failed to status pod:%s due to %s", request->pod_sandbox_id().c_str(),
               error.GetMessage().c_str());
-        return grpc::Status(grpc::StatusCode::UNKNOWN, error.GetMessage());
+        return ToGRPCStatus(error);
     }
 
     INFO("Event: {Object: CRI, Type: Statused Pod: %s}", request->pod_sandbox_id().c_str());
@@ -608,7 +625,7 @@ RuntimeV1RuntimeServiceImpl::UpdateContainerResources(grpc::ServerContext *conte
     if (error.NotEmpty()) {
         ERROR("Object: CRI, Type: Failed to update container:%s due to %s", request->container_id().c_str(),
               error.GetMessage().c_str());
-        return grpc::Status(grpc::StatusCode::UNKNOWN, error.GetMessage());
+        return ToGRPCStatus(error);
     }
 
     WARN("Event: {Object: CRI, Type: Updated container resources: %s}", request->container_id().c_str());
@@ -633,7 +650,7 @@ grpc::Status RuntimeV1RuntimeServiceImpl::Exec(grpc::ServerContext *context,
     if (!error.Empty()) {
         ERROR("Object: CRI, Type: Failed to exec container:%s due to %s", request->container_id().c_str(),
               error.GetMessage().c_str());
-        return grpc::Status(grpc::StatusCode::UNKNOWN, error.GetMessage());
+        return ToGRPCStatus(error);
     }
 
     EVENT("Event: {Object: CRI, Type: execed Container: %s}", request->container_id().c_str());
@@ -658,7 +675,7 @@ grpc::Status RuntimeV1RuntimeServiceImpl::Attach(grpc::ServerContext *context,
     if (!error.Empty()) {
         ERROR("Object: CRI, Type: Failed to attach container:%s due to %s", request->container_id().c_str(),
               error.GetMessage().c_str());
-        return grpc::Status(grpc::StatusCode::UNKNOWN, error.GetMessage());
+        return ToGRPCStatus(error);
     }
 
     EVENT("Event: {Object: CRI, Type: attched Container: %s}", request->container_id().c_str());
diff --git a/src/daemon/entry/connect/grpc/cri/v1/cri_v1_runtime_runtime_service.h b/src/daemon/entry/connect/grpc/cri/v1/cri_v1_runtime_runtime_service.h
index 842d1811..1cf375a4 100644
--- a/src/daemon/entry/connect/grpc/cri/v1/cri_v1_runtime_runtime_service.h
+++ b/src/daemon/entry/connect/grpc/cri/v1/cri_v1_runtime_runtime_service.h
@@ -114,6 +114,9 @@ public:
                                     grpc::ServerWriter<runtime::v1::ContainerEventResponse> *writer) override;
 
 private:
+
+    grpc::Status ToGRPCStatus(Errors &error);
+
     std::unique_ptr<CRIV1::CRIRuntimeService> m_rService;
     bool m_enablePodEvents;
 };
diff --git a/src/daemon/entry/connect/grpc/cri/v1alpha/cri_runtime_runtime_service.cc b/src/daemon/entry/connect/grpc/cri/v1alpha/cri_runtime_runtime_service.cc
index 5e85702c..1c83f4ca 100644
--- a/src/daemon/entry/connect/grpc/cri/v1alpha/cri_runtime_runtime_service.cc
+++ b/src/daemon/entry/connect/grpc/cri/v1alpha/cri_runtime_runtime_service.cc
@@ -23,6 +23,23 @@
 
 using namespace CRI;
 
+grpc::Status RuntimeRuntimeServiceImpl::ToGRPCStatus(Errors &error)
+{
+    if (error.Empty()) {
+        return grpc::Status::OK;
+    }
+    if (error.GetMessage().find("Failed to find") != std::string::npos) {
+        return grpc::Status(grpc::StatusCode::NOT_FOUND, error.GetMessage());
+    }
+
+    // Attach exceeded timeout for lxc and Exec container error;exec timeout for runc
+    if (error.GetMessage().find("Attach exceeded timeout") != std::string::npos
+        || error.GetMessage().find("Exec container error;exec timeout") != std::string::npos) {
+        return grpc::Status(grpc::StatusCode::DEADLINE_EXCEEDED, error.GetMessage());
+    }
+    return grpc::Status(grpc::StatusCode::UNKNOWN, error.GetMessage());
+}
+
 void RuntimeRuntimeServiceImpl::Init(std::string &podSandboxImage,
                                      std::shared_ptr<Network::PluginManager> networkPlugin, Errors &err)
 {
@@ -80,7 +97,7 @@ grpc::Status RuntimeRuntimeServiceImpl::CreateContainer(grpc::ServerContext *con
         m_rService->CreateContainer(request->pod_sandbox_id(), request->config(), request->sandbox_config(), error);
     if (!error.Empty() || responseID.empty()) {
         ERROR("Object: CRI, Type: Failed to create container");
-        return grpc::Status(grpc::StatusCode::UNKNOWN, error.GetMessage());
+        return ToGRPCStatus(error);
     }
     reply->set_container_id(responseID);
 
@@ -105,7 +122,7 @@ grpc::Status RuntimeRuntimeServiceImpl::StartContainer(grpc::ServerContext *cont
     m_rService->StartContainer(request->container_id(), error);
     if (!error.Empty()) {
         ERROR("Object: CRI, Type: Failed to start container %s", request->container_id().c_str());
-        return grpc::Status(grpc::StatusCode::UNKNOWN, error.GetMessage());
+        return ToGRPCStatus(error);
     }
 
     EVENT("Event: {Object: CRI, Type: Started Container: %s}", request->container_id().c_str());
@@ -129,7 +146,7 @@ grpc::Status RuntimeRuntimeServiceImpl::StopContainer(grpc::ServerContext *conte
     m_rService->StopContainer(request->container_id(), (int64_t)request->timeout(), error);
     if (!error.Empty()) {
         ERROR("Object: CRI, Type: Failed to stop container %s", request->container_id().c_str());
-        return grpc::Status(grpc::StatusCode::UNKNOWN, error.GetMessage());
+        return ToGRPCStatus(error);
     }
 
     EVENT("Event: {Object: CRI, Type: Stopped Container: %s}", request->container_id().c_str());
@@ -153,7 +170,7 @@ grpc::Status RuntimeRuntimeServiceImpl::RemoveContainer(grpc::ServerContext *con
     m_rService->RemoveContainer(request->container_id(), error);
     if (!error.Empty()) {
         ERROR("Object: CRI, Type: Failed to remove container %s", request->container_id().c_str());
-        return grpc::Status(grpc::StatusCode::UNKNOWN, error.GetMessage());
+        return ToGRPCStatus(error);
     }
 
     EVENT("Event: {Object: CRI, Type: Removed Container: %s}", request->container_id().c_str());
@@ -272,7 +289,7 @@ grpc::Status RuntimeRuntimeServiceImpl::ContainerStatus(grpc::ServerContext *con
         m_rService->ContainerStatus(request->container_id(), error);
     if (!error.Empty() || !contStatus) {
         ERROR("Object: CRI, Type: Failed to get container status %s", request->container_id().c_str());
-        return grpc::Status(grpc::StatusCode::UNKNOWN, error.GetMessage());
+        return ToGRPCStatus(error);
     }
     *(reply->mutable_status()) = *contStatus;
 
@@ -297,7 +314,7 @@ grpc::Status RuntimeRuntimeServiceImpl::ExecSync(grpc::ServerContext *context,
     m_rService->ExecSync(request->container_id(), request->cmd(), request->timeout(), reply, error);
     if (!error.Empty()) {
         ERROR("Object: CRI, Type: Failed to sync exec container: %s", request->container_id().c_str());
-        return grpc::Status(grpc::StatusCode::UNKNOWN, error.GetMessage());
+        return ToGRPCStatus(error);
     }
 
     WARN("Event: {Object: CRI, Type: sync execed Container: %s}", request->container_id().c_str());
@@ -351,7 +368,7 @@ grpc::Status RuntimeRuntimeServiceImpl::StopPodSandbox(grpc::ServerContext *cont
     if (!error.Empty()) {
         ERROR("Object: CRI, Type: Failed to stop pod:%s due to %s", request->pod_sandbox_id().c_str(),
               error.GetMessage().c_str());
-        return grpc::Status(grpc::StatusCode::UNKNOWN, error.GetMessage());
+        return ToGRPCStatus(error);
     }
 
     EVENT("Event: {Object: CRI, Type: Stopped Pod: %s}", request->pod_sandbox_id().c_str());
@@ -376,7 +393,7 @@ grpc::Status RuntimeRuntimeServiceImpl::RemovePodSandbox(grpc::ServerContext *co
     if (!error.Empty()) {
         ERROR("Object: CRI, Type: Failed to remove pod:%s due to %s", request->pod_sandbox_id().c_str(),
               error.GetMessage().c_str());
-        return grpc::Status(grpc::StatusCode::UNKNOWN, error.GetMessage());
+        return ToGRPCStatus(error);
     }
 
     EVENT("Event: {Object: CRI, Type: Removed Pod: %s}", request->pod_sandbox_id().c_str());
@@ -402,7 +419,7 @@ grpc::Status RuntimeRuntimeServiceImpl::PodSandboxStatus(grpc::ServerContext *co
     if (!error.Empty() || !podStatus) {
         ERROR("Object: CRI, Type: Failed to status pod:%s due to %s", request->pod_sandbox_id().c_str(),
               error.GetMessage().c_str());
-        return grpc::Status(grpc::StatusCode::UNKNOWN, error.GetMessage());
+        return ToGRPCStatus(error);
     }
     *(reply->mutable_status()) = *podStatus;
 
@@ -523,7 +540,7 @@ RuntimeRuntimeServiceImpl::UpdateContainerResources(grpc::ServerContext *context
     if (error.NotEmpty()) {
         ERROR("Object: CRI, Type: Failed to update container:%s due to %s", request->container_id().c_str(),
               error.GetMessage().c_str());
-        return grpc::Status(grpc::StatusCode::UNKNOWN, error.GetMessage());
+        return ToGRPCStatus(error);
     }
 
     WARN("Event: {Object: CRI, Type: Updated container resources: %s}", request->container_id().c_str());
@@ -548,7 +565,7 @@ grpc::Status RuntimeRuntimeServiceImpl::Exec(grpc::ServerContext *context,
     if (!error.Empty()) {
         ERROR("Object: CRI, Type: Failed to exec container:%s due to %s", request->container_id().c_str(),
               error.GetMessage().c_str());
-        return grpc::Status(grpc::StatusCode::UNKNOWN, error.GetMessage());
+        return ToGRPCStatus(error);
     }
 
     EVENT("Event: {Object: CRI, Type: execed Container: %s}", request->container_id().c_str());
@@ -573,7 +590,7 @@ grpc::Status RuntimeRuntimeServiceImpl::Attach(grpc::ServerContext *context,
     if (!error.Empty()) {
         ERROR("Object: CRI, Type: Failed to attach container:%s due to %s", request->container_id().c_str(),
               error.GetMessage().c_str());
-        return grpc::Status(grpc::StatusCode::UNKNOWN, error.GetMessage());
+        return ToGRPCStatus(error);
     }
 
     EVENT("Event: {Object: CRI, Type: attched Container: %s}", request->container_id().c_str());
diff --git a/src/daemon/entry/connect/grpc/cri/v1alpha/cri_runtime_runtime_service.h b/src/daemon/entry/connect/grpc/cri/v1alpha/cri_runtime_runtime_service.h
index e0f75897..210e67cc 100644
--- a/src/daemon/entry/connect/grpc/cri/v1alpha/cri_runtime_runtime_service.h
+++ b/src/daemon/entry/connect/grpc/cri/v1alpha/cri_runtime_runtime_service.h
@@ -103,6 +103,8 @@ public:
                         runtime::v1alpha2::StatusResponse *reply) override;
 
 private:
+    grpc::Status ToGRPCStatus(Errors &err);
+
     std::unique_ptr<CRI::CRIRuntimeService> m_rService;
 };
 
-- 
2.34.1

