Add hpa conditions

This commit is contained in:
Johannes 'fish' Ziemke 2018-04-17 12:48:55 +02:00
parent dffb17cf8b
commit f9658cad01
4 changed files with 38 additions and 28 deletions

View File

@ -20,6 +20,7 @@ import (
"time" "time"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/fields" "k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest" "k8s.io/client-go/rest"
@ -62,3 +63,28 @@ func (sil SharedInformerList) Run(stopCh <-chan struct{}) {
go sinf.Run(stopCh) go sinf.Run(stopCh)
} }
} }
func boolFloat64(b bool) float64 {
if b {
return 1
}
return 0
}
// addConditionMetrics generates one metric for each possible node condition
// status. For this function to work properly, the last label in the metric
// description must be the condition.
func addConditionMetrics(ch chan<- prometheus.Metric, desc *prometheus.Desc, cs v1.ConditionStatus, lv ...string) {
ch <- prometheus.MustNewConstMetric(
desc, prometheus.GaugeValue, boolFloat64(cs == v1.ConditionTrue),
append(lv, "true")...,
)
ch <- prometheus.MustNewConstMetric(
desc, prometheus.GaugeValue, boolFloat64(cs == v1.ConditionFalse),
append(lv, "false")...,
)
ch <- prometheus.MustNewConstMetric(
desc, prometheus.GaugeValue, boolFloat64(cs == v1.ConditionUnknown),
append(lv, "unknown")...,
)
}

View File

@ -21,7 +21,7 @@ import (
"github.com/golang/glog" "github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
autoscaling "k8s.io/api/autoscaling/v1" autoscaling "k8s.io/api/autoscaling/v2beta1"
"k8s.io/client-go/kubernetes" "k8s.io/client-go/kubernetes"
) )
@ -60,6 +60,11 @@ var (
descHorizontalPodAutoscalerLabelsHelp, descHorizontalPodAutoscalerLabelsHelp,
descHorizontalPodAutoscalerLabelsDefaultLabels, nil, descHorizontalPodAutoscalerLabelsDefaultLabels, nil,
) )
descHorizontalPodAutoscalerCondition = prometheus.NewDesc(
"kube_hpa_status_condition",
"The condition of this autoscaler.",
[]string{"namespace", "hpa", "condition", "status"}, nil,
)
) )
type HPALister func() (autoscaling.HorizontalPodAutoscalerList, error) type HPALister func() (autoscaling.HorizontalPodAutoscalerList, error)
@ -69,7 +74,7 @@ func (l HPALister) List() (autoscaling.HorizontalPodAutoscalerList, error) {
} }
func RegisterHorizontalPodAutoScalerCollector(registry prometheus.Registerer, kubeClient kubernetes.Interface, namespaces []string) { func RegisterHorizontalPodAutoScalerCollector(registry prometheus.Registerer, kubeClient kubernetes.Interface, namespaces []string) {
client := kubeClient.Autoscaling().RESTClient() client := kubeClient.AutoscalingV2beta1().RESTClient()
glog.Infof("collect hpa with %s", client.APIVersion()) glog.Infof("collect hpa with %s", client.APIVersion())
hpainfs := NewSharedInformerList(client, "horizontalpodautoscalers", namespaces, &autoscaling.HorizontalPodAutoscaler{}) hpainfs := NewSharedInformerList(client, "horizontalpodautoscalers", namespaces, &autoscaling.HorizontalPodAutoscaler{})
@ -144,4 +149,8 @@ func (hc *hpaCollector) collectHPA(ch chan<- prometheus.Metric, h autoscaling.Ho
addGauge(descHorizontalPodAutoscalerSpecMinReplicas, float64(*h.Spec.MinReplicas)) addGauge(descHorizontalPodAutoscalerSpecMinReplicas, float64(*h.Spec.MinReplicas))
addGauge(descHorizontalPodAutoscalerStatusCurrentReplicas, float64(h.Status.CurrentReplicas)) addGauge(descHorizontalPodAutoscalerStatusCurrentReplicas, float64(h.Status.CurrentReplicas))
addGauge(descHorizontalPodAutoscalerStatusDesiredReplicas, float64(h.Status.DesiredReplicas)) addGauge(descHorizontalPodAutoscalerStatusDesiredReplicas, float64(h.Status.DesiredReplicas))
for _, c := range h.Status.Conditions {
addConditionMetrics(ch, descHorizontalPodAutoscalerCondition, c.Status, h.Name, h.Namespace, string(c.Type))
}
} }

View File

@ -19,7 +19,7 @@ package collectors
import ( import (
"testing" "testing"
autoscaling "k8s.io/api/autoscaling/v1" autoscaling "k8s.io/api/autoscaling/v2beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
) )

View File

@ -266,28 +266,3 @@ func (nc *nodeCollector) collectNode(ch chan<- prometheus.Metric, n v1.Node) {
addResource(descNodeStatusAllocatableMemory, n.Status.Allocatable, v1.ResourceMemory) addResource(descNodeStatusAllocatableMemory, n.Status.Allocatable, v1.ResourceMemory)
addResource(descNodeStatusAllocatablePods, n.Status.Allocatable, v1.ResourcePods) addResource(descNodeStatusAllocatablePods, n.Status.Allocatable, v1.ResourcePods)
} }
// addConditionMetrics generates one metric for each possible node condition
// status. For this function to work properly, the last label in the metric
// description must be the condition.
func addConditionMetrics(ch chan<- prometheus.Metric, desc *prometheus.Desc, cs v1.ConditionStatus, lv ...string) {
ch <- prometheus.MustNewConstMetric(
desc, prometheus.GaugeValue, boolFloat64(cs == v1.ConditionTrue),
append(lv, "true")...,
)
ch <- prometheus.MustNewConstMetric(
desc, prometheus.GaugeValue, boolFloat64(cs == v1.ConditionFalse),
append(lv, "false")...,
)
ch <- prometheus.MustNewConstMetric(
desc, prometheus.GaugeValue, boolFloat64(cs == v1.ConditionUnknown),
append(lv, "unknown")...,
)
}
func boolFloat64(b bool) float64 {
if b {
return 1
}
return 0
}