查询域和集群接口修改

This commit is contained in:
zhangwei 2023-06-17 00:02:29 +08:00
parent 3b397371a8
commit 61a4d503be
5 changed files with 56 additions and 36 deletions

View File

@ -1,9 +1,11 @@
package app
import (
"context"
"fmt"
"github.com/gin-gonic/gin"
"github.com/karmada-io/karmada/pkg/util"
v2 "k8s.io/apimachinery/pkg/apis/meta/v1"
"net/http"
"strconv"
)
@ -240,6 +242,16 @@ func DescribeDomain(c *gin.Context) {
Response(c, http.StatusOK, "success", domain)
}
func ListClusters(c *gin.Context) {
namespaceName := c.Query("namespace")
deploymentName := c.Query("deployment_name")
propagationPolicy, _ := KarmadaClient.PolicyV1alpha1().PropagationPolicies(namespaceName).Get(context.TODO(), "deployment."+namespaceName+"."+deploymentName, v2.GetOptions{})
Response(c, http.StatusOK, "success", propagationPolicy.Spec.Placement.ClusterAffinity.ClusterNames)
}
// ListByDeployment 根据项目名称和工作负载查询域列表
// @Summary 根据项目名称和工作负载查询域列表
@ -253,45 +265,27 @@ func DescribeDomain(c *gin.Context) {
// @Failure 500
// @Router /api/v1/domain/listByDeployment [get]
func ListByDeployment(c *gin.Context) {
domainList := make([]Domain, 0)
namespaceName := c.Query("namespace")
deploymentName := c.Query("deployment_name")
domainList = getDomainsByDeployment(namespaceName, deploymentName)
domainList := getDomainsByDeployment(namespaceName, deploymentName)
Response(c, http.StatusOK, "success", domainList)
}
// 通过ns名称+deploy名称查询deploy所在域的信息单独方法供复用
func getDomainsByDeployment(namespaceName string, deploymentName string) (domainList []Domain) {
deployJson := GetDeployFromOS(namespaceName, deploymentName, "")
hits, _ := deployJson.Get("hits").Get("hits").Array()
for i := 0; i < len(hits); i++ {
cluster, _ := deployJson.Get("hits").Get("hits").GetIndex(i).Get("_source").Get("metadata").Get("annotations").Get("resource.karmada.io/cached-from-cluster").String()
domain := Domain{}
//获取域列表
rows, _ := DB.Query("select domain_name,domain_id,cluster_name from domain_cluster where cluster_name = ?", cluster)
var clusters []string
for rows.Next() {
var domainName string
var clusterName string
var domainId int32
err := rows.Scan(&domainName, &domainId, &clusterName)
if err != nil {
return nil
}
domain.DomainName = domainName
domain.DomainId = domainId
clusters = append(clusters, clusterName)
domain.Clusters = clusters
domainList = append(domainList, domain)
}
func getDomainsByDeployment(namespaceName string, deploymentName string) []string {
// 获取策略信息
propagationPolicy, err := KarmadaClient.PolicyV1alpha1().PropagationPolicies(namespaceName).Get(context.TODO(), "deployment."+namespaceName+"."+deploymentName, v2.GetOptions{})
if err != nil {
return nil
}
//去重
domainList = RemoveRepeatedDomain(domainList)
var clusterList []string
for _, clusterName := range propagationPolicy.Spec.Placement.ClusterAffinity.ClusterNames {
clusterList = append(clusterList, clusterName)
}
var domainList []string
//strings.Join(clusterList, ",")
Gorm.Debug().Raw("SELECT domain_name from domain_cluster where cluster_name in (?)", clusterList).Scan(&domainList)
return domainList
}

View File

@ -472,7 +472,6 @@ func DescribeNamespace(c *gin.Context) {
}
}
deploySet = append(deploySet, deployName+":"+clusterName)
/////////
var totalAllocatableCPU int64
var totalAllocatableMemory int64

View File

@ -13,6 +13,9 @@ import (
"github.com/opensearch-project/opensearch-go/v2"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
v2 "gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/schema"
_ "jcc-schedule/docs"
"k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
kubeclient "k8s.io/client-go/kubernetes"
@ -31,7 +34,7 @@ var ClientSet *kubeclient.Clientset
var OpenSearchClient *opensearch.Client
var CrDClient *clientset.Clientset
var MyRedis *redis.Client
var Gorm *gorm.DB
var ConfigNacos = GetNacosConfig()
func InitRouter() *gin.Engine {
@ -54,6 +57,13 @@ func InitRouter() *gin.Engine {
DBPCM.SetMaxOpenConns(int(ConfigNacos.Mysql.MaxOpenConn))
DBPCM.SetMaxIdleConns(int(ConfigNacos.Mysql.MaxIdleConn))
//启动Gorm支持
gorm, _ := gorm.Open(v2.Open(dsn), &gorm.Config{
NamingStrategy: schema.NamingStrategy{
SingularTable: true, // 使用单数表名,启用该选项,此时,`User` 的表名应该是 `t_user`
},
})
Gorm = gorm
//redis连接
MyRedis = redis.NewClient(&redis.Options{
Addr: ConfigNacos.Redis.Host,
@ -123,6 +133,7 @@ func InitRouter() *gin.Engine {
domain.GET("/describe", DescribeDomain)
domain.GET("/list", ListDomain)
domain.GET("/listByDeployment", ListByDeployment)
domain.GET("/listClusters", ListClusters)
domain.GET("/usedRate", QueryDomainUsedRate)
//Pod

7
go.mod
View File

@ -6,7 +6,7 @@ require (
github.com/bitly/go-simplejson v0.5.0
github.com/gin-gonic/gin v1.8.1
github.com/go-redis/redis/v8 v8.11.5
github.com/go-sql-driver/mysql v1.6.0
github.com/go-sql-driver/mysql v1.7.0
github.com/golang/glog v1.0.0
github.com/jmoiron/sqlx v1.3.5
github.com/karmada-io/karmada v1.3.0
@ -21,6 +21,8 @@ require (
golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd
golang.org/x/exp v0.0.0-20221126150942-6ab00d035af9
gopkg.in/yaml.v3 v3.0.1
gorm.io/driver/mysql v1.5.1
gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55
k8s.io/api v0.25.0
k8s.io/apiextensions-apiserver v0.25.0
k8s.io/apimachinery v0.25.0
@ -74,6 +76,8 @@ require (
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/jonboulle/clockwork v0.2.2 // indirect
github.com/josharian/intern v1.0.0 // indirect
@ -85,6 +89,7 @@ require (
github.com/mailru/easyjson v0.7.6 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/mattn/go-sqlite3 v1.14.17 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
github.com/moby/spdystream v0.2.0 // indirect

15
go.sum
View File

@ -335,8 +335,9 @@ github.com/go-playground/validator/v10 v10.10.0 h1:I7mrTYv78z8k8VXa/qJlOlEXn/nBh
github.com/go-playground/validator/v10 v10.10.0/go.mod h1:74x4gJWsvQexRdW8Pn3dXSGrTK4nAUsbPlLADvpJkos=
github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI=
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc=
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
github.com/gobuffalo/flect v0.2.0/go.mod h1:W3K3X9ksuZfir8f/LrfVtWmCDQFfayuylOJ7sz/Fj80=
@ -506,6 +507,10 @@ github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
@ -587,8 +592,9 @@ github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzp
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUbg=
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM=
github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 h1:I0XW9+e1XWDxdcEniV4rQAIOPUGDq67JSCiRCgGCZLI=
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
@ -1405,6 +1411,11 @@ gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/mysql v1.5.1 h1:WUEH5VF9obL/lTtzjmML/5e6VfFR/788coz2uaVCAZw=
gorm.io/driver/mysql v1.5.1/go.mod h1:Jo3Xu7mMhCyj8dlrb3WoCaRd1FhsVh+yMXb1jUInf5o=
gorm.io/gorm v1.25.1/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55 h1:sC1Xj4TYrLqg1n3AN10w871An7wJM0gzgcm8jkIkECQ=
gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk=