/
render.go
129 lines (114 loc) · 3.59 KB
/
render.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright (c) 2021 Terminus, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package workloadStatus
import (
"context"
"encoding/json"
"fmt"
"github.com/pkg/errors"
"github.com/erda-project/erda-infra/base/servicehub"
"github.com/erda-project/erda-infra/providers/component-protocol/cptype"
cputil2 "github.com/erda-project/erda-infra/providers/component-protocol/utils/cputil"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/modules/cmp"
"github.com/erda-project/erda/modules/cmp/component-protocol/cputil"
"github.com/erda-project/erda/modules/openapi/component-protocol/components/base"
)
func init() {
base.InitProviderWithCreator("cmp-dashboard-workload-detail", "workloadStatus", func() servicehub.Provider {
return &ComponentWorkloadStatus{}
})
}
var steveServer cmp.SteveServer
func (s *ComponentWorkloadStatus) Init(ctx servicehub.Context) error {
server, ok := ctx.Service("cmp").(cmp.SteveServer)
if !ok {
return errors.New("failed to init component, cmp service in ctx is not a steveServer")
}
steveServer = server
return s.DefaultProvider.Init(ctx)
}
func (s *ComponentWorkloadStatus) Render(ctx context.Context, component *cptype.Component, _ cptype.Scenario,
event cptype.ComponentEvent, _ *cptype.GlobalStateData) error {
s.InitComponent(ctx)
if err := s.GenComponentState(component); err != nil {
return fmt.Errorf("failed to gen workloadStatue component state, %v", err)
}
if err := s.SetComponentValue(); err != nil {
return fmt.Errorf("failed to set workoadStatus component value, %v", err)
}
s.Transfer(component)
return nil
}
func (s *ComponentWorkloadStatus) InitComponent(ctx context.Context) {
sdk := cputil2.SDK(ctx)
s.sdk = sdk
s.ctx = ctx
s.server = steveServer
}
func (s *ComponentWorkloadStatus) GenComponentState(component *cptype.Component) error {
if component == nil || component.State == nil {
return nil
}
var state State
jsonData, err := json.Marshal(component.State)
if err != nil {
return err
}
if err = json.Unmarshal(jsonData, &state); err != nil {
return err
}
s.State = state
return nil
}
func (s *ComponentWorkloadStatus) SetComponentValue() error {
kind, namespace, name, err := cputil.ParseWorkloadID(s.State.WorkloadID)
if err != nil {
return err
}
userID := s.sdk.Identity.UserID
orgID := s.sdk.Identity.OrgID
req := apistructs.SteveRequest{
UserID: userID,
OrgID: orgID,
Type: kind,
ClusterName: s.State.ClusterName,
Name: name,
Namespace: namespace,
}
resp, err := s.server.GetSteveResource(s.ctx, &req)
if err != nil {
return err
}
obj := resp.Data()
status, color, err := cputil.ParseWorkloadStatus(obj)
if err != nil {
return err
}
s.Data.Labels.Label = s.sdk.I18n(status)
s.Data.Labels.Color = color
s.Props.Size = "default"
s.Props.RequestIgnore = []string{"data"}
return nil
}
func (s *ComponentWorkloadStatus) Transfer(c *cptype.Component) {
c.Props = s.Props
c.Data = map[string]interface{}{
"labels": s.Data.Labels,
}
c.State = map[string]interface{}{
"clusterName": s.State.ClusterName,
"workloadId": s.State.WorkloadID,
}
}