fix hpa target report
parent
d570c70e67
commit
0b0a723c68
|
|
@ -15,14 +15,14 @@ func NewHPA() Res {
|
|||
// Get a service.
|
||||
func (*HPA) Get(ns, n string) (interface{}, error) {
|
||||
opts := metav1.GetOptions{}
|
||||
return conn.dialOrDie().Autoscaling().HorizontalPodAutoscalers(ns).Get(n, opts)
|
||||
return conn.dialOrDie().AutoscalingV2beta2().HorizontalPodAutoscalers(ns).Get(n, opts)
|
||||
}
|
||||
|
||||
// List all services in a given namespace
|
||||
func (*HPA) List(ns string) (Collection, error) {
|
||||
opts := metav1.ListOptions{}
|
||||
|
||||
rr, err := conn.dialOrDie().Autoscaling().HorizontalPodAutoscalers(ns).List(opts)
|
||||
rr, err := conn.dialOrDie().AutoscalingV2beta2().HorizontalPodAutoscalers(ns).List(opts)
|
||||
if err != nil {
|
||||
return Collection{}, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,16 +3,18 @@ package resource
|
|||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/derailed/k9s/internal/k8s"
|
||||
log "github.com/sirupsen/logrus"
|
||||
v1 "k8s.io/api/autoscaling/v1"
|
||||
autoscalingv2beta2 "k8s.io/api/autoscaling/v2beta2"
|
||||
)
|
||||
|
||||
// HPA tracks a kubernetes resource.
|
||||
type HPA struct {
|
||||
*Base
|
||||
instance *v1.HorizontalPodAutoscaler
|
||||
instance *autoscalingv2beta2.HorizontalPodAutoscaler
|
||||
}
|
||||
|
||||
// NewHPAList returns a new resource list.
|
||||
|
|
@ -45,10 +47,10 @@ func NewHPAWithArgs(r k8s.Res) *HPA {
|
|||
func (*HPA) NewInstance(i interface{}) Columnar {
|
||||
cm := NewHPA()
|
||||
switch i.(type) {
|
||||
case *v1.HorizontalPodAutoscaler:
|
||||
cm.instance = i.(*v1.HorizontalPodAutoscaler)
|
||||
case v1.HorizontalPodAutoscaler:
|
||||
ii := i.(v1.HorizontalPodAutoscaler)
|
||||
case *autoscalingv2beta2.HorizontalPodAutoscaler:
|
||||
cm.instance = i.(*autoscalingv2beta2.HorizontalPodAutoscaler)
|
||||
case autoscalingv2beta2.HorizontalPodAutoscaler:
|
||||
ii := i.(autoscalingv2beta2.HorizontalPodAutoscaler)
|
||||
cm.instance = &ii
|
||||
default:
|
||||
log.Fatalf("Unknown %#v", i)
|
||||
|
|
@ -96,20 +98,10 @@ func (r *HPA) Fields(ns string) Row {
|
|||
ff = append(ff, i.Namespace)
|
||||
}
|
||||
|
||||
target := "<unknown>"
|
||||
if i.Status.CurrentCPUUtilizationPercentage != nil {
|
||||
target = strconv.Itoa(int(*i.Status.CurrentCPUUtilizationPercentage))
|
||||
}
|
||||
|
||||
var current int32
|
||||
if i.Spec.TargetCPUUtilizationPercentage != nil {
|
||||
current = *i.Spec.TargetCPUUtilizationPercentage
|
||||
}
|
||||
|
||||
return append(ff,
|
||||
i.ObjectMeta.Name,
|
||||
i.Spec.ScaleTargetRef.Name,
|
||||
fmt.Sprintf("%s٪/%d٪", target, current),
|
||||
toMetrics(i.Spec.Metrics, i.Status.CurrentMetrics),
|
||||
strconv.Itoa(int(*i.Spec.MinReplicas)),
|
||||
strconv.Itoa(int(i.Spec.MaxReplicas)),
|
||||
strconv.Itoa(int(i.Status.CurrentReplicas)),
|
||||
|
|
@ -121,3 +113,70 @@ func (r *HPA) Fields(ns string) Row {
|
|||
func (*HPA) ExtFields() Properties {
|
||||
return Properties{}
|
||||
}
|
||||
|
||||
func toMetrics(specs []autoscalingv2beta2.MetricSpec, statuses []autoscalingv2beta2.MetricStatus) string {
|
||||
if len(specs) == 0 {
|
||||
return "<none>"
|
||||
}
|
||||
list, max, more, count := []string{}, 2, false, 0
|
||||
for i, spec := range specs {
|
||||
switch spec.Type {
|
||||
case autoscalingv2beta2.ExternalMetricSourceType:
|
||||
current := "<unknown>"
|
||||
if spec.External.Target.AverageValue != nil {
|
||||
if len(statuses) > i && statuses[i].External != nil && &statuses[i].External.Current.AverageValue != nil {
|
||||
current = statuses[i].External.Current.AverageValue.String()
|
||||
}
|
||||
list = append(list, fmt.Sprintf("%s/%s (avg)", current, spec.External.Target.AverageValue.String()))
|
||||
} else {
|
||||
if len(statuses) > i && statuses[i].External != nil {
|
||||
current = statuses[i].External.Current.Value.String()
|
||||
}
|
||||
list = append(list, fmt.Sprintf("%s/%s", current, spec.External.Target.Value.String()))
|
||||
}
|
||||
case autoscalingv2beta2.PodsMetricSourceType:
|
||||
current := "<unknown>"
|
||||
if len(statuses) > i && statuses[i].Pods != nil {
|
||||
current = statuses[i].Pods.Current.AverageValue.String()
|
||||
}
|
||||
list = append(list, fmt.Sprintf("%s/%s", current, spec.Pods.Target.AverageValue.String()))
|
||||
case autoscalingv2beta2.ObjectMetricSourceType:
|
||||
current := "<unknown>"
|
||||
if len(statuses) > i && statuses[i].Object != nil {
|
||||
current = statuses[i].Object.Current.Value.String()
|
||||
}
|
||||
list = append(list, fmt.Sprintf("%s/%s", current, spec.Object.Target.Value.String()))
|
||||
case autoscalingv2beta2.ResourceMetricSourceType:
|
||||
current := "<unknown>"
|
||||
if spec.Resource.Target.AverageValue != nil {
|
||||
if len(statuses) > i && statuses[i].Resource != nil {
|
||||
current = statuses[i].Resource.Current.AverageValue.String()
|
||||
}
|
||||
list = append(list, fmt.Sprintf("%s/%s", current, spec.Resource.Target.AverageValue.String()))
|
||||
} else {
|
||||
if len(statuses) > i && statuses[i].Resource != nil && statuses[i].Resource.Current.AverageUtilization != nil {
|
||||
current = fmt.Sprintf("%d%%", *statuses[i].Resource.Current.AverageUtilization)
|
||||
}
|
||||
target := "<auto>"
|
||||
if spec.Resource.Target.AverageUtilization != nil {
|
||||
target = fmt.Sprintf("%d%%", *spec.Resource.Target.AverageUtilization)
|
||||
}
|
||||
list = append(list, fmt.Sprintf("%s/%s", current, target))
|
||||
}
|
||||
default:
|
||||
list = append(list, "<unknown type>")
|
||||
}
|
||||
count++
|
||||
}
|
||||
|
||||
if count > max {
|
||||
list = list[:max]
|
||||
more = true
|
||||
}
|
||||
|
||||
ret := strings.Join(list, ", ")
|
||||
if more {
|
||||
return fmt.Sprintf("%s + %d more...", ret, count-max)
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue