summaryrefslogtreecommitdiff
path: root/0059-modify-some-grpc-status-codes-of-cri-in-case-of-erro.patch
blob: 1153418a2476b7dfc37d8096ad083ec924bfceca (plain)
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
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