feat(api): add health check endpoint for rbd-api (#1500)

* ci: add rbd-shell packaging (#1471)

* feat(api): add health check endpoint for rbd-api

Co-authored-by: Guox <gx_927@163.com>
This commit is contained in:
丁鹏 2023-01-04 10:21:10 +08:00 committed by GitHub
parent 6652735086
commit 948663534f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 16 deletions

View File

@ -55,7 +55,7 @@ import (
"github.com/sirupsen/logrus"
)
//Manager apiserver
// Manager apiserver
type Manager struct {
ctx context.Context
cancel context.CancelFunc
@ -67,7 +67,7 @@ type Manager struct {
exporter *metric.Exporter
}
//NewManager newManager
// NewManager newManager
func NewManager(c option.Config, etcdcli *clientv3.Client) *Manager {
ctx, cancel := context.WithCancel(context.Background())
manager := &Manager{
@ -83,7 +83,7 @@ func NewManager(c option.Config, etcdcli *clientv3.Client) *Manager {
return manager
}
//SetMiddleware set api meddleware
// SetMiddleware set api meddleware
func (m *Manager) SetMiddleware() {
c := m.conf
r := m.r
@ -117,14 +117,14 @@ func (m *Manager) SetMiddleware() {
r.Use(apimiddleware.Proxy)
}
//Start manager
// Start manager
func (m *Manager) Start() error {
go m.Do()
logrus.Info("start api router success.")
return nil
}
//Do do
// Do do
func (m *Manager) Do() {
for {
select {
@ -136,14 +136,14 @@ func (m *Manager) Do() {
}
}
//Stop manager
// Stop manager
func (m *Manager) Stop() error {
logrus.Info("api router is stopped.")
m.cancel()
return nil
}
//Run run
// Run run
func (m *Manager) Run() {
v2R := &version2.V2{
Cfg: &m.conf,
@ -182,6 +182,8 @@ func (m *Manager) Run() {
logrus.Fatal(http.ListenAndServe(m.conf.WebsocketAddr, websocketRouter))
}
}()
// api ssl
if m.conf.APISSL {
go func() {
pool := x509.NewCertPool()
@ -203,11 +205,24 @@ func (m *Manager) Run() {
logrus.Fatal(s.ListenAndServeTLS(m.conf.APICertFile, m.conf.APIKeyFile))
}()
}
// health check
go func() {
healthzRouter := chi.NewRouter()
healthzRouter.Get("/healthz", func(res http.ResponseWriter, req *http.Request) {
res.Write([]byte("ok"))
res.WriteHeader(http.StatusOK)
})
logrus.Infof("health check listen on (HTTP) %s", m.conf.APIHealthzAddr)
logrus.Fatal(http.ListenAndServe(m.conf.APIHealthzAddr, healthzRouter))
}()
// api
logrus.Infof("api listen on (HTTP) %s", m.conf.APIAddr)
logrus.Fatal(http.ListenAndServe(m.conf.APIAddr, m.r))
}
//EventLogInstance 查询event server instance
// EventLogInstance 查询event server instance
func (m *Manager) EventLogInstance(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithCancel(m.ctx)
defer cancel()
@ -231,7 +246,7 @@ func (m *Manager) EventLogInstance(w http.ResponseWriter, r *http.Request) {
return
}
//PrometheusAPI prometheus api 代理
// PrometheusAPI prometheus api 代理
func (m *Manager) PrometheusAPI(w http.ResponseWriter, r *http.Request) {
handler.GetPrometheusProxy().Proxy(w, r)
}
@ -241,7 +256,7 @@ func (m *Manager) KuberntesDashboardAPI(w http.ResponseWriter, r *http.Request)
handler.GetKubernetesDashboardProxy().Proxy(w, r)
}
//Metric prometheus metric
// Metric prometheus metric
func (m *Manager) Metric() {
prometheus.MustRegister(version.NewCollector("rbd_api"))
exporter := metric.NewExporter()
@ -250,7 +265,7 @@ func (m *Manager) Metric() {
m.r.Handle("/metrics", promhttp.Handler())
}
//RequestMetric request metric midd
// RequestMetric request metric midd
func (m *Manager) RequestMetric(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor)

View File

@ -25,10 +25,11 @@ import (
"github.com/spf13/pflag"
)
//Config config
// Config config
type Config struct {
DBType string
APIAddr string
APIHealthzAddr string
APIAddrSSL string
DBConnectionInfo string
EventLogServers []string
@ -65,24 +66,25 @@ type Config struct {
GrctlImage string
}
//APIServer apiserver server
// APIServer apiserver server
type APIServer struct {
Config
LogLevel string
StartRegionAPI bool
}
//NewAPIServer new server
// NewAPIServer new server
func NewAPIServer() *APIServer {
return &APIServer{}
}
//AddFlags config
// AddFlags config
func (a *APIServer) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&a.LogLevel, "log-level", "info", "the api log level")
fs.StringVar(&a.DBType, "db-type", "mysql", "db type mysql or etcd")
fs.StringVar(&a.DBConnectionInfo, "mysql", "admin:admin@tcp(127.0.0.1:3306)/region", "mysql db connection info")
fs.StringVar(&a.APIAddr, "api-addr", "127.0.0.1:8888", "the api server listen address")
fs.StringVar(&a.APIHealthzAddr, "api-healthz-addr", "0.0.0.0:8889", "the api server health check listen address")
fs.StringVar(&a.APIAddrSSL, "api-addr-ssl", "0.0.0.0:8443", "the api server listen address")
fs.StringVar(&a.WebsocketAddr, "ws-addr", "0.0.0.0:6060", "the websocket server listen address")
fs.BoolVar(&a.APISSL, "api-ssl-enable", false, "whether to enable websocket SSL")
@ -119,7 +121,7 @@ func (a *APIServer) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&a.GrctlImage, "shell-image", "registry.cn-hangzhou.aliyuncs.com/goodrain/rbd-shell:v5.10.0-release", "use shell image")
}
//SetLog 设置log
// SetLog 设置log
func (a *APIServer) SetLog() {
level, err := logrus.ParseLevel(a.LogLevel)
if err != nil {