This repository is currently being migrated. It's locked while the migration is in progress.
/
node.go
144 lines (119 loc) · 3.63 KB
/
node.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package formatter
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/docker/go-units"
"github.com/storageos/go-api/types"
)
const (
defaultNodeQuietFormat = "{{.Name}}"
defaultNodeTableFormat = "table {{.Name}}\t{{.Address}}\t{{.Health}}\t{{.Scheduler}}\t{{.Volumes}}\t{{.Capacity}}\t{{.CapacityUsed}}\t{{.Version}}\t{{.Labels}}"
nodeNameHeader = "NAME"
nodeAddressHeader = "ADDRESS"
nodeHealthHeader = "HEALTH"
nodeSchedulerHeader = "SCHEDULER"
nodeVolumesHeader = "VOLUMES"
nodeTotalCapacityHeader = "TOTAL"
nodeCapacityUsedHeader = "USED"
nodeVersionUsedHeader = "VERSION"
nodeLabelHeader = "LABEL"
)
// NewNodeFormat returns a format for use with a node Context
func NewNodeFormat(source string, quiet bool) Format {
switch source {
case TableFormatKey:
if quiet {
return defaultNodeQuietFormat
}
return defaultNodeTableFormat
case RawFormatKey:
if quiet {
return `name: {{.Name}}`
}
return `name: {{.Name}}\naddress: {{.Address}}\nhealth: {{.Health}}\nscheduler: {{.Scheduler}}\nvolumes: {{.Volumes}}\ncapacity: {{.Capacity}}\ncapacityUsed: {{.CapacityUsed}}\nversion: {{.Version}}\nlabels: {{.Labels}}\n`
}
return Format(source)
}
// NodeWrite writes formatted nodes using the Context
func NodeWrite(ctx Context, nodes []*types.Controller) error {
render := func(format func(subContext subContext) error) error {
for _, node := range nodes {
if err := format(&nodeContext{v: *node}); err != nil {
return err
}
}
return nil
}
return ctx.Write(&nodeContext{}, render)
}
type nodeContext struct {
HeaderContext
v types.Controller
}
func (c *nodeContext) MarshalJSON() ([]byte, error) {
return marshalJSON(c)
}
func (c *nodeContext) Name() string {
c.AddHeader(nodeNameHeader)
return c.v.Name
}
func (c *nodeContext) Address() string {
c.AddHeader(nodeAddressHeader)
return c.v.Address
}
func (c *nodeContext) Health() string {
c.AddHeader(nodeHealthHeader)
if c.v.HealthUpdatedAt.IsZero() {
return strings.Title(c.v.Health)
}
return fmt.Sprintf("%s %s", strings.Title(c.v.Health), units.HumanDuration(time.Since(c.v.HealthUpdatedAt)))
}
func (c *nodeContext) Scheduler() string {
c.AddHeader(nodeSchedulerHeader)
return strconv.FormatBool(c.v.Scheduler)
}
func (c *nodeContext) Volumes() string {
c.AddHeader(nodeVolumesHeader)
return fmt.Sprintf("M: %d, R: %d", c.v.VolumeStats.MasterVolumeCount, c.v.VolumeStats.ReplicaVolumeCount)
}
func (c *nodeContext) Capacity() string {
c.AddHeader(nodeTotalCapacityHeader)
if c.v.CapacityStats.TotalCapacityBytes == 0 {
return "-"
}
return units.BytesSize(float64(c.v.CapacityStats.TotalCapacityBytes))
}
func (c *nodeContext) CapacityUsed() string {
c.AddHeader(nodeCapacityUsedHeader)
if c.v.CapacityStats.TotalCapacityBytes == 0 {
return "-"
}
return fmt.Sprintf("%.2f%%", float64(c.v.CapacityStats.TotalCapacityBytes-c.v.CapacityStats.AvailableCapacityBytes)*100/float64(c.v.CapacityStats.TotalCapacityBytes))
}
func (c *nodeContext) Version() string {
c.AddHeader(nodeVersionUsedHeader)
return fmt.Sprintf("%s (%s rev)", c.v.VersionInfo["storageos"].Version, c.v.VersionInfo["storageos"].Revision)
}
func (c *nodeContext) Labels() string {
c.AddHeader(labelsHeader)
if c.v.Labels == nil {
return ""
}
var joinLabels []string
for k, v := range c.v.Labels {
joinLabels = append(joinLabels, fmt.Sprintf("%s=%s", k, v))
}
return strings.Join(joinLabels, ",")
}
func (c *nodeContext) Label(name string) string {
n := strings.Split(name, ".")
r := strings.NewReplacer("-", " ", "_", " ")
h := r.Replace(n[len(n)-1])
c.AddHeader(h)
if c.v.Labels == nil {
return ""
}
return c.v.Labels[name]
}