Skip to content

Commit

Permalink
Merge pull request #39 from GMH233/feature/autoscaler
Browse files Browse the repository at this point in the history
refactor: 减少debug信息
  • Loading branch information
GMH233 authored May 27, 2024
2 parents 62c4c21 + 7a2b8d2 commit e66c6db
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 31 deletions.
81 changes: 55 additions & 26 deletions pkg/controller/podautoscaler/horizonal.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"time"
)

var hpadbg bool = false

type HorizonalController interface {
Run() error
}
Expand Down Expand Up @@ -70,7 +72,9 @@ func (hc *horizonalController) Run() error {

// 1. 获取所有的HorizontalPodAutoscaler
allHPAScalers, err := hc.kube_cli.GetAllHPAScalers()
log.Printf("[HPA] Get all HorizontalPodAutoscaler: %v", allHPAScalers)
if hpadbg {
log.Printf("[HPA] Get all HorizontalPodAutoscaler: %v", allHPAScalers)
}
if err != nil {
log.Printf("[HPA] Get all HorizontalPodAutoscaler failed, error: %v", err)
// return err
Expand All @@ -82,14 +86,18 @@ func (hc *horizonalController) Run() error {

// 2. 获取所有的Pod
allPods, err := hc.kube_cli.GetAllPods()
log.Printf("[HPA] Get all Pods: %v", allPods)
if hpadbg {
log.Printf("[HPA] Get all Pods: %v", allPods)
}
if err != nil {
log.Printf("[HPA] Get all Pods failed, error: %v", err)
// return err
}

for _, hpa := range allHPAScalers {
log.Printf("[HPA] Conducting HPA: %v\n", hpa)
if hpadbg {
log.Printf("[HPA] Conducting HPA: %v\n", hpa)
}
reps, err := hc.kube_cli.GetAllReplicaSets()
if err != nil {
log.Printf("[HPA] Get all ReplicaSets failed, error: %v\n", err)
Expand All @@ -105,7 +113,9 @@ func (hc *horizonalController) Run() error {
log.Printf("[HPA] ReplicaSet not found\n")
continue
}
log.Printf("[HPA] ReplicaSet Matched: %v\n", rep)
if hpadbg {
log.Printf("[HPA] ReplicaSet Matched: %v\n", rep)
}
// 根据ReplicaSet的labels筛选所有的Pod
podsMatch, err := oneMatchRpsLabels(rep, allPods)
if err != nil {
Expand All @@ -116,9 +126,9 @@ func (hc *horizonalController) Run() error {
log.Printf("[HPA] No matched pods!\n")
continue
}

log.Printf("[HPA] Pods Matched: %v\n", podsMatch)

if hpadbg {
log.Printf("[HPA] Pods Matched: %v\n", podsMatch)
}
// replicaSet目前的副本数
var curRpsNum int32 = rep.Spec.Replicas

Expand Down Expand Up @@ -201,7 +211,9 @@ func (hc *horizonalController) changeRpsPodNum(name string, namespace string, re
// TODOS 由ratio计算副本数的函数抽象成一个
func genRepNumFromCPU(hpa *v1.HorizontalPodAutoscaler, metricTypePos int, podsMatch []*v1.Pod, curRpsNum int32, kube_cli kubeclient.Client) int32 {

log.Printf("[HPA] genRepNumFromCPU\n")
if hpadbg {
log.Printf("[HPA] genRepNumFromCPU\n")
}
//需要取得hpa中相关的策略字段,以获取相关的统计窗口大小
upBehavior := hpa.Spec.Behavior.ScaleUp
downBehavior := hpa.Spec.Behavior.ScaleDown
Expand All @@ -218,9 +230,9 @@ func genRepNumFromCPU(hpa *v1.HorizontalPodAutoscaler, metricTypePos int, podsMa

upPeriod := upBehavior.PeriodSeconds
downPeriod := downBehavior.PeriodSeconds

log.Printf("[HPA] UpPeriod: %v, DownPeriod: %v\n", upPeriod, downPeriod)

if hpadbg {
log.Printf("[HPA] UpPeriod: %v, DownPeriod: %v\n", upPeriod, downPeriod)
}
// spec中metrics的模板,包含资源类型,目标值等
metricsTplt := hpa.Spec.Metrics[metricTypePos]
if metricsTplt.Target.Type != v1.UtilizationMetricType {
Expand All @@ -238,8 +250,9 @@ func genRepNumFromCPU(hpa *v1.HorizontalPodAutoscaler, metricTypePos int, podsMa
if ratio > 1.1 {
var newRepNum int32 = 0
// 可以扩容
log.Printf("[HPA] UpScale: %v\n", hpa)

if hpadbg {
log.Printf("[HPA] UpScale: %v\n", hpa)
}
tryAdd := (int32)(math.Ceil((float64)((ratio - 1) * float32(curRpsNum))))
if hpa.Spec.Behavior.ScaleUp.Type == v1.PercentScalingPolicy {
tryAddByPercent := (int32)(math.Ceil((float64)(hpa.Spec.Behavior.ScaleUp.Value * curRpsNum / 100)))
Expand All @@ -266,8 +279,9 @@ func genRepNumFromCPU(hpa *v1.HorizontalPodAutoscaler, metricTypePos int, podsMa
if ratio < 0.9 {
var newRepNum int32 = 0
// 可以缩容
log.Printf("[HPA] DownScale: %v\n", hpa)

if hpadbg {
log.Printf("[HPA] DownScale: %v\n", hpa)
}
trySub := (int32)(math.Ceil((float64)((1 - ratio) * float32(curRpsNum))))
if hpa.Spec.Behavior.ScaleDown.Type == v1.PercentScalingPolicy {
trySubByPercent := (int32)(math.Ceil((float64)(hpa.Spec.Behavior.ScaleDown.Value * curRpsNum / 100)))
Expand Down Expand Up @@ -334,15 +348,19 @@ func genAvgCpuUsage(windowSz int32, podsMatch []*v1.Pod, kube_cli kubeclient.Cli
}
if len(oneCtnr) != 0 {
avg = sum / float32(len(oneCtnr))
log.Printf("[HPA] Pod Metrics avg cpu / one container: %v\n", avg)
if hpadbg {
log.Printf("[HPA] Pod Metrics avg cpu / one container: %v\n", avg)
}
}
podCpuSum += avg
}
podCpuAvg := podCpuSum / float32(len(oneMetrics.ContainerInfo))

// 计算Pod的平均cpu使用率
// 默认是所有container的平均值
log.Printf("[HPA] Pod Metrics avg cpu / all containers in one pod: %v\n", podCpuAvg)
if hpadbg {
log.Printf("[HPA] Pod Metrics avg cpu / all containers in one pod: %v\n", podCpuAvg)
}
allPodCpuAvg += podCpuAvg
}

Expand All @@ -353,7 +371,10 @@ func genAvgCpuUsage(windowSz int32, podsMatch []*v1.Pod, kube_cli kubeclient.Cli
}

func genRepNumFromMemory(hpa *v1.HorizontalPodAutoscaler, metricPos int, podsMatch []*v1.Pod, curRpsNum int32, kube_cli kubeclient.Client) int32 {
log.Printf("[HPA] genRepNumFromMemory\n")

if hpadbg {
log.Printf("[HPA] genRepNumFromMemory\n")
}
//需要取得hpa中相关的策略字段,以获取相关的统计窗口大小
upBehavior := hpa.Spec.Behavior.ScaleUp
downBehavior := hpa.Spec.Behavior.ScaleDown
Expand All @@ -367,8 +388,9 @@ func genRepNumFromMemory(hpa *v1.HorizontalPodAutoscaler, metricPos int, podsMat

upPeriod := upBehavior.PeriodSeconds
downPeriod := downBehavior.PeriodSeconds
log.Printf("[HPA] UpPeriod: %v, DownPeriod: %v\n", upPeriod, downPeriod)

if hpadbg {
log.Printf("[HPA] UpPeriod: %v, DownPeriod: %v\n", upPeriod, downPeriod)
}
// spec中metrics的模板,包含资源类型,目标值等
metricsTplt := hpa.Spec.Metrics[metricPos]
fmt.Print(string(metricsTplt.Name))
Expand All @@ -388,7 +410,9 @@ func genRepNumFromMemory(hpa *v1.HorizontalPodAutoscaler, metricPos int, podsMat
if ratio > 1.5 {
var newRepNum int32 = 0
// 可以扩容
log.Printf("[HPA] UpScale: %v\n", hpa)
if hpadbg {
log.Printf("[HPA] UpScale: %v\n", hpa)
}

tryAdd := (int32)(math.Ceil((float64)((ratio - 1) * float32(curRpsNum))))
if hpa.Spec.Behavior.ScaleUp.Type == v1.PercentScalingPolicy {
Expand Down Expand Up @@ -416,8 +440,9 @@ func genRepNumFromMemory(hpa *v1.HorizontalPodAutoscaler, metricPos int, podsMat
if ratio < 0.5 {
var newRepNum int32 = 0
// 可以缩容
log.Printf("[HPA] DownScale: %v\n", hpa)

if hpadbg {
log.Printf("[HPA] DownScale: %v\n", hpa)
}
trySub := (int32)(math.Ceil((float64)((1 - ratio) * float32(curRpsNum))))
if hpa.Spec.Behavior.ScaleDown.Type == v1.PercentScalingPolicy {
trySubByPercent := (int32)(math.Ceil((float64)(hpa.Spec.Behavior.ScaleDown.Value * curRpsNum / 100)))
Expand All @@ -437,7 +462,7 @@ func genRepNumFromMemory(hpa *v1.HorizontalPodAutoscaler, metricPos int, podsMat
}

// 保持不变
log.Printf("[HPA] In tolerance, keep the same: %v\n", hpa)
log.Printf("[HPA] In tolerance interval, keep the same: %v\n", hpa)
return curRpsNum
}

Expand Down Expand Up @@ -487,15 +512,19 @@ func genAvgMemoryUsage(windowSz int32, podsMatch []*v1.Pod, kube_cli kubeclient.
}
if len(oneCtnr) != 0 {
avg = sum / float32(len(oneCtnr))
log.Printf("[HPA] Pod Metrics avg memory / one container: %v\n", avg)
if hpadbg {
log.Printf("[HPA] Pod Metrics avg memory / one container: %v\n", avg)
}
}
podMemSum += avg
}
podMemAvg := podMemSum / float32(len(oneMetrics.ContainerInfo))

// 计算Pod的平均内存使用率
// 默认是所有container的平均值
log.Printf("[HPA] Pod Metrics avg memory / all containers in one pod: %v\n", podMemAvg)
if hpadbg {
log.Printf("[HPA] Pod Metrics avg memory / all containers in one pod: %v\n", podMemAvg)
}
allPodMemAvg += podMemAvg
}

Expand Down
18 changes: 13 additions & 5 deletions pkg/kubelet/metrics/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
// mc.Run()
// }

var mdebug bool = true
var mdebug bool = false

type metricsCollector struct {
// 从cadvisor中获取容器指标的操作者
Expand Down Expand Up @@ -101,7 +101,9 @@ func (mc *metricsCollector) Run() {
if err != nil {
log.Printf("get metrics err: %v", err.Error())
}
log.Printf("metrics to be uploaded: %v", curMetrics)
if mdebug {
log.Printf("metrics to be uploaded: %v", curMetrics)
}
// 上传指标
err = mc.uploadMetrics(curMetrics)
if err != nil {
Expand Down Expand Up @@ -140,7 +142,9 @@ func (mc *metricsCollector) init() {
func (mc *metricsCollector) SetPodInfo(podStats []*runtime.PodStatus) {
// 由kubelet设置pod信息
// 注意是Pod 到 容器非人的字符串 的映射
log.Printf("set pod info: %v", podStats)
if mdebug {
log.Printf("set pod info: %v", podStats)
}
mc.podStatsLock.Lock()
if mc.podStats == nil {
mc.podStats = make([]*runtime.PodStatus, 0)
Expand Down Expand Up @@ -206,12 +210,16 @@ func (mc *metricsCollector) getMetrics() ([]*v1.PodRawMetrics, error) {
//如果比上次时间戳还小,就不用上传
if lastTime, ok := mc.conLastTime[dockerId]; ok {
if !item.Timestamp.After(lastTime) {
fmt.Printf("Timestamp is not later than last time, no update\n")
if mdebug {
fmt.Printf("Timestamp is not later than last time, no update\n")
}
continue
}
}
// 更新时间戳
fmt.Printf("update Timestamp\n")
if mdebug {
fmt.Printf("update Timestamp\n")
}
mc.conLastTime[dockerId] = item.Timestamp

cMetricItem.TimeStamp = item.Timestamp
Expand Down

0 comments on commit e66c6db

Please sign in to comment.