From b9dbbddda952c24594d4a3f0b20bd5b41168e1f8 Mon Sep 17 00:00:00 2001 From: qishiyao Date: Thu, 8 Sep 2022 18:13:42 +0800 Subject: [PATCH] * fix add status list api --- backend/internal/app/model/app_response.go | 9 +++ backend/internal/app/router/app.go | 72 +++++++++++++++---- backend/internal/app/router/router.go | 1 + .../app/service/app/instance/instance.go | 4 -- backend/pkg/logging/logging.go | 2 +- 5 files changed, 68 insertions(+), 20 deletions(-) diff --git a/backend/internal/app/model/app_response.go b/backend/internal/app/model/app_response.go index ac8189a7..4973103a 100644 --- a/backend/internal/app/model/app_response.go +++ b/backend/internal/app/model/app_response.go @@ -27,6 +27,15 @@ type AppRespStatusComponent struct { Age int64 `json:"age"` } +type NamespacedAppRespStatus struct { + Name string `json:"name"` + Namespace string `json:"namespace"` + Status string `json:"status"` + Version string `json:"version"` + Age int64 `json:"age"` + AccessHost string `json:"access_host"` +} + type NamespaceAppMetric struct { Namespace string `json:"namespace"` Name string `json:"name"` diff --git a/backend/internal/app/router/app.go b/backend/internal/app/router/app.go index ca588c6c..92bd503f 100644 --- a/backend/internal/app/router/app.go +++ b/backend/internal/app/router/app.go @@ -6,7 +6,6 @@ package router import ( "fmt" - "gitlab.zcorp.cc/pangu/cne-api/pkg/logging" "net/http" "sync" @@ -238,30 +237,18 @@ func AppStatus(c *gin.Context) { query model.AppModel ) - tmpLogger := logging.DefaultLogger() - tmpLogger.Infof("start status api") - - tmpLogger.Infof("start lookup app") ctx, i, code, err := LookupApp(c, &query) if err != nil { renderError(c, code, err) return } - tmpLogger.Infof("end lookup app") - tmpLogger.Infof("start get logger") logger := i.GetLogger() - logger.Infof("end get logger") - - logger.Infof("start parse status") + logger.Debug("start parse status") data := i.ParseStatus() - logger.Infof("end parse status") - - logger.Debug("parse status success") /* parse App Uri */ - logger.Infof("start parse access host") data.AccessHost = "" ingressHosts := i.ListIngressHosts() if len(ingressHosts) > 0 { @@ -276,11 +263,66 @@ func AppStatus(c *gin.Context) { } } } - logger.Infof("end parse access host") renderJson(c, http.StatusOK, data) } +func AppListStatus(c *gin.Context) { + var ( + ctx = c.Request.Context() + wg sync.WaitGroup + err error + body model.AppListModel + ) + + if err = c.ShouldBindJSON(&body); err != nil { + renderError(c, http.StatusBadRequest, err) + return + } + + logger := getLogger(ctx) + statusList := make([]model.NamespacedAppRespStatus, len(body.Apps)) + + for id, a := range body.Apps { + wg.Add(1) + go func(index int, na model.NamespacedApp) { + defer wg.Done() + + statusList[index].Namespace = na.Namespace + statusList[index].Name = na.Name + + i, e := service.Apps(ctx, body.Cluster, na.Namespace).GetApp(na.Name) + if e != nil { + logger.WithError(e).WithFields(logrus.Fields{"name": na.Name, "namespace": na.Namespace}).Error(errGetAppFailed) + return + } + + status := i.ParseStatus() + statusList[index].Status = status.Status + statusList[index].Age = status.Age + statusList[index].Version = status.Version + + ingressHosts := i.ListIngressHosts() + if len(ingressHosts) > 0 { + statusList[index].AccessHost = ingressHosts[0] + } else { + nodePort := i.ParseNodePort() + if nodePort > 0 { + nodePortIPS := service.Nodes(ctx, body.Cluster).ListNodePortIPS() + if len(nodePortIPS) != 0 { + accessHost := fmt.Sprintf("%s:%d", nodePortIPS[0], nodePort) + statusList[index].AccessHost = accessHost + } + } + } + + }(id, a) + } + wg.Wait() + + renderJson(c, http.StatusOK, statusList) +} + func AppSimpleSettings(c *gin.Context) { var ( query model.AppSettingMode diff --git a/backend/internal/app/router/router.go b/backend/internal/app/router/router.go index 4407f422..9bfbac92 100644 --- a/backend/internal/app/router/router.go +++ b/backend/internal/app/router/router.go @@ -54,6 +54,7 @@ func Config(r *gin.Engine) { api.POST("/app/settings", AppPatchSettings) api.GET("/app/settings/simple", AppSimpleSettings) api.GET("/app/status", AppStatus) + api.POST("/app/status/multi", AppListStatus) api.GET("/app/metric", AppMetric) api.GET("/app/pvc", AppPvcList) api.GET("/app/account", AppAccountInfo) diff --git a/backend/internal/app/service/app/instance/instance.go b/backend/internal/app/service/app/instance/instance.go index 2f1237dc..48680ac8 100644 --- a/backend/internal/app/service/app/instance/instance.go +++ b/backend/internal/app/service/app/instance/instance.go @@ -44,12 +44,10 @@ func NewInstance(ctx context.Context, name string, clusterName, namespace string }), } - i.logger.Debugf("start fetch release") i.release = i.fetchRelease() if i.release == nil { return nil, ErrAppNotFound } - i.logger.Debugf("start prepare") err := i.prepare() return i, err } @@ -73,12 +71,10 @@ func (i *Instance) fetchRelease() *release.Release { namespace: i.namespace, store: i.Ks.Store, } - i.logger.Debug("start fetch release secrets") rel, err := getter.Last(i.name) if err != nil { i.logger.WithError(err).Error("parse release failed") } - i.logger.Debug("end fetch release secrets") return rel } diff --git a/backend/pkg/logging/logging.go b/backend/pkg/logging/logging.go index 7d002b97..b9c62a2e 100644 --- a/backend/pkg/logging/logging.go +++ b/backend/pkg/logging/logging.go @@ -32,7 +32,7 @@ func NewLogger() *logrus.Logger { logger.Formatter = &logrus.TextFormatter{ DisableColors: true, ForceQuote: true, - TimestampFormat: "2006-01-02T15:04:05.999999999Z07:00", + TimestampFormat: "2006-01-02T15:04:05.999Z07:00", FullTimestamp: true, QuoteEmptyFields: true, CallerPrettyfier: func(f *runtime.Frame) (string, string) {