feat: switch base delivery to awx task logs
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/1024XEngineer/xinfra/server/internal/model"
|
||||
)
|
||||
@@ -57,6 +58,22 @@ type ContainerBusinessLineQuery struct {
|
||||
Namespaces []model.BusinessLineWayneNamespace
|
||||
}
|
||||
|
||||
type WayneDeploymentHistory struct {
|
||||
ID int64 `json:"id"`
|
||||
Type int `json:"type"`
|
||||
ResourceID int64 `json:"resourceId"`
|
||||
ResourceName string `json:"resourceName"`
|
||||
TemplateID int64 `json:"templateId"`
|
||||
Cluster string `json:"cluster"`
|
||||
Status int `json:"status"`
|
||||
Message string `json:"message"`
|
||||
User string `json:"user"`
|
||||
CreateTime string `json:"createTime"`
|
||||
CreatedAt time.Time `json:"-"`
|
||||
BusinessLineID uint64 `json:"businessLineId"`
|
||||
NamespaceID uint64 `json:"namespaceId"`
|
||||
}
|
||||
|
||||
func (s *WayneRoleBindingService) ContainerServiceData(ctx context.Context, query ContainerBusinessLineQuery) (*ContainerServiceData, error) {
|
||||
summary := ContainerServiceSummary{
|
||||
BusinessLineID: query.BusinessLineID,
|
||||
@@ -143,6 +160,65 @@ func (s *WayneRoleBindingService) ContainerServiceData(ctx context.Context, quer
|
||||
return &ContainerServiceData{Summary: summary, Workloads: workloads}, nil
|
||||
}
|
||||
|
||||
func (s *WayneRoleBindingService) ListDeploymentHistories(ctx context.Context, namespaces []model.BusinessLineWayneNamespace, limit int) ([]WayneDeploymentHistory, error) {
|
||||
if limit <= 0 {
|
||||
limit = 100
|
||||
}
|
||||
histories := make([]WayneDeploymentHistory, 0)
|
||||
seenDeployments := map[int64]struct{}{}
|
||||
for _, binding := range namespaces {
|
||||
apps, err := s.listWayneNamespaceApps(ctx, binding.WayneNamespaceID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, app := range apps {
|
||||
deployments, err := s.listWayneAppDeployments(ctx, app.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, deployment := range deployments {
|
||||
if deployment.ID == 0 {
|
||||
continue
|
||||
}
|
||||
if _, ok := seenDeployments[deployment.ID]; ok {
|
||||
continue
|
||||
}
|
||||
seenDeployments[deployment.ID] = struct{}{}
|
||||
items, err := s.listWaynePublishHistories(ctx, deployment.ID, 20)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, item := range items {
|
||||
item.BusinessLineID = binding.BusinessLineID
|
||||
item.NamespaceID = binding.WayneNamespaceID
|
||||
if item.ResourceName == "" {
|
||||
item.ResourceName = deployment.Name
|
||||
}
|
||||
histories = append(histories, item)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
sortWayneDeploymentHistories(histories)
|
||||
if len(histories) > limit {
|
||||
histories = histories[:limit]
|
||||
}
|
||||
return histories, nil
|
||||
}
|
||||
|
||||
func (s *WayneRoleBindingService) GetDeploymentHistory(ctx context.Context, namespaces []model.BusinessLineWayneNamespace, resourceID, historyID int64) (*WayneDeploymentHistory, error) {
|
||||
histories, err := s.ListDeploymentHistories(ctx, namespaces, 500)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, history := range histories {
|
||||
if history.ResourceID == resourceID && history.ID == historyID {
|
||||
return &history, nil
|
||||
}
|
||||
}
|
||||
return nil, fmt.Errorf("wayne deployment history %d was not found", historyID)
|
||||
}
|
||||
|
||||
func (s *WayneRoleBindingService) getWayneNamespace(ctx context.Context, id uint64) (wayneNamespaceDetail, error) {
|
||||
result, err := s.callRaw(ctx, http.MethodGet, fmt.Sprintf("/api/v1/namespaces/%d", id), nil)
|
||||
if err != nil {
|
||||
@@ -162,6 +238,32 @@ func (s *WayneRoleBindingService) listWayneNamespaceApps(ctx context.Context, na
|
||||
return parseWayneApps(result.Body)
|
||||
}
|
||||
|
||||
func (s *WayneRoleBindingService) listWayneAppDeployments(ctx context.Context, appID uint64) ([]wayneDeployment, error) {
|
||||
values := url.Values{}
|
||||
values.Set("pageNo", "1")
|
||||
values.Set("pageSize", "500")
|
||||
values.Set("deleted", "false")
|
||||
result, err := s.callRaw(ctx, http.MethodGet, fmt.Sprintf("/api/v1/apps/%d/deployments?%s", appID, values.Encode()), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return parseWayneDeployments(result.Body)
|
||||
}
|
||||
|
||||
func (s *WayneRoleBindingService) listWaynePublishHistories(ctx context.Context, resourceID int64, pageSize int) ([]WayneDeploymentHistory, error) {
|
||||
values := url.Values{}
|
||||
values.Set("pageNo", "1")
|
||||
values.Set("pageSize", strconv.Itoa(pageSize))
|
||||
values.Set("type", "0")
|
||||
values.Set("resourceId", strconv.FormatInt(resourceID, 10))
|
||||
values.Set("sortby", "-createTime")
|
||||
result, err := s.callRaw(ctx, http.MethodGet, "/api/v1/publish/histories?"+values.Encode(), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return parseWaynePublishHistories(result.Body)
|
||||
}
|
||||
|
||||
func (s *WayneRoleBindingService) getWayneNodes(ctx context.Context, cluster string) (wayneNodeSummary, error) {
|
||||
result, err := s.callRaw(ctx, http.MethodGet, fmt.Sprintf("/api/v1/kubernetes/nodes/clusters/%s", url.PathEscape(cluster)), nil)
|
||||
if err != nil {
|
||||
@@ -216,6 +318,11 @@ type wayneApp struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type wayneDeployment struct {
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type wayneNodeSummary struct {
|
||||
Total int
|
||||
Ready int
|
||||
@@ -248,6 +355,33 @@ func parseWayneApps(body []byte) ([]wayneApp, error) {
|
||||
return wrapped.Data.List, nil
|
||||
}
|
||||
|
||||
func parseWayneDeployments(body []byte) ([]wayneDeployment, error) {
|
||||
var wrapped struct {
|
||||
Data struct {
|
||||
List []wayneDeployment `json:"list"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &wrapped); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return wrapped.Data.List, nil
|
||||
}
|
||||
|
||||
func parseWaynePublishHistories(body []byte) ([]WayneDeploymentHistory, error) {
|
||||
var wrapped struct {
|
||||
Data struct {
|
||||
List []WayneDeploymentHistory `json:"list"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &wrapped); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for i := range wrapped.Data.List {
|
||||
wrapped.Data.List[i].CreatedAt = parseWayneTime(wrapped.Data.List[i].CreateTime)
|
||||
}
|
||||
return wrapped.Data.List, nil
|
||||
}
|
||||
|
||||
func parseWayneMapList(body []byte) ([]map[string]any, error) {
|
||||
var wrapped struct {
|
||||
Data struct {
|
||||
@@ -260,6 +394,32 @@ func parseWayneMapList(body []byte) ([]map[string]any, error) {
|
||||
return wrapped.Data.List, nil
|
||||
}
|
||||
|
||||
func parseWayneTime(raw string) time.Time {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
return time.Time{}
|
||||
}
|
||||
layouts := []string{time.RFC3339, "2006-01-02T15:04:05Z07:00", "2006-01-02 15:04:05", "2006-01-02T15:04:05"}
|
||||
for _, layout := range layouts {
|
||||
if parsed, err := time.Parse(layout, raw); err == nil {
|
||||
return parsed
|
||||
}
|
||||
}
|
||||
return time.Time{}
|
||||
}
|
||||
|
||||
func sortWayneDeploymentHistories(items []WayneDeploymentHistory) {
|
||||
for i := 1; i < len(items); i++ {
|
||||
item := items[i]
|
||||
j := i - 1
|
||||
for j >= 0 && items[j].CreatedAt.Before(item.CreatedAt) {
|
||||
items[j+1] = items[j]
|
||||
j--
|
||||
}
|
||||
items[j+1] = item
|
||||
}
|
||||
}
|
||||
|
||||
func parseWayneNodeSummary(body []byte) (wayneNodeSummary, error) {
|
||||
var wrapped struct {
|
||||
Data struct {
|
||||
|
||||
Reference in New Issue
Block a user