-
Notifications
You must be signed in to change notification settings - Fork 43
/
shadow.go
79 lines (64 loc) · 1.69 KB
/
shadow.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
package models
import (
"encoding/json"
"time"
"github.com/baetyl/baetyl-go/v2/spec/v1"
"github.com/baetyl/baetyl-cloud/v2/common"
)
type Shadow struct {
Namespace string `json:"namespace,omitempty"`
Name string `json:"name,omitempty"`
Report v1.Report `json:"report,omitempty"`
Desire v1.Desire `json:"desire,omitempty"`
CreationTimestamp time.Time `json:"createTime,omitempty"`
}
// NodeViewList node view list
type ShadowList struct {
Total int `json:"total"`
ListOptions *ListOptions `json:"listOptions"`
Items []Shadow `json:"items"`
}
func NewShadow(namespace, name string) *Shadow {
return &Shadow{
Name: name,
Namespace: namespace,
Report: BuildEmptyApps(),
Desire: BuildEmptyApps(),
}
}
func NewShadowFromNode(node *v1.Node) *Shadow {
shadow := &Shadow{
Name: node.Name,
Namespace: node.Namespace,
Report: node.Report,
Desire: node.Desire,
CreationTimestamp: node.CreationTimestamp.UTC(),
}
if node.Desire == nil {
shadow.Desire = BuildEmptyApps()
}
if node.Report == nil {
shadow.Report = BuildEmptyApps()
}
return shadow
}
func (s *Shadow) GetDesireString() (string, error) {
desire, err := json.Marshal(s.Desire)
if err != nil {
return "", err
}
return string(desire), nil
}
func (s *Shadow) GetReportString() (string, error) {
report, err := json.Marshal(s.Report)
if err != nil {
return "", err
}
return string(report), nil
}
func BuildEmptyApps() map[string]interface{} {
return map[string]interface{}{
common.DesiredApplications: make([]v1.AppInfo, 0),
common.DesiredSysApplications: make([]v1.AppInfo, 0),
}
}