Skip to content

Commit

Permalink
Fix node kubelet collector mode for KSM pod metrics collection
Browse files Browse the repository at this point in the history
  • Loading branch information
L3n41c committed Nov 5, 2024
1 parent beedc1c commit 235b2e2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
11 changes: 7 additions & 4 deletions pkg/collector/corechecks/cluster/ksm/customresources/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,13 @@ func (f *extendedPodFactory) customResourceGenerator(p *v1.Pod, resourceType str

func wrapPodFunc(f func(*v1.Pod) *metric.Family) func(interface{}) *metric.Family {
return func(obj interface{}) *metric.Family {
pod := &v1.Pod{}
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.(*unstructured.Unstructured).Object, pod); err != nil {
log.Warnf("cannot decode object %q into v1.Pod, err=%s, skipping", obj.(*unstructured.Unstructured).Object["apiVersion"], err)
return nil
var pod *v1.Pod
var ok bool
if pod, ok = obj.(*v1.Pod); !ok {
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.(*unstructured.Unstructured).Object, pod); err != nil {
log.Warnf("cannot decode object %q into v1.Pod, err=%s, skipping", obj.(*unstructured.Unstructured).Object["apiVersion"], err)
return nil
}
}

metricFamily := f(pod)
Expand Down
26 changes: 16 additions & 10 deletions pkg/kubestatemetrics/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
Expand Down Expand Up @@ -190,15 +191,21 @@ func GenerateStores[T any](
filteredMetricFamilies := generator.FilterFamilyGenerators(b.allowDenyList, metricFamilies)
composedMetricGenFuncs := generator.ComposeMetricGenFuncs(filteredMetricFamilies)

isPod := false
if _, ok := expectedType.(*corev1.Pod); ok {
isPod = true
} else if u, ok := expectedType.(*unstructured.Unstructured); ok {
isPod = u.GetAPIVersion() == "v1" && u.GetKind() == "Pod"
}

if b.namespaces.IsAllNamespaces() {
store := store.NewMetricsStore(composedMetricGenFuncs, reflect.TypeOf(expectedType).String())

switch expectedType.(type) {
// Pods are handled differently because depending on the configuration
// they're collected from the API server or the Kubelet.
case *corev1.Pod:
if isPod {
// Pods are handled differently because depending on the configuration
// they're collected from the API server or the Kubelet.
handlePodCollection(b, store, client, listWatchFunc, corev1.NamespaceAll, useAPIServerCache)
default:
} else {
listWatcher := listWatchFunc(client, corev1.NamespaceAll, b.fieldSelectorFilter)
b.startReflector(expectedType, store, listWatcher, useAPIServerCache)
}
Expand All @@ -209,12 +216,11 @@ func GenerateStores[T any](
stores := make([]cache.Store, 0, len(b.namespaces))
for _, ns := range b.namespaces {
store := store.NewMetricsStore(composedMetricGenFuncs, reflect.TypeOf(expectedType).String())
switch expectedType.(type) {
// Pods are handled differently because depending on the configuration
// they're collected from the API server or the Kubelet.
case *corev1.Pod:
if isPod {
// Pods are handled differently because depending on the configuration
// they're collected from the API server or the Kubelet.
handlePodCollection(b, store, client, listWatchFunc, ns, useAPIServerCache)
default:
} else {
listWatcher := listWatchFunc(client, ns, b.fieldSelectorFilter)
b.startReflector(expectedType, store, listWatcher, useAPIServerCache)
}
Expand Down

0 comments on commit 235b2e2

Please sign in to comment.