Skip to content

Commit

Permalink
add metric ext_cpu_total_distribute for local state
Browse files Browse the repository at this point in the history
  • Loading branch information
shijieqin committed May 20, 2022
1 parent b7218fc commit a19070e
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: ensurance.crane.io/v1alpha1
kind: NodeQOSEnsurancePolicy
metadata:
name: "disablescheduling-when-ext-cpu-total-distribute"
labels:
app: "system"
spec:
nodeQualityProbe:
timeoutSeconds: 10
nodeLocalGet:
localCacheTTLSeconds: 60
objectiveEnsurances:
- name: "ext_cpu_total_distribute"
avoidanceThreshold: 2
restoreThreshold: 2
actionName: "disablescheduling"
strategy: "None"
metricRule:
name: "ext_cpu_total_distribute"
value: 110
10 changes: 10 additions & 0 deletions pkg/ensurance/collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/gocrane/crane/pkg/common"
"github.com/gocrane/crane/pkg/ensurance/collector/cadvisor"
"github.com/gocrane/crane/pkg/ensurance/collector/nodelocal"
"github.com/gocrane/crane/pkg/ensurance/collector/noderesource"
"github.com/gocrane/crane/pkg/ensurance/collector/types"
"github.com/gocrane/crane/pkg/features"
"github.com/gocrane/crane/pkg/known"
Expand Down Expand Up @@ -179,6 +180,15 @@ func (s *StateCollector) UpdateCollectors() {
if _, exists := s.collectors.Load(types.CadvisorCollectorType); !exists {
s.collectors.Store(types.CadvisorCollectorType, cadvisor.NewCadvisorCollector(s.podLister, s.GetCadvisorManager()))
}

if nodeResourceGate := utilfeature.DefaultFeatureGate.Enabled(features.CraneNodeResource); nodeResourceGate {
if _, exists := s.collectors.Load(types.NodeResourceCollectorType); !exists {
c := noderesource.NewNodeResourceCollector(s.nodeName, s.nodeLister, s.podLister)
if c != nil {
s.collectors.Store(types.NodeResourceCollectorType, c)
}
}
}
break
}

Expand Down
63 changes: 63 additions & 0 deletions pkg/ensurance/collector/noderesource/noderesource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package noderesource

import (
"fmt"
"time"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/labels"
v1 "k8s.io/client-go/listers/core/v1"
"k8s.io/klog/v2"

"github.com/gocrane/crane/pkg/common"
"github.com/gocrane/crane/pkg/ensurance/collector/types"
"github.com/gocrane/crane/pkg/utils"
)

type NodeResource struct {
nodeName string
nodeLister v1.NodeLister
podLister v1.PodLister
}

func NewNodeResourceCollector(nodeName string, nodeLister v1.NodeLister, podLister v1.PodLister) *NodeResource {
klog.V(4).Infof("NodeResourceCollector create")
return &NodeResource{
nodeName: nodeName,
nodeLister: nodeLister,
podLister: podLister,
}
}

func (n *NodeResource) GetType() types.CollectType {
return types.NodeResourceCollectorType
}

func (n *NodeResource) Collect() (map[string][]common.TimeSeries, error) {
klog.V(6).Infof("NodeResourceCollector Collect")
node, err := n.nodeLister.Get(n.nodeName)
if err != nil {
return nil, err
}
pods, err := n.podLister.List(labels.Everything())
if err != nil {
return nil, err
}

allExtCpu := node.Status.Allocatable.Name(corev1.ResourceName(fmt.Sprintf(utils.ExtResourcePrefixFormat, corev1.ResourceCPU.String())), resource.DecimalSI).MilliValue()
var distributeExtCpu int64 = 0
for _, pod := range pods {
for _, container := range pod.Spec.Containers {
if quantity, ok := container.Resources.Requests[corev1.ResourceName(fmt.Sprintf(utils.ExtResourcePrefixFormat, corev1.ResourceCPU.String()))]; ok {
distributeExtCpu += quantity.MilliValue()
}
}
}
klog.V(4).Infof("allExtCpu: %d, distributeExtCpu: %d", allExtCpu, distributeExtCpu)
return map[string][]common.TimeSeries{string(types.MetricNameExtCpuTotalDistribute): {{Samples: []common.Sample{{Value: (float64(distributeExtCpu) / float64(allExtCpu)) * 100, Timestamp: time.Now().Unix()}}}}}, nil
}

func (n *NodeResource) Stop() error {
return nil
}
2 changes: 2 additions & 0 deletions pkg/ensurance/collector/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const (
CadvisorCollectorType CollectType = "cadvisor"
EbpfCollectorType CollectType = "ebpf"
MetricsServerCollectorType CollectType = "metrics-server"
NodeResourceCollectorType CollectType = "node-resource"
)

type MetricName string
Expand Down Expand Up @@ -51,6 +52,7 @@ const (
MetricNameContainerSchedRunQueueTime MetricName = "container_sched_run_queue_time"

MetricNameExtResContainerCpuTotalUsage MetricName = "ext_res_container_cpu_total_usage"
MetricNameExtCpuTotalDistribute MetricName = "ext_cpu_total_distribute"
)

func GetCgroupPath(p *v1.Pod) string {
Expand Down

0 comments on commit a19070e

Please sign in to comment.