Skip to content

Commit

Permalink
Added uptime to Node information
Browse files Browse the repository at this point in the history
  • Loading branch information
ernoaapa committed Mar 5, 2018
1 parent 7e23dc0 commit 058a2f4
Show file tree
Hide file tree
Showing 16 changed files with 317 additions and 40 deletions.
1 change: 1 addition & 0 deletions pkg/api/mapping/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
// MapInfoToAPIModel maps internal node info model to API model
func MapInfoToAPIModel(info *model.NodeInfo) *node.Info {
return &node.Info{
Uptime: info.Uptime,
Labels: mapLabelsToAPIModel(info.Labels),
Hostname: info.Hostname,
Addresses: addressesToString(info.Addresses),
Expand Down
72 changes: 41 additions & 31 deletions pkg/api/services/node/v1/node.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pkg/api/services/node/v1/node.proto
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ message Info {

// Filesystem infos
repeated Filesystem filesystems = 11;

// Seconds since node boot up
uint64 uptime = 12;
}

message Label {
Expand Down
3 changes: 3 additions & 0 deletions pkg/model/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ type NodeInfo struct {

// Filesystems
Filesystems []Filesystem

// Seconds since node boot up
Uptime uint64
}

// NodeState describes current state of the node
Expand Down
7 changes: 3 additions & 4 deletions pkg/node/resolver_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,17 @@ func (r *Resolver) GetInfo() *model.NodeInfo {

return &model.NodeInfo{
Version: r.version,
Uptime: 0,
Labels: r.labels,
Arch: runtime.GOARCH,
OS: runtime.GOOS,
Hostname: hostname,
Addresses: getAddresses(),
GrpcPort: r.grpcPort,

MachineID: parseFieldFromIoregOutput(ioregOutput, "IOPlatformSerialNumber"),

MachineID: parseFieldFromIoregOutput(ioregOutput, "IOPlatformSerialNumber"),
SystemUUID: parseFieldFromIoregOutput(ioregOutput, "IOPlatformUUID"),

BootID: runCommandOrFail("/usr/bin/uuidgen"),
BootID: runCommandOrFail("/usr/bin/uuidgen"),

Filesystems: resolveFilesystems(),
}
Expand Down
1 change: 0 additions & 1 deletion pkg/node/resolver_darwin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,4 @@ func TestGetInfo(t *testing.T) {
assert.NotEmpty(t, info.SystemUUID, "should resolve SystemUUID")
assert.Equal(t, labels, info.Labels, "should have given node labels")
assert.Equal(t, 5000, info.GrpcPort, "should have given node grpc port")
assert.True(t, len(info.Filesystems) > 0, "should have at least one disk")
}
6 changes: 4 additions & 2 deletions pkg/node/resolver_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func (r *Resolver) GetInfo() *model.NodeInfo {
hostname, _ := os.Hostname()
return &model.NodeInfo{
Version: r.version,
Uptime: resolveUptime(),
Labels: r.labels,
Arch: runtime.GOARCH,
OS: runtime.GOOS,
Expand Down Expand Up @@ -60,10 +61,11 @@ func resolveUptime() uint64 {
sysinfo := syscall.Sysinfo_t{}

if err := syscall.Sysinfo(&sysinfo); err != nil {
return err
log.Warnf("Cannot resolve uptime. Error: %s", err)
return 0
}

return sysinfo.Uptime
return uint64(sysinfo.Uptime)
}

// resolveFilesystems resolves filesystems from /etc/mtab file
Expand Down
20 changes: 20 additions & 0 deletions pkg/node/resolver_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,27 @@ import (
"github.com/stretchr/testify/assert"
)

func TestGetInfo(t *testing.T) {
labels := map[string]string{
"foo": "bar",
}

info := NewResolver(5000, "test-version", labels).GetInfo()

assert.NotEmpty(t, info.BootID, "should resolve BootID")
assert.NotEmpty(t, info.MachineID, "should resolve MachineID")
assert.NotEmpty(t, info.SystemUUID, "should resolve SystemUUID")
assert.Equal(t, labels, info.Labels, "should have given node labels")
assert.Equal(t, 5000, info.GrpcPort, "should have given node grpc port")
assert.True(t, len(info.Filesystems) > 0, "should have at least one disk")
}

func TestResolveFilesystems(t *testing.T) {
// Warning: we're assuming that we run in environment where is filesystem info is available
assert.True(t, len(resolveFilesystems()) > 0)
}

func TestResolveUptime(t *testing.T) {
// Warning: we're assuming that we run in environment where is uptime info is available
assert.True(t, resolveUptime() > 0)
}
12 changes: 12 additions & 0 deletions pkg/printers/humanreadable.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"sort"
"strconv"
"strings"
"time"

containers "github.com/ernoaapa/eliot/pkg/api/services/containers/v1"
node "github.com/ernoaapa/eliot/pkg/api/services/node/v1"
Expand All @@ -16,6 +17,8 @@ import (
"github.com/ernoaapa/eliot/pkg/printers/humanreadable"
"github.com/ernoaapa/eliot/pkg/utils"
"github.com/pkg/errors"

"github.com/hako/durafmt"
)

// HumanReadablePrinter is an implementation of ResourcePrinter which prints
Expand Down Expand Up @@ -102,6 +105,10 @@ func (p *HumanReadablePrinter) PrintNodes(nodes []*node.Info, writer io.Writer)
func (p *HumanReadablePrinter) PrintNode(info *node.Info, writer io.Writer) error {
t := template.New("node-details").Funcs(template.FuncMap{
"FormatPercent": formatPercent,
"FormatUptime": formatUptime,
"Subtract": func(a, b uint64) uint64 {
return a - b
},
})
t, err := t.Parse(humanreadable.NodeDetailsTemplate)
if err != nil {
Expand Down Expand Up @@ -129,6 +136,11 @@ func formatPercent(total, free, available uint64) string {
return strconv.FormatFloat(percent, 'f', -1, 64) + "%"
}

func formatUptime(uptime uint64) string {
var duration = time.Duration(uptime)
return durafmt.Parse(duration).String()
}

// PrintPod writes a pod in human readable detailed format to the writer
func (p *HumanReadablePrinter) PrintPod(pod *pods.Pod, writer io.Writer) error {
t := template.New("pod-details").Funcs(template.FuncMap{
Expand Down
3 changes: 2 additions & 1 deletion pkg/printers/humanreadable/NodeDetailsTemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package humanreadable

// NodeDetailsTemplate is go template for printing node details
const NodeDetailsTemplate = `Hostname: {{.Hostname}}
Uptime: {{FormatUptime .Uptime}}
Arch/OS: {{.Os}}/{{.Arch}}
Version: {{.Version}}
Labels:{{range .Labels}}
Expand All @@ -19,7 +20,7 @@ Filesystems:
Filesystem Type Size Used Available Use% Mounted on
---------- ---- ---- ---- --------- ---- ----------
{{- range .Filesystems}}
{{.Filesystem}} {{.TypeName}} {{.Total}} {{.Total - .Free}} {{.Available}} {{FormatPercent .Total .Free .Available}} {{.MountDir}}
{{.Filesystem}} {{.TypeName}} {{.Total}} {{Subtract .Total .Free}} {{.Available}} {{FormatPercent .Total .Free .Available}} {{.MountDir}}
{{- end}}
{{- end}}
`
5 changes: 5 additions & 0 deletions pkg/printers/humanreadable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ import (
func TestFormatPercent(t *testing.T) {
assert.Equal(t, "80%", formatPercent(100*1024, 20*1024, 20*1024))
}

func TestFormatDuration(t *testing.T) {
assert.Equal(t, "292 years 24 weeks 3 days 23 hours 47 minutes 16 seconds 854 milliseconds", formatUptime(9223372036854775807), "should format maximum int64 (half of uint64)")
assert.Equal(t, "-", formatUptime(18446744073709551615), "Should not break if goes above int64 (e.g. if maximum uint64)")
}
2 changes: 1 addition & 1 deletion pkg/printers/interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func testPrintNode(t *testing.T, printer ResourcePrinter) {
Arch: "amd64",
Os: "linux",
Filesystems: []*node.Filesystem{
{Filesystem: "overlay", TypeName: "overlay", Total: 1023856, Used: 8, Available: 1023848, MountDir: "/"},
{Filesystem: "overlay", TypeName: "overlay", Total: 1023856, Free: 1023848, Available: 1023848, MountDir: "/"},
},
}

Expand Down
1 change: 1 addition & 0 deletions vendor.conf
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ k8s.io/kubernetes v1.9.0-alpha.2
github.com/thejerf/suture v2.0.1
github.com/ghodss/yaml v1.0.0
github.com/syndtr/gocapability db04d3cc01c8b54962a58ec7e491717d06cfcc16
github.com/hako/durafmt 987f93c94e473e74aadc826871e61ae6b3360ebb
21 changes: 21 additions & 0 deletions vendor/github.com/hako/durafmt/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 80 additions & 0 deletions vendor/github.com/hako/durafmt/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 058a2f4

Please sign in to comment.