Skip to content

Commit

Permalink
* fix add status list api
Browse files Browse the repository at this point in the history
  • Loading branch information
qishiyao committed Sep 8, 2022
1 parent a5fe7ec commit b9dbbdd
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 20 deletions.
9 changes: 9 additions & 0 deletions backend/internal/app/model/app_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
72 changes: 57 additions & 15 deletions backend/internal/app/router/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package router

import (
"fmt"
"gitlab.zcorp.cc/pangu/cne-api/pkg/logging"
"net/http"
"sync"

Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions backend/internal/app/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 0 additions & 4 deletions backend/internal/app/service/app/instance/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion backend/pkg/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit b9dbbdd

Please sign in to comment.