From 6c5d64203b6d607bb827aae35721eb31564a10ab Mon Sep 17 00:00:00 2001 From: timggggggg Date: Tue, 28 Oct 2025 20:02:03 +0300 Subject: [PATCH 01/18] K8s pod meta caching --- plugin/input/k8s/k8s.go | 7 +++ plugin/input/k8s/meta/gatherer.go | 93 ++++++++++++++++++++++++++++++- 2 files changed, 97 insertions(+), 3 deletions(-) diff --git a/plugin/input/k8s/k8s.go b/plugin/input/k8s/k8s.go index 3f2184694..29c72d26b 100644 --- a/plugin/input/k8s/k8s.go +++ b/plugin/input/k8s/k8s.go @@ -137,6 +137,12 @@ type Config struct { // > // > Example: ```component: '{{ index .pod.Labels "component" | default .k8s_container }}'``` K8sMeta cfg.MetaTemplates `json:"meta"` // * + + // > @3@4@5@6 + // > + // > K8sMeta file + // > + K8sMetaFile string `json:"meta_file" default:""` // * } var startCounter atomic.Int32 @@ -176,6 +182,7 @@ func (p *Plugin) Start(config pipeline.AnyConfig, params *pipeline.InputPluginPa if startCounter == 1 { meta.DeletedPodsCacheSize = p.config.DeletedPodsCacheSize + meta.MetaFileSaver = meta.NewMetaSaver(p.config.K8sMetaFile) meta.EnableGatherer(p.logger) } diff --git a/plugin/input/k8s/meta/gatherer.go b/plugin/input/k8s/meta/gatherer.go index 72e04ec1c..9eaa61278 100644 --- a/plugin/input/k8s/meta/gatherer.go +++ b/plugin/input/k8s/meta/gatherer.go @@ -2,6 +2,9 @@ package meta import ( "context" + "encoding/json" + "fmt" + "math/rand/v2" "os" "path/filepath" "strings" @@ -9,6 +12,7 @@ import ( "time" lru "github.com/hashicorp/golang-lru/v2" + "github.com/ozontech/file.d/logger" "go.uber.org/atomic" "go.uber.org/zap" corev1 "k8s.io/api/core/v1" @@ -20,6 +24,8 @@ import ( "k8s.io/client-go/tools/clientcmd" ) +const metaFileTempSuffix = ".atomic" + type ( PodName string Namespace string @@ -42,9 +48,10 @@ type ( ) var ( - client *kubernetes.Clientset - MetaData = make(meta) - metaDataMu = &sync.RWMutex{} + client *kubernetes.Clientset + MetaData = make(meta) + metaDataMu = &sync.RWMutex{} + MetaFileSaver = &MetaSaver{} DeletedPodsCacheSize = 1024 deletedPodsCache *lru.Cache[PodName, bool] // to mark deleted pods or for which we are miss k8s meta and don't wanna wait for timeout for each event @@ -84,6 +91,8 @@ func EnableGatherer(l *zap.SugaredLogger) { localLogger = l localLogger.Info("enabling k8s meta gatherer") + MetaFileSaver.loadLimits() + var err error deletedPodsCache, err = lru.New[PodName, bool](DeletedPodsCacheSize) if err != nil { @@ -101,6 +110,7 @@ func EnableGatherer(l *zap.SugaredLogger) { func DisableGatherer() { localLogger.Info("disabling k8s meta gatherer") + MetaFileSaver.saveMetaFile() if !DisableMetaUpdates { informerStop <- struct{}{} } @@ -217,6 +227,7 @@ func maintenance() { default: time.Sleep(MaintenanceInterval) removeExpired() + MetaFileSaver.saveMetaFile() } } } @@ -399,3 +410,79 @@ func getNamespace() string { } return strings.TrimSpace(string(data)) } + +type MetaSaver struct { + metaFile string + metaFileTmp string +} + +func NewMetaSaver(metaFile string) *MetaSaver { + return &MetaSaver{ + metaFile: metaFile, + metaFileTmp: metaFile + metaFileTempSuffix, + } +} + +func (ms *MetaSaver) saveMetaFile() { + tmpWithRandom := fmt.Sprintf("%s.%08d", ms.metaFileTmp, rand.Uint32()) + + file, err := os.OpenFile(tmpWithRandom, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o600) + if err != nil { + localLogger.Errorf("can't open k8s meta file %s, %s", ms.metaFileTmp, err.Error()) + return + } + defer func() { + err := file.Close() + if err != nil { + localLogger.Errorf("can't close k8s meta file %s, %s", ms.metaFileTmp, err.Error()) + } + }() + + metaDataMu.RLock() + buf, err := json.MarshalIndent(MetaData, "", " ") + metaDataMu.RUnlock() + if err != nil { + localLogger.Errorf("can't marshall k8s meta map into json: %s", err.Error()) + } + + _, err = file.Write(buf) + if err != nil { + localLogger.Errorf("can't write k8s meta file %s, %s", ms.metaFileTmp, err.Error()) + } + + err = file.Sync() + if err != nil { + localLogger.Errorf("can't sync k8s meta file %s, %s", ms.metaFileTmp, err.Error()) + } + + err = os.Rename(tmpWithRandom, ms.metaFile) + if err != nil { + logger.Errorf("failed renaming temporary k8s meta file to current: %s", err.Error()) + } +} + +func (ms *MetaSaver) loadLimits() error { + info, err := os.Stat(ms.metaFile) + if os.IsNotExist(err) { + return nil + } + + if info.IsDir() { + return fmt.Errorf("file %s is dir", ms.metaFile) + } + + data, err := os.ReadFile(ms.metaFile) + if err != nil { + return fmt.Errorf("can't read k8s meta file: %w", err) + } + + if len(data) == 0 { + return nil + } + + if err := json.Unmarshal(data, &MetaData); err != nil { + return fmt.Errorf("can't unmarshal map: %w", err) + } + + return nil +} From f97596f7c9abd846faffabda85e57e6df3adf251 Mon Sep 17 00:00:00 2001 From: timggggggg Date: Tue, 28 Oct 2025 20:22:50 +0300 Subject: [PATCH 02/18] fix doc, fix lint --- plugin/input/k8s/README.md | 6 ++++++ plugin/input/k8s/k8s.go | 3 +-- plugin/input/k8s/meta/gatherer.go | 12 +++++++++--- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/plugin/input/k8s/README.md b/plugin/input/k8s/README.md index e22ce942a..27aca503d 100755 --- a/plugin/input/k8s/README.md +++ b/plugin/input/k8s/README.md @@ -116,6 +116,12 @@ Example: ```component: '{{ index .pod.Labels "component" | default .k8s_containe
+**`meta_file`** *`string`* + +The filename to store current k8s pod meta. Meta are loaded only on initialization + +
+ ### Meta params **`pod_name`** - string diff --git a/plugin/input/k8s/k8s.go b/plugin/input/k8s/k8s.go index 29c72d26b..746b4ff8e 100644 --- a/plugin/input/k8s/k8s.go +++ b/plugin/input/k8s/k8s.go @@ -140,8 +140,7 @@ type Config struct { // > @3@4@5@6 // > - // > K8sMeta file - // > + // > The filename to store current k8s pod meta. Meta are loaded only on initialization K8sMetaFile string `json:"meta_file" default:""` // * } diff --git a/plugin/input/k8s/meta/gatherer.go b/plugin/input/k8s/meta/gatherer.go index 9eaa61278..f48fddb28 100644 --- a/plugin/input/k8s/meta/gatherer.go +++ b/plugin/input/k8s/meta/gatherer.go @@ -91,7 +91,12 @@ func EnableGatherer(l *zap.SugaredLogger) { localLogger = l localLogger.Info("enabling k8s meta gatherer") - MetaFileSaver.loadLimits() + if MetaFileSaver.metaFile != "" { + err := MetaFileSaver.loadLimits() + if err != nil { + localLogger.Errorf("can't load limits: %s", err.Error()) + } + } var err error deletedPodsCache, err = lru.New[PodName, bool](DeletedPodsCacheSize) @@ -110,7 +115,6 @@ func EnableGatherer(l *zap.SugaredLogger) { func DisableGatherer() { localLogger.Info("disabling k8s meta gatherer") - MetaFileSaver.saveMetaFile() if !DisableMetaUpdates { informerStop <- struct{}{} } @@ -227,7 +231,9 @@ func maintenance() { default: time.Sleep(MaintenanceInterval) removeExpired() - MetaFileSaver.saveMetaFile() + if MetaFileSaver.metaFile != "" { + MetaFileSaver.saveMetaFile() + } } } } From cd41d86da756fffafaf7ac3a66637575c53a57f6 Mon Sep 17 00:00:00 2001 From: timggggggg Date: Tue, 28 Oct 2025 20:25:57 +0300 Subject: [PATCH 03/18] fix --- plugin/input/k8s/meta/gatherer.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugin/input/k8s/meta/gatherer.go b/plugin/input/k8s/meta/gatherer.go index f48fddb28..5d6dbcb43 100644 --- a/plugin/input/k8s/meta/gatherer.go +++ b/plugin/input/k8s/meta/gatherer.go @@ -92,9 +92,9 @@ func EnableGatherer(l *zap.SugaredLogger) { localLogger.Info("enabling k8s meta gatherer") if MetaFileSaver.metaFile != "" { - err := MetaFileSaver.loadLimits() + err := MetaFileSaver.loadMeta() if err != nil { - localLogger.Errorf("can't load limits: %s", err.Error()) + localLogger.Errorf("can't load k8s pod meta: %s", err.Error()) } } @@ -467,7 +467,7 @@ func (ms *MetaSaver) saveMetaFile() { } } -func (ms *MetaSaver) loadLimits() error { +func (ms *MetaSaver) loadMeta() error { info, err := os.Stat(ms.metaFile) if os.IsNotExist(err) { return nil From 268008b33b24c91a926aaadc2fa0dc8f9eda7cb0 Mon Sep 17 00:00:00 2001 From: timggggggg Date: Thu, 6 Nov 2025 19:32:56 +0300 Subject: [PATCH 04/18] update k8s client-go version --- go.mod | 29 +++++------ go.sum | 81 ++++++++++++++++--------------- plugin/input/k8s/meta/gatherer.go | 18 +++++-- 3 files changed, 72 insertions(+), 56 deletions(-) diff --git a/go.mod b/go.mod index 468b32619..4e9a37c39 100644 --- a/go.mod +++ b/go.mod @@ -50,13 +50,13 @@ require ( go.uber.org/automaxprocs v1.5.3 go.uber.org/zap v1.25.0 golang.org/x/net v0.35.0 - google.golang.org/protobuf v1.33.1-0.20240408130810-98873a205002 + google.golang.org/protobuf v1.34.2 gopkg.in/yaml.v2 v2.4.0 gopkg.in/yaml.v3 v3.0.1 - k8s.io/api v0.29.14 - k8s.io/apimachinery v0.29.14 - k8s.io/client-go v0.29.14 - sigs.k8s.io/yaml v1.3.0 + k8s.io/api v0.31.13 + k8s.io/apimachinery v0.31.13 + k8s.io/client-go v0.31.13 + sigs.k8s.io/yaml v1.4.0 ) require ( @@ -68,20 +68,21 @@ require ( github.com/cilium/ebpf v0.9.1 // indirect github.com/containerd/cgroups/v3 v3.0.1 // indirect github.com/coreos/go-systemd/v22 v22.3.2 // indirect - github.com/davecgh/go-spew v1.1.1 // indirect + github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/dmarkham/enumer v1.5.8 // indirect github.com/docker/go-units v0.4.0 // indirect github.com/emicklei/go-restful/v3 v3.11.0 // indirect + github.com/fxamacker/cbor/v2 v2.7.0 // indirect github.com/go-faster/city v1.0.1 // indirect github.com/go-faster/errors v0.6.1 // indirect github.com/go-ini/ini v1.62.0 // indirect github.com/go-jose/go-jose/v4 v4.0.4 // indirect - github.com/go-logr/logr v1.3.0 // indirect + github.com/go-logr/logr v1.4.2 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/go-openapi/jsonpointer v0.19.6 // indirect github.com/go-openapi/jsonreference v0.20.2 // indirect - github.com/go-openapi/swag v0.22.3 // indirect + github.com/go-openapi/swag v0.22.4 // indirect github.com/godbus/dbus/v5 v5.0.4 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/protobuf v1.5.4 // indirect @@ -121,7 +122,7 @@ require ( github.com/opencontainers/runtime-spec v1.0.2 // indirect github.com/pascaldekloe/name v1.0.1 // indirect github.com/pierrec/lz4/v4 v4.1.21 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect github.com/ryanuber/go-glob v1.0.0 // indirect github.com/segmentio/asm v1.2.0 // indirect github.com/sirupsen/logrus v1.8.1 // indirect @@ -132,6 +133,7 @@ require ( github.com/timtadh/data-structures v0.6.1 // indirect github.com/twmb/franz-go/pkg/kmsg v1.8.0 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect + github.com/x448/float16 v0.8.4 // indirect github.com/xdg-go/pbkdf2 v1.0.0 // indirect github.com/xdg-go/stringprep v1.0.4 // indirect github.com/yuin/gopher-lua v1.1.0 // indirect @@ -141,19 +143,18 @@ require ( go.uber.org/multierr v1.11.0 // indirect golang.org/x/crypto v0.33.0 // indirect golang.org/x/mod v0.17.0 // indirect - golang.org/x/oauth2 v0.10.0 // indirect + golang.org/x/oauth2 v0.21.0 // indirect golang.org/x/sync v0.11.0 // indirect golang.org/x/sys v0.30.0 // indirect golang.org/x/term v0.29.0 // indirect golang.org/x/text v0.22.0 // indirect golang.org/x/time v0.10.0 // indirect golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect - google.golang.org/appengine v1.6.7 // indirect gopkg.in/inf.v0 v0.9.1 // indirect gopkg.in/ini.v1 v1.62.0 // indirect - k8s.io/klog/v2 v2.110.1 // indirect - k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 // indirect - k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect + k8s.io/klog/v2 v2.130.1 // indirect + k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect + k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect ) diff --git a/go.sum b/go.sum index bb0a9091c..c6d028954 100644 --- a/go.sum +++ b/go.sum @@ -52,8 +52,9 @@ github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSV github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= github.com/dmarkham/enumer v1.5.8 h1:fIF11F9l5jyD++YYvxcSH5WgHfeaSGPaN/T4kOQ4qEM= @@ -72,6 +73,8 @@ github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM= github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE= github.com/frankban/quicktest v1.14.0 h1:+cqqvzZV87b4adx/5ayVOaYZ2CrvM4ejQvUdBzPPUss= github.com/frankban/quicktest v1.14.0/go.mod h1:NeW+ay9A/U67EYXNFA1nPE8e/tnQv/09mUdL/ijj8og= +github.com/fxamacker/cbor/v2 v2.7.0 h1:iM5WgngdRBanHcxugY4JySA0nk1wZorNOpTgCMedv5E= +github.com/fxamacker/cbor/v2 v2.7.0/go.mod h1:pxXPTn3joSm21Gbwsv0w9OSA2y1HFR9qXEeXQVeNoDQ= github.com/go-faster/city v1.0.1 h1:4WAxSZ3V2Ws4QRDrscLEDcibJY8uf41H6AhXDrNDcGw= github.com/go-faster/city v1.0.1/go.mod h1:jKcUJId49qdW3L1qKHH/3wPeUstCVpVSXTM6vO3VcTw= github.com/go-faster/errors v0.6.1 h1:nNIPOBkprlKzkThvS/0YaX8Zs9KewLCOSFQS5BU06FI= @@ -85,19 +88,20 @@ github.com/go-jose/go-jose/v4 v4.0.4/go.mod h1:NKb5HO1EZccyMpiZNbdUw/14tiXNyUJh1 github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY= github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= -github.com/go-logr/logr v1.3.0 h1:2y3SDp0ZXuc6/cjLSZ+Q3ir+QB9T/iG5yYRXqsagWSY= -github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY= +github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= github.com/go-openapi/jsonpointer v0.19.6 h1:eCs3fxoIi3Wh6vtgmLTOjdhSpiqphQ+DaPn38N2ZdrE= github.com/go-openapi/jsonpointer v0.19.6/go.mod h1:osyAmYz/mB/C3I+WsTTSgw1ONzaLJoLCyoi6/zppojs= github.com/go-openapi/jsonreference v0.20.2 h1:3sVjiK66+uXK/6oQ8xgcRKcFgQ5KXa2KvnJRumpMGbE= github.com/go-openapi/jsonreference v0.20.2/go.mod h1:Bl1zwGIM8/wsvqjsOQLJ/SH+En5Ap4rVB5KVcIDZG2k= -github.com/go-openapi/swag v0.22.3 h1:yMBqmnQ0gyZvEb/+KzuWZOXgllrXT4SADYbvDaXHv/g= github.com/go-openapi/swag v0.22.3/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14= +github.com/go-openapi/swag v0.22.4 h1:QLMzNJnMGPRNDCbySlcj1x01tzU8/9LTTL9hZZZogBU= +github.com/go-openapi/swag v0.22.4/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= -github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI= -github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls= +github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1vB6EwHI= +github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8= github.com/go-test/deep v1.0.2 h1:onZX1rnHT3Wv6cqNgYyFOOlgVKJrksuCMCRvJStbMYw= github.com/go-test/deep v1.0.2/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA= github.com/godbus/dbus/v5 v5.0.4 h1:9349emZab16e7zQvpmsbtjc18ykshndd8y2PG3sgJbA= @@ -109,7 +113,6 @@ github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69 github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc= github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.5/go.mod h1:6O5/vntMXwX2lRkT1hjjk0nAC1IDOTvTlVgjlRvqsdk= github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= @@ -121,8 +124,8 @@ github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0= github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= -github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 h1:K6RDEckDVWvDI9JAJYCmNdQXq6neHJOYx3V6jnqNEec= -github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= +github.com/google/pprof v0.0.0-20240525223248-4bfdf5a9a2af h1:kmjWCqn2qkEml422C2Rrd27c3VGxi6a/6HNq8QmHRKM= +github.com/google/pprof v0.0.0-20240525223248-4bfdf5a9a2af/go.mod h1:K1liHPHnj73Fdn/EKuT8nrFqBihUSKXoLYU0BuatOYo= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= @@ -268,10 +271,10 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= -github.com/onsi/ginkgo/v2 v2.13.0 h1:0jY9lJquiL8fcf3M4LAXN5aMlS/b2BV86HFFPCPMgE4= -github.com/onsi/ginkgo/v2 v2.13.0/go.mod h1:TE309ZR8s5FsKKpuB1YAQYBzCaAfUgatB/xlT/ETL/o= -github.com/onsi/gomega v1.29.0 h1:KIA/t2t5UBzoirT4H9tsML45GEbo3ouUnBHsCfD2tVg= -github.com/onsi/gomega v1.29.0/go.mod h1:9sxs+SwGrKI0+PWe4Fxa9tFQQBG5xSsSbMXOI8PPpoQ= +github.com/onsi/ginkgo/v2 v2.19.0 h1:9Cnnf7UHo57Hy3k6/m5k3dRfGTMXGvxhHFvkDTCTpvA= +github.com/onsi/ginkgo/v2 v2.19.0/go.mod h1:rlwLi9PilAFJ8jCg9UE1QP6VBpd6/xj3SRC0d6TU0To= +github.com/onsi/gomega v1.19.0 h1:4ieX6qQjPP/BfC3mpsAtIGGlxTWPeA3Inl/7DtXw1tw= +github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro= github.com/opencontainers/runtime-spec v1.0.2 h1:UfAcuLBJB9Coz72x1hgl8O5RVzTdNiaglX6v2DM6FI0= github.com/opencontainers/runtime-spec v1.0.2/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0= github.com/ozontech/insane-json v0.1.9 h1:JG5cEsmuSDwmU7KTJTHfTJ40XMgvtPdsUQbXdbPv+bY= @@ -283,8 +286,9 @@ github.com/pierrec/lz4/v4 v4.1.21/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFu github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prashantv/gostub v1.1.0 h1:BTyx3RfQjRHnUWaGF9oQos79AlQ5k8WNktv7VGvVH4g= github.com/prashantv/gostub v1.1.0/go.mod h1:A5zLQHz7ieHGG7is6LLXLz7I8+3LZzsrV0P1IAHhP5U= github.com/prometheus/client_golang v1.16.0 h1:yk/hx9hDbrGHovbci4BY+pRMfSuuat626eFsHb7tmT8= @@ -300,8 +304,8 @@ github.com/redis/go-redis/v9 v9.8.0/go.mod h1:huWgSWd8mW6+m0VPhJjSSQ+d6Nh1VICQ6Q github.com/rjeczalik/notify v0.9.3 h1:6rJAzHTGKXGj76sbRgDiDcYj/HniypXmSJo1SWakZeY= github.com/rjeczalik/notify v0.9.3/go.mod h1:gF3zSOrafR9DQEWSE8TjfI9NkooDxbyT4UgRGKZA0lc= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= -github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= -github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= +github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8= +github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ= github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU= github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc= @@ -365,6 +369,8 @@ github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6Kllzaw github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= github.com/valyala/fasthttp v1.48.0 h1:oJWvHb9BIZToTQS3MuQ2R3bJZiNSa2KiNdeI8A+79Tc= github.com/valyala/fasthttp v1.48.0/go.mod h1:k2zXd82h/7UZc3VOdJ2WaUqt1uZ/XpXAfE9i+HBC3lA= +github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM= +github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg= github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c= github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI= github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY= @@ -394,8 +400,8 @@ go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE= go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0= go.uber.org/automaxprocs v1.5.3 h1:kWazyxZUrS3Gs4qUpbwo5kEIMGe/DAvi5Z4tl2NW4j8= go.uber.org/automaxprocs v1.5.3/go.mod h1:eRbA25aqJrxAbsLO0xy5jVwPt7FQnRgjW+efnwa1WM0= -go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A= -go.uber.org/goleak v1.2.1/go.mod h1:qlT2yGI9QafXHhZZLxlSuNsMw3FFLxBr+tBRlmO1xH4= +go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= +go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= go.uber.org/multierr v1.3.0/go.mod h1:VgVr7evmIr6uPjLBxg28wmKNXyqE9akIJ5XnfpiKl+4= go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU= @@ -435,7 +441,6 @@ golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA= golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= @@ -448,8 +453,8 @@ golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= golang.org/x/net v0.35.0 h1:T5GQRQb2y08kTAByq9L4/bz8cipCdA8FbRTXewonqY8= golang.org/x/net v0.35.0/go.mod h1:EglIi67kWsHKlRzzVMUD93VMSWGFOMSZgxFjparz1Qk= -golang.org/x/oauth2 v0.10.0 h1:zHCpF2Khkwy4mMB4bv0U37YtJdTGW8jI0glAApi0Kh8= -golang.org/x/oauth2 v0.10.0/go.mod h1:kTpgurOux7LqtuxjuyZa4Gj2gdezIt/jQtGnNFfypQI= +golang.org/x/oauth2 v0.21.0 h1:tsimM75w1tF/uws5rbeHzIWxEqElMehnc+iW793zsZs= +golang.org/x/oauth2 v0.21.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -528,10 +533,8 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c= -google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= -google.golang.org/protobuf v1.33.1-0.20240408130810-98873a205002 h1:V7Da7qt0MkY3noVANIMVBk28nOnijADeOR3i5Hcvpj4= -google.golang.org/protobuf v1.33.1-0.20240408130810-98873a205002/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= +google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= +google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= @@ -551,21 +554,21 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= -k8s.io/api v0.29.14 h1:JWFh5ufowH3Y6tCgEzY3URVJHb27f0tEDEej0nCjWDw= -k8s.io/api v0.29.14/go.mod h1:IV8YqKxMm8JGLBLlHM13Npn5lCITH10XYipWEW+YEOQ= -k8s.io/apimachinery v0.29.14 h1:IDhwnGNCp836SLOwW1SoEfFNV77wxIklhxeAHX9vmSo= -k8s.io/apimachinery v0.29.14/go.mod h1:i3FJVwhvSp/6n8Fl4K97PJEP8C+MM+aoDq4+ZJBf70Y= -k8s.io/client-go v0.29.14 h1:OSnzZ9DClaFRgl3zMAY2kGZhNjdGJkEb+RDz+MW2h6k= -k8s.io/client-go v0.29.14/go.mod h1:XtZt5n5UxKfPJ+sCoTPcEavWgZbLFFxMnAFFRQGK1RY= -k8s.io/klog/v2 v2.110.1 h1:U/Af64HJf7FcwMcXyKm2RPM22WZzyR7OSpYj5tg3cL0= -k8s.io/klog/v2 v2.110.1/go.mod h1:YGtd1984u+GgbuZ7e08/yBuAfKLSO0+uR1Fhi6ExXjo= -k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 h1:aVUu9fTY98ivBPKR9Y5w/AuzbMm96cd3YHRTU83I780= -k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00/go.mod h1:AsvuZPBlUDVuCdzJ87iajxtXuR9oktsTctW/R9wwouA= -k8s.io/utils v0.0.0-20230726121419-3b25d923346b h1:sgn3ZU783SCgtaSJjpcVVlRqd6GSnlTLKgpAAttJvpI= -k8s.io/utils v0.0.0-20230726121419-3b25d923346b/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0= +k8s.io/api v0.31.13 h1:sco9Cq2pY4Ysv9qZiWzcR97MmA/35nwYQ/VCTzOcWmc= +k8s.io/api v0.31.13/go.mod h1:4D8Ry8RqqLDemNLwGYC6v5wOy51N7hitr4WQ6oSWfLY= +k8s.io/apimachinery v0.31.13 h1:rkG0EiBkBkEzURo/8dKGx/oBF202Z2LqHuSD8Cm3bG4= +k8s.io/apimachinery v0.31.13/go.mod h1:rsPdaZJfTfLsNJSQzNHQvYoTmxhoOEofxtOsF3rtsMo= +k8s.io/client-go v0.31.13 h1:Q0LG51uFbzNd9fzIj5ilA0Sm1wUholHvDaNwVKzqdCA= +k8s.io/client-go v0.31.13/go.mod h1:UB4yTzQeRAv+vULOKp2jdqA5LSwV55bvc3RQ5tM48LM= +k8s.io/klog/v2 v2.130.1 h1:n9Xl7H1Xvksem4KFG4PYbdQCQxqc/tTUyrgXaOhHSzk= +k8s.io/klog/v2 v2.130.1/go.mod h1:3Jpz1GvMt720eyJH1ckRHK1EDfpxISzJ7I9OYgaDtPE= +k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 h1:BZqlfIlq5YbRMFko6/PM7FjZpUb45WallggurYhKGag= +k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340/go.mod h1:yD4MZYeKMBwQKVht279WycxKyM84kkAx2DPrTXaeb98= +k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 h1:pUdcCO1Lk/tbT5ztQWOBi5HBgbBP1J8+AsQnQCKsi8A= +k8s.io/utils v0.0.0-20240711033017-18e509b52bc8/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0= sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo= sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd/go.mod h1:B8JuhiUyNFVKdsE8h686QcCxMaH6HrOAZj4vswFpcB0= sigs.k8s.io/structured-merge-diff/v4 v4.4.1 h1:150L+0vs/8DA78h1u02ooW1/fFq/Lwr+sGiqlzvrtq4= sigs.k8s.io/structured-merge-diff/v4 v4.4.1/go.mod h1:N8hJocpFajUSSeSJ9bOZ77VzejKZaXsTtZo4/u7Io08= -sigs.k8s.io/yaml v1.3.0 h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo= -sigs.k8s.io/yaml v1.3.0/go.mod h1:GeOyir5tyXNByN85N/dRIT9es5UQNerPYEKK56eTBm8= +sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E= +sigs.k8s.io/yaml v1.4.0/go.mod h1:Ejl7/uTz7PSA4eKMyQCUTnhZYNmLIl+5c2lQPGR2BPY= diff --git a/plugin/input/k8s/meta/gatherer.go b/plugin/input/k8s/meta/gatherer.go index 5d6dbcb43..3012741cc 100644 --- a/plugin/input/k8s/meta/gatherer.go +++ b/plugin/input/k8s/meta/gatherer.go @@ -166,8 +166,16 @@ func initInformer() { if err != nil { localLogger.Fatalf("can't create k8s field selector: %s", err.Error()) } + podListWatcher := cache.NewListWatchFromClient(client.CoreV1().RESTClient(), "pods", "", selector) - _, c := cache.NewIndexerInformer(podListWatcher, &corev1.Pod{}, MetaExpireDuration/4, cache.ResourceEventHandlerFuncs{ + informer := cache.NewSharedIndexInformer( + podListWatcher, + &corev1.Pod{}, + MetaExpireDuration/4, + cache.Indexers{}, + ) + + _, err = informer.AddEventHandler(cache.ResourceEventHandlerFuncs{ AddFunc: func(obj any) { PutMeta(obj.(*corev1.Pod)) }, @@ -180,8 +188,12 @@ func initInformer() { deletedPodsCache.Add(PodName(pod.Name), true) deletedPodsCounter.Inc() }, - }, cache.Indexers{}) - controller = c + }) + if err != nil { + localLogger.Fatalf("can't add event handler: %s", err.Error()) + } + + controller = informer } func initRuntime(ctx context.Context) { From dc362597eead41467d1f61c1ea5b7d6ac664bf3b Mon Sep 17 00:00:00 2001 From: timggggggg Date: Thu, 6 Nov 2025 20:06:31 +0300 Subject: [PATCH 05/18] stop cleanup if meta update isn't possible --- plugin/input/k8s/meta/gatherer.go | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/plugin/input/k8s/meta/gatherer.go b/plugin/input/k8s/meta/gatherer.go index 3012741cc..e9853e4ef 100644 --- a/plugin/input/k8s/meta/gatherer.go +++ b/plugin/input/k8s/meta/gatherer.go @@ -58,7 +58,8 @@ var ( controller cache.Controller - expiredItems = make([]*MetaItem, 0, 16) // temporary list of expired items + canUpdateMetaData atomic.Bool + expiredItems = make([]*MetaItem, 0, 16) // temporary list of expired items informerStop = make(chan struct{}, 1) maintenanceStop = make(chan struct{}, 1) @@ -105,6 +106,8 @@ func EnableGatherer(l *zap.SugaredLogger) { } if !DisableMetaUpdates { + canUpdateMetaData.Store(true) + initGatherer() go controller.Run(informerStop) @@ -193,6 +196,14 @@ func initInformer() { localLogger.Fatalf("can't add event handler: %s", err.Error()) } + err = informer.SetWatchErrorHandler(func(r *cache.Reflector, err error) { + canUpdateMetaData.Store(false) + localLogger.Errorf("can't update meta data: %s", err.Error()) + }) + if err != nil { + localLogger.Fatalf("can't set error handler: %s", err.Error()) + } + controller = informer } @@ -242,7 +253,9 @@ func maintenance() { return default: time.Sleep(MaintenanceInterval) - removeExpired() + if canUpdateMetaData.Load() { + removeExpired() + } if MetaFileSaver.metaFile != "" { MetaFileSaver.saveMetaFile() } @@ -349,6 +362,8 @@ func PutMeta(podData *corev1.Pod) { return } + canUpdateMetaData.Store(true) + podCopy := podData pod := PodName(podCopy.Name) @@ -502,5 +517,13 @@ func (ms *MetaSaver) loadMeta() error { return fmt.Errorf("can't unmarshal map: %w", err) } + for _, podNames := range MetaData { + for _, containerIDs := range podNames { + for _, podData := range containerIDs { + podData.updateTime = time.Now() + } + } + } + return nil } From b458d8768f3b0053f68d96e264ab078e2e819db9 Mon Sep 17 00:00:00 2001 From: timggggggg Date: Fri, 7 Nov 2025 12:25:20 +0300 Subject: [PATCH 06/18] split MetaWaitTimeout cases --- plugin/input/k8s/meta/gatherer.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/plugin/input/k8s/meta/gatherer.go b/plugin/input/k8s/meta/gatherer.go index e9853e4ef..d6d029f65 100644 --- a/plugin/input/k8s/meta/gatherer.go +++ b/plugin/input/k8s/meta/gatherer.go @@ -324,6 +324,7 @@ func GetPodMeta(ns Namespace, pod PodName, cid ContainerID) (bool, *podMeta) { var success bool i := time.Nanosecond + canUpdateMetaDataBeforeRetries := canUpdateMetaData.Load() for { metaDataMu.RLock() pm, has := MetaData[ns][pod][cid] @@ -349,8 +350,12 @@ func GetPodMeta(ns Namespace, pod PodName, cid ContainerID) (bool, *podMeta) { i += metaRecheckInterval if i-MetaWaitTimeout >= 0 { - deletedPodsCache.Add(pod, true) - localLogger.Errorf("maybe pod %q have deleted, cause k8s meta retrieve timeout ns=%s", string(pod), string(ns)) + if canUpdateMetaDataBeforeRetries && canUpdateMetaData.Load() { + deletedPodsCache.Add(pod, true) + localLogger.Errorf("pod %q was deleted, causing k8s meta retrieval timeout ns=%s", string(pod), string(ns)) + } else { + localLogger.Errorf("k8s meta retrieval timeout pod=%q ns=%s", string(pod), string(ns)) + } return success, podMeta } From f75f7a7df748a87b6e2cb40081c8e30cf3217639 Mon Sep 17 00:00:00 2001 From: timggggggg Date: Mon, 10 Nov 2025 16:02:20 +0300 Subject: [PATCH 07/18] skip waiting for meta if can't update it for 5 minutes --- plugin/input/k8s/meta/gatherer.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/plugin/input/k8s/meta/gatherer.go b/plugin/input/k8s/meta/gatherer.go index d6d029f65..3465e387c 100644 --- a/plugin/input/k8s/meta/gatherer.go +++ b/plugin/input/k8s/meta/gatherer.go @@ -58,8 +58,9 @@ var ( controller cache.Controller - canUpdateMetaData atomic.Bool - expiredItems = make([]*MetaItem, 0, 16) // temporary list of expired items + canUpdateMetaData atomic.Bool + metaLastUnavailableTime atomic.Time + expiredItems = make([]*MetaItem, 0, 16) // temporary list of expired items informerStop = make(chan struct{}, 1) maintenanceStop = make(chan struct{}, 1) @@ -68,9 +69,10 @@ var ( MaintenanceInterval = 15 * time.Second MetaExpireDuration = 5 * time.Minute - metaRecheckInterval = 250 * time.Millisecond - metaWaitWarn = 5 * time.Second - MetaWaitTimeout = 120 * time.Second + metaRecheckInterval = 250 * time.Millisecond + metaWaitWarn = 5 * time.Second + MetaWaitTimeout = 120 * time.Second + metaWaitAvailabilityTimeout = 5 * time.Minute stopWg = &sync.WaitGroup{} @@ -198,6 +200,7 @@ func initInformer() { err = informer.SetWatchErrorHandler(func(r *cache.Reflector, err error) { canUpdateMetaData.Store(false) + metaLastUnavailableTime.Store(time.Now()) localLogger.Errorf("can't update meta data: %s", err.Error()) }) if err != nil { @@ -341,6 +344,10 @@ func GetPodMeta(ns Namespace, pod PodName, cid ContainerID) (bool, *podMeta) { return success, podMeta } + if !canUpdateMetaData.Load() && time.Since(metaLastUnavailableTime.Load()) > metaWaitAvailabilityTimeout { + return success, podMeta + } + // fast skip deleted pods if isDeleted { return success, podMeta From 8c4fbd17817e701d92eba0befc06438f724abd84 Mon Sep 17 00:00:00 2001 From: timggggggg Date: Mon, 10 Nov 2025 18:44:24 +0300 Subject: [PATCH 08/18] doc + metaFirstUnavailableTime --- plugin/input/k8s/README.md | 4 ++++ plugin/input/k8s/k8s.go | 4 ++++ plugin/input/k8s/meta/gatherer.go | 4 +++- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/plugin/input/k8s/README.md b/plugin/input/k8s/README.md index 27aca503d..d9a7f8459 100755 --- a/plugin/input/k8s/README.md +++ b/plugin/input/k8s/README.md @@ -120,6 +120,10 @@ Example: ```component: '{{ index .pod.Labels "component" | default .k8s_containe The filename to store current k8s pod meta. Meta are loaded only on initialization +> This feature is used to reuse metadata that we have already received, in particular if the kube-apiserver is unavailable. +> +> The plugin considers kube-apiserver to be unavailable from the moment the SetWatchErrorHandler handler is called until one of the ResourceEventHandlerFuncs handlers is called. +
diff --git a/plugin/input/k8s/k8s.go b/plugin/input/k8s/k8s.go index 746b4ff8e..2f03c231b 100644 --- a/plugin/input/k8s/k8s.go +++ b/plugin/input/k8s/k8s.go @@ -141,6 +141,10 @@ type Config struct { // > @3@4@5@6 // > // > The filename to store current k8s pod meta. Meta are loaded only on initialization + // > + // > > This feature is used to reuse metadata that we have already received, in particular if the kube-apiserver is unavailable. + // > > + // > > The plugin considers kube-apiserver to be unavailable from the moment the SetWatchErrorHandler handler is called until one of the ResourceEventHandlerFuncs handlers is called. K8sMetaFile string `json:"meta_file" default:""` // * } diff --git a/plugin/input/k8s/meta/gatherer.go b/plugin/input/k8s/meta/gatherer.go index 3465e387c..48b2c14b6 100644 --- a/plugin/input/k8s/meta/gatherer.go +++ b/plugin/input/k8s/meta/gatherer.go @@ -199,8 +199,10 @@ func initInformer() { } err = informer.SetWatchErrorHandler(func(r *cache.Reflector, err error) { + if canUpdateMetaData.Load() { + metaLastUnavailableTime.Store(time.Now()) + } canUpdateMetaData.Store(false) - metaLastUnavailableTime.Store(time.Now()) localLogger.Errorf("can't update meta data: %s", err.Error()) }) if err != nil { From c82fdb76ebce75b2a1608b0490368ddc47126bc0 Mon Sep 17 00:00:00 2001 From: timggggggg Date: Mon, 10 Nov 2025 18:46:13 +0300 Subject: [PATCH 09/18] metaFirstUnavailableTime --- plugin/input/k8s/meta/gatherer.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/plugin/input/k8s/meta/gatherer.go b/plugin/input/k8s/meta/gatherer.go index 48b2c14b6..1b3ace6f9 100644 --- a/plugin/input/k8s/meta/gatherer.go +++ b/plugin/input/k8s/meta/gatherer.go @@ -58,9 +58,9 @@ var ( controller cache.Controller - canUpdateMetaData atomic.Bool - metaLastUnavailableTime atomic.Time - expiredItems = make([]*MetaItem, 0, 16) // temporary list of expired items + canUpdateMetaData atomic.Bool + metaFirstUnavailableTime atomic.Time + expiredItems = make([]*MetaItem, 0, 16) // temporary list of expired items informerStop = make(chan struct{}, 1) maintenanceStop = make(chan struct{}, 1) @@ -200,7 +200,7 @@ func initInformer() { err = informer.SetWatchErrorHandler(func(r *cache.Reflector, err error) { if canUpdateMetaData.Load() { - metaLastUnavailableTime.Store(time.Now()) + metaFirstUnavailableTime.Store(time.Now()) } canUpdateMetaData.Store(false) localLogger.Errorf("can't update meta data: %s", err.Error()) @@ -346,7 +346,7 @@ func GetPodMeta(ns Namespace, pod PodName, cid ContainerID) (bool, *podMeta) { return success, podMeta } - if !canUpdateMetaData.Load() && time.Since(metaLastUnavailableTime.Load()) > metaWaitAvailabilityTimeout { + if !canUpdateMetaData.Load() && time.Since(metaFirstUnavailableTime.Load()) > metaWaitAvailabilityTimeout { return success, podMeta } From eef05c08e4cc73260c27660df9f7aae66fcd0f24 Mon Sep 17 00:00:00 2001 From: timggggggg Date: Thu, 13 Nov 2025 16:48:31 +0300 Subject: [PATCH 10/18] get node name from MetaData cache --- plugin/input/k8s/meta/gatherer.go | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/plugin/input/k8s/meta/gatherer.go b/plugin/input/k8s/meta/gatherer.go index 1b3ace6f9..474244eef 100644 --- a/plugin/input/k8s/meta/gatherer.go +++ b/plugin/input/k8s/meta/gatherer.go @@ -58,9 +58,9 @@ var ( controller cache.Controller - canUpdateMetaData atomic.Bool - metaFirstUnavailableTime atomic.Time - expiredItems = make([]*MetaItem, 0, 16) // temporary list of expired items + canUpdateMetaData atomic.Bool + metaLastAvailableTime atomic.Time + expiredItems = make([]*MetaItem, 0, 16) // temporary list of expired items informerStop = make(chan struct{}, 1) maintenanceStop = make(chan struct{}, 1) @@ -158,7 +158,14 @@ func initNodeInfo(ctx context.Context) { localLogger.Fatalf("can't get host name for k8s plugin: %s", err.Error()) panic("") } - pod, err := client.CoreV1().Pods(getNamespace()).Get(ctx, podName, metav1.GetOptions{}) + + ns := getNamespace() + SelfNodeName = getNodeName(Namespace(ns), PodName(podName)) + if SelfNodeName != "" { + return + } + + pod, err := client.CoreV1().Pods(ns).Get(ctx, podName, metav1.GetOptions{}) if err != nil { localLogger.Fatalf("can't detect node name for k8s plugin using pod %q: %s", podName, err.Error()) panic("") @@ -200,7 +207,7 @@ func initInformer() { err = informer.SetWatchErrorHandler(func(r *cache.Reflector, err error) { if canUpdateMetaData.Load() { - metaFirstUnavailableTime.Store(time.Now()) + metaLastAvailableTime.Store(time.Now()) } canUpdateMetaData.Store(false) localLogger.Errorf("can't update meta data: %s", err.Error()) @@ -346,7 +353,7 @@ func GetPodMeta(ns Namespace, pod PodName, cid ContainerID) (bool, *podMeta) { return success, podMeta } - if !canUpdateMetaData.Load() && time.Since(metaFirstUnavailableTime.Load()) > metaWaitAvailabilityTimeout { + if !canUpdateMetaData.Load() && time.Since(metaLastAvailableTime.Load()) > metaWaitAvailabilityTimeout { return success, podMeta } @@ -458,6 +465,16 @@ func getNamespace() string { return strings.TrimSpace(string(data)) } +func getNodeName(ns Namespace, podName PodName) string { + for _, podData := range MetaData[ns][podName] { + if podData.Spec.NodeName != "" { + return podData.Spec.NodeName + } + } + + return "" +} + type MetaSaver struct { metaFile string metaFileTmp string From f38498d03ce82691d461ec35822086d45595fe1f Mon Sep 17 00:00:00 2001 From: timggggggg Date: Mon, 17 Nov 2025 17:57:58 +0300 Subject: [PATCH 11/18] add CriType and NodeLabels in cache --- plugin/input/k8s/k8s.go | 2 +- plugin/input/k8s/k8s_test.go | 2 +- plugin/input/k8s/meta/gatherer.go | 72 ++++++++++++++--------- plugin/input/k8s/multiline_action.go | 2 +- plugin/input/k8s/multiline_action_test.go | 2 +- 5 files changed, 49 insertions(+), 31 deletions(-) diff --git a/plugin/input/k8s/k8s.go b/plugin/input/k8s/k8s.go index 2f03c231b..f6d74da72 100644 --- a/plugin/input/k8s/k8s.go +++ b/plugin/input/k8s/k8s.go @@ -189,7 +189,7 @@ func (p *Plugin) Start(config pipeline.AnyConfig, params *pipeline.InputPluginPa meta.EnableGatherer(p.logger) } - if meta.CriType == "docker" { + if meta.MetaData.CriType == "docker" { p.params.Controller.SuggestDecoder(decoder.JSON) } else { p.params.Controller.SuggestDecoder(decoder.CRI) diff --git a/plugin/input/k8s/k8s_test.go b/plugin/input/k8s/k8s_test.go index ef19ffcea..d81686814 100644 --- a/plugin/input/k8s/k8s_test.go +++ b/plugin/input/k8s/k8s_test.go @@ -242,5 +242,5 @@ func TestCleanUp(t *testing.T) { meta.DisableGatherer() p.Stop() - assert.Equal(t, 0, len(meta.MetaData)) + assert.Equal(t, 0, len(meta.MetaData.PodMeta)) } diff --git a/plugin/input/k8s/meta/gatherer.go b/plugin/input/k8s/meta/gatherer.go index 474244eef..a67d33901 100644 --- a/plugin/input/k8s/meta/gatherer.go +++ b/plugin/input/k8s/meta/gatherer.go @@ -24,7 +24,11 @@ import ( "k8s.io/client-go/tools/clientcmd" ) -const metaFileTempSuffix = ".atomic" +const ( + metaFileTempSuffix = ".atomic" + + defaultCriType = "docker" +) type ( PodName string @@ -45,11 +49,21 @@ type ( } meta map[Namespace]map[PodName]map[ContainerID]*podMeta + + metaCache struct { + PodMeta meta + CriType string + NodeLabels map[string]string + } ) var ( - client *kubernetes.Clientset - MetaData = make(meta) + client *kubernetes.Clientset + MetaData = &metaCache{ + PodMeta: make(meta), + CriType: "", + NodeLabels: nil, + } metaDataMu = &sync.RWMutex{} MetaFileSaver = &MetaSaver{} @@ -82,9 +96,6 @@ var ( expiredItemsCounter atomic.Int64 deletedPodsCounter atomic.Int64 - CriType = "docker" - NodeLabels = make(map[string]string) - SelfNodeName string localLogger *zap.SugaredLogger @@ -160,7 +171,7 @@ func initNodeInfo(ctx context.Context) { } ns := getNamespace() - SelfNodeName = getNodeName(Namespace(ns), PodName(podName)) + SelfNodeName = getNodeName(Namespace(ns)) if SelfNodeName != "" { return } @@ -220,6 +231,10 @@ func initInformer() { } func initRuntime(ctx context.Context) { + if MetaData.NodeLabels != nil && MetaData.CriType != "" { + return + } + node, err := client.CoreV1().Nodes().Get(ctx, SelfNodeName, metav1.GetOptions{}) if err != nil || node == nil { localLogger.Fatalf("can't detect CRI runtime for node %s, api call is unsuccessful: %s", node, err.Error()) @@ -231,8 +246,8 @@ func initRuntime(ctx context.Context) { localLogger.Fatalf("can't detect CRI runtime for node %s, wrong runtime version: %s", node, runtimeVer) } - NodeLabels = node.Labels - CriType = runtimeVer[:pos] + MetaData.NodeLabels = node.Labels + MetaData.CriType = runtimeVer[:pos] } func removeExpired() { @@ -280,7 +295,7 @@ func getTotalItems() int { defer metaDataMu.RUnlock() totalItems := 0 - for _, podNames := range MetaData { + for _, podNames := range MetaData.PodMeta { for _, containerIDs := range podNames { totalItems += len(containerIDs) } @@ -296,7 +311,7 @@ func getExpiredItems(out []*MetaItem) []*MetaItem { defer metaDataMu.RUnlock() // find pods which aren't in k8s pod list for some time and add them to the expiration list - for ns, podNames := range MetaData { + for ns, podNames := range MetaData.PodMeta { for pod, containerIDs := range podNames { for cid, podData := range containerIDs { if now.Sub(podData.updateTime) > MetaExpireDuration { @@ -319,14 +334,14 @@ func cleanUpItems(items []*MetaItem) { for _, item := range items { expiredItemsCounter.Inc() - delete(MetaData[item.Namespace][item.PodName], item.ContainerID) + delete(MetaData.PodMeta[item.Namespace][item.PodName], item.ContainerID) - if len(MetaData[item.Namespace][item.PodName]) == 0 { - delete(MetaData[item.Namespace], item.PodName) + if len(MetaData.PodMeta[item.Namespace][item.PodName]) == 0 { + delete(MetaData.PodMeta[item.Namespace], item.PodName) } - if len(MetaData[item.Namespace]) == 0 { - delete(MetaData, item.Namespace) + if len(MetaData.PodMeta[item.Namespace]) == 0 { + delete(MetaData.PodMeta, item.Namespace) } } } @@ -339,7 +354,7 @@ func GetPodMeta(ns Namespace, pod PodName, cid ContainerID) (bool, *podMeta) { canUpdateMetaDataBeforeRetries := canUpdateMetaData.Load() for { metaDataMu.RLock() - pm, has := MetaData[ns][pod][cid] + pm, has := MetaData.PodMeta[ns][pod][cid] isDeleted := deletedPodsCache.Contains(pod) metaDataMu.RUnlock() @@ -391,11 +406,11 @@ func PutMeta(podData *corev1.Pod) { ns := Namespace(podCopy.Namespace) metaDataMu.Lock() - if MetaData[ns] == nil { - MetaData[ns] = make(map[PodName]map[ContainerID]*podMeta) + if MetaData.PodMeta[ns] == nil { + MetaData.PodMeta[ns] = make(map[PodName]map[ContainerID]*podMeta) } - if MetaData[ns][pod] == nil { - MetaData[ns][pod] = make(map[ContainerID]*podMeta) + if MetaData.PodMeta[ns][pod] == nil { + MetaData.PodMeta[ns][pod] = make(map[ContainerID]*podMeta) } metaDataMu.Unlock() @@ -453,7 +468,7 @@ func putContainerMeta(ns Namespace, pod PodName, fullContainerID string, podInfo } metaDataMu.Lock() - MetaData[ns][pod][containerID] = meta + MetaData.PodMeta[ns][pod][containerID] = meta metaDataMu.Unlock() } @@ -465,10 +480,12 @@ func getNamespace() string { return strings.TrimSpace(string(data)) } -func getNodeName(ns Namespace, podName PodName) string { - for _, podData := range MetaData[ns][podName] { - if podData.Spec.NodeName != "" { - return podData.Spec.NodeName +func getNodeName(ns Namespace) string { + for _, containerIDs := range MetaData.PodMeta[ns] { + for _, podData := range containerIDs { + if podData.Spec.NodeName != "" { + return podData.Spec.NodeName + } } } @@ -505,6 +522,7 @@ func (ms *MetaSaver) saveMetaFile() { metaDataMu.RLock() buf, err := json.MarshalIndent(MetaData, "", " ") metaDataMu.RUnlock() + if err != nil { localLogger.Errorf("can't marshall k8s meta map into json: %s", err.Error()) } @@ -548,7 +566,7 @@ func (ms *MetaSaver) loadMeta() error { return fmt.Errorf("can't unmarshal map: %w", err) } - for _, podNames := range MetaData { + for _, podNames := range MetaData.PodMeta { for _, containerIDs := range podNames { for _, podData := range containerIDs { podData.updateTime = time.Now() diff --git a/plugin/input/k8s/multiline_action.go b/plugin/input/k8s/multiline_action.go index 00c1b8cd7..df661a6e9 100644 --- a/plugin/input/k8s/multiline_action.go +++ b/plugin/input/k8s/multiline_action.go @@ -185,7 +185,7 @@ func (p *MultilineAction) Do(event *pipeline.Event) pipeline.ActionResult { event.Root.AddFieldNoAlloc(event.Root, pipeline.ByteToStringUnsafe(event.Buf[l:])).MutateToString(labelValue) } - for labelName, labelValue := range meta.NodeLabels { + for labelName, labelValue := range meta.MetaData.NodeLabels { if len(p.allowedNodeLabels) != 0 { _, has := p.allowedNodeLabels[labelName] diff --git a/plugin/input/k8s/multiline_action_test.go b/plugin/input/k8s/multiline_action_test.go index 06e471f1a..67bcabdfc 100644 --- a/plugin/input/k8s/multiline_action_test.go +++ b/plugin/input/k8s/multiline_action_test.go @@ -31,7 +31,7 @@ func TestMultilineAction_Do(t *testing.T) { filename := getLogFilename("k8s", item) meta.PutMeta(getPodInfo(item, true)) meta.SelfNodeName = "node_1" - meta.NodeLabels = map[string]string{"zone": "z34"} + meta.MetaData.NodeLabels = map[string]string{"zone": "z34"} tcs := []struct { Name string From e7a71cc66437f618b30d34c4f3b9ad52f6d299e9 Mon Sep 17 00:00:00 2001 From: timggggggg Date: Mon, 17 Nov 2025 18:05:43 +0300 Subject: [PATCH 12/18] remove defaultCriType --- plugin/input/k8s/meta/gatherer.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/plugin/input/k8s/meta/gatherer.go b/plugin/input/k8s/meta/gatherer.go index a67d33901..3fa28e4f1 100644 --- a/plugin/input/k8s/meta/gatherer.go +++ b/plugin/input/k8s/meta/gatherer.go @@ -26,8 +26,6 @@ import ( const ( metaFileTempSuffix = ".atomic" - - defaultCriType = "docker" ) type ( From 8cbee7777d872ef83ed10d89fe925ff471ebb903 Mon Sep 17 00:00:00 2001 From: timggggggg Date: Wed, 19 Nov 2025 17:40:48 +0300 Subject: [PATCH 13/18] refactor getNodeName --- plugin/input/k8s/meta/gatherer.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/plugin/input/k8s/meta/gatherer.go b/plugin/input/k8s/meta/gatherer.go index 3fa28e4f1..694460b03 100644 --- a/plugin/input/k8s/meta/gatherer.go +++ b/plugin/input/k8s/meta/gatherer.go @@ -168,13 +168,12 @@ func initNodeInfo(ctx context.Context) { panic("") } - ns := getNamespace() - SelfNodeName = getNodeName(Namespace(ns)) + SelfNodeName = getNodeName() if SelfNodeName != "" { return } - pod, err := client.CoreV1().Pods(ns).Get(ctx, podName, metav1.GetOptions{}) + pod, err := client.CoreV1().Pods(getNamespace()).Get(ctx, podName, metav1.GetOptions{}) if err != nil { localLogger.Fatalf("can't detect node name for k8s plugin using pod %q: %s", podName, err.Error()) panic("") @@ -478,11 +477,13 @@ func getNamespace() string { return strings.TrimSpace(string(data)) } -func getNodeName(ns Namespace) string { - for _, containerIDs := range MetaData.PodMeta[ns] { - for _, podData := range containerIDs { - if podData.Spec.NodeName != "" { - return podData.Spec.NodeName +func getNodeName() string { + for _, podNames := range MetaData.PodMeta { + for _, containerIDs := range podNames { + for _, podData := range containerIDs { + if podData.Spec.NodeName != "" { + return podData.Spec.NodeName + } } } } From 2f0fced4a1639f33b78af1e17bdfcc1cc6985a1f Mon Sep 17 00:00:00 2001 From: timggggggg Date: Wed, 19 Nov 2025 18:05:03 +0300 Subject: [PATCH 14/18] fail start if can't get meta from api and cache --- plugin/input/k8s/meta/gatherer.go | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/plugin/input/k8s/meta/gatherer.go b/plugin/input/k8s/meta/gatherer.go index 694460b03..c6bcfcd08 100644 --- a/plugin/input/k8s/meta/gatherer.go +++ b/plugin/input/k8s/meta/gatherer.go @@ -168,15 +168,15 @@ func initNodeInfo(ctx context.Context) { panic("") } - SelfNodeName = getNodeName() - if SelfNodeName != "" { - return - } - pod, err := client.CoreV1().Pods(getNamespace()).Get(ctx, podName, metav1.GetOptions{}) if err != nil { - localLogger.Fatalf("can't detect node name for k8s plugin using pod %q: %s", podName, err.Error()) - panic("") + localLogger.Warnf("can't detect node name for k8s plugin using pod %q: %s", podName, err.Error()) + SelfNodeName = getNodeName() + if SelfNodeName != "" { + localLogger.Fatalf("can't get node name from cache for k8s plugin using pod %q", podName) + panic("") + } + return } SelfNodeName = pod.Spec.NodeName } @@ -228,14 +228,14 @@ func initInformer() { } func initRuntime(ctx context.Context) { - if MetaData.NodeLabels != nil && MetaData.CriType != "" { - return - } - node, err := client.CoreV1().Nodes().Get(ctx, SelfNodeName, metav1.GetOptions{}) if err != nil || node == nil { - localLogger.Fatalf("can't detect CRI runtime for node %s, api call is unsuccessful: %s", node, err.Error()) - panic("_") + localLogger.Warnf("can't detect CRI runtime for node %s, api call is unsuccessful: %s", node, err.Error()) + if MetaData.CriType == "" { + localLogger.Fatalf("can't get CRI runtime for node %s from cache", node) + panic("") + } + return } runtimeVer := node.Status.NodeInfo.ContainerRuntimeVersion pos := strings.IndexByte(runtimeVer, ':') From d8621618aa99d9c3d9ebfcb713160ebdc1d72793 Mon Sep 17 00:00:00 2001 From: timggggggg Date: Wed, 19 Nov 2025 18:07:05 +0300 Subject: [PATCH 15/18] fix --- plugin/input/k8s/meta/gatherer.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/input/k8s/meta/gatherer.go b/plugin/input/k8s/meta/gatherer.go index c6bcfcd08..ba35ec602 100644 --- a/plugin/input/k8s/meta/gatherer.go +++ b/plugin/input/k8s/meta/gatherer.go @@ -172,7 +172,7 @@ func initNodeInfo(ctx context.Context) { if err != nil { localLogger.Warnf("can't detect node name for k8s plugin using pod %q: %s", podName, err.Error()) SelfNodeName = getNodeName() - if SelfNodeName != "" { + if SelfNodeName == "" { localLogger.Fatalf("can't get node name from cache for k8s plugin using pod %q", podName) panic("") } From 1e5df8a4633d6a6abbe4ce003e2985b6be058b60 Mon Sep 17 00:00:00 2001 From: timggggggg Date: Thu, 20 Nov 2025 17:50:05 +0300 Subject: [PATCH 16/18] change ready and live handlers log level to debug --- fd/file.d.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fd/file.d.go b/fd/file.d.go index 11e1eb0eb..223384c67 100644 --- a/fd/file.d.go +++ b/fd/file.d.go @@ -390,11 +390,11 @@ func (f *FileD) serveReady(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusServiceUnavailable) return } - logger.Infof("ready OK") + logger.Debug("ready OK") } func (f *FileD) serveLive(_ http.ResponseWriter, _ *http.Request) { - logger.Infof("live OK") + logger.Debug("live OK") } type valueChangerHandler struct { From 072b982d4b3e6177140f49b4deba568c0f329df3 Mon Sep 17 00:00:00 2001 From: timggggggg Date: Wed, 26 Nov 2025 15:53:12 +0300 Subject: [PATCH 17/18] update doc --- plugin/input/k8s/README.md | 8 ++++++-- plugin/input/k8s/k8s.go | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/plugin/input/k8s/README.md b/plugin/input/k8s/README.md index d9a7f8459..90e06880c 100755 --- a/plugin/input/k8s/README.md +++ b/plugin/input/k8s/README.md @@ -118,11 +118,15 @@ Example: ```component: '{{ index .pod.Labels "component" | default .k8s_containe **`meta_file`** *`string`* -The filename to store current k8s pod meta. Meta are loaded only on initialization +The filename to store current k8s pod metadata (`map[Namespace]map[PodName]map[ContainerID]*podMeta`, where `podMeta` is a wrapper for `corev1.Pod`). + +Metadata is saved once per gatherer `MaintenanceInterval` and is loaded only on initialization. + +If parameter is empty, the metadata won't be cached. > This feature is used to reuse metadata that we have already received, in particular if the kube-apiserver is unavailable. > -> The plugin considers kube-apiserver to be unavailable from the moment the SetWatchErrorHandler handler is called until one of the ResourceEventHandlerFuncs handlers is called. +> The plugin considers kube-apiserver to be unavailable from the moment the `SetWatchErrorHandler` handler is called until one of the `ResourceEventHandlerFuncs` handlers is called.
diff --git a/plugin/input/k8s/k8s.go b/plugin/input/k8s/k8s.go index f6d74da72..67d0c699e 100644 --- a/plugin/input/k8s/k8s.go +++ b/plugin/input/k8s/k8s.go @@ -140,11 +140,15 @@ type Config struct { // > @3@4@5@6 // > - // > The filename to store current k8s pod meta. Meta are loaded only on initialization + // > The filename to store current k8s pod metadata (`map[Namespace]map[PodName]map[ContainerID]*podMeta`, where `podMeta` is a wrapper for `corev1.Pod`). + // > + // > Metadata is saved once per gatherer `MaintenanceInterval` and is loaded only on initialization. + // > + // > If parameter is empty, the metadata won't be cached. // > // > > This feature is used to reuse metadata that we have already received, in particular if the kube-apiserver is unavailable. // > > - // > > The plugin considers kube-apiserver to be unavailable from the moment the SetWatchErrorHandler handler is called until one of the ResourceEventHandlerFuncs handlers is called. + // > > The plugin considers kube-apiserver to be unavailable from the moment the `SetWatchErrorHandler` handler is called until one of the `ResourceEventHandlerFuncs` handlers is called. K8sMetaFile string `json:"meta_file" default:""` // * } From e606132f218436a45668878ca97dedf7cf284a75 Mon Sep 17 00:00:00 2001 From: timggggggg Date: Thu, 27 Nov 2025 18:03:08 +0300 Subject: [PATCH 18/18] update doc --- plugin/input/k8s/README.md | 7 ++++++- plugin/input/k8s/k8s.go | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/plugin/input/k8s/README.md b/plugin/input/k8s/README.md index 90e06880c..5ae893a57 100755 --- a/plugin/input/k8s/README.md +++ b/plugin/input/k8s/README.md @@ -118,7 +118,12 @@ Example: ```component: '{{ index .pod.Labels "component" | default .k8s_containe **`meta_file`** *`string`* -The filename to store current k8s pod metadata (`map[Namespace]map[PodName]map[ContainerID]*podMeta`, where `podMeta` is a wrapper for `corev1.Pod`). +The filename to store current k8s pod metadata. + +Metadata contains: +* PodMeta `map[Namespace]map[PodName]map[ContainerID]*podMeta`, where `podMeta` is a wrapper for `corev1.Pod` +* CriType `string` +* NodeLabels `map[string]string` Metadata is saved once per gatherer `MaintenanceInterval` and is loaded only on initialization. diff --git a/plugin/input/k8s/k8s.go b/plugin/input/k8s/k8s.go index 67d0c699e..5d88a12ad 100644 --- a/plugin/input/k8s/k8s.go +++ b/plugin/input/k8s/k8s.go @@ -140,7 +140,12 @@ type Config struct { // > @3@4@5@6 // > - // > The filename to store current k8s pod metadata (`map[Namespace]map[PodName]map[ContainerID]*podMeta`, where `podMeta` is a wrapper for `corev1.Pod`). + // > The filename to store current k8s pod metadata. + // > + // > Metadata contains: + // > * PodMeta `map[Namespace]map[PodName]map[ContainerID]*podMeta`, where `podMeta` is a wrapper for `corev1.Pod` + // > * CriType `string` + // > * NodeLabels `map[string]string` // > // > Metadata is saved once per gatherer `MaintenanceInterval` and is loaded only on initialization. // >