|
|
@@ -9,9 +9,11 @@ import (
|
|
|
|
|
|
"github.com/kubecost/cost-model/pkg/cloud"
|
|
|
"github.com/kubecost/cost-model/pkg/clustercache"
|
|
|
+ "github.com/kubecost/cost-model/pkg/env"
|
|
|
"github.com/kubecost/cost-model/pkg/errors"
|
|
|
"github.com/kubecost/cost-model/pkg/log"
|
|
|
"github.com/kubecost/cost-model/pkg/prom"
|
|
|
+ "github.com/kubecost/cost-model/pkg/util"
|
|
|
|
|
|
promclient "github.com/prometheus/client_golang/api"
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
@@ -42,8 +44,10 @@ func (sc StatefulsetCollector) Collect(ch chan<- prometheus.Metric) {
|
|
|
ds := sc.KubeClusterCache.GetAllStatefulSets()
|
|
|
for _, statefulset := range ds {
|
|
|
labels, values := prom.KubeLabelsToLabels(statefulset.Spec.Selector.MatchLabels)
|
|
|
- m := newStatefulsetMetric(statefulset.GetName(), statefulset.GetNamespace(), "statefulSet_match_labels", labels, values)
|
|
|
- ch <- m
|
|
|
+ if len(labels) > 0 {
|
|
|
+ m := newStatefulsetMetric(statefulset.GetName(), statefulset.GetNamespace(), "statefulSet_match_labels", labels, values)
|
|
|
+ ch <- m
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -128,8 +132,10 @@ func (sc DeploymentCollector) Collect(ch chan<- prometheus.Metric) {
|
|
|
ds := sc.KubeClusterCache.GetAllDeployments()
|
|
|
for _, deployment := range ds {
|
|
|
labels, values := prom.KubeLabelsToLabels(deployment.Spec.Selector.MatchLabels)
|
|
|
- m := newDeploymentMetric(deployment.GetName(), deployment.GetNamespace(), "deployment_match_labels", labels, values)
|
|
|
- ch <- m
|
|
|
+ if len(labels) > 0 {
|
|
|
+ m := newDeploymentMetric(deployment.GetName(), deployment.GetNamespace(), "deployment_match_labels", labels, values)
|
|
|
+ ch <- m
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -214,8 +220,10 @@ func (sc ServiceCollector) Collect(ch chan<- prometheus.Metric) {
|
|
|
svcs := sc.KubeClusterCache.GetAllServices()
|
|
|
for _, svc := range svcs {
|
|
|
labels, values := prom.KubeLabelsToLabels(svc.Spec.Selector)
|
|
|
- m := newServiceMetric(svc.GetName(), svc.GetNamespace(), "service_selector_labels", labels, values)
|
|
|
- ch <- m
|
|
|
+ if len(labels) > 0 {
|
|
|
+ m := newServiceMetric(svc.GetName(), svc.GetNamespace(), "service_selector_labels", labels, values)
|
|
|
+ ch <- m
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -300,8 +308,10 @@ func (nsac NamespaceAnnotationCollector) Collect(ch chan<- prometheus.Metric) {
|
|
|
namespaces := nsac.KubeClusterCache.GetAllNamespaces()
|
|
|
for _, namespace := range namespaces {
|
|
|
labels, values := prom.KubeAnnotationsToLabels(namespace.Annotations)
|
|
|
- m := newNamespaceAnnotationsMetric(namespace.GetName(), "kube_namespace_annotations", labels, values)
|
|
|
- ch <- m
|
|
|
+ if len(labels) > 0 {
|
|
|
+ m := newNamespaceAnnotationsMetric(namespace.GetName(), "kube_namespace_annotations", labels, values)
|
|
|
+ ch <- m
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -380,8 +390,10 @@ func (pac PodAnnotationCollector) Collect(ch chan<- prometheus.Metric) {
|
|
|
pods := pac.KubeClusterCache.GetAllPods()
|
|
|
for _, pod := range pods {
|
|
|
labels, values := prom.KubeAnnotationsToLabels(pod.Annotations)
|
|
|
- m := newPodAnnotationMetric(pod.GetNamespace(), pod.GetName(), "kube_pod_annotations", labels, values)
|
|
|
- ch <- m
|
|
|
+ if len(labels) > 0 {
|
|
|
+ m := newPodAnnotationMetric(pod.GetNamespace(), pod.GetName(), "kube_pod_annotations", labels, values)
|
|
|
+ ch <- m
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -403,6 +415,7 @@ type PodAnnotationsMetric struct {
|
|
|
func newPodAnnotationMetric(namespace, name, fqname string, labelNames []string, labelValues []string) PodAnnotationsMetric {
|
|
|
return PodAnnotationsMetric{
|
|
|
namespace: namespace,
|
|
|
+ name: name,
|
|
|
fqName: fqname,
|
|
|
labelNames: labelNames,
|
|
|
labelValues: labelValues,
|
|
|
@@ -644,6 +657,18 @@ func initCostModelMetrics(clusterCache clustercache.ClusterCache, provider cloud
|
|
|
KubeClientSet: clusterCache.GetClient(),
|
|
|
Cloud: provider,
|
|
|
})
|
|
|
+
|
|
|
+ if env.IsEmitNamespaceAnnotationsMetric() {
|
|
|
+ prometheus.MustRegister(NamespaceAnnotationCollector{
|
|
|
+ KubeClusterCache: clusterCache,
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ if env.IsEmitPodAnnotationsMetric() {
|
|
|
+ prometheus.MustRegister(PodAnnotationCollector{
|
|
|
+ KubeClusterCache: clusterCache,
|
|
|
+ })
|
|
|
+ }
|
|
|
})
|
|
|
}
|
|
|
|
|
|
@@ -766,7 +791,11 @@ func (cmme *CostModelMetricsEmitter) Start() bool {
|
|
|
var defaultRegion string = ""
|
|
|
nodeList := cmme.KubeClusterCache.GetAllNodes()
|
|
|
if len(nodeList) > 0 {
|
|
|
- defaultRegion = nodeList[0].Labels[v1.LabelZoneRegion]
|
|
|
+ var ok bool
|
|
|
+ defaultRegion, ok = util.GetRegion(nodeList[0].Labels)
|
|
|
+ if !ok {
|
|
|
+ log.DedupedWarningf(5, "Failed to locate default region")
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
for {
|