From 3d1461f8ce6923e3723efd9c1f93182cab0e627c Mon Sep 17 00:00:00 2001 From: zuoxuesong-worker Date: Mon, 8 Sep 2025 11:07:36 +0800 Subject: [PATCH] bugfix : fix issue 6796 (#2752) bugfix : fix bug 6796 bugfix : fix bug 6796 bugfix : fix bug 6796 bugfix : fix issue 6796 Signed-off-by: xuesongzuo@yunify.com --- pkg/web/api/result.go | 12 ++++++++++++ pkg/web/handler/resources.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/pkg/web/api/result.go b/pkg/web/api/result.go index 3a044d59..dd70cfc7 100644 --- a/pkg/web/api/result.go +++ b/pkg/web/api/result.go @@ -164,6 +164,17 @@ type SchemaTable struct { Playbook map[string]SchemaTablePlaybook `json:"playbook"` // Map of playbook labels to playbook details } +// InventoryConnect only use for list ip connect check +type InventoryConnect struct { + Connector InventoryConnectHostPort `json:"connector"` +} + +// InventoryConnectHostPort only use for list ip connect check ,show connector host and port +type InventoryConnectHostPort struct { + Host string `json:"host"` + Port string `json:"port"` +} + // IPTable represents an IP address entry and its SSH status information. // It indicates whether the IP is a localhost, if SSH is reachable, and if SSH authorization is present. type IPTable struct { @@ -172,6 +183,7 @@ type IPTable struct { Localhost bool `json:"localhost"` // Whether the IP is a localhost IP SSHReachable bool `json:"sshReachable"` // Whether SSH port is reachable on this IP SSHAuthorized bool `json:"sshAuthorized"` // Whether SSH is authorized for this IP + Added bool `json:"added"` // Indicates whether this IP has already been added to the inventory } // SchemaFile2Table converts a SchemaFile and its filename into a SchemaTable structure. diff --git a/pkg/web/handler/resources.go b/pkg/web/handler/resources.go index 08f9a0af..c7d9cb53 100644 --- a/pkg/web/handler/resources.go +++ b/pkg/web/handler/resources.go @@ -190,6 +190,30 @@ func (h ResourceHandler) ListIP(request *restful.Request, response *restful.Resp cidr := request.QueryParameter("cidr") sshPort := query.DefaultString(request.QueryParameter("sshPort"), "22") + var existsInventoryList kkcorev1.InventoryList + err := h.client.List(request.Request.Context(), &existsInventoryList) + + if err != nil && ctrlclient.IgnoreNotFound(err) != nil { + klog.Errorf("failed to list inventory objects: %v", err) + _ = response.WriteError(http.StatusInternalServerError, err) + return + } + + var addedPorts = make(map[string]struct{}) + for _, item := range existsInventoryList.Items { + for _, iData := range item.Spec.Hosts { + var iHost api.InventoryConnect + err = json.Unmarshal(iData.Raw, &iHost) + if err != nil { + continue + } + if iHost.Connector.Port == "" { + iHost.Connector.Port = "22" + } + addedPorts[iHost.Connector.Host+":"+iHost.Connector.Port] = struct{}{} + } + } + ips := utils.ParseIP(cidr) ipTable := make([]api.IPTable, 0, len(ips)) maxConcurrency := 20 @@ -202,12 +226,17 @@ func (h ResourceHandler) ListIP(request *restful.Request, response *restful.Resp for ip := range jobChannel { if utils.IsLocalhostIP(ip) { mu.Lock() + var added = false + if _, ok := addedPorts[ip+":"+sshPort]; ok { + added = true + } ipTable = append(ipTable, api.IPTable{ IP: ip, SSHPort: sshPort, Localhost: true, SSHReachable: true, SSHAuthorized: true, + Added: added, }) mu.Unlock() continue