feat(inputs.kube_inventory): Support filtering pods and nodes by node name (#13993)
This commit is contained in:
parent
72fad0ed4d
commit
01b5834cb7
|
|
@ -55,6 +55,9 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
|
||||||
## Namespace to use. Set to "" to use all namespaces.
|
## Namespace to use. Set to "" to use all namespaces.
|
||||||
# namespace = "default"
|
# namespace = "default"
|
||||||
|
|
||||||
|
## Node name to filter to. No filtering by default.
|
||||||
|
# node_name = ""
|
||||||
|
|
||||||
## Use bearer token for authorization. ('bearer_token' takes priority)
|
## Use bearer token for authorization. ('bearer_token' takes priority)
|
||||||
##
|
##
|
||||||
## Ignored if url is empty and in-cluster config is used.
|
## Ignored if url is empty and in-cluster config is used.
|
||||||
|
|
@ -112,7 +115,6 @@ list "persistentvolumes" and "nodes". You will then need to make an [aggregated
|
||||||
ClusterRole][agg] that will eventually be bound to a user or group.
|
ClusterRole][agg] that will eventually be bound to a user or group.
|
||||||
|
|
||||||
[rbac]: https://kubernetes.io/docs/reference/access-authn-authz/rbac/
|
[rbac]: https://kubernetes.io/docs/reference/access-authn-authz/rbac/
|
||||||
|
|
||||||
[agg]: https://kubernetes.io/docs/reference/access-authn-authz/rbac/#aggregated-clusterroles
|
[agg]: https://kubernetes.io/docs/reference/access-authn-authz/rbac/#aggregated-clusterroles
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
|
|
@ -366,7 +368,7 @@ tls_key = "/run/telegraf-kubernetes-key"
|
||||||
The node status ready can mean 3 different values.
|
The node status ready can mean 3 different values.
|
||||||
|
|
||||||
| Tag value | Corresponding field value | Meaning |
|
| Tag value | Corresponding field value | Meaning |
|
||||||
| --------- | ------------------------- | -------
|
| --------- | ------------------------- | -------- |
|
||||||
| ready | 0 | NotReady |
|
| ready | 0 | NotReady |
|
||||||
| ready | 1 | Ready |
|
| ready | 1 | Ready |
|
||||||
| ready | 2 | Unknown |
|
| ready | 2 | Unknown |
|
||||||
|
|
|
||||||
|
|
@ -86,10 +86,14 @@ func (c *client) getIngress(ctx context.Context) (*netv1.IngressList, error) {
|
||||||
return c.NetworkingV1().Ingresses(c.namespace).List(ctx, metav1.ListOptions{})
|
return c.NetworkingV1().Ingresses(c.namespace).List(ctx, metav1.ListOptions{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *client) getNodes(ctx context.Context) (*corev1.NodeList, error) {
|
func (c *client) getNodes(ctx context.Context, name string) (*corev1.NodeList, error) {
|
||||||
ctx, cancel := context.WithTimeout(ctx, c.timeout)
|
ctx, cancel := context.WithTimeout(ctx, c.timeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
return c.CoreV1().Nodes().List(ctx, metav1.ListOptions{})
|
var fieldSelector string
|
||||||
|
if name != "" {
|
||||||
|
fieldSelector = "metadata.name=" + name
|
||||||
|
}
|
||||||
|
return c.CoreV1().Nodes().List(ctx, metav1.ListOptions{FieldSelector: fieldSelector})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *client) getPersistentVolumes(ctx context.Context) (*corev1.PersistentVolumeList, error) {
|
func (c *client) getPersistentVolumes(ctx context.Context) (*corev1.PersistentVolumeList, error) {
|
||||||
|
|
@ -104,10 +108,14 @@ func (c *client) getPersistentVolumeClaims(ctx context.Context) (*corev1.Persist
|
||||||
return c.CoreV1().PersistentVolumeClaims(c.namespace).List(ctx, metav1.ListOptions{})
|
return c.CoreV1().PersistentVolumeClaims(c.namespace).List(ctx, metav1.ListOptions{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *client) getPods(ctx context.Context) (*corev1.PodList, error) {
|
func (c *client) getPods(ctx context.Context, nodeName string) (*corev1.PodList, error) {
|
||||||
ctx, cancel := context.WithTimeout(ctx, c.timeout)
|
ctx, cancel := context.WithTimeout(ctx, c.timeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
return c.CoreV1().Pods(c.namespace).List(ctx, metav1.ListOptions{})
|
var fieldSelector string
|
||||||
|
if nodeName != "" {
|
||||||
|
fieldSelector = "spec.nodeName=" + nodeName
|
||||||
|
}
|
||||||
|
return c.CoreV1().Pods(c.namespace).List(ctx, metav1.ListOptions{FieldSelector: fieldSelector})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *client) getServices(ctx context.Context) (*corev1.ServiceList, error) {
|
func (c *client) getServices(ctx context.Context) (*corev1.ServiceList, error) {
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,7 @@ type KubernetesInventory struct {
|
||||||
|
|
||||||
SelectorInclude []string `toml:"selector_include"`
|
SelectorInclude []string `toml:"selector_include"`
|
||||||
SelectorExclude []string `toml:"selector_exclude"`
|
SelectorExclude []string `toml:"selector_exclude"`
|
||||||
|
NodeName string `toml:"node_name"`
|
||||||
Log telegraf.Logger `toml:"-"`
|
Log telegraf.Logger `toml:"-"`
|
||||||
|
|
||||||
tls.ClientConfig
|
tls.ClientConfig
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func collectNodes(ctx context.Context, acc telegraf.Accumulator, ki *KubernetesInventory) {
|
func collectNodes(ctx context.Context, acc telegraf.Accumulator, ki *KubernetesInventory) {
|
||||||
list, err := ki.client.getNodes(ctx)
|
list, err := ki.client.getNodes(ctx, ki.NodeName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
acc.AddError(err)
|
acc.AddError(err)
|
||||||
return
|
return
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func collectPods(ctx context.Context, acc telegraf.Accumulator, ki *KubernetesInventory) {
|
func collectPods(ctx context.Context, acc telegraf.Accumulator, ki *KubernetesInventory) {
|
||||||
list, err := ki.client.getPods(ctx)
|
list, err := ki.client.getPods(ctx, ki.NodeName)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
acc.AddError(err)
|
acc.AddError(err)
|
||||||
return
|
return
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,9 @@
|
||||||
## Namespace to use. Set to "" to use all namespaces.
|
## Namespace to use. Set to "" to use all namespaces.
|
||||||
# namespace = "default"
|
# namespace = "default"
|
||||||
|
|
||||||
|
## Node name to filter to. No filtering by default.
|
||||||
|
# node_name = ""
|
||||||
|
|
||||||
## Use bearer token for authorization. ('bearer_token' takes priority)
|
## Use bearer token for authorization. ('bearer_token' takes priority)
|
||||||
##
|
##
|
||||||
## Ignored if url is empty and in-cluster config is used.
|
## Ignored if url is empty and in-cluster config is used.
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue