This commit is contained in:
zhangwei 2024-04-08 15:07:38 +08:00
parent 2f749dba3b
commit db4ecceff4
14 changed files with 273 additions and 114 deletions

View File

@ -1,7 +1,22 @@
FROM alpine:3.16.2
WORKDIR /home
FROM golang:1.20.2-alpine3.17 AS builder
# 修改alpine源为上海交通大学
WORKDIR /app
LABEL stage=gobuilder
ENV CGO_ENABLED 0
ENV GOARCH amd64
ENV GOPROXY https://goproxy.cn,direct
COPY . .
COPY etc/ /app/
RUN go mod download
RUN go build -o /app/pcm-participant-kubernetes /app/main.go
FROM alpine:3.16.2
WORKDIR /app
#修改alpine源为上海交通大学
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.sjtug.sjtu.edu.cn/g' /etc/apk/repositories && \
apk update && \
apk upgrade && \
@ -9,10 +24,11 @@ RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.sjtug.sjtu.edu.cn/g' /etc/apk/repos
apk add --update tzdata && \
rm -rf /var/cache/apk/*
COPY --from=builder /app/pcm-participant-kubernetes .
COPY etc/config.yaml /app/etc/
COPY jcce-schedule /home/
ENV TZ=Asia/Shanghai
EXPOSE 8082
EXPOSE 2003
ENTRYPOINT ./jcce-schedule
ENTRYPOINT ./pcm-participant-kubernetes -f etc/config.yaml

25
etc/config.go Normal file
View File

@ -0,0 +1,25 @@
package etc
import (
"github.com/spf13/viper"
)
type ServerConfig struct {
Name string `mapstructure:"name"`
Port int `mapstructure:"port"`
AdapterId int64 `mapstructure:"adapterId"`
CoreServerUrl string `mapstructure:"coreServerUrl"`
}
var ServerConf *ServerConfig
func init() {
v := viper.New()
v.SetConfigFile("etc/config.yaml")
if err := v.ReadInConfig(); err != nil {
panic(err)
}
if err := v.Unmarshal(&ServerConf); err != nil {
panic(err)
}
}

7
etc/config.yaml Normal file
View File

@ -0,0 +1,7 @@
name: pcm-kubernetes
port: 8022
adapterId: 1770658294298316800
coreServerUrl: http://pcm-core-api.ns-admin:8999
#coreServerUrl: http://47.92.39.128:31031

View File

@ -1,30 +0,0 @@
package config
import (
"github.com/spf13/viper"
)
type ServerConfig struct {
Name string `mapstructure:"name"`
Port int `mapstructure:"port"`
Core Core `mapstructure:"core"`
AdapterId int64 `mapstructure:"adapterId"`
}
type Core struct {
url string `mapstructure:"url"`
}
var ServerConf *ServerConfig
func init() {
v := viper.New()
v.SetConfigFile("etc/config/config.yaml")
if err := v.ReadInConfig(); err != nil {
panic(err)
}
//serverConfig := ServerConfig{}
if err := v.Unmarshal(&ServerConf); err != nil {
panic(err)
}
}

View File

@ -1,7 +0,0 @@
name: pcm-kubernetes
port: 8022
core:
url: localhost:8080
adapterId: 1770658294298316800

26
go.mod
View File

@ -7,27 +7,27 @@ require (
github.com/go-resty/resty/v2 v2.10.0
github.com/golang/glog v1.1.2
github.com/json-iterator/go v1.1.12
github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.72.0
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
golang.org/x/crypto v0.19.0
k8s.io/api v0.29.2
k8s.io/apimachinery v0.29.2
k8s.io/client-go v0.29.1
sigs.k8s.io/yaml v1.4.0
)
require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-logr/logr v1.3.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-openapi/jsonpointer v0.20.0 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.4 // indirect
@ -47,15 +47,12 @@ require (
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/spf13/afero v1.8.0 // indirect
github.com/spf13/cast v1.4.1 // indirect
@ -67,10 +64,10 @@ require (
go.opentelemetry.io/otel/sdk v1.21.0 // indirect
go.opentelemetry.io/otel/trace v1.21.0 // indirect
go.uber.org/automaxprocs v1.5.3 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/oauth2 v0.15.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/term v0.16.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/term v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
@ -79,10 +76,9 @@ require (
gopkg.in/ini.v1 v1.66.3 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/klog/v2 v2.110.1 // indirect
k8s.io/klog/v2 v2.120.1 // indirect
k8s.io/kube-openapi v0.0.0-20231206194836-bf4651e18aa8 // indirect
k8s.io/utils v0.0.0-20231127182322-b307cd553661 // indirect
k8s.io/utils v0.0.0-20240102154912-e7106e64919e // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)

41
go.sum
View File

@ -75,8 +75,8 @@ github.com/gin-gonic/gin v1.8.1/go.mod h1:ji8BvRH1azfM+SYow9zQ6SZMvR8qOMZHmsCuWR
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-logr/logr v1.3.0 h1:2y3SDp0ZXuc6/cjLSZ+Q3ir+QB9T/iG5yYRXqsagWSY=
github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/go-openapi/jsonpointer v0.19.6/go.mod h1:osyAmYz/mB/C3I+WsTTSgw1ONzaLJoLCyoi6/zppojs=
@ -240,10 +240,13 @@ github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdU
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prashantv/gostub v1.1.0 h1:BTyx3RfQjRHnUWaGF9oQos79AlQ5k8WNktv7VGvVH4g=
github.com/prashantv/gostub v1.1.0/go.mod h1:A5zLQHz7ieHGG7is6LLXLz7I8+3LZzsrV0P1IAHhP5U=
github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.72.0 h1:9h7PxMhT1S8lOdadEKJnBh3ELMdO60XkoDV98grYjuM=
github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.72.0/go.mod h1:4FiLCL664L4dNGeqZewiiD0NS7hhqi/CxyM4UOq5dfM=
github.com/prometheus/client_golang v1.18.0 h1:HzFfmkOzH5Q8L8G+kSJKUx5dtG87sewO+FoDDqP5Tbk=
github.com/prometheus/client_golang v1.18.0/go.mod h1:T+GXkCk5wSJyOqMIzVgvvjFDlkOQntgjkJWKrN5txjA=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
@ -327,8 +330,8 @@ golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5y
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc=
golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg=
golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo=
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
@ -399,8 +402,8 @@ golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo=
golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
@ -468,15 +471,15 @@ golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U=
golang.org/x/term v0.16.0 h1:m+B6fahuftsE9qjo0VWp2FW0mB3MTJvR0BaMQrq0pmE=
golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY=
golang.org/x/term v0.17.0 h1:mkTF7LCd6WGJNL3K1Ad7kwxNfYAW6a8a8QqtMblp/4U=
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
@ -672,18 +675,18 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
k8s.io/api v0.29.1 h1:DAjwWX/9YT7NQD4INu49ROJuZAAAP/Ijki48GUPzxqw=
k8s.io/api v0.29.1/go.mod h1:7Kl10vBRUXhnQQI8YR/R327zXC8eJ7887/+Ybta+RoQ=
k8s.io/apimachinery v0.29.1 h1:KY4/E6km/wLBguvCZv8cKTeOwwOBqFNjwJIdMkMbbRc=
k8s.io/apimachinery v0.29.1/go.mod h1:6HVkd1FwxIagpYrHSwJlQqZI3G9LfYWRPAkUvLnXTKU=
k8s.io/api v0.29.2 h1:hBC7B9+MU+ptchxEqTNW2DkUosJpp1P+Wn6YncZ474A=
k8s.io/api v0.29.2/go.mod h1:sdIaaKuU7P44aoyyLlikSLayT6Vb7bvJNCX105xZXY0=
k8s.io/apimachinery v0.29.2 h1:EWGpfJ856oj11C52NRCHuU7rFDwxev48z+6DSlGNsV8=
k8s.io/apimachinery v0.29.2/go.mod h1:6HVkd1FwxIagpYrHSwJlQqZI3G9LfYWRPAkUvLnXTKU=
k8s.io/client-go v0.29.1 h1:19B/+2NGEwnFLzt0uB5kNJnfTsbV8w6TgQRz9l7ti7A=
k8s.io/client-go v0.29.1/go.mod h1:TDG/psL9hdet0TI9mGyHJSgRkW3H9JZk2dNEUS7bRks=
k8s.io/klog/v2 v2.110.1 h1:U/Af64HJf7FcwMcXyKm2RPM22WZzyR7OSpYj5tg3cL0=
k8s.io/klog/v2 v2.110.1/go.mod h1:YGtd1984u+GgbuZ7e08/yBuAfKLSO0+uR1Fhi6ExXjo=
k8s.io/klog/v2 v2.120.1 h1:QXU6cPEOIslTGvZaXvFWiP9VKyeet3sawzTOvdXb4Vw=
k8s.io/klog/v2 v2.120.1/go.mod h1:3Jpz1GvMt720eyJH1ckRHK1EDfpxISzJ7I9OYgaDtPE=
k8s.io/kube-openapi v0.0.0-20231206194836-bf4651e18aa8 h1:vzKzxN5uyJZLY8HL1/OovW7BJefnsBIWt8T7Gjh2boQ=
k8s.io/kube-openapi v0.0.0-20231206194836-bf4651e18aa8/go.mod h1:AsvuZPBlUDVuCdzJ87iajxtXuR9oktsTctW/R9wwouA=
k8s.io/utils v0.0.0-20231127182322-b307cd553661 h1:FepOBzJ0GXm8t0su67ln2wAZjbQ6RxQGZDnzuLcrUTI=
k8s.io/utils v0.0.0-20231127182322-b307cd553661/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
k8s.io/utils v0.0.0-20240102154912-e7106e64919e h1:eQ/4ljkx21sObifjzXwlPKpdGLrCfRziVtos3ofG/sQ=
k8s.io/utils v0.0.0-20240102154912-e7106e64919e/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=

View File

@ -3,7 +3,7 @@ package apiserver
import (
"github.com/go-resty/resty/v2"
"github.com/rs/zerolog/log"
"jcc-schedule/etc/config"
"jcc-schedule/etc"
"jcc-schedule/pkg/monitoring"
"jcc-schedule/pkg/monitoring/prometheus"
"k8s.io/client-go/dynamic"
@ -48,14 +48,13 @@ func (s *APIServer) installClusters() {
resp, err := s.HttpClient.R().
SetQueryParams(map[string]string{
//"adapterId": "1752857389213683712",
"adapterId": strconv.FormatInt(config.ServerConf.AdapterId, 10),
"adapterId": strconv.FormatInt(etc.ServerConf.AdapterId, 10),
"pageNum": "1",
"pageSize": "999",
}).
SetResult(&remoteResp).
ForceContentType("application/json").
Get("http://localhost:8999/pcm/v1/adapter/cluster/list")
//Get("http://47.92.39.128:31031/pcm/v1/adapter/cluster/list")
Get(etc.ServerConf.CoreServerUrl + "/pcm/v1/adapter/cluster/list")
if err != nil || resp.StatusCode() != 200 {
panic("init clusters error")

View File

@ -2,7 +2,8 @@ package cron
import (
"github.com/prometheus/common/model"
"jcc-schedule/etc/config"
"github.com/zeromicro/go-zero/core/logx"
"jcc-schedule/etc"
"jcc-schedule/pkg/apiserver"
"jcc-schedule/pkg/monitoring"
"sync"
@ -52,17 +53,19 @@ func syncClusterLoadRecords() {
}
resp, err := apiserver.ApiServer.HttpClient.R().SetBody(req).
ForceContentType("application/json").
Post("http://localhost:8999/pcm/v1/core/syncClusterLoad")
//Post("http://47.92.39.128:31031/pcm/v1/core/syncClusterLoad")
Post(etc.ServerConf.CoreServerUrl + "/pcm/v1/core/syncClusterLoad")
if err != nil || resp.StatusCode() != 200 {
panic("init clusters error")
if err != nil {
logx.Error(err)
}
if resp.StatusCode() != 200 {
logx.Error(resp.Error())
}
}
func fetchClusterResourceLoad(clusterName string, client monitoring.Interface) (ClusterLoadRecord, error) {
clusterResourceLoad := ClusterLoadRecord{
AdapterId: config.ServerConf.AdapterId,
AdapterId: etc.ServerConf.AdapterId,
ClusterName: clusterName,
}
// cpu utilisation

View File

@ -19,12 +19,6 @@ import (
"strings"
)
const (
StatefulSet = "StatefulSet"
DaemonSet = "DaemonSet"
Deployment = "Deployment"
)
var promQLTemplates = map[string]string{
// cluster
"cluster_cpu_total": "sum(node:node_num_cpu:sum)",
@ -35,6 +29,7 @@ var promQLTemplates = map[string]string{
"cluster_disk_total": `sum(max(node_filesystem_size_bytes{device=~"/dev/.*", device!~"/dev/loop\\d+", job="node-exporter"}) by (device, instance))`,
// node
"node_cpu_utilisation": `topk(5,instance:node_cpu:ratio)`,
"node_memory_utilisation": `topk(5,(node_memory_MemTotal_bytes - node_memory_MemAvailable_bytes)/ node_memory_MemTotal_bytes)`,
//namespace
"namespace_cpu_usage": `round(namespace:container_cpu_usage_seconds_total:sum_rate{namespace!="", $1}, 0.001)`,
"namespace_memory_usage": `namespace:container_memory_usage_bytes:sum{namespace!="", $1}`,

View File

@ -45,13 +45,6 @@ type MetricData struct {
MetricValues `json:"result,omitempty" description:"metric data including labels, time series and values" csv:"metric_values"`
}
type DashboardEntity struct {
GrafanaDashboardUrl string `json:"grafanaDashboardUrl,omitempty"`
GrafanaDashboardContent string `json:"grafanaDashboardContent,omitempty"`
Description string `json:"description,omitempty"`
Namespace string `json:"namespace,omitempty"`
}
// The first element is the timestamp, the second is the metric value.
// eg, [1585658599.195, 0.528]
type Point [2]float64
@ -63,16 +56,6 @@ type MetricValue struct {
// To allow empty Sample, we should declare Sample to type *Point
Sample *Point `json:"value,omitempty" description:"time series, values of vector type"`
Series []Point `json:"values,omitempty" description:"time series, values of matrix type"`
ExportSample *ExportPoint `json:"exported_value,omitempty" description:"exported time series, values of vector type"`
ExportedSeries []ExportPoint `json:"exported_values,omitempty" description:"exported time series, values of matrix type"`
MinValue string `json:"min_value" description:"minimum value from monitor points"`
MaxValue string `json:"max_value" description:"maximum value from monitor points"`
AvgValue string `json:"avg_value" description:"average value from monitor points"`
SumValue string `json:"sum_value" description:"sum value from monitor points"`
Fee string `json:"fee" description:"resource fee"`
ResourceUnit string `json:"resource_unit"`
CurrencyUnit string `json:"currency_unit"`
}
func (p Point) Timestamp() float64 {

View File

@ -2,8 +2,14 @@ package v1
import (
"github.com/gin-gonic/gin"
promv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
"jcc-schedule/pkg/apiserver"
"jcc-schedule/pkg/monitoring"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/json"
"net/http"
"time"
)
@ -22,6 +28,34 @@ type ControllerMetricsResp struct {
Data interface{} `json:"data"`
}
func K8sUnstructured(dataString string, target interface{}) {
unstructuredList := unstructured.UnstructuredList{}
json.Unmarshal([]byte(dataString), &unstructuredList)
runtime.DefaultUnstructuredConverter.FromUnstructured(unstructuredList.UnstructuredContent(), target)
}
func RuleSelector(c *gin.Context) {
var p MetricsReq
if err := c.ShouldBind(&p); err != nil {
Response(c, http.StatusBadRequest, "invalid request params.", "")
return
}
if _, exists := apiserver.ApiServer.DynamicClientMap[p.ClusterName]; !exists {
Response(c, http.StatusInternalServerError, "prometheus client error", "")
}
unstructuredList, err := apiserver.ApiServer.DynamicClientMap[p.ClusterName].Resource(schema.GroupVersionResource{
Group: "monitoring.coreos.com",
Version: "v1", Resource: "prometheuses"}).
Namespace("").List(c, metav1.ListOptions{})
if err != nil {
Response(c, http.StatusInternalServerError, "success", nil)
}
var prometheusList promv1.PrometheusList
runtime.DefaultUnstructuredConverter.FromUnstructured(unstructuredList.UnstructuredContent(), &prometheusList)
Response(c, http.StatusOK, "success", prometheusList.Items[0])
}
func PodMetrics(c *gin.Context) {
var p MetricsReq
if err := c.ShouldBind(&p); err != nil {
@ -46,10 +80,24 @@ func ApiServerMetrics(c *gin.Context) {
Response(c, http.StatusInternalServerError, "prometheus client error", "")
}
var result []monitoring.Metric
result = apiserver.ApiServer.MonitoringClientMap[p.ClusterName].GetNamedMetrics(p.Metrics, time.Now(), monitoring.ApiServerOption{})
result = apiserver.ApiServer.MonitoringClientMap[p.ClusterName].GetNamedMetrics(p.Metrics, time.Now(), monitoring.ClusterOption{})
Response(c, http.StatusOK, "success", result)
}
//func ClusterLoadMetrics(c *gin.Context) {
// var p MetricsReq
// if err := c.ShouldBind(&p); err != nil {
// Response(c, http.StatusBadRequest, "invalid request params.", "")
// return
// }
// if _, exists := apiserver.ApiServer.MonitoringClientMap[p.ClusterName]; !exists {
// Response(c, http.StatusInternalServerError, "prometheus client error", "")
// }
// var result []monitoring.Metric
// result = apiserver.ApiServer.MonitoringClientMap[p.ClusterName].GetNamedMetrics(p.Metrics, time.Now(), monitoring.ClusterOption{})
// Response(c, http.StatusOK, "success", result)
//}
func NodeMetrics(c *gin.Context) {
var p MetricsReq
if err := c.ShouldBind(&p); err != nil {

115
routers/api/v1/operate.go Normal file
View File

@ -0,0 +1,115 @@
package v1
import (
"bytes"
"github.com/gin-gonic/gin"
"io"
"jcc-schedule/pkg/apiserver"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
syaml "k8s.io/apimachinery/pkg/runtime/serializer/yaml"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/restmapper"
"net/http"
sigyaml "sigs.k8s.io/yaml"
)
type OperateStruct struct {
ClusterName string `json:"clusterName"`
YamlString string `json:"yamlString"`
}
func Apply(ctx *gin.Context) {
var p OperateStruct
if err := ctx.BindJSON(&p); err != nil {
Response(ctx, http.StatusBadRequest, "invalid request params.", "")
return
}
if _, ok := apiserver.ApiServer.ClientSetMap[p.ClusterName]; !ok {
Response(ctx, http.StatusBadRequest, "clientSet nil.", "")
return
}
d := yaml.NewYAMLOrJSONDecoder(bytes.NewBufferString(p.YamlString), 4096)
var err error
for {
var rawObj runtime.RawExtension
err = d.Decode(&rawObj)
if err == io.EOF {
break
}
if err != nil {
}
obj := &unstructured.Unstructured{}
syaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme).Decode(rawObj.Raw, nil, obj)
if err != nil {
Response(ctx, http.StatusInternalServerError, "Decode err.", "")
return
}
unstructuredMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
if err != nil {
Response(ctx, http.StatusInternalServerError, "Unstructured err.", "")
return
}
unStructureObj := &unstructured.Unstructured{Object: unstructuredMap}
gvr, err := GetGVR(apiserver.ApiServer.ClientSetMap[p.ClusterName], unStructureObj.GroupVersionKind())
if err != nil {
Response(ctx, http.StatusInternalServerError, "Get GVR err.", "")
return
}
unstructuredYaml, err := sigyaml.Marshal(unStructureObj)
if err != nil {
Response(ctx, http.StatusInternalServerError, "Marshal err.", "")
return
}
if unStructureObj.GetNamespace() == "" {
unStructureObj.SetNamespace("default")
}
_, getErr := apiserver.ApiServer.DynamicClientMap[p.ClusterName].Resource(gvr).Namespace(unStructureObj.GetNamespace()).Get(ctx, unStructureObj.GetName(), metav1.GetOptions{})
if getErr != nil {
_, createErr := apiserver.ApiServer.DynamicClientMap[p.ClusterName].Resource(gvr).Namespace(unStructureObj.GetNamespace()).Create(ctx, unStructureObj, metav1.CreateOptions{})
if createErr != nil {
Response(ctx, http.StatusInternalServerError, "Create namespace err.", "")
return
}
}
force := true
_, err = apiserver.ApiServer.DynamicClientMap[p.ClusterName].Resource(gvr).
Namespace(unStructureObj.GetNamespace()).
Patch(ctx,
unStructureObj.GetName(),
types.ApplyPatchType,
unstructuredYaml, metav1.PatchOptions{
FieldManager: unStructureObj.GetName(),
Force: &force,
})
if err != nil {
Response(ctx, http.StatusInternalServerError, "Patch err.", "")
return
}
}
}
// GetGVR 获取GVR
func GetGVR(clientSet *kubernetes.Clientset, gvk schema.GroupVersionKind) (schema.GroupVersionResource, error) {
gr, err := restmapper.GetAPIGroupResources(clientSet)
if err != nil {
return schema.GroupVersionResource{}, err
}
mapper := restmapper.NewDiscoveryRESTMapper(gr)
mapping, err := mapper.RESTMapping(gvk.GroupKind(), gvk.Version)
if err != nil {
return schema.GroupVersionResource{}, err
}
return mapping.Resource, nil
}

View File

@ -50,6 +50,12 @@ func InitRouter() *gin.Engine {
monitoring.GET("/node", v1.NodeMetrics)
monitoring.GET("/apiServer", v1.ApiServerMetrics)
monitoring.GET("/pod", v1.ApiServerMetrics)
monitoring.GET("/rule/selector", v1.RuleSelector)
//monitoring.GET("/cluster/load", v1.ClusterLoadMetrics)
//operate
operate := apiv1.Group("operate")
operate.POST("/apply", v1.Apply)
}
return r