| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package k8s_state |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "strings" |
| 8 | |
| 9 | corev1 "k8s.io/api/core/v1" |
| 10 | |
| 11 | "github.com/netdata/netdata/go/plugins/logger" |
| 12 | |
| 13 | "k8s.io/client-go/tools/cache" |
| 14 | "k8s.io/client-go/util/workqueue" |
| 15 | ) |
| 16 | |
| 17 | func newPodDiscoverer(si cache.SharedInformer, l *logger.Logger) *podDiscoverer { |
| 18 | if si == nil { |
| 19 | panic("nil pod shared informer") |
| 20 | } |
| 21 | |
| 22 | queue := workqueue.NewTypedWithConfig(workqueue.TypedQueueConfig[string]{Name: "pod"}) |
| 23 | |
| 24 | _, _ = si.AddEventHandler(cache.ResourceEventHandlerFuncs{ |
| 25 | AddFunc: func(obj any) { enqueue(queue, obj) }, |
| 26 | UpdateFunc: func(_, obj any) { enqueue(queue, obj) }, |
| 27 | DeleteFunc: func(obj any) { enqueue(queue, obj) }, |
| 28 | }) |
| 29 | |
| 30 | return &podDiscoverer{ |
| 31 | Logger: l, |
| 32 | informer: si, |
| 33 | queue: queue, |
| 34 | readyCh: make(chan struct{}), |
| 35 | stopCh: make(chan struct{}), |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | type podResource struct { |
| 40 | src string |
| 41 | val any |
| 42 | } |
| 43 | |
| 44 | func (r podResource) source() string { return r.src } |
| 45 | func (r podResource) kind() kubeResourceKind { return kubeResourcePod } |
| 46 | func (r podResource) value() any { return r.val } |
| 47 | |
| 48 | type podDiscoverer struct { |
| 49 | *logger.Logger |
| 50 | informer cache.SharedInformer |
| 51 | queue *workqueue.Typed[string] |
| 52 | readyCh chan struct{} |
| 53 | stopCh chan struct{} |
| 54 | } |
| 55 | |
| 56 | func (d *podDiscoverer) run(ctx context.Context, in chan<- resource) { |
| 57 | d.Info("pod_discoverer is started") |
| 58 | defer func() { close(d.stopCh); d.Info("pod_discoverer is stopped") }() |
| 59 | |
| 60 | defer d.queue.ShutDown() |
| 61 | |
| 62 | go d.informer.Run(ctx.Done()) |
| 63 | |
| 64 | if !cache.WaitForCacheSync(ctx.Done(), d.informer.HasSynced) { |
| 65 | return |
| 66 | } |
| 67 | |
| 68 | go d.runDiscover(ctx, in) |
| 69 | close(d.readyCh) |
| 70 | |
| 71 | <-ctx.Done() |
| 72 | } |
| 73 | |
| 74 | func (d *podDiscoverer) ready() bool { return isChanClosed(d.readyCh) } |
| 75 | func (d *podDiscoverer) stopped() bool { return isChanClosed(d.stopCh) } |
| 76 | |
| 77 | func (d *podDiscoverer) runDiscover(ctx context.Context, in chan<- resource) { |
| 78 | for { |
| 79 | key, shutdown := d.queue.Get() |
| 80 | if shutdown { |
| 81 | return |
| 82 | } |
| 83 | |
| 84 | func() { |
| 85 | defer d.queue.Done(key) |
| 86 | |
| 87 | ns, name, err := cache.SplitMetaNamespaceKey(key) |
| 88 | if err != nil { |
| 89 | return |
| 90 | } |
| 91 | |
| 92 | item, exists, err := d.informer.GetStore().GetByKey(key) |
| 93 | if err != nil { |
| 94 | return |
| 95 | } |
| 96 | |
| 97 | r := &podResource{src: podSource(ns, name)} |
| 98 | if exists { |
| 99 | r.val = item |
| 100 | } |
| 101 | if pod, err := toPod(r); err == nil && hasIgnoreAnnotation(pod) { |
| 102 | return |
| 103 | } |
| 104 | send(ctx, in, r) |
| 105 | }() |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | func podSource(namespace, name string) string { |
| 110 | return "k8s/pod/" + namespace + "/" + name |
| 111 | } |
| 112 | |
| 113 | func hasIgnoreAnnotation(pod *corev1.Pod) bool { |
| 114 | if pod == nil { |
| 115 | return false |
| 116 | } |
| 117 | v := pod.Annotations["netdata.cloud/ignore"] |
| 118 | return strings.EqualFold(v, "true") || strings.EqualFold(v, "yes") |
| 119 | } |