feat: 根据域查询集群下Namespace列表

Signed-off-by: devad <cossjie@foxmail.com>
This commit is contained in:
devad 2023-05-19 09:14:46 +08:00
parent 769991bb95
commit 6519bfb799
2 changed files with 79 additions and 0 deletions

View File

@ -85,6 +85,26 @@ type NsResources struct {
ServiceCount int `json:"serviceCount"`
}
type NsDomain struct {
DomainName string `json:"domainName"`
Clusters []NsCluster `json:"clusters"`
}
type NsCluster struct {
ClusterName string `json:"clusterName"`
NameSpaces []NsNameSpace `json:"nameSpaces"`
}
type NsNameSpace struct {
NameSpace string `json:"nameSpace"`
NameSpaceType string `json:"nameSpaceType"`
}
type NsClusterType struct {
ClusterName string `db:"cluster_name" json:"clusterName"`
ClusterType string `db:"cluster_type" json:"ClusterType"`
}
func GetNamespaceResources(c *gin.Context) {
clusterName := c.Query("clusterName")
namespace := c.Query("namespace")
@ -320,6 +340,64 @@ func ListNamespace(c *gin.Context) {
Response(c, http.StatusOK, "success", nameList)
}
// ListNamespaceByDomain 根据域查询集群下Namespace列表
// @Summary 根据域查询集群下Namespace列表
// @Description 根据域查询集群下Namespace列表
// @Tags namespace
// @accept json
// @Produce json
// @Success 200
// @Failure 400
// @Router /api/v1/namespace/nameSpaceByRegion [get]
func ListNamespaceByDomain(c *gin.Context) {
domainNameList := make([]string, 0)
domainList := make([]NsDomain, 0)
clusterNameList := make([]NsClusterType, 0)
var namespaceObject NamespaceObject
//查询所有域
err := DB.Select(&domainNameList, `select domain_name from domain_cluster group by domain_name`)
if err != nil {
Response(c, http.StatusInternalServerError, "failed", err)
return
}
//查询域下面的集群
for _, s := range domainNameList {
cerr := DB.Select(&clusterNameList, `select cluster_name,cluster_type from domain_cluster where domain_name=?`, s)
if cerr != nil {
Response(c, http.StatusInternalServerError, "failed", cerr.Error())
return
}
clusterList := make([]NsCluster, 0)
//查询集群下的namespace
for _, c := range clusterNameList {
cluster := NsCluster{}
cluster.ClusterName = c.ClusterName
result := SearchObject(namespaceConst, c.ClusterName, "", "")
raw, _ := result.Raw()
json.Unmarshal(raw, &namespaceObject)
namespaceList := make([]NsNameSpace, 0)
for _, ns := range namespaceObject.Items {
namespace := NsNameSpace{}
namespace.NameSpace = ns.Name
namespace.NameSpaceType = c.ClusterType
namespaceList = append(namespaceList, namespace)
cluster.NameSpaces = namespaceList
}
clusterList = append(clusterList, cluster)
}
domain := NsDomain{}
domain.DomainName = s
domain.Clusters = clusterList
domainList = append(domainList, domain)
}
Response(c, http.StatusOK, "success", domainList)
}
func DetailNamespace(c *gin.Context) {
clusterName := c.Query("clusterName")
name := c.Query("name")

View File

@ -107,6 +107,7 @@ func InitRouter() *gin.Engine {
namespace.GET("/getBatchMetrics", GetBatchNamespaceMetrics)
namespace.GET("/getMetrics", GetNamespaceMetrics)
namespace.GET("/resources", GetNamespaceResources)
namespace.GET("/nameSpaceByRegion", ListNamespaceByDomain)
//Domain
domain := v1.Group("domain")