This commit is contained in:
zhangwei 2024-03-11 17:33:35 +08:00
parent 480c1b553f
commit e2419be5fd
6 changed files with 122 additions and 16 deletions

2
go.mod
View File

@ -10,13 +10,13 @@ require (
github.com/prometheus/client_golang v1.18.0
github.com/prometheus/common v0.45.0
github.com/robfig/cron/v3 v3.0.1
github.com/rs/zerolog v1.28.0
github.com/spf13/viper v1.10.1
github.com/zeromicro/go-zero v1.6.2
golang.org/x/crypto v0.18.0
k8s.io/api v0.29.1
k8s.io/apimachinery v0.29.1
k8s.io/client-go v0.29.1
gitlink.org.cn/JointCloud/pcm-coordinator v0.0.0-20240302013624-9de512ee32ab
)
require (

9
go.sum
View File

@ -50,6 +50,7 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@ -100,6 +101,7 @@ github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEe
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls=
github.com/goccy/go-json v0.9.7 h1:IcB+Aqpx/iMHu5Yooh7jEzJk1JZ7Pjtmys2ukPr7EeM=
github.com/goccy/go-json v0.9.7/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
@ -207,8 +209,10 @@ github.com/magiconair/properties v1.8.5 h1:b6kJs+EmPFMYGkow9GiUyCyOvIwYetYJ3fSaW
github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
@ -256,6 +260,9 @@ github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTE
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.28.0 h1:MirSo27VyNi7RJYP3078AA1+Cyzd2GB66qy3aUHvsWY=
github.com/rs/zerolog v1.28.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0=
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spf13/afero v1.8.0 h1:5MmtuhAgYeU6qpa7w7bP0dv6MBYuup0vekhSpSkoq60=
@ -294,7 +301,6 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/zeromicro/go-zero v1.6.2 h1:c1gXp6JTO0e+dtfwNZRE7OZgzjipfW8i1iBMoBnDwBI=
github.com/zeromicro/go-zero v1.6.2/go.mod h1:mQKK/c/er/sbIAo7DWyFBZX8oa0eOkc7QJdG15b2GBw=
gitlink.org.cn/JointCloud/pcm-coordinator v0.0.0-20240302013624-9de512ee32ab/go.mod h1:JDp5tWVskac81zhZQA8Ia6NxlUls2DVyke2ufT32aC0=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
@ -454,6 +460,7 @@ golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

View File

@ -2,6 +2,7 @@ package apiserver
import (
"github.com/go-resty/resty/v2"
"github.com/rs/zerolog/log"
"jcc-schedule/pkg/monitoring"
"jcc-schedule/pkg/monitoring/prometheus"
"k8s.io/client-go/dynamic"
@ -14,16 +15,20 @@ type APIServer struct {
MonitoringClientMap map[string]monitoring.Interface
ClientSetMap map[string]*kubernetes.Clientset
DynamicClientMap map[string]*dynamic.DynamicClient
Clusters []*Cluster
Clusters []*Cluster `json:"list"`
}
var ApiServer APIServer
func NewAPIServer() {
apiServer := &APIServer{}
apiServer.installClusters()
apiServer.installK8sClient()
apiServer.installMonitoring()
ApiServer = APIServer{
ClientSetMap: make(map[string]*kubernetes.Clientset),
MonitoringClientMap: make(map[string]monitoring.Interface),
DynamicClientMap: make(map[string]*dynamic.DynamicClient),
}
ApiServer.installClusters()
ApiServer.installK8sClient()
ApiServer.installMonitoring()
}
type Cluster struct {
@ -34,18 +39,28 @@ type Cluster struct {
}
func (s *APIServer) installClusters() {
remoteResp := RemoteResp{}
client := resty.New()
resp, err := client.R().
SetQueryParams(map[string]string{
"adapterId": "1752857389213683712",
}).
SetResult(&s.Clusters).
SetResult(&remoteResp).
ForceContentType("application/json").
Get("http://localhost:8999/pcm/v1/adapter/cluster/list")
println(resp.Status())
if err != nil {
return
if err != nil || resp.StatusCode() != 200 {
panic("init clusters error")
}
for _, remoteClusterInfo := range remoteResp.Data.List {
if remoteClusterInfo.Label == "kubernetes" {
s.Clusters = append(s.Clusters, &Cluster{
Name: remoteClusterInfo.Name,
Server: remoteClusterInfo.Server,
BearerToken: remoteClusterInfo.Token,
MonitorServer: remoteClusterInfo.MonitorServer,
})
}
}
}
@ -60,9 +75,15 @@ func (s *APIServer) installK8sClient() {
Insecure: true,
},
}
dynamicClient, _ := dynamic.NewForConfig(restConfig)
dynamicClient, err := dynamic.NewForConfig(restConfig)
if err != nil {
log.Printf("init dynamicClient error.")
}
s.DynamicClientMap[cluster.Name] = dynamicClient
clientSet, _ := kubernetes.NewForConfig(restConfig)
clientSet, err := kubernetes.NewForConfig(restConfig)
if err != nil {
log.Printf("init clientSet error.")
}
s.ClientSetMap[cluster.Name] = clientSet
}
}

33
pkg/apiserver/types.go Normal file
View File

@ -0,0 +1,33 @@
package apiserver
type RemoteResp struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data ClusterResp `json:"data,omitempty"`
}
type ClusterResp struct {
List []ClusterInfo `json:"list,omitempty"`
}
type ClusterInfo struct {
Id string `json:"id,omitempty" db:"id"`
AdapterId string `json:"adapterId,omitempty" db:"adapter_id"`
Name string `json:"name,omitempty" db:"name"`
Nickname string `json:"nickname,omitempty" db:"nickname"`
Description string `json:"description,omitempty" db:"description"`
Server string `json:"server,omitempty" db:"server"`
MonitorServer string `json:"monitorServer,omitempty" db:"monitor_server"`
Username string `json:"username,omitempty" db:"username"`
Password string `json:"password,omitempty" db:"password"`
Token string `json:"token,omitempty" db:"token"`
Ak string `json:"ak,omitempty" db:"ak"`
Sk string `json:"sk,omitempty" db:"sk"`
Region string `json:"region,omitempty" db:"region"`
ProjectId string `json:"projectId,omitempty" db:"project_id"`
Version string `json:"version,omitempty" db:"version"`
Label string `json:"label,omitempty" db:"label"`
OwnerId string `json:"ownerId,omitempty" db:"owner_id"`
AuthType string `json:"authType,omitempty" db:"auth_type"`
CreateTime string `json:"createTime,omitempty" db:"created_time" gorm:"autoCreateTime"`
}

