2019-02-05 04:28:43 +08:00
|
|
|
package kube_inventory
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
|
2021-03-18 05:35:25 +08:00
|
|
|
v1 "k8s.io/api/apps/v1"
|
2019-02-05 04:28:43 +08:00
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func collectStatefulSets(ctx context.Context, acc telegraf.Accumulator, ki *KubernetesInventory) {
|
|
|
|
|
list, err := ki.client.getStatefulSets(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
acc.AddError(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
for _, s := range list.Items {
|
2021-03-18 05:35:25 +08:00
|
|
|
if err = ki.gatherStatefulSet(s, acc); err != nil {
|
2019-02-05 04:28:43 +08:00
|
|
|
acc.AddError(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-24 06:35:37 +08:00
|
|
|
func (ki *KubernetesInventory) gatherStatefulSet(s v1.StatefulSet, acc telegraf.Accumulator) error {
|
2019-02-05 04:28:43 +08:00
|
|
|
status := s.Status
|
|
|
|
|
fields := map[string]interface{}{
|
2021-03-18 05:35:25 +08:00
|
|
|
"created": s.GetCreationTimestamp().UnixNano(),
|
|
|
|
|
"generation": s.Generation,
|
|
|
|
|
"replicas": status.Replicas,
|
|
|
|
|
"replicas_current": status.CurrentReplicas,
|
|
|
|
|
"replicas_ready": status.ReadyReplicas,
|
|
|
|
|
"replicas_updated": status.UpdatedReplicas,
|
2019-02-05 04:28:43 +08:00
|
|
|
"spec_replicas": *s.Spec.Replicas,
|
2021-03-18 05:35:25 +08:00
|
|
|
"observed_generation": s.Status.ObservedGeneration,
|
2019-02-05 04:28:43 +08:00
|
|
|
}
|
|
|
|
|
tags := map[string]string{
|
2021-03-18 05:35:25 +08:00
|
|
|
"statefulset_name": s.Name,
|
|
|
|
|
"namespace": s.Namespace,
|
2019-02-05 04:28:43 +08:00
|
|
|
}
|
2021-03-18 05:35:25 +08:00
|
|
|
for key, val := range s.Spec.Selector.MatchLabels {
|
2020-06-19 03:08:52 +08:00
|
|
|
if ki.selectorFilter.Match(key) {
|
|
|
|
|
tags["selector_"+key] = val
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-05 04:28:43 +08:00
|
|
|
|
|
|
|
|
acc.AddFields(statefulSetMeasurement, fields, tags)
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|