Skip to content

Commit

Permalink
[corechecks/ksm] Support HPA v2beta2 again (#18580)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidor committed Aug 4, 2023
1 parent 8846241 commit 0ef845e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
17 changes: 9 additions & 8 deletions pkg/collector/corechecks/cluster/ksm/customresources/hpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ package customresources
// This file has most of its logic copied from the KSM hpa metric family
// generators available at
// https://github.com/kubernetes/kube-state-metrics/blob/release-2.4/internal/store/horizontalpodautoscaler.go
// It exists here to provide backwards compatibility with k8s >1.25, as KSM 2.4
// uses API v2beta2 instead of v2.
// It exists here to provide backwards compatibility with kubernetes versions
// that use autoscaling/v2beta2, as the KSM version that we depend on uses API
// v2 instead of v2beta2.

import (
"context"

"github.com/DataDog/datadog-agent/pkg/util/kubernetes/apiserver"
autoscaling "k8s.io/api/autoscaling/v2"
autoscaling "k8s.io/api/autoscaling/v2beta2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
Expand Down Expand Up @@ -54,9 +55,9 @@ var (
targetMetricLabels = []string{"metric_name", "metric_target_type"}
)

// NewHorizontalPodAutoscalerV2Factory returns a new HorizontalPodAutoscaler
// metric family generator factory.
func NewHorizontalPodAutoscalerV2Factory(client *apiserver.APIClient) customresource.RegistryFactory {
// NewHorizontalPodAutoscalerV2Beta2Factory returns a new
// HorizontalPodAutoscaler metric family generator factory.
func NewHorizontalPodAutoscalerV2Beta2Factory(client *apiserver.APIClient) customresource.RegistryFactory {
return &hpav2Factory{
client: client.Cl,
}
Expand Down Expand Up @@ -363,11 +364,11 @@ func (f *hpav2Factory) ListWatch(customResourceClient interface{}, ns string, fi
return &cache.ListWatch{
ListFunc: func(opts metav1.ListOptions) (runtime.Object, error) {
opts.FieldSelector = fieldSelector
return client.AutoscalingV2().HorizontalPodAutoscalers(ns).List(context.TODO(), opts)
return client.AutoscalingV2beta2().HorizontalPodAutoscalers(ns).List(context.TODO(), opts)
},
WatchFunc: func(opts metav1.ListOptions) (watch.Interface, error) {
opts.FieldSelector = fieldSelector
return client.AutoscalingV2().HorizontalPodAutoscalers(ns).Watch(context.TODO(), opts)
return client.AutoscalingV2beta2().HorizontalPodAutoscalers(ns).Watch(context.TODO(), opts)
},
}
}
Expand Down
7 changes: 2 additions & 5 deletions pkg/collector/corechecks/cluster/ksm/kubernetes_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,11 +431,8 @@ func manageResourcesReplacement(c *apiserver.APIClient, factories []customresour
"policy/v1": {
"PodDisruptionBudget": customresources.NewPodDisruptionBudgetV1Beta1Factory,
},

// support for newer k8s versions where the newer resources are
// not yet supported by KSM
"autoscaling/v2beta2": {
"HorizontalPodAutoscaler": customresources.NewHorizontalPodAutoscalerV2Factory,
"autoscaling/v2": {
"HorizontalPodAutoscaler": customresources.NewHorizontalPodAutoscalerV2Beta2Factory,
},
}

Expand Down
11 changes: 11 additions & 0 deletions releasenotes-dca/ksm-support-hpa-v2beta2-e745b1d2519a7d6c.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Each section from every release note are combined when the
# CHANGELOG.rst is rendered. So the text needs to be worded so that
# it does not depend on any information only available in another
# section. This may mean repeating some details, but each section
# must be readable independently of the other.
#
# Each section note must be formatted as reStructuredText.
---
fixes:
- |
Fixes the KSM check to support HPA v2beta2 again. This stopped working in Agent v7.44.0.
11 changes: 11 additions & 0 deletions releasenotes/notes/ksm-support-hpa-v2beta2-e745b1d2519a7d6c.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Each section from every release note are combined when the
# CHANGELOG.rst is rendered. So the text needs to be worded so that
# it does not depend on any information only available in another
# section. This may mean repeating some details, but each section
# must be readable independently of the other.
#
# Each section note must be formatted as reStructuredText.
---
fixes:
- |
Fixes the KSM check to support HPA v2beta2 again. This stopped working in Agent v7.44.0.

0 comments on commit 0ef845e

Please sign in to comment.