Skip to content

Commit

Permalink
Fix: avoid panic in ksm-core ingresses collector
Browse files Browse the repository at this point in the history
* Update kube-state-metrics dependencies to v2.7.0 to benefit from
  upstream fixes.
* Add kube-state-metrics patch on top of v.2.7.0 to make this version
  usage in the kubernetes_state_core check.
  • Loading branch information
clamoriniere committed Feb 15, 2023
1 parent 36e51dd commit efa8a54
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 29 deletions.
8 changes: 6 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -230,13 +230,13 @@ require (
k8s.io/apiextensions-apiserver v0.25.5
k8s.io/apimachinery v0.25.5
k8s.io/apiserver v0.25.5
k8s.io/autoscaler/vertical-pod-autoscaler v0.10.0
k8s.io/autoscaler/vertical-pod-autoscaler v0.12.0
k8s.io/client-go v0.25.5
k8s.io/cri-api v0.25.5 // Cannot be upgraded to 0.26 without lossing CRI API v1alpha2
k8s.io/klog v1.0.1-0.20200310124935-4ad0115ba9e4 // Min version that includes fix for Windows Nano
k8s.io/klog/v2 v2.80.1
k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280
k8s.io/kube-state-metrics/v2 v2.4.2
k8s.io/kube-state-metrics/v2 v2.7.0
k8s.io/kubelet v0.25.5
k8s.io/metrics v0.25.5
k8s.io/utils v0.0.0-20221108210102-8e77b1f39fe2
Expand Down Expand Up @@ -574,6 +574,10 @@ replace github.com/golang/glog v1.0.0 => github.com/paulcacheux/glog v1.0.1-0.20

replace github.com/vishvananda/netlink => github.com/DataDog/netlink v1.0.1-0.20220504230202-f7323aba1f6c

// Replace kube-state-metrics repo until https://github.com/kubernetes/kube-state-metrics/pull/1994 is merged and cherry-pick on v2.7.1
// Else we will need to wait v2.9.0 release.
replace k8s.io/kube-state-metrics/v2 => github.com/clamoriniere/kube-state-metrics/v2 v2.0.0-20230215123345-97ec322d24fd

// Use custom Trivy fork to reduce binary size
// Pull in replacements needed by upstream Trivy
replace (
Expand Down
13 changes: 4 additions & 9 deletions go.sum

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

6 changes: 4 additions & 2 deletions pkg/collector/corechecks/cluster/ksm/kubernetes_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,9 @@ func (k *KSMCheck) Configure(integrationConfigDigest uint64, config, initConfig
allowedLabels[collector] = []string{"*"}
}

builder.WithAllowLabels(allowedLabels)
if err = builder.WithAllowLabels(allowedLabels); err != nil {
return err
}

// Enable exposing resource annotations explicitly for kube_<resource>_annotations metadata metrics.
// Equivalent to configuring --metric-annotations-allowlist.
Expand All @@ -263,7 +265,7 @@ func (k *KSMCheck) Configure(integrationConfigDigest uint64, config, initConfig
namespaces = options.DefaultNamespaces
}

builder.WithNamespaces(namespaces, "")
builder.WithNamespaces(namespaces)

allowDenyList, err := allowdenylist.New(options.MetricSet{}, buildDeniedMetricsSet(collectors))
if err != nil {
Expand Down
37 changes: 21 additions & 16 deletions pkg/kubestatemetrics/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ import (
type Builder struct {
ksmBuilder ksmtypes.BuilderInterface

kubeClient clientset.Interface
vpaClient vpaclientset.Interface
namespaces options.NamespaceList
namespaceFilter string
ctx context.Context
allowDenyList generator.FamilyGeneratorFilter
metrics *watch.ListWatchMetrics
shard int32
totalShards int
kubeClient clientset.Interface
vpaClient vpaclientset.Interface
namespaces options.NamespaceList
fieldSelectorFilter string
ctx context.Context
allowDenyList generator.FamilyGeneratorFilter
metrics *watch.ListWatchMetrics
shard int32
totalShards int

resync time.Duration
}
Expand All @@ -55,10 +55,9 @@ func New() *Builder {
}

// WithNamespaces sets the namespaces property of a Builder.
func (b *Builder) WithNamespaces(nss options.NamespaceList, nsFilter string) {
func (b *Builder) WithNamespaces(nss options.NamespaceList) {
b.namespaces = nss
b.namespaceFilter = nsFilter
b.ksmBuilder.WithNamespaces(nss, nsFilter)
b.ksmBuilder.WithNamespaces(nss)
}

// WithFamilyGeneratorFilter configures the white or blacklisted metric to be
Expand All @@ -68,6 +67,12 @@ func (b *Builder) WithFamilyGeneratorFilter(l generator.FamilyGeneratorFilter) {
b.ksmBuilder.WithFamilyGeneratorFilter(l)
}

// WithFieldSelectorFilter sets the fieldSelector property of a Builder.
func (b *Builder) WithFieldSelectorFilter(fieldSelectors string) {
b.fieldSelectorFilter = fieldSelectors
b.ksmBuilder.WithFieldSelectorFilter(fieldSelectors)
}

// WithSharding sets the shard and totalShards property of a Builder.
func (b *Builder) WithSharding(shard int32, totalShards int) {
b.shard = shard
Expand Down Expand Up @@ -130,8 +135,8 @@ func (b *Builder) WithCustomResourceStoreFactories(fs ...customresource.Registry
}

// WithAllowLabels configures which labels can be returned for metrics
func (b *Builder) WithAllowLabels(l map[string][]string) {
b.ksmBuilder.WithAllowLabels(l)
func (b *Builder) WithAllowLabels(l map[string][]string) error {
return b.ksmBuilder.WithAllowLabels(l)
}

// WithAllowAnnotations configures which annotations can be returned for metrics
Expand Down Expand Up @@ -168,7 +173,7 @@ func (b *Builder) GenerateStores(

if b.namespaces.IsAllNamespaces() {
store := store.NewMetricsStore(composedMetricGenFuncs, reflect.TypeOf(expectedType).String())
listWatcher := listWatchFunc(b.kubeClient, corev1.NamespaceAll, b.namespaceFilter)
listWatcher := listWatchFunc(b.kubeClient, corev1.NamespaceAll, b.fieldSelectorFilter)
b.startReflector(expectedType, store, listWatcher)
return []cache.Store{store}

Expand All @@ -177,7 +182,7 @@ func (b *Builder) GenerateStores(
stores := make([]cache.Store, 0, len(b.namespaces))
for _, ns := range b.namespaces {
store := store.NewMetricsStore(composedMetricGenFuncs, reflect.TypeOf(expectedType).String())
listWatcher := listWatchFunc(b.kubeClient, ns, b.namespaceFilter)
listWatcher := listWatchFunc(b.kubeClient, ns, b.fieldSelectorFilter)
b.startReflector(expectedType, store, listWatcher)
stores = append(stores, store)
}
Expand Down

0 comments on commit efa8a54

Please sign in to comment.