发布到演示环境

This commit is contained in:
zw 2022-11-24 09:13:03 +08:00
parent a98e5d15ca
commit 636d764b2e
12 changed files with 187 additions and 20 deletions

View File

@ -9,7 +9,6 @@ import (
"gorm.io/gorm/clause"
"gorm.io/gorm/schema"
"jcc-schedule/model"
//"github.com/go-sql-driver/mysql"
"github.com/golang/glog"
"github.com/karmada-io/karmada/pkg/karmadactl"
@ -39,6 +38,8 @@ type Cluster struct {
CreateTime time.Time `json:"create_time"`
Description *string `json:"description"`
NodeNum int32 `json:"node_num"`
Edge bool `json:"edge"`
Monitoring bool `json:"monitoring"`
}
type JoinRequest struct {
@ -124,7 +125,18 @@ func ListCluster(c *gin.Context) {
}
}
rows.Close()
// 查询是否有边缘节点
edge := GetRedis(c, clusters.Items[i].Name)
if strings.EqualFold(edge, "edge") {
cluster.Edge = true
}
//查询是否有监控
monitoring := GetMonitoringClusters()
for _, clusterName := range monitoring {
if strings.EqualFold(clusterName, clusters.Items[i].Name) {
cluster.Monitoring = true
}
}
if len(clusterName) != 0 && !strings.Contains(cluster.ClusterName, clusterName) {
continue
} else {
@ -132,12 +144,12 @@ func ListCluster(c *gin.Context) {
}
}
total := len(clusterList)
page := &Page[Cluster]{}
page.List = clusterList
pageNum := c.Query("pageNum")
pageSize := c.Query("pageSize")
data := Paginator(page, int64(total), pageNum, pageSize)
// 分页
page := &Page[Cluster]{}
page.List = clusterList
data := Paginator(page, int64(len(clusterList)), pageNum, pageSize)
Response(c, http.StatusOK, "success", data)
}

View File

@ -1,6 +1,7 @@
package app
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/karmada-io/karmada/pkg/util"
"net/http"
@ -21,6 +22,12 @@ type Domain struct {
LabelId []int32 `json:"labelId"`
}
type DomainUsedRate struct {
Rate float64 `json:"rate"`
DomainName string `json:"domainName"`
ClusterCount float64 `json:"clusterCount"`
}
// CreateDomain 创建域
// @Summary 创建域
// @Description 创建域
@ -287,3 +294,28 @@ func getDomainsByDeployment(namespaceName string, deploymentName string) (domain
return domainList
}
func QueryDomainUsedRate(c *gin.Context) {
var domainUsedRates []DomainUsedRate
rows, err := DB.Query("SELECT d.domain_name,COUNT(dc.id) clusterCount from domain d inner join domain_cluster dc on dc.domain_id = d.domain_id group by d.domain_id ")
if err != nil {
Response(c, http.StatusInternalServerError, "update namespace failed", err)
return
}
var domainCount float64
for rows.Next() {
var domainUsedRate DomainUsedRate
err := rows.Scan(&domainUsedRate.DomainName, &domainUsedRate.ClusterCount)
if err != nil {
return
}
domainUsedRates = append(domainUsedRates, domainUsedRate)
domainCount = domainCount + domainUsedRate.ClusterCount
}
rows.Close()
for index, _ := range domainUsedRates {
float, _ := strconv.ParseFloat(fmt.Sprintf("%.4f", domainUsedRates[index].ClusterCount/domainCount), 64)
domainUsedRates[index].Rate = float
}
Response(c, http.StatusOK, "success", domainUsedRates)
}

View File

