-
Notifications
You must be signed in to change notification settings - Fork 131
app/vlagent: feat: add namespace labels/annotations metadata and flags #949
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,10 @@ type kubernetesCollector struct { | |
|
|
||
| currentNode node | ||
|
|
||
| // namespaces caches metadata for already requested namespaces to avoid repeated API calls. | ||
| namespaces map[string]namespace | ||
| namespacesLock sync.Mutex | ||
|
|
||
| ctx context.Context | ||
| cancel context.CancelFunc | ||
| wg sync.WaitGroup | ||
|
|
@@ -59,6 +63,7 @@ func startKubernetesCollector(client *kubeAPIClient, currentNodeName, logsPath, | |
| ctx: ctx, | ||
| cancel: cancel, | ||
| logsPath: logsPath, | ||
| namespaces: make(map[string]namespace), | ||
| } | ||
|
|
||
| storage := &remotewrite.Storage{} | ||
|
|
@@ -172,7 +177,8 @@ func (kc *kubernetesCollector) startWatchCluster(ctx context.Context, resourceVe | |
|
|
||
| func (kc *kubernetesCollector) startReadPodLogs(pod pod) { | ||
| startRead := func(pc podContainer, cs containerStatus) { | ||
| commonFields := getCommonFields(kc.currentNode, pod, cs) | ||
| ns := kc.getNamespace(pod.Metadata.Namespace) | ||
|
withlin marked this conversation as resolved.
Outdated
|
||
| commonFields := getCommonFields(kc.currentNode, ns, pod, cs) | ||
|
|
||
| filePath := kc.getLogFilePath(pod, pc, cs) | ||
|
|
||
|
|
@@ -202,7 +208,7 @@ func (kc *kubernetesCollector) startReadPodLogs(pod pod) { | |
| // Must be synced with getCommonFields. | ||
| var streamFieldNames = []string{"kubernetes.container_name", "kubernetes.pod_name", "kubernetes.pod_namespace"} | ||
|
|
||
| func getCommonFields(n node, p pod, cs containerStatus) []logstorage.Field { | ||
| func getCommonFields(n node, ns namespace, p pod, cs containerStatus) []logstorage.Field { | ||
| var fs logstorage.Fields | ||
|
|
||
| // Fields should match vector.dev kubernetes_source for easy migration. | ||
|
|
@@ -222,6 +228,15 @@ func getCommonFields(n node, p pod, cs containerStatus) []logstorage.Field { | |
| fs.Add(fieldName, v) | ||
| } | ||
|
|
||
| for k, v := range ns.Metadata.Labels { | ||
| fieldName := "kubernetes.namespace_labels." + k | ||
| fs.Add(fieldName, v) | ||
| } | ||
| for k, v := range ns.Metadata.Annotations { | ||
| fieldName := "kubernetes.namespace_annotations." + k | ||
| fs.Add(fieldName, v) | ||
| } | ||
|
|
||
| for k, v := range n.Metadata.Labels { | ||
| fieldName := "kubernetes.node_labels." + k | ||
| fs.Add(fieldName, v) | ||
|
|
@@ -257,3 +272,28 @@ func (kc *kubernetesCollector) stop() { | |
| kc.wg.Wait() | ||
| kc.fileCollector.stop() | ||
| } | ||
|
|
||
| func (kc *kubernetesCollector) getNamespace(namespaceName string) namespace { | ||
| kc.namespacesLock.Lock() | ||
| ns, ok := kc.namespaces[namespaceName] | ||
| kc.namespacesLock.Unlock() | ||
| if ok { | ||
| return ns | ||
| } | ||
|
|
||
| ns, err := kc.client.getNamespace(kc.ctx, namespaceName) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @valyala, can we use For example: pf := &prefixfilter.Filter{}
fc.excludeFilter.UpdateNeededFields(pf)
if *includeNamespaceLabels || *includeNamespaceAnnotations ||
pf.MatchStringOrWildcard("kubernetes.namespace_labels.*") ||
pf.MatchStringOrWildcard("kubernetes.namespace_annotations.*") {
// fetch namespace labels and annotations from Kubernetes API server...
}If the answer is 'yes' let's address this in a separate PR to keep this contribution simple.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I'm not entirely convinced this is a useful feature, as it won't really save much. There aren't many namespaces, and storing them in memory is practically free. However it could allow us to avoid requesting permissions for resources we don't actually need and completely avoid synchronous network calls within the |
||
| if err != nil { | ||
| logger.Errorf("cannot get namespace %q metadata: %s", namespaceName, err) | ||
| return namespace{ | ||
|
vadimalekseev marked this conversation as resolved.
Outdated
|
||
| Metadata: namespaceMetadata{ | ||
| Name: namespaceName, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| kc.namespacesLock.Lock() | ||
| kc.namespaces[namespaceName] = ns | ||
| kc.namespacesLock.Unlock() | ||
|
|
||
| return ns | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.