Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions cmd/node/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ type setNodeStatusOptions struct {
}

func (o *setNodeStatusOptions) run(ctx context.Context) error {
err := o.heartbeat(ctx)
if o.interval == 0 {
return err
return o.heartbeat(ctx)
}

logger := log.WithFunc("node.setNodeStatusOptions.run")
logger.Error(ctx, err, "heartbeat")
logger.Error(ctx, o.heartbeat(ctx), "heartbeat")
ticker := time.NewTicker(time.Duration(o.interval) * time.Second)
defer ticker.Stop()

Expand Down
23 changes: 13 additions & 10 deletions describe/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,28 +89,31 @@ type nodePercent struct {
}

func nodePercents(ctx context.Context, resources ...*corepb.NodeResource) []nodePercent {
logger := log.WithFunc("describe.nodePercents")
rv := make([]nodePercent, 0, len(resources))
for _, resource := range resources {
cr, sr, err := ToResourcePercent(resource)
if err != nil {
logger.Errorf(ctx, err, "resource percent of node %s", resource.Name)
continue
if percent, ok := nodePercentOf(ctx, resource); ok {
rv = append(rv, percent)
}
rv = append(rv, nodePercent{resource, cr, sr})
}
return rv
}

func nodePercentOf(ctx context.Context, resource *corepb.NodeResource) (nodePercent, bool) {
cr, sr, err := ToResourcePercent(resource)
if err != nil {
log.WithFunc("describe.nodePercentOf").Errorf(ctx, err, "resource percent of node %s", resource.Name)
return nodePercent{}, false
}
return nodePercent{resource, cr, sr}, true
}

func nodePercentChan(ctx context.Context, resources <-chan *corepb.NodeResource, keep NodeResourceFilter) <-chan nodePercent {
rv := make(chan nodePercent)
go func() {
defer close(rv)
for resource := range resources {
for _, percent := range nodePercents(ctx, resource) {
if keep == nil || keep(percent.cpumem, percent.storage) {
rv <- percent
}
if percent, ok := nodePercentOf(ctx, resource); ok && (keep == nil || keep(percent.cpumem, percent.storage)) {
rv <- percent
}
}
}()
Expand Down
16 changes: 5 additions & 11 deletions describe/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/jedib0t/go-pretty/v6/table"
resourcetypes "github.com/projecteru2/core/resource/types"
corepb "github.com/projecteru2/core/rpc/gen"
coreutils "github.com/projecteru2/core/utils"
"sigs.k8s.io/yaml"

"github.com/projecteru2/cli/cmd/utils"
Expand All @@ -37,23 +38,16 @@ func ToResourcePercent(resource *corepb.NodeResource) (cpumem, storage map[strin
storageCap := resCap[utils.ResourceStorage]
cr, sr := map[string]float64{}, map[string]float64{}
if cpumemUsage != nil && cpumemCap != nil {
cr["cpu"] = ratio(cpumemUsage.Float64("cpu"), cpumemCap.Float64("cpu"))
cr["memory"] = ratio(cpumemUsage.Float64("memory"), cpumemCap.Float64("memory"))
cr["cpu"] = coreutils.AdvancedDivide(cpumemUsage.Float64("cpu"), cpumemCap.Float64("cpu"))
cr["memory"] = coreutils.AdvancedDivide(cpumemUsage.Float64("memory"), cpumemCap.Float64("memory"))
}
if storageUsage != nil && storageCap != nil {
sr["storage"] = ratio(storageUsage.Float64("storage"), storageCap.Float64("storage"))
sr["volumes"] = ratio(sumParams(storageUsage.RawParams("volumes")), sumParams(storageCap.RawParams("volumes")))
sr["storage"] = coreutils.AdvancedDivide(storageUsage.Float64("storage"), storageCap.Float64("storage"))
sr["volumes"] = coreutils.AdvancedDivide(sumParams(storageUsage.RawParams("volumes")), sumParams(storageCap.RawParams("volumes")))
}
return cr, sr, nil
}

func ratio(usage, capacity float64) float64 {
if capacity == 0 {
return 0
}
return usage / capacity
}

func sumParams(params resourcetypes.RawParams) float64 {
sum := 0.0
for key := range params {
Expand Down