@ -25,6 +25,11 @@ type Config struct {
MaxOpenConn int `yaml:"max-open-conn"`
MaxIdleConn int `yaml:"max-idle-conn"`
}
Redis struct {
Host string `yaml:"host"`
Password string `yaml:"password"`
DB int `yaml:"db"`
}
Karmada struct {
ConfigPath string `yaml:"config-path"`
MemberConfigPath string `yaml:"member-config-path"`

View File

@ -150,18 +150,22 @@ func CreateNamespace(c *gin.Context) {
return
}
_, createErr := ClientSet.CoreV1().Namespaces().Create(context.TODO(), &param.Namespace, metav1.CreateOptions{})
if err != nil {
if createErr != nil {
Response(c, http.StatusInternalServerError, "failed", createErr)
return
}
// 创建调度策略实例
CreatePropagationPolicies(PropagationPolicy{
policyErr := CreatePropagationPolicies(PropagationPolicy{
ClusterName: param.ClusterName,
TemplateId: param.TemplateId,
ResourceName: param.Namespace.Name,
Name: "namespace" + "." + param.Namespace.Name,
Kind: "Namespace",
})
if policyErr != nil {
Response(c, http.StatusInternalServerError, "failed", createErr)
return
}
} else {
result := PostObject(namespaceConst, param.ClusterName[0], "", "", param.Namespace)
if result.Error() != nil {
@ -403,6 +407,9 @@ func DescribeNamespace(c *gin.Context) {
}
domain.Clusters = append(domain.Clusters, clusterName)
cluster, _, _ := util.GetClusterWithKarmadaClient(KarmadaClient, clusterName)
if cluster == nil {
continue
}
allocatableCPU := cluster.Status.ResourceSummary.Allocatable.Cpu().MilliValue()
allocatableMemory := cluster.Status.ResourceSummary.Allocatable.Memory().MilliValue()
allocatedCPU := cluster.Status.ResourceSummary.Allocated.Cpu().MilliValue()

View File

@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"github.com/robfig/cron/v3"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
@ -15,9 +16,10 @@ import (
"strconv"
"strings"
"sync"
"time"
)
type NodeRes struct {
type NodeObject struct {
Kind string `json:"kind"`
ApiVersion string `json:"apiVersion"`
Metadata string `json:"metadata"`
@ -84,7 +86,7 @@ func NodeCount(c *gin.Context) {
clusterName := c.Query("clusterName")
result := SearchObject(nodeConst, clusterName, "", "")
raw, _ := result.Raw()
var nodeRes NodeRes
var nodeRes NodeObject
json.Unmarshal(raw, &nodeRes)
var masterCount int
var edgerCount int
@ -146,7 +148,7 @@ func ListNode(c *gin.Context) {
status := c.Query("status")
nodeResult := SearchObject(nodeConst, clusterName, "", "")
raw, _ := nodeResult.Raw()
var nodeRes NodeRes
var nodeRes NodeObject
json.Unmarshal(raw, &nodeRes)
var result []v1.Node
if strings.EqualFold(queryType, "edge") {
@ -296,7 +298,7 @@ func queryNodeInfo(c *gin.Context) {
// 查询节点信息
nodeRes := SearchObject(nodeConst, clusterName, "", "")
nodeRaw, _ := nodeRes.Raw()
var nodeList NodeRes
var nodeList NodeObject
json.Unmarshal(nodeRaw, &nodeList)
// 查询pod信息
podResult := SearchObject(podDetailConst, clusterName, namespace, podName)
@ -327,7 +329,7 @@ func ListEdgeNode(c *gin.Context) {
clusterName := c.Query("clusterName")
result := SearchObject(nodeConst, clusterName, "", "")
raw, _ := result.Raw()
var nodeRes NodeRes
var nodeRes NodeObject
json.Unmarshal(raw, &nodeRes)
var res []v1.Node
for _, node := range nodeRes.Items {
@ -457,3 +459,34 @@ func GetNodeMetrics8h(c *gin.Context) {
Response(c, http.StatusOK, "success", mr)
}
func QueryNodeEdgeInfo() {
// 查询已有的集群列表
var clusterNameList []string
rows, _ := DB.Query(`SELECT cluster_name FROM domain_cluster`)
for rows.Next() {
var clusterName string
err := rows.Scan(&clusterName)
if err != nil {
return
}
clusterNameList = append(clusterNameList, clusterName)
}
//创建定时任务
c := cron.New()
EntryID, err := c.AddFunc("*/1 * * * *", func() {
for _, clusterName := range clusterNameList {
nodeRes := SearchObject(detailNodeConst, clusterName, "", "")
nodeRaw, _ := nodeRes.Raw()
var nodeObject NodeObject
json.Unmarshal(nodeRaw, &nodeObject)
for _, node := range nodeObject.Items {
if _, ok := node.Labels["node-role.kubernetes.io/edge"]; ok {
SetRedis(context.TODO(), clusterName, "edge", 120)
}
}
}
})
fmt.Println(time.Now(), EntryID, err)
c.Start()
}

View File

@ -11,7 +11,7 @@ import (
)
const (
kubesphere_clusters = "group by (cluster_name) (node:load15:ratio)"
monitoring_clusters = "group by (cluster_name) (node:load15:ratio)"
apiserver_request_rate = "apiserver:apiserver_request_total:sum_irate{cluster_name='clusterName', prometheus_replica='prometheus-k8s-0'}"
apiserver_request_latencies = "apiserver:apiserver_request_duration:avg{cluster_name='clusterName', prometheus_replica='prometheus-k8s-0'}"
@ -61,6 +61,12 @@ type Overview struct {
Pod int32 `json:"pod"`
}
type Computility struct {
All int `json:"all"`
Top int `json:"top"`
Now int `json:"now"`
}
// @Summary 获取所有kubesphere定义的监控集群
// @Description 获取所有kubesphere定义的监控集群
// @Tags overview
@ -69,10 +75,10 @@ type Overview struct {
// @Success 200
// @Failure 400
// @Router /api/v1/overview/getKubesphereClusters [get]
func GetKubesphereClusters(c *gin.Context) {
func GetMonitoringClusters() []string {
metricMap := map[string][]MetricUrl{
"kubesphere_clusters": {{"", "", GetMetricUrl(kubesphere_clusters, nil, "", metric_range_1s, steps_1s)}},
"monitoring_clusters": {{"", "", GetMetricUrl(monitoring_clusters, nil, "", metric_range_1s, steps_1s)}},
}
ch := make(chan MetricResult, len(metricMap))
@ -101,7 +107,7 @@ func GetKubesphereClusters(c *gin.Context) {
}
}
Response(c, http.StatusOK, "success", clusters)
return clusters
}
// @Summary 获取容器集群调度器监控
@ -316,7 +322,7 @@ func GetApiServerMetrics(c *gin.Context) {
func ResourceCount(c *gin.Context) {
overview := &Overview{}
//纳管资源域、纳管集群总计、容器创建数量 资源域
rows, _ := DB.Query("select count(*) domain from domain")
rows, _ := DB.Query("SELECT count( t.domain) from (select DISTINCT d.domain_id domain from domain d inner join domain_cluster dc on dc.domain_id = d.domain_id) t ")
for rows.Next() {
err := rows.Scan(&overview.Domain)
if err != nil {
@ -415,3 +421,11 @@ func GetOverallMetrics(c *gin.Context) {
Response(c, http.StatusOK, "success", mr)
}
func getComputility(c *gin.Context) {
Response(c, http.StatusOK, "success", Computility{
All: 100,
Top: 79,
Now: 1,
})
}

View File

@ -65,7 +65,7 @@ func CreatePropagationPolicies(propagationPolicy PropagationPolicy) error {
rst.ReplicaSchedulingType = policyv1alpha1.ReplicaSchedulingTypeDivided
rst.ReplicaDivisionPreference = policyv1alpha1.ReplicaDivisionPreferenceWeighted
}
if strings.EqualFold(propagationPolicy.Kind, "PersistentVolume") {
if strings.EqualFold(propagationPolicy.Kind, "PersistentVolume") || strings.EqualFold(propagationPolicy.Kind, "Namespace") {
policy := &policyv1alpha1.ClusterPropagationPolicy{
ObjectMeta: v1.ObjectMeta{
Name: propagationPolicy.Name,

View File

@ -1,9 +1,11 @@
package app
import (
"context"
"crypto/tls"
"database/sql"
"github.com/gin-gonic/gin"
"github.com/go-redis/redis/v8"
_ "github.com/go-sql-driver/mysql"
karmadaclientset "github.com/karmada-io/karmada/pkg/generated/clientset/versioned"
"github.com/karmada-io/karmada/pkg/karmadactl"
@ -27,10 +29,12 @@ var ControlPlaneRestConfig *rest.Config
var ClientSet *kubeclient.Clientset
var OpenSearchClient *opensearch.Client
var CrDClient *clientset.Clientset
var MyRedis *redis.Client
var ConfigNacos = GetNacosConfig()
func InitRouter() *gin.Engine {
r := gin.New()
r.Use(gin.Logger())
r.Use(gin.Recovery())
@ -42,6 +46,14 @@ func InitRouter() *gin.Engine {
DB.SetMaxOpenConns(int(ConfigNacos.Mysql.MaxOpenConn))
DB.SetMaxIdleConns(int(ConfigNacos.Mysql.MaxIdleConn))
//redis连接
MyRedis = redis.NewClient(&redis.Options{
Addr: ConfigNacos.Redis.Host,
Password: ConfigNacos.Redis.Password, // no password set
DB: ConfigNacos.Redis.DB, // use default DB
})
MyRedis.Ping(context.Background()).Result()
//Karmada Client
dir, _ := os.Getwd()
KarmadaConfig = karmadactl.NewKarmadaConfig(clientcmd.NewDefaultPathOptions())
@ -60,6 +72,8 @@ func InitRouter() *gin.Engine {
Password: ConfigNacos.OpenSearch.PassWord,
})
//创建定时任务
//api分组
api := r.Group("/api")
v1 := api.Group("/v1")
@ -99,6 +113,7 @@ func InitRouter() *gin.Engine {
domain.GET("/describe", DescribeDomain)
domain.GET("/list", ListDomain)
domain.GET("/listByDeployment", ListByDeployment)
domain.GET("/usedRate", QueryDomainUsedRate)
//Pod
pod := v1.Group("pod")
@ -243,7 +258,7 @@ func InitRouter() *gin.Engine {
overview.GET("/getClusterMetrics", GetClusterMetrics)
overview.GET("/getScheduleMetrics", GetScheduleMetrics)
overview.GET("/getOverallMetrics", GetOverallMetrics)
overview.GET("/getKubesphereClusters", GetKubesphereClusters)
overview.GET("/getComputility", getComputility)
}
return r

View File

@ -1,9 +1,12 @@
package app
import (
"context"
"fmt"
gossh "golang.org/x/crypto/ssh"
"net"
"strings"
"time"
)
// Cli 连接信息
@ -68,3 +71,38 @@ func LabelsConvertToMap(label string) map[string]string {
}
return labelMap
}
func SetRedis(ctx context.Context, key string, value string, t int64) bool {
expire := time.Duration(t) * time.Second
if err := MyRedis.Set(ctx, key, value, expire).Err(); err != nil {
return false
}
return true
}
func GetRedis(ctx context.Context, key string) string {
result, err := MyRedis.Get(ctx, key).Result()
if err != nil {
return ""
}
return result
}
func DelRedis(ctx context.Context, key string) bool {
_, err := MyRedis.Del(ctx, key).Result()
if err != nil {
fmt.Println(err)
return false
}
return true
}
func ExpireRedis(ctx context.Context, key string, t int64) bool {
// 延长过期时间
expire := time.Duration(t) * time.Second
if err := MyRedis.Expire(ctx, key, expire).Err(); err != nil {
fmt.Println(err)
return false
}
return true
}

3
go.mod
View File

@ -40,6 +40,7 @@ require (
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/chai2010/gettext-go v0.0.0-20160711120539-c6fed771bfd5 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/emicklei/go-restful/v3 v3.8.0 // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
@ -57,6 +58,7 @@ require (
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.10.0 // indirect
github.com/go-redis/redis/v8 v8.11.5 // indirect
github.com/goccy/go-json v0.9.7 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
@ -101,6 +103,7 @@ require (
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/rogpeppe/go-internal v1.8.0 // indirect
github.com/russross/blackfriday v1.5.2 // indirect
github.com/spf13/cobra v1.4.0 // indirect

7
go.sum
View File

@ -135,6 +135,7 @@ github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx2
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/certifi/gocertifi v0.0.0-20191021191039-0944d244cd40/go.mod h1:sGbDF6GwGcLpkNXPUTkMRoywsNa/ol15pxFe6ERfguA=
github.com/certifi/gocertifi v0.0.0-20200922220541-2c3bb06c6054/go.mod h1:sGbDF6GwGcLpkNXPUTkMRoywsNa/ol15pxFe6ERfguA=
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
@ -185,6 +186,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/daviddengcn/go-colortext v0.0.0-20160507010035-511bcaf42ccd/go.mod h1:dv4zxwHi5C/8AeI+4gX4dCWOIvNi7I6JCSX0HvlKPgE=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/docker/distribution v2.7.1+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=
github.com/docker/distribution v2.8.1+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=
@ -331,6 +334,8 @@ github.com/go-playground/universal-translator v0.18.0 h1:82dyy6p4OuJq4/CByFNOn/j
github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA=
github.com/go-playground/validator/v10 v10.10.0 h1:I7mrTYv78z8k8VXa/qJlOlEXn/nBh+BF8dHX5nt/dr0=
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-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
@ -707,6 +712,8 @@ github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=

View File

@ -7,6 +7,7 @@ import "jcc-schedule/app"
// @description jcc
func main() {
router := app.InitRouter()
app.QueryNodeEdgeInfo()
go app.Watch()
_ = router.Run(":8082")
}