View File

@ -0,0 +1,44 @@
package v1
import (
"github.com/gin-gonic/gin"
"jcc-schedule/pkg/apiserver"
"jcc-schedule/pkg/monitoring"
"net/http"
"time"
)
type ControllerMetricsReq struct {
ClusterName string `form:"clusterName"`
Metrics []string `form:"metrics"`
Pod string `form:"pod"`
WorkloadName string `form:"workloadName"`
Steps string `form:"steps"`
Start string `form:"start"`
End string `form:"end"`
Level string `form:"level"`
}
type ControllerMetricsResp struct {
Data interface{} `json:"data"`
}
func ControllerMetrics(c *gin.Context) {
var p = &ControllerMetricsReq{}
if err := c.BindJSON(p); err != nil {
Response(c, http.StatusBadRequest, "invalid request params.", "")
return
}
var result []monitoring.Metric
if len(p.Pod) != 0 {
result = apiserver.ApiServer.MonitoringClientMap[p.ClusterName].GetNamedMetricsByTime(p.Metrics, p.Start, p.End, 60*time.Minute, monitoring.PodOption{
PodName: p.Pod,
})
} else {
result = apiserver.ApiServer.MonitoringClientMap[p.ClusterName].GetNamedMetricsByTime(p.Metrics, p.Start, p.End, 60*time.Minute, monitoring.ControllerOption{
WorkloadName: p.WorkloadName,
})
}
Response(c, http.StatusOK, "success", result)
}

View File

@ -12,9 +12,7 @@ func InitRouter() *gin.Engine {
//api分组
apiv1 := r.Group("/api/v1")
{
//Namespace
namespace := apiv1.Group("namespace")
namespace.GET("/list", v1.ListNamespace)
@ -46,6 +44,9 @@ func InitRouter() *gin.Engine {
statefulSet := apiv1.Group("statefulSet")
statefulSet.PUT("/restart", v1.RestartStatefulSet)
//monitoring
monitoring := apiv1.Group("monitoring")
monitoring.GET("/controller", v1.ControllerMetrics)
}
return r