代码拉取完成,页面将自动刷新
From 6d5121eacdb42a4dd7c2ea498c534b893b80e405 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=AD=A6=E7=A7=AF=E8=B6=85?= <wujichao1@huawei.com>
Date: Thu, 7 Nov 2024 20:17:41 +0800
Subject: [PATCH 148/156] fix issues: Isula ps cannot display port mapping
---
src/api/services/containers/container.proto | 1 +
.../connect/grpc/grpc_containers_client.cc | 2 +
src/client/connect/protocol_type.c | 4 ++
src/client/connect/protocol_type.h | 1 +
src/cmd/isula/information/ps.c | 9 ++-
.../connect/grpc/container/list_service.cc | 3 +
src/daemon/executor/container_cb/list.c | 2 +
src/daemon/modules/api/container_api.h | 2 +
src/daemon/modules/container/container_unix.c | 56 ++++++++++++++++++-
9 files changed, 77 insertions(+), 3 deletions(-)
diff --git a/src/api/services/containers/container.proto b/src/api/services/containers/container.proto
index 8d05681e..113fcb10 100644
--- a/src/api/services/containers/container.proto
+++ b/src/api/services/containers/container.proto
@@ -65,6 +65,7 @@ message Container {
string name = 16;
string health_state = 17;
int64 created = 18;
+ string ports = 19;
}
message Container_info {
diff --git a/src/client/connect/grpc/grpc_containers_client.cc b/src/client/connect/grpc/grpc_containers_client.cc
index adeac0ba..f042290e 100644
--- a/src/client/connect/grpc/grpc_containers_client.cc
+++ b/src/client/connect/grpc/grpc_containers_client.cc
@@ -1241,6 +1241,8 @@ private:
util_strdup_s("none");
response->container_summary[index]->command = !in.command().empty() ? util_strdup_s(in.command().c_str()) :
util_strdup_s("-");
+ response->container_summary[index]->ports = !in.ports().empty() ? util_strdup_s(in.ports().c_str()) :
+ nullptr;
const char *starttime = !in.startat().empty() ? in.startat().c_str() : "-";
response->container_summary[index]->startat = util_strdup_s(starttime);
diff --git a/src/client/connect/protocol_type.c b/src/client/connect/protocol_type.c
index 57dcf753..d0ab9732 100644
--- a/src/client/connect/protocol_type.c
+++ b/src/client/connect/protocol_type.c
@@ -531,6 +531,10 @@ void isula_list_response_free(struct isula_list_response *response)
free(response->container_summary[i]->command);
response->container_summary[i]->command = NULL;
}
+ if (response->container_summary[i]->ports != NULL) {
+ free(response->container_summary[i]->ports);
+ response->container_summary[i]->ports = NULL;
+ }
if (response->container_summary[i]->startat != NULL) {
free(response->container_summary[i]->startat);
response->container_summary[i]->startat = NULL;
diff --git a/src/client/connect/protocol_type.h b/src/client/connect/protocol_type.h
index 2b445c5a..02e315bf 100644
--- a/src/client/connect/protocol_type.h
+++ b/src/client/connect/protocol_type.h
@@ -204,6 +204,7 @@ struct isula_container_summary_info {
char *id;
char *image;
char *command;
+ char *ports;
char *name;
Container_Status status;
uint32_t exit_code;
diff --git a/src/cmd/isula/information/ps.c b/src/cmd/isula/information/ps.c
index 13c35dc7..6bdc157f 100644
--- a/src/cmd/isula/information/ps.c
+++ b/src/cmd/isula/information/ps.c
@@ -332,7 +332,8 @@ static void print_basic_container_info_item(const struct isula_container_summary
} else if (strcmp(name, "Created") == 0) {
print_created_field(in->created, length->created_length);
} else if (strcmp(name, "Ports") == 0) {
- printf("%-*s", (int)length->ports_length, " ");
+ const char *ports = (in->ports != NULL ? in->ports : "N/A");
+ printf("%-*s", (int)length->ports_length, ports);
}
}
@@ -515,6 +516,12 @@ static void list_field_width(struct isula_container_summary_info **info, const s
l->command_length = (unsigned int)cmd_len;
}
}
+ if (in->ports != NULL) {
+ size_t ports_len = strlen(in->ports);
+ if (ports_len > l->ports_length) {
+ l->ports_length = (unsigned int)ports_len;
+ }
+ }
calculate_str_length(in->name, &l->name_length);
calculate_str_length(in->runtime, &l->runtime_length);
diff --git a/src/daemon/entry/connect/grpc/container/list_service.cc b/src/daemon/entry/connect/grpc/container/list_service.cc
index e831acbc..dfd48191 100644
--- a/src/daemon/entry/connect/grpc/container/list_service.cc
+++ b/src/daemon/entry/connect/grpc/container/list_service.cc
@@ -121,6 +121,9 @@ void ContainerListService::FillResponseTogRPC(void *containerRes, containers::Li
if (response->containers[i]->command != nullptr) {
container->set_command(response->containers[i]->command);
}
+ if (response->containers[i]->ports != nullptr) {
+ container->set_ports(response->containers[i]->ports);
+ }
container->set_exit_code(response->containers[i]->exit_code);
container->set_restartcount(response->containers[i]->restartcount);
if (response->containers[i]->startat != nullptr) {
diff --git a/src/daemon/executor/container_cb/list.c b/src/daemon/executor/container_cb/list.c
index 243eb4d2..2e8c7a79 100644
--- a/src/daemon/executor/container_cb/list.c
+++ b/src/daemon/executor/container_cb/list.c
@@ -609,6 +609,8 @@ static int fill_container_info(container_container *container_info, const contai
container_info->status = (int)container_state_judge_status(cont_state);
+ container_info->ports = container_get_ports(cont);
+
container_info->command = container_get_command(cont);
image = container_get_image(cont);
container_info->image = image ? image : util_strdup_s("none");
diff --git a/src/daemon/modules/api/container_api.h b/src/daemon/modules/api/container_api.h
index a6ec8e22..e4d7fc25 100644
--- a/src/daemon/modules/api/container_api.h
+++ b/src/daemon/modules/api/container_api.h
@@ -185,6 +185,8 @@ int container_v2_spec_merge_container_spec(container_config_v2_common_config *v2
char *container_get_command(const container_t *cont);
+char *container_get_ports(const container_t *cont);
+
char *container_get_image(const container_t *cont);
int container_exit_on_next(container_t *cont);
diff --git a/src/daemon/modules/container/container_unix.c b/src/daemon/modules/container/container_unix.c
index 6f345ea0..812ab9e1 100644
--- a/src/daemon/modules/container/container_unix.c
+++ b/src/daemon/modules/container/container_unix.c
@@ -20,6 +20,7 @@
#include <isula_libutils/container_config_v2.h>
#include <isula_libutils/host_config.h>
#include <isula_libutils/json_common.h>
+#include <isula_libutils/auto_cleanup.h>
#include <limits.h>
#include <pthread.h>
#include <stdbool.h>
@@ -1253,7 +1254,7 @@ char *container_get_command(const container_t *cont)
if (cont->common_config->path != NULL) {
nret = util_array_append(&args, cont->common_config->path);
if (nret < 0) {
- ERROR("Appned string failed");
+ ERROR("Append string failed");
goto cleanup;
}
}
@@ -1268,7 +1269,7 @@ char *container_get_command(const container_t *cont)
nret = util_array_append(&args, arg);
free(arg);
if (nret < 0) {
- ERROR("Appned string failed");
+ ERROR("Append string failed");
goto cleanup;
}
}
@@ -1280,6 +1281,57 @@ cleanup:
return cmd;
}
+/* container get ports */
+char *container_get_ports(const container_t *cont)
+{
+ int nret;
+ size_t i, j, ports_num = 0;
+ __isula_auto_array_t char **args = NULL;
+ char *ports = NULL;
+
+ if (cont == NULL || cont->hostconfig == NULL || cont->hostconfig->port_bindings == NULL) {
+ return NULL;
+ }
+
+ for (i = 0; cont->hostconfig->port_bindings->keys != NULL && i < cont->hostconfig->port_bindings->len; i++) {
+ for (j = 0; j < cont->hostconfig->port_bindings->values[i]->element->host_len; j++) {
+ __isula_auto_free char *arg = NULL;
+ char *host_ip = cont->hostconfig->port_bindings->values[i]->element->host[j]->host_ip;
+ if (host_ip == NULL) {
+ host_ip = "0.0.0.0";
+ }
+ char *host_port = cont->hostconfig->port_bindings->values[i]->element->host[j]->host_port;
+ if (host_port == NULL) {
+ host_port = "";
+ }
+ char *cont_port_type = cont->hostconfig->port_bindings->keys[i];
+ if (cont_port_type == NULL) {
+ cont_port_type = "";
+ }
+ size_t total_len = strlen(host_ip) + strlen(host_port) + strlen(cont_port_type) + 4; // 4 for ":->" and null terminator
+ arg = util_common_calloc_s(total_len);
+ if (arg == NULL) {
+ ERROR("Out of memory");
+ return ports;
+ }
+ nret = snprintf(arg, total_len, "%s:%s->%s", host_ip, host_port, cont_port_type);
+ if (nret < 0 || (size_t)nret >= total_len) {
+ ERROR("Failed to print string");
+ return ports;
+ }
+ nret = util_array_append(&args, arg);
+ if (nret < 0) {
+ ERROR("Append string failed");
+ return ports;
+ }
+ ports_num++;
+ }
+ }
+ ports = util_string_join(", ", (const char **)args, ports_num);
+
+ return ports;
+}
+
/* container get image */
char *container_get_image(const container_t *cont)
{
--
2.34.1
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。