Skip to content

Commit

Permalink
[CONTINT-4412] Switch back to static client and structured types for …
Browse files Browse the repository at this point in the history
…KSM check (#30914)
  • Loading branch information
L3n41c authored Nov 8, 2024
1 parent be5f444 commit 92bf87a
Show file tree
Hide file tree
Showing 12 changed files with 157 additions and 230 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -1074,3 +1074,6 @@ replace github.com/ProtonMail/go-crypto => github.com/ProtonMail/go-crypto v1.0.
exclude go.opentelemetry.io/proto/otlp v1.1.0

replace github.com/google/gopacket v1.1.19 => github.com/DataDog/gopacket v0.0.0-20240626205202-4ac4cee31f14

// Remove once https://github.com/kubernetes/kube-state-metrics/pull/2553 is merged
replace k8s.io/kube-state-metrics/v2 v2.13.1-0.20241025121156-110f03d7331f => github.com/L3n41c/kube-state-metrics/v2 v2.13.1-0.20241108192007-8859a4289d92
4 changes: 2 additions & 2 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 16 additions & 23 deletions pkg/collector/corechecks/cluster/ksm/customresources/apiservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,15 @@ package customresources
import (
"context"

"github.com/DataDog/datadog-agent/pkg/util/log"
"github.com/DataDog/datadog-agent/pkg/util/kubernetes/apiserver"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
basemetrics "k8s.io/component-base/metrics"
v1 "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1"
apiregistrationclient "k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset/typed/apiregistration/v1"
"k8s.io/kube-state-metrics/v2/pkg/customresource"
"k8s.io/kube-state-metrics/v2/pkg/metric"
generator "k8s.io/kube-state-metrics/v2/pkg/metric_generator"
Expand All @@ -35,22 +33,18 @@ var (
)

// NewAPIServiceFactory returns a new APIService metric family generator factory.
func NewAPIServiceFactory(client *dynamic.DynamicClient) customresource.RegistryFactory {
func NewAPIServiceFactory(client *apiserver.APIClient) customresource.RegistryFactory {
return &apiserviceFactory{
client: client,
client: client.APISInformerClient,
}
}

type apiserviceFactory struct {
client *dynamic.DynamicClient
client apiregistrationclient.ApiregistrationV1Interface
}

func (f *apiserviceFactory) CreateClient(_ *rest.Config) (interface{}, error) {
return f.client.Resource(schema.GroupVersionResource{
Group: v1.GroupName,
Version: v1.SchemeGroupVersion.Version,
Resource: "apiservices",
}), nil
return f.client, nil
}

func (f *apiserviceFactory) Name() string {
Expand Down Expand Up @@ -126,33 +120,32 @@ func (f *apiserviceFactory) MetricFamilyGenerators() []generator.FamilyGenerator
}

func (f *apiserviceFactory) ExpectedType() interface{} {
u := unstructured.Unstructured{}
u.SetGroupVersionKind(v1.SchemeGroupVersion.WithKind("APIService"))
return &u
return &v1.APIService{
TypeMeta: metav1.TypeMeta{
Kind: "APIService",
APIVersion: v1.SchemeGroupVersion.String(),
},
}
}

func (f *apiserviceFactory) ListWatch(customresourceClient interface{}, _ string, fieldSelector string) cache.ListerWatcher {
client := customresourceClient.(dynamic.ResourceInterface)
client := customresourceClient.(apiregistrationclient.ApiregistrationV1Interface)
ctx := context.Background()
return &cache.ListWatch{
ListFunc: func(opts metav1.ListOptions) (runtime.Object, error) {
opts.FieldSelector = fieldSelector
return client.List(ctx, opts)
return client.APIServices().List(ctx, opts)
},
WatchFunc: func(opts metav1.ListOptions) (watch.Interface, error) {
opts.FieldSelector = fieldSelector
return client.Watch(ctx, opts)
return client.APIServices().Watch(ctx, opts)
},
}
}

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

metricFamily := f(apiservice)

Expand Down
40 changes: 17 additions & 23 deletions pkg/collector/corechecks/cluster/ksm/customresources/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,20 @@ package customresources
import (
"context"

"github.com/DataDog/datadog-agent/pkg/util/log"
"github.com/DataDog/datadog-agent/pkg/util/kubernetes/apiserver"
v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
basemetrics "k8s.io/component-base/metrics"

"k8s.io/kube-state-metrics/v2/pkg/customresource"
"k8s.io/kube-state-metrics/v2/pkg/metric"
generator "k8s.io/kube-state-metrics/v2/pkg/metric_generator"

"k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
)

var (
Expand All @@ -37,14 +36,14 @@ var (

// NewCustomResourceDefinitionFactory returns a new CustomResourceDefinition
// metric family generator factory.
func NewCustomResourceDefinitionFactory(client *dynamic.DynamicClient) customresource.RegistryFactory {
func NewCustomResourceDefinitionFactory(client *apiserver.APIClient) customresource.RegistryFactory {
return &crdFactory{
client: client,
client: client.CRDInformerClient,
}
}

type crdFactory struct {
client *dynamic.DynamicClient
client clientset.Interface
}

func (f *crdFactory) MetricFamilyGenerators() []generator.FamilyGenerator {
Expand Down Expand Up @@ -120,41 +119,36 @@ func (f *crdFactory) Name() string {
}

func (f *crdFactory) CreateClient(_ *rest.Config) (interface{}, error) {
return f.client.Resource(schema.GroupVersionResource{
Group: v1.GroupName,
Version: v1.SchemeGroupVersion.Version,
Resource: "customresourcedefinitions",
}), nil
return f.client, nil
}

func (f *crdFactory) ExpectedType() interface{} {
u := unstructured.Unstructured{}
u.SetGroupVersionKind(v1.SchemeGroupVersion.WithKind("CustomResourceDefinition"))
return &u
return &v1.CustomResourceDefinition{
TypeMeta: metav1.TypeMeta{
Kind: "CustomResourceDefinition",
APIVersion: v1.SchemeGroupVersion.String(),
},
}
}

func (f *crdFactory) ListWatch(customResourceClient interface{}, _ string, fieldSelector string) cache.ListerWatcher {
client := customResourceClient.(dynamic.ResourceInterface)
client := customResourceClient.(clientset.Interface)
ctx := context.Background()
return &cache.ListWatch{
ListFunc: func(opts metav1.ListOptions) (runtime.Object, error) {
opts.FieldSelector = fieldSelector
return client.List(ctx, opts)
return client.ApiextensionsV1().CustomResourceDefinitions().List(ctx, opts)
},
WatchFunc: func(opts metav1.ListOptions) (watch.Interface, error) {
opts.FieldSelector = fieldSelector
return client.Watch(ctx, opts)
return client.ApiextensionsV1().CustomResourceDefinitions().Watch(ctx, opts)
},
}
}

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

metricFamily := f(crd)

Expand Down
39 changes: 16 additions & 23 deletions pkg/collector/corechecks/cluster/ksm/customresources/cronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@ import (
"context"
"time"

"github.com/DataDog/datadog-agent/pkg/util/log"
"github.com/DataDog/datadog-agent/pkg/util/kubernetes/apiserver"
"github.com/pkg/errors"
"github.com/robfig/cron/v3"
batchv1beta1 "k8s.io/api/batch/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
basemetrics "k8s.io/component-base/metrics"
Expand All @@ -44,26 +42,22 @@ var (
)

// NewCronJobV1Beta1Factory returns a new CronJob metric family generator factory.
func NewCronJobV1Beta1Factory(client *dynamic.DynamicClient) customresource.RegistryFactory {
func NewCronJobV1Beta1Factory(client *apiserver.APIClient) customresource.RegistryFactory {
return &cronjobv1beta1Factory{
client: client,
client: client.Cl,
}
}

type cronjobv1beta1Factory struct {
client *dynamic.DynamicClient
client kubernetes.Interface
}

func (f *cronjobv1beta1Factory) Name() string {
return "cronjobs"
}

func (f *cronjobv1beta1Factory) CreateClient(_ *rest.Config) (interface{}, error) {
return f.client.Resource(schema.GroupVersionResource{
Group: batchv1beta1.GroupName,
Version: batchv1beta1.SchemeGroupVersion.Version,
Resource: "cronjobs",
}), nil
return f.client, nil
}

func (f *cronjobv1beta1Factory) MetricFamilyGenerators() []generator.FamilyGenerator {
Expand Down Expand Up @@ -317,11 +311,7 @@ func (f *cronjobv1beta1Factory) MetricFamilyGenerators() []generator.FamilyGener

func wrapCronJobFunc(f func(*batchv1beta1.CronJob) *metric.Family) func(interface{}) *metric.Family {
return func(obj interface{}) *metric.Family {
cronJob := &batchv1beta1.CronJob{}
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.(*unstructured.Unstructured).Object, cronJob); err != nil {
log.Warnf("cannot decode object %q into batchv1beta1.CronJob, err=%s, skipping", obj.(*unstructured.Unstructured).Object["apiVersion"], err)
return nil
}
cronJob := obj.(*batchv1beta1.CronJob)

metricFamily := f(cronJob)

Expand All @@ -334,22 +324,25 @@ func wrapCronJobFunc(f func(*batchv1beta1.CronJob) *metric.Family) func(interfac
}

func (f *cronjobv1beta1Factory) ExpectedType() interface{} {
u := unstructured.Unstructured{}
u.SetGroupVersionKind(batchv1beta1.SchemeGroupVersion.WithKind("CronJob"))
return &u
return &batchv1beta1.CronJob{
TypeMeta: metav1.TypeMeta{
Kind: "CronJob",
APIVersion: batchv1beta1.SchemeGroupVersion.String(),
},
}
}

func (f *cronjobv1beta1Factory) ListWatch(customResourceClient interface{}, ns string, fieldSelector string) cache.ListerWatcher {
client := customResourceClient.(dynamic.NamespaceableResourceInterface).Namespace(ns)
client := customResourceClient.(kubernetes.Interface)
ctx := context.Background()
return &cache.ListWatch{
ListFunc: func(opts metav1.ListOptions) (runtime.Object, error) {
opts.FieldSelector = fieldSelector
return client.List(ctx, opts)
return client.BatchV1beta1().CronJobs(ns).List(ctx, opts)
},
WatchFunc: func(opts metav1.ListOptions) (watch.Interface, error) {
opts.FieldSelector = fieldSelector
return client.Watch(ctx, opts)
return client.BatchV1beta1().CronJobs(ns).Watch(ctx, opts)
},
}
}
Expand Down
39 changes: 16 additions & 23 deletions pkg/collector/corechecks/cluster/ksm/customresources/hpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ package customresources
import (
"context"

"github.com/DataDog/datadog-agent/pkg/util/log"
"github.com/DataDog/datadog-agent/pkg/util/kubernetes/apiserver"
autoscaling "k8s.io/api/autoscaling/v2beta2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
basemetrics "k8s.io/component-base/metrics"
Expand Down Expand Up @@ -60,26 +58,22 @@ var (

// NewHorizontalPodAutoscalerV2Beta2Factory returns a new
// HorizontalPodAutoscaler metric family generator factory.
func NewHorizontalPodAutoscalerV2Beta2Factory(client *dynamic.DynamicClient) customresource.RegistryFactory {
func NewHorizontalPodAutoscalerV2Beta2Factory(client *apiserver.APIClient) customresource.RegistryFactory {
return &hpav2Factory{
client: client,
client: client.Cl,
}
}

type hpav2Factory struct {
client *dynamic.DynamicClient
client kubernetes.Interface
}

func (f *hpav2Factory) Name() string {
return "horizontalpodautoscalers"
}

func (f *hpav2Factory) CreateClient(_ *rest.Config) (interface{}, error) {
return f.client.Resource(schema.GroupVersionResource{
Group: autoscaling.GroupName,
Version: autoscaling.SchemeGroupVersion.Version,
Resource: "horizontalpodautoscalers",
}), nil
return f.client, nil
}

func (f *hpav2Factory) MetricFamilyGenerators() []generator.FamilyGenerator {
Expand Down Expand Up @@ -374,33 +368,32 @@ func (f *hpav2Factory) MetricFamilyGenerators() []generator.FamilyGenerator {
}

func (f *hpav2Factory) ExpectedType() interface{} {
u := unstructured.Unstructured{}
u.SetGroupVersionKind(autoscaling.SchemeGroupVersion.WithKind("HorizontalPodAutoscaler"))
return &u
return &autoscaling.HorizontalPodAutoscaler{
TypeMeta: metav1.TypeMeta{
Kind: "HorizontalPodAutoscaler",
APIVersion: autoscaling.SchemeGroupVersion.String(),
},
}
}

func (f *hpav2Factory) ListWatch(customResourceClient interface{}, ns string, fieldSelector string) cache.ListerWatcher {
client := customResourceClient.(dynamic.NamespaceableResourceInterface).Namespace(ns)
client := customResourceClient.(kubernetes.Interface)
ctx := context.Background()
return &cache.ListWatch{
ListFunc: func(opts metav1.ListOptions) (runtime.Object, error) {
opts.FieldSelector = fieldSelector
return client.List(ctx, opts)
return client.AutoscalingV2beta2().HorizontalPodAutoscalers(ns).List(ctx, opts)
},
WatchFunc: func(opts metav1.ListOptions) (watch.Interface, error) {
opts.FieldSelector = fieldSelector
return client.Watch(ctx, opts)
return client.AutoscalingV2beta2().HorizontalPodAutoscalers(ns).Watch(ctx, opts)
},
}
}

func wrapHPAFunc(f func(*autoscaling.HorizontalPodAutoscaler) *metric.Family) func(interface{}) *metric.Family {
return func(obj interface{}) *metric.Family {
hpa := &autoscaling.HorizontalPodAutoscaler{}
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.(*unstructured.Unstructured).Object, hpa); err != nil {
log.Warnf("cannot decode object %q into autoscaling/v2beta2.HorizontalPodAutoscaler, err=%s, skipping", obj.(*unstructured.Unstructured).Object["apiVersion"], err)
return nil
}
hpa := obj.(*autoscaling.HorizontalPodAutoscaler)

metricFamily := f(hpa)

Expand Down
Loading

0 comments on commit 92bf87a

Please sign in to comment.