You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

applistlogic.go 4.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package apps
  2. import (
  3. "context"
  4. "gitlink.org.cn/JointCloud/pcm-kubernetes/kubernetes"
  5. "gorm.io/datatypes"
  6. "gorm.io/gorm"
  7. "time"
  8. "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc"
  9. "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types"
  10. "github.com/zeromicro/go-zero/core/logx"
  11. )
  12. type AppListLogic struct {
  13. logx.Logger
  14. ctx context.Context
  15. svcCtx *svc.ServiceContext
  16. }
  17. func NewAppListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AppListLogic {
  18. return &AppListLogic{
  19. Logger: logx.WithContext(ctx),
  20. ctx: ctx,
  21. svcCtx: svcCtx,
  22. }
  23. }
  24. type Task struct {
  25. Id int64 `db:"id"` // id
  26. Name string `db:"name"` // 作业名称
  27. Description string `db:"description"` // 作业描述
  28. Status string `db:"status"` // 作业状态
  29. Strategy int64 `db:"strategy"` // 策略
  30. SynergyStatus int64 `db:"synergy_status"` // 协同状态(0-未协同、1-已协同)
  31. CommitTime time.Time `db:"commit_time"` // 提交时间
  32. StartTime string `db:"start_time"` // 开始时间
  33. EndTime string `db:"end_time"` // 结束运行时间
  34. RunningTime int64 `db:"running_time"` // 已运行时间(单位秒)
  35. YamlString datatypes.JSON `db:"yaml_string"`
  36. Result string `db:"result"` // 作业结果
  37. DeletedAt gorm.DeletedAt `gorm:"index"`
  38. NsID string `db:"ns_id"`
  39. PName string `db:"p_name"` // p端名称
  40. PId int64 `db:"p_id"` // p端id
  41. }
  42. func (l *AppListLogic) AppList(req *types.AppListReq) (resp *types.AppListResp, err error) {
  43. var tasks []Task
  44. resp = &types.AppListResp{}
  45. l.svcCtx.DbEngin.Raw("select * from task t where t.`ns_id` = ? AND t.`deleted_at` IS NULL ORDER BY t.created_time Desc", req.NsID).Scan(&tasks)
  46. for _, task := range tasks {
  47. //调用p端接口查询应用状态 running、creating、waiting、error、pause
  48. data, err := l.svcCtx.K8sRpc.GetAppByAppName(context.Background(), &kubernetes.DeploymentDetailReq{
  49. Namespace: req.NsID,
  50. Name: task.Name,
  51. })
  52. if err != nil {
  53. logx.Errorf("调用p端接口查询应用失败,err:%v", err)
  54. return resp, err
  55. }
  56. minReplicas := ""
  57. maxReplicas := ""
  58. status := "creating"
  59. if data.Data.Deployment != nil {
  60. app := data.Data.Deployment
  61. maxReplicas = app.Metadata.Annotations["deploy.cloud.sealos.io/maxReplicas"]
  62. minReplicas = app.Metadata.Annotations["deploy.cloud.sealos.io/minReplicas"]
  63. if app.Status != nil {
  64. if app.Status.Replicas == nil && app.Status.AvailableReplicas == nil {
  65. status = "pause"
  66. } else if app.Status.Replicas != nil && app.Status.AvailableReplicas == nil {
  67. status = "creating"
  68. } else if *app.Status.Replicas == *app.Status.AvailableReplicas {
  69. status = "running"
  70. }
  71. }
  72. } else if data.Data.StatefulSet != nil {
  73. app := data.Data.StatefulSet
  74. maxReplicas = app.Metadata.Annotations["deploy.cloud.sealos.io/maxReplicas"]
  75. minReplicas = app.Metadata.Annotations["deploy.cloud.sealos.io/minReplicas"]
  76. if app.Status != nil {
  77. replicas := app.Status.Replicas
  78. availableReplicas := app.Status.AvailableReplicas
  79. if *replicas == 0 && *availableReplicas == 0 {
  80. status = "pause"
  81. } else if *replicas == *availableReplicas {
  82. status = "running"
  83. } else if *replicas > *availableReplicas {
  84. status = "creating"
  85. }
  86. }
  87. }
  88. var details []types.AppLocation
  89. sql :=
  90. `select phy.id as participant_id, phy.name as participant_name, c.kind
  91. from cloud c
  92. join sc_participant_phy_info phy on c.participant_id = phy.id
  93. WHERE c.kind in ('Deployment', 'StatefulSet')
  94. and task_id = ?`
  95. l.svcCtx.DbEngin.Raw(sql, task.Id).Scan(&details)
  96. resp.Apps = append(resp.Apps, types.App{
  97. Id: task.Id,
  98. Name: task.Name,
  99. Status: status,
  100. CreateTime: task.CommitTime.Format("2006-01-02 15:04:05"),
  101. MinReplicas: minReplicas,
  102. MaxReplicas: maxReplicas,
  103. AppLocations: details,
  104. })
  105. }
  106. return
  107. }

PCM is positioned as Software stack over Cloud, aiming to build the standards and ecology of heterogeneous cloud collaboration for JCC in a non intrusive and autonomous peer-to-peer manner.