164 lines
7.4 KiB
Diff
164 lines
7.4 KiB
Diff
From 2bf2acb51aec12e734c970b02cd7802f088a2222 Mon Sep 17 00:00:00 2001
|
|
From: haozi007 <liuhao27@huawei.com>
|
|
Date: Tue, 14 Nov 2023 10:29:34 +0800
|
|
Subject: [PATCH 14/14] compatibility for manage pods which created by old
|
|
iSulad
|
|
|
|
Signed-off-by: haozi007 <liuhao27@huawei.com>
|
|
---
|
|
.../cri_pod_sandbox_manager_service.cc | 12 +++-
|
|
src/daemon/entry/cri/v1alpha/naming.cc | 72 ++++++++++++++++---
|
|
src/daemon/entry/cri/v1alpha/naming.h | 2 +-
|
|
3 files changed, 72 insertions(+), 14 deletions(-)
|
|
|
|
diff --git a/src/daemon/entry/cri/v1alpha/cri_pod_sandbox_manager_service.cc b/src/daemon/entry/cri/v1alpha/cri_pod_sandbox_manager_service.cc
|
|
index 6e8f40b9..8533bb8c 100644
|
|
--- a/src/daemon/entry/cri/v1alpha/cri_pod_sandbox_manager_service.cc
|
|
+++ b/src/daemon/entry/cri/v1alpha/cri_pod_sandbox_manager_service.cc
|
|
@@ -1153,7 +1153,11 @@ void PodSandboxManagerService::PodSandboxStatusToGRPC(const container_inspect *i
|
|
|
|
CRIHelpers::ExtractLabels(inspect->config->labels, *podStatus->mutable_labels());
|
|
CRIHelpers::ExtractAnnotations(inspect->config->annotations, *podStatus->mutable_annotations());
|
|
- CRINaming::ParseSandboxName(podStatus->annotations(), *podStatus->mutable_metadata(), error);
|
|
+ std::string name;
|
|
+ if (inspect->name != nullptr) {
|
|
+ name = std::string(inspect->name);
|
|
+ }
|
|
+ CRINaming::ParseSandboxName(name, podStatus->annotations(), *podStatus->mutable_metadata(), error);
|
|
if (error.NotEmpty()) {
|
|
return;
|
|
}
|
|
@@ -1266,7 +1270,11 @@ void PodSandboxManagerService::ListPodSandboxToGRPC(container_list_response *res
|
|
|
|
CRIHelpers::ExtractAnnotations(response->containers[i]->annotations, *pod->mutable_annotations());
|
|
|
|
- CRINaming::ParseSandboxName(pod->annotations(), *pod->mutable_metadata(), error);
|
|
+ std::string name;
|
|
+ if (response->containers[i]->name != nullptr) {
|
|
+ name = std::string(response->containers[i]->name);
|
|
+ }
|
|
+ CRINaming::ParseSandboxName(name, pod->annotations(), *pod->mutable_metadata(), error);
|
|
|
|
if (filterOutReadySandboxes && pod->state() == runtime::v1alpha2::SANDBOX_READY) {
|
|
continue;
|
|
diff --git a/src/daemon/entry/cri/v1alpha/naming.cc b/src/daemon/entry/cri/v1alpha/naming.cc
|
|
index abb6e57d..de47a97d 100644
|
|
--- a/src/daemon/entry/cri/v1alpha/naming.cc
|
|
+++ b/src/daemon/entry/cri/v1alpha/naming.cc
|
|
@@ -26,6 +26,38 @@
|
|
#include "utils.h"
|
|
|
|
namespace CRINaming {
|
|
+// default sandbox name create by MakeSandboxName();
|
|
+// format is 'k8s_containername_metadataname_namespace_uid_attempt'
|
|
+static int parseName(const std::string &name, std::vector<std::string> &items, unsigned int &attempt, Errors &err)
|
|
+{
|
|
+ std::istringstream f(name);
|
|
+ std::string part;
|
|
+
|
|
+ while (getline(f, part, CRI::Constants::nameDelimiterChar)) {
|
|
+ items.push_back(part);
|
|
+ }
|
|
+
|
|
+ // format: k8s_containername_metadataname_namespace_uid_attempt
|
|
+ // so split name by '_', length of result array must be 6
|
|
+ if (items.size() != 6) {
|
|
+ err.Errorf("failed to parse the sandbox name: %s", name.c_str());
|
|
+ return -1;
|
|
+ }
|
|
+
|
|
+ if (items[0] != CRI::Constants::kubePrefix) {
|
|
+ err.Errorf("container is not managed by kubernetes: %s", name.c_str());
|
|
+ return -1;
|
|
+ }
|
|
+
|
|
+ // last item index is 5, and must be attempt
|
|
+ if (util_safe_uint(items[5].c_str(), &attempt)) {
|
|
+ err.Errorf("failed to parse the sandbox name %s: %s", name.c_str(), strerror(errno));
|
|
+ return -1;
|
|
+ }
|
|
+
|
|
+ return 0;
|
|
+}
|
|
+
|
|
std::string MakeSandboxName(const runtime::v1alpha2::PodSandboxMetadata &metadata)
|
|
{
|
|
std::string sname;
|
|
@@ -44,9 +76,12 @@ std::string MakeSandboxName(const runtime::v1alpha2::PodSandboxMetadata &metadat
|
|
return sname;
|
|
}
|
|
|
|
-void ParseSandboxName(const google::protobuf::Map<std::string, std::string> &annotations,
|
|
+void ParseSandboxName(const std::string &name, const google::protobuf::Map<std::string, std::string> &annotations,
|
|
runtime::v1alpha2::PodSandboxMetadata &metadata, Errors &err)
|
|
{
|
|
+ // need check uid and attemp 2 items
|
|
+ int needSetUidAndAttemp = 2;
|
|
+
|
|
if (annotations.count(CRIHelpers::Constants::SANDBOX_NAME_ANNOTATION_KEY) == 0) {
|
|
err.Errorf("annotation don't contains the sandbox name, failed to parse it");
|
|
return;
|
|
@@ -57,21 +92,36 @@ void ParseSandboxName(const google::protobuf::Map<std::string, std::string> &ann
|
|
return;
|
|
}
|
|
|
|
- if (annotations.count(CRIHelpers::Constants::SANDBOX_UID_ANNOTATION_KEY) == 0) {
|
|
- err.Errorf("annotation don't contains the sandbox uid, failed to parse it");
|
|
- return;
|
|
+ metadata.set_name(annotations.at(CRIHelpers::Constants::SANDBOX_NAME_ANNOTATION_KEY));
|
|
+ metadata.set_namespace_(annotations.at(CRIHelpers::Constants::SANDBOX_NAMESPACE_ANNOTATION_KEY));
|
|
+
|
|
+ if (annotations.count(CRIHelpers::Constants::SANDBOX_UID_ANNOTATION_KEY) != 0) {
|
|
+ metadata.set_uid(annotations.at(CRIHelpers::Constants::SANDBOX_UID_ANNOTATION_KEY));
|
|
+ needSetUidAndAttemp--;
|
|
+ }
|
|
+
|
|
+ if (annotations.count(CRIHelpers::Constants::SANDBOX_ATTEMPT_ANNOTATION_KEY) != 0) {
|
|
+ auto sandboxAttempt = annotations.at(CRIHelpers::Constants::SANDBOX_ATTEMPT_ANNOTATION_KEY);
|
|
+ metadata.set_attempt(static_cast<google::protobuf::uint32>(std::stoul(sandboxAttempt)));
|
|
+ needSetUidAndAttemp--;
|
|
}
|
|
|
|
- if (annotations.count(CRIHelpers::Constants::SANDBOX_ATTEMPT_ANNOTATION_KEY) == 0) {
|
|
- err.Errorf("annotation don't contains the sandbox attempt, failed to parse it");
|
|
+ if (needSetUidAndAttemp == 0) {
|
|
return;
|
|
}
|
|
|
|
- metadata.set_name(annotations.at(CRIHelpers::Constants::SANDBOX_NAME_ANNOTATION_KEY));
|
|
- metadata.set_namespace_(annotations.at(CRIHelpers::Constants::SANDBOX_NAMESPACE_ANNOTATION_KEY));
|
|
- metadata.set_uid(annotations.at(CRIHelpers::Constants::SANDBOX_UID_ANNOTATION_KEY));
|
|
- auto sandboxAttempt = annotations.at(CRIHelpers::Constants::SANDBOX_ATTEMPT_ANNOTATION_KEY);
|
|
- metadata.set_attempt(static_cast<google::protobuf::uint32>(std::stoul(sandboxAttempt)));
|
|
+ // get uid and attempt from name,
|
|
+ // compatibility to new iSulad manage pods created by old version iSulad
|
|
+ // maybe should remove in next version of iSulad
|
|
+ std::vector<std::string> items;
|
|
+ unsigned int attempt;
|
|
+
|
|
+ if (parseName(name, items, attempt, err) != 0) {
|
|
+ return;
|
|
+ }
|
|
+ // index 4 in split array, must be uid
|
|
+ metadata.set_uid(items[4]);
|
|
+ metadata.set_attempt(static_cast<google::protobuf::uint32>(attempt));
|
|
}
|
|
|
|
std::string MakeContainerName(const runtime::v1alpha2::PodSandboxConfig &s, const runtime::v1alpha2::ContainerConfig &c)
|
|
diff --git a/src/daemon/entry/cri/v1alpha/naming.h b/src/daemon/entry/cri/v1alpha/naming.h
|
|
index 7eab41d3..f2d51a98 100644
|
|
--- a/src/daemon/entry/cri/v1alpha/naming.h
|
|
+++ b/src/daemon/entry/cri/v1alpha/naming.h
|
|
@@ -26,7 +26,7 @@ std::string MakeSandboxName(const runtime::v1alpha2::PodSandboxMetadata &metadat
|
|
std::string MakeContainerName(const runtime::v1alpha2::PodSandboxConfig &s,
|
|
const runtime::v1alpha2::ContainerConfig &c);
|
|
|
|
-void ParseSandboxName(const google::protobuf::Map<std::string, std::string> &annotations,
|
|
+void ParseSandboxName(const std::string &name, const google::protobuf::Map<std::string, std::string> &annotations,
|
|
runtime::v1alpha2::PodSandboxMetadata &metadata, Errors &err);
|
|
|
|
void ParseContainerName(const google::protobuf::Map<std::string, std::string> &annotations,
|
|
--
|
|
2.42.0
|
|
|