From 3cec25fa8512281ca8ceeafc40e5278de0b7f583 Mon Sep 17 00:00:00 2001 From: jbeemster Date: Wed, 28 Aug 2024 11:44:58 +0200 Subject: [PATCH] Add support for controller-name use with StatefulSet (closes #33507) --- .../awscontainerinsightreceiver/config.go | 5 ++++ .../awscontainerinsightreceiver/factory.go | 14 ++++++---- .../internal/stores/podstore.go | 28 +++++++++++-------- .../internal/stores/store.go | 4 +-- .../awscontainerinsightreceiver/receiver.go | 2 +- 5 files changed, 34 insertions(+), 19 deletions(-) diff --git a/receiver/awscontainerinsightreceiver/config.go b/receiver/awscontainerinsightreceiver/config.go index 24cca3dcf3f2..5f60d17062a6 100644 --- a/receiver/awscontainerinsightreceiver/config.go +++ b/receiver/awscontainerinsightreceiver/config.go @@ -28,4 +28,9 @@ type Config struct { // If false FullPodName label is not added // The default value is false AddFullPodNameMetricLabel bool `mapstructure:"add_full_pod_name_metric_label"` + + // By default a StatefulSet will always use its full "PodName" when storing metrics. + // If true this will instead use the name of the relevant controller instead to match Deployment, Daemonset, Job, ... + // The default value is false + PrefContollerNameForStatefulSet bool `mapstructure:"prefer_controller_name_for_stateful_set"` } diff --git a/receiver/awscontainerinsightreceiver/factory.go b/receiver/awscontainerinsightreceiver/factory.go index 9134ba9fcf94..bccb006d228d 100644 --- a/receiver/awscontainerinsightreceiver/factory.go +++ b/receiver/awscontainerinsightreceiver/factory.go @@ -30,6 +30,9 @@ const ( // Don't tag pod full name by default defaultAddFullPodNameMetricLabel = false + + // Don't use controller name for StatefulSet metrics by default + defaultPrefContollerNameForStatefulSet = false ) // NewFactory creates a factory for AWS container insight receiver @@ -43,11 +46,12 @@ func NewFactory() receiver.Factory { // createDefaultConfig returns a default config for the receiver. func createDefaultConfig() component.Config { return &Config{ - CollectionInterval: defaultCollectionInterval, - ContainerOrchestrator: defaultContainerOrchestrator, - TagService: defaultTagService, - PrefFullPodName: defaultPrefFullPodName, - AddFullPodNameMetricLabel: defaultAddFullPodNameMetricLabel, + CollectionInterval: defaultCollectionInterval, + ContainerOrchestrator: defaultContainerOrchestrator, + TagService: defaultTagService, + PrefFullPodName: defaultPrefFullPodName, + AddFullPodNameMetricLabel: defaultAddFullPodNameMetricLabel, + PrefContollerNameForStatefulSet: defaultPrefContollerNameForStatefulSet, } } diff --git a/receiver/awscontainerinsightreceiver/internal/stores/podstore.go b/receiver/awscontainerinsightreceiver/internal/stores/podstore.go index bf464069a349..7ea4976e7530 100644 --- a/receiver/awscontainerinsightreceiver/internal/stores/podstore.go +++ b/receiver/awscontainerinsightreceiver/internal/stores/podstore.go @@ -102,10 +102,11 @@ type PodStore struct { prefFullPodName bool logger *zap.Logger sync.Mutex - addFullPodNameMetricLabel bool + addFullPodNameMetricLabel bool + prefControllerNameForStatefulSet bool } -func NewPodStore(hostIP string, prefFullPodName bool, addFullPodNameMetricLabel bool, logger *zap.Logger) (*PodStore, error) { +func NewPodStore(hostIP string, prefFullPodName bool, addFullPodNameMetricLabel bool, prefControllerNameForStatefulSet bool, logger *zap.Logger) (*PodStore, error) { podClient, err := kubeletutil.NewKubeletClient(hostIP, ci.KubeSecurePort, logger) if err != nil { return nil, err @@ -122,14 +123,15 @@ func NewPodStore(hostIP string, prefFullPodName bool, addFullPodNameMetricLabel } podStore := &PodStore{ - cache: newMapWithExpiry(podsExpiry), - prevMeasurements: make(map[string]*mapWithExpiry), - podClient: podClient, - nodeInfo: newNodeInfo(logger), - prefFullPodName: prefFullPodName, - k8sClient: k8sClient, - logger: logger, - addFullPodNameMetricLabel: addFullPodNameMetricLabel, + cache: newMapWithExpiry(podsExpiry), + prevMeasurements: make(map[string]*mapWithExpiry), + podClient: podClient, + nodeInfo: newNodeInfo(logger), + prefFullPodName: prefFullPodName, + k8sClient: k8sClient, + logger: logger, + addFullPodNameMetricLabel: addFullPodNameMetricLabel, + prefControllerNameForStatefulSet: prefControllerNameForStatefulSet, } return podStore, nil @@ -589,7 +591,11 @@ func (p *PodStore) addPodOwnersAndPodName(metric CIMetric, pod *corev1.Pod, kube if podName == "" { if owner.Kind == ci.StatefulSet { - podName = pod.Name + if p.prefControllerNameForStatefulSet { + podName = name + } else { + podName = pod.Name + } } else if owner.Kind == ci.DaemonSet || owner.Kind == ci.Job || owner.Kind == ci.ReplicaSet || owner.Kind == ci.ReplicationController { podName = name diff --git a/receiver/awscontainerinsightreceiver/internal/stores/store.go b/receiver/awscontainerinsightreceiver/internal/stores/store.go index da9be89624ae..c20df713a734 100644 --- a/receiver/awscontainerinsightreceiver/internal/stores/store.go +++ b/receiver/awscontainerinsightreceiver/internal/stores/store.go @@ -44,7 +44,7 @@ type K8sDecorator struct { podStore *PodStore } -func NewK8sDecorator(ctx context.Context, tagService bool, prefFullPodName bool, addFullPodNameMetricLabel bool, logger *zap.Logger) (*K8sDecorator, error) { +func NewK8sDecorator(ctx context.Context, tagService bool, prefFullPodName bool, addFullPodNameMetricLabel bool, prefControllerNameForStatefulSet bool, logger *zap.Logger) (*K8sDecorator, error) { hostIP := os.Getenv("HOST_IP") if hostIP == "" { return nil, errors.New("environment variable HOST_IP is not set in k8s deployment config") @@ -54,7 +54,7 @@ func NewK8sDecorator(ctx context.Context, tagService bool, prefFullPodName bool, ctx: ctx, } - podstore, err := NewPodStore(hostIP, prefFullPodName, addFullPodNameMetricLabel, logger) + podstore, err := NewPodStore(hostIP, prefFullPodName, addFullPodNameMetricLabel, prefControllerNameForStatefulSet, logger) if err != nil { return nil, err diff --git a/receiver/awscontainerinsightreceiver/receiver.go b/receiver/awscontainerinsightreceiver/receiver.go index 01dd1deedf94..08391760099a 100644 --- a/receiver/awscontainerinsightreceiver/receiver.go +++ b/receiver/awscontainerinsightreceiver/receiver.go @@ -63,7 +63,7 @@ func (acir *awsContainerInsightReceiver) Start(ctx context.Context, host compone } if acir.config.ContainerOrchestrator == ci.EKS { - k8sDecorator, err := stores.NewK8sDecorator(ctx, acir.config.TagService, acir.config.PrefFullPodName, acir.config.AddFullPodNameMetricLabel, acir.settings.Logger) + k8sDecorator, err := stores.NewK8sDecorator(ctx, acir.config.TagService, acir.config.PrefFullPodName, acir.config.AddFullPodNameMetricLabel, acir.config.PrefControllerNameForStatefulSet, acir.settings.Logger) if err != nil { return err }