Skip to content

Commit

Permalink
refactor: 💡 abolish get resource with extra (#2298) (#2305)
Browse files Browse the repository at this point in the history
Co-authored-by: bugaolengdeyuxiaoer <[email protected]>
  • Loading branch information
erda-bot and bugaolengdeyuxiaoer authored Oct 11, 2021
1 parent a58e306 commit 770c469
Show file tree
Hide file tree
Showing 15 changed files with 273 additions and 183 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ require (
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/sys v0.0.0-20210915083310-ed5796bab164 // indirect
golang.org/x/text v0.3.7
golang.org/x/time v0.0.0-20210611083556-38a9dc6acbc6 // indirect
golang.org/x/time v0.0.0-20210611083556-38a9dc6acbc6
google.golang.org/genproto v0.0.0-20210916144049-3192f974c780
google.golang.org/grpc v1.40.0
google.golang.org/protobuf v1.27.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@

package charts

import "github.com/erda-project/erda/modules/openapi/component-protocol/components/base"
import (
"github.com/erda-project/erda-infra/providers/component-protocol/cptype"
"github.com/erda-project/erda/modules/openapi/component-protocol/components/base"
)

type Charts struct {
Type string `json:"type"`
Props Props `json:"props"`
*cptype.SDK
base.DefaultProvider
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,144 @@ package charts
import (
"context"

"github.com/pkg/errors"
"github.com/rancher/wrangler/pkg/data"
"k8s.io/apimachinery/pkg/api/resource"

"github.com/erda-project/erda-infra/base/servicehub"
"github.com/erda-project/erda-infra/providers/component-protocol/cptype"
"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/components/cmp-dashboard-nodes/common"
"github.com/erda-project/erda/modules/cmp/component-protocol/components/cmp-dashboard-nodes/common/chart"
cputil2 "github.com/erda-project/erda/modules/cmp/component-protocol/cputil"
"github.com/erda-project/erda/modules/cmp/metrics"
"github.com/erda-project/erda/modules/openapi/component-protocol/components/base"
)

func (chart Charts) Render(ctx context.Context, c *cptype.Component, scenario cptype.Scenario, event cptype.ComponentEvent, gs *cptype.GlobalStateData) error {
chart.Props = Props{
var steveServer cmp.SteveServer
var mServer metrics.Interface

func (cht *Charts) 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")
}
mserver, ok := ctx.Service("cmp").(metrics.Interface)
if !ok {
return errors.New("failed to init component, cmp service in ctx is not a metrics server")
}
steveServer = server
mServer = mserver
return cht.DefaultProvider.Init(ctx)
}
func (cht Charts) Render(ctx context.Context, c *cptype.Component, scenario cptype.Scenario, event cptype.ComponentEvent, gs *cptype.GlobalStateData) error {
var podsMap = make(map[string][]data.Object)
cht.Props = Props{
ContentSetting: "between",
SpaceSize: "big",
}
c.Props = chart.Props
c.Props = cht.Props
cht.SDK = cputil.SDK(ctx)
podReq := &apistructs.SteveRequest{}
podReq.OrgID = cht.SDK.Identity.OrgID
podReq.UserID = cht.SDK.Identity.UserID
podReq.Type = apistructs.K8SPod

if cht.SDK.InParams["clusterName"] != nil {
podReq.ClusterName = cht.SDK.InParams["clusterName"].(string)
} else {
return common.ClusterNotFoundErr
}
resp, err := steveServer.ListSteveResource(ctx, podReq)
if err != nil {
return err
}
for _, pod := range resp {
nodeName := pod.Data().StringSlice("metadata", "fields")[6]
podsMap[nodeName] = append(podsMap[nodeName], pod.Data())
}
nodes := (*gs)["nodes"].([]data.Object)
resourceNames := []string{chart.CPU, chart.Memory, chart.Pods}
for _, resourceName := range resourceNames {
resourceType := resource.DecimalSI
if resourceName == chart.Memory {
resourceType = resource.BinarySI
}
requestQuantity := resource.NewQuantity(0, resourceType)
unAllocatableQuantity := resource.NewQuantity(0, resourceType)
leftQuantity := resource.NewQuantity(0, resourceType)
if len(nodes) == 0 {
(*gs)[resourceName+"Chart"] = []chart.DataItem{}
}
for _, node := range nodes {
nodeName := node.StringSlice("metadata", "fields")[0]
cpu, mem, pod := cputil2.GetNodeAllocatedRes(nodeName, podsMap[nodeName])
switch resourceName {
case chart.CPU:
unallocatedCPU, _, leftCPU, _, _ := cputil2.CalculateNodeRes(node, cpu, 0, 0)
unAllocatableQuantity.Add(*resource.NewMilliQuantity(unallocatedCPU, resource.DecimalSI))
leftQuantity.Add(*resource.NewMilliQuantity(leftCPU, resource.DecimalSI))
requestQuantity.Add(*resource.NewMilliQuantity(cpu, resource.DecimalSI))
case chart.Memory:
_, unallocatedMem, _, leftMem, _ := cputil2.CalculateNodeRes(node, 0, mem, 0)
unAllocatableQuantity.Add(*resource.NewQuantity(unallocatedMem, resource.BinarySI))
leftQuantity.Add(*resource.NewQuantity(leftMem, resource.BinarySI))
requestQuantity.Add(*resource.NewQuantity(mem, resource.BinarySI))
case chart.Pods:
_, _, _, _, leftPods := cputil2.CalculateNodeRes(node, 0, 0, pod)
leftQuantity.Add(*resource.NewQuantity(leftPods, resource.DecimalSI))
requestQuantity.Add(*resource.NewQuantity(pod, resource.DecimalSI))
}
}
var requestStr, leftStr, unAllocatableStr string
var requestValue, leftValue, unAllocatableValue float64
switch resourceName {
case chart.CPU:
requestStr = cputil2.ResourceToString(cht.SDK, float64(requestQuantity.MilliValue()), resource.DecimalSI)
leftStr = cputil2.ResourceToString(cht.SDK, float64(leftQuantity.MilliValue()), resource.DecimalSI)
unAllocatableStr = cputil2.ResourceToString(cht.SDK, float64(unAllocatableQuantity.MilliValue()), resource.DecimalSI)
case chart.Memory:
requestStr = cputil2.ResourceToString(cht.SDK, float64(requestQuantity.Value()), resource.BinarySI)
leftStr = cputil2.ResourceToString(cht.SDK, float64(leftQuantity.Value()), resource.BinarySI)
unAllocatableStr = cputil2.ResourceToString(cht.SDK, float64(unAllocatableQuantity.Value()), resource.BinarySI)
case chart.Pods:
requestStr = cputil2.ResourceToString(cht.SDK, float64(requestQuantity.Value()), "")
leftStr = cputil2.ResourceToString(cht.SDK, float64(leftQuantity.Value()), "")
unAllocatableStr = cputil2.ResourceToString(cht.SDK, float64(unAllocatableQuantity.Value()), "")
}
requestValue = float64(requestQuantity.MilliValue()) / 1000
leftValue = float64(leftQuantity.MilliValue()) / 1000
unAllocatableValue = float64(unAllocatableQuantity.MilliValue()) / 1000
var di []chart.DataItem
var distributedDesc, freeDesc, lockedDesc string
if requestValue != 0 {
distributedDesc = chart.DefaultFormat + requestStr
di = append(di, chart.DataItem{
Value: requestValue,
Name: cht.SDK.I18n(chart.Allocated),
Label: chart.Label{Formatter: distributedDesc},
})
}
if leftValue != 0 {
freeDesc = chart.DefaultFormat + leftStr
di = append(di, chart.DataItem{
Value: leftValue,
Name: cht.SDK.I18n(chart.Free_Allocate),
Label: chart.Label{Formatter: freeDesc},
})
}
if unAllocatableValue != 0 {
lockedDesc = chart.DefaultFormat + unAllocatableStr
di = append(di, chart.DataItem{
Value: unAllocatableValue,
Name: cht.SDK.I18n(chart.Cannot_Allocate),
Label: chart.Label{Formatter: lockedDesc},
})
}
(*gs)[resourceName+"Chart"] = di
}
return nil
}
func init() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ package chart

import (
"context"
"fmt"

"github.com/rancher/wrangler/pkg/data"
"k8s.io/apimachinery/pkg/api/resource"

"github.com/erda-project/erda-infra/providers/component-protocol/cptype"
Expand All @@ -34,9 +32,9 @@ var (
Free_Allocate = "Free-Allocate"
Cannot_Allocate = "Cannot-Allocate"

Memory = "Memory"
CPU = "CPU"
Pods = "Pods"
Memory = "memory"
CPU = "cpu"
Pods = "pods"

DefaultFormat = "{d}%\n"
)
Expand All @@ -53,72 +51,6 @@ type ChartInterface interface {
ChartRender(ctx context.Context, c *cptype.Component, scenario cptype.Scenario, event cptype.ComponentEvent, gs *cptype.GlobalStateData) error
}

func (cht Chart) setData(nodes []data.Object, resourceName string) []DataItem {
//var allocatableTotal, capacityTotal, unAllocatableTotal float64
resourceType := resource.DecimalSI
if resourceName == Memory {
resourceType = resource.BinarySI
}
allocatableQuantity := resource.NewQuantity(0, resourceType)
capacityQuantity := resource.NewQuantity(0, resourceType)
unAllocatableQuantity := resource.NewQuantity(0, resourceType)
if len(nodes) == 0 {
return []DataItem{}
}
for _, node := range nodes {
allocatableQuantity.Add(*parseResource(node.String("extra", "parsedResource", "allocated", resourceName), resourceType))
capacityQuantity.Add(*parseResource(node.String("extra", "parsedResource", "capacity", resourceName), resourceType))
unAllocatableQuantity.Add(*parseResource(node.String("extra", "parsedResource", "unallocatable", resourceName), resourceType))
}
allocatableQuantity.ToUnstructured()
capacityQuantity.Sub(*unAllocatableQuantity)
capacityQuantity.Sub(*allocatableQuantity)

allocatableQuantityValue := float64(allocatableQuantity.Value())
capacityQuantityValue := float64(capacityQuantity.Value())
unAllocatableQuantityValue := float64(unAllocatableQuantity.Value())

allocatableStr, unAllocatableStr, capacityStr := GetScaleValue(allocatableQuantity, unAllocatableQuantity, capacityQuantity)
if resourceName == CPU {
allocatableStr = fmt.Sprintf("%.1f"+cht.SDK.I18n("cores"), allocatableQuantityValue/1000)
capacityStr = fmt.Sprintf("%.1f"+cht.SDK.I18n("cores"), capacityQuantityValue/1000)
unAllocatableStr = fmt.Sprintf("%.1f"+cht.SDK.I18n("cores"), unAllocatableQuantityValue/1000)
}

var di []DataItem
distributedDesc := DefaultFormat + allocatableStr
if allocatableQuantity.Value() == 0 {
distributedDesc = ""
} else {
di = append(di, DataItem{
Value: allocatableQuantityValue,
Name: cht.SDK.I18n(Allocated),
Label: Label{Formatter: distributedDesc},
})
}
freeDesc := DefaultFormat + capacityStr
if capacityQuantity.Value() == 0 {
freeDesc = ""
} else {
di = append(di, DataItem{
Value: capacityQuantityValue,
Name: cht.SDK.I18n(Free_Allocate),
Label: Label{Formatter: freeDesc},
})
}
lockedDesc := DefaultFormat + unAllocatableStr
if unAllocatableQuantity.Value() == 0 {
lockedDesc = ""
} else {
di = append(di, DataItem{
Value: unAllocatableQuantityValue,
Name: cht.SDK.I18n(Cannot_Allocate),
Label: Label{Formatter: lockedDesc},
})
}
return di
}

func GetScaleValue(quantity1 *resource.Quantity, quantity2 *resource.Quantity, quantity3 *resource.Quantity) (string, string, string) {
factor := 10
for ; (quantity1.Value() != 0 && quantity1.Value() > int64(1<<factor)) && ((quantity1.Value() != 0) && quantity2.Value() > int64(1<<factor)) && (quantity3.Value() != 0 && quantity3.Value() > int64(1<<factor)); factor += 10 {
Expand Down Expand Up @@ -153,9 +85,7 @@ func parseResource(str string, format resource.Format) *resource.Quantity {
func (cht *Chart) ChartRender(ctx context.Context, c *cptype.Component, scenario cptype.Scenario, event cptype.ComponentEvent, gs *cptype.GlobalStateData, ResourceType string) error {
cht.CtxBdl = ctx.Value(types.GlobalCtxKeyBundle).(*bundle.Bundle)
cht.SDK = cputil.SDK(ctx)
var nodes []data.Object
nodes = (*gs)["nodes"].([]data.Object)
cht.Props.Option.Series[0].Data = cht.setData(nodes, ResourceType)
cht.Props.Option.Series[0].Data = (*gs)[ResourceType+"Chart"].([]DataItem)
return common.Transfer(cht.Props, &c.Props)
}

Expand Down Expand Up @@ -217,7 +147,7 @@ func (cht *Chart) GetProps(name string) Props {
Title: name,
Option: Option{
Color: []string{"orange", "green", "red"},
Legend: Legend{Data: []string{cht.SDK.I18n(Allocated), cht.SDK.I18n(Cannot_Allocate), cht.SDK.I18n(Free_Allocate)}, Bottom: "0"},
Legend: Legend{Data: []string{cht.SDK.I18n(Allocated), cht.SDK.I18n(Free_Allocate), cht.SDK.I18n(Cannot_Allocate)}, Bottom: "0"},
Grid: Grid{
Bottom: 0,
Top: 0,
Expand Down
Loading

0 comments on commit 770c469

Please sign in to comment.