-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Add autodiscover for kubernetes #6055
Conversation
Can one of the admins verify this patch? |
libbeat/common/kubernetes/util.go
Outdated
return client, nil | ||
} | ||
|
||
func DiscoverKubernetesNode(host string, client *k8s.Client) string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exported function DiscoverKubernetesNode should have comment or be unexported
libbeat/common/kubernetes/util.go
Outdated
"github.com/elastic/beats/libbeat/logp" | ||
) | ||
|
||
func GetKubernetesClient(in_cluster bool, kube_config string) (client *k8s.Client, err error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exported function GetKubernetesClient should have comment or be unexported
don't use underscores in Go names; func parameter in_cluster should be inCluster
don't use underscores in Go names; func parameter kube_config should be kubeConfig
libbeat/common/kubernetes/types.go
Outdated
@@ -107,6 +108,17 @@ type Pod struct { | |||
Status PodStatus `json:"status"` | |||
} | |||
|
|||
func (s *PodContainerStatus) GetContainerID() string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exported method PodContainerStatus.GetContainerID should have comment or be unexported
p.bus.Publish(event) | ||
} | ||
|
||
func (p *Provider) Stop() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exported method Provider.Stop should have comment or be unexported
}, nil | ||
} | ||
|
||
func (p *Provider) Start() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exported method Provider.Start should have comment or be unexported
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vjsamuel lately we have been trying to make hound happy for most of its messages, could you add these comments?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
327c662
to
27a1825
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -47,6 +47,9 @@ func (g *metaGenerator) PodMetadata(pod *Pod) common.MapStr { | |||
"pod": common.MapStr{ | |||
"name": pod.Metadata.Name, | |||
}, | |||
"node": common.MapStr{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this need an update to our fields.yml
or is node
already in there?
libbeat/common/kubernetes/util.go
Outdated
) | ||
|
||
func GetKubernetesClient(in_cluster bool, kube_config string) (client *k8s.Client, err error) { | ||
if in_cluster == true { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We normally use inCluster
for naming, same for kubeConfig
libbeat/common/kubernetes/util.go
Outdated
if podName == "localhost" { | ||
host = "localhost" | ||
} else { | ||
pod, error := client.CoreV1().GetPod(ctx, podName, client.Namespace) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: We often use err
instead of error
jenkins, test it |
|
||
} | ||
} | ||
config.Host = kubernetes.DiscoverKubernetesNode(config.Host, client) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The behavior of this Func is odd when beat is put outside of k8s cluster. As far as I know, host is used as a node selector while making watch requests. So when beat is deployed outside the cluster, host should be ignored or it won't get any watched info from k8s. And if beat is put in cluster, host should be set by k8s downward api I think. So should we remove this host config since it is either wrong in case outside cluster or error-prone while user specify a wrong host in config?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@walktall, this piece of code when provided with a host just uses the host. it is good especially when you are testing locally without having to deploy the beat onto Kubernetes. when running in cluster, this code, gets the pod name and uses it to get the pod spec and figures out what host the pod is deployed on.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it is useful while test beat locally since user can set host to k8s node name. But if it is deployed outside the cluster or as a k8s deployment, we still need to ignore host anyway. Say I put beat in another k8s cluster, the result will be wrong here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when running in cluster, this code, gets the pod name and uses it to get the pod spec and figures out what host the pod is deployed on.
I prefer using k8s downward api here to get pod name though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general, watching all nodes from outside the cluster may suffer from other issues, like scalability.
These settings work best when running from inside the cluster, or orchestrating beats as part of the deployment (one per node, with the host value set).
We can discuss this in a new issue if you find the idea worth exploring, as I want to move this PR forward, and it only moved code around to get a cleaner interface.
61a916a
to
e487576
Compare
libbeat/common/kubernetes/types.go
Outdated
@@ -98,3 +107,32 @@ type Pod struct { | |||
Spec PodSpec `json:"spec"` | |||
Status PodStatus `json:"status"` | |||
} | |||
|
|||
func (s *PodContainerStatus) GetContainerID() string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exported method PodContainerStatus.GetContainerID should have comment or be unexported
metricbeat/module/uwsgi/uwsgi.go
Outdated
} | ||
} | ||
|
||
func NewModule(base mb.BaseModule) (mb.Module, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exported function NewModule should have comment or be unexported
libbeat/common/kubernetes/types.go
Outdated
@@ -98,3 +107,32 @@ type Pod struct { | |||
Spec PodSpec `json:"spec"` | |||
Status PodStatus `json:"status"` | |||
} | |||
|
|||
func (s *PodContainerStatus) GetContainerID() string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exported method PodContainerStatus.GetContainerID should have comment or be unexported
metricbeat/module/uwsgi/uwsgi.go
Outdated
} | ||
} | ||
|
||
func NewModule(base mb.BaseModule) (mb.Module, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exported function NewModule should have comment or be unexported
e487576
to
8415b40
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, nice helper functions, and the node name addition.
We will need to open a follow-up PR for documentation
ok to test |
libbeat/common/kubernetes/types.go
Outdated
@@ -107,6 +108,18 @@ type Pod struct { | |||
Status PodStatus `json:"status"` | |||
} | |||
|
|||
// Parse the container ID to get the actual ID string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comment on exported method PodContainerStatus.GetContainerID should be of the form "GetContainerID ..."
libbeat/common/kubernetes/types.go
Outdated
@@ -107,6 +108,18 @@ type Pod struct { | |||
Status PodStatus `json:"status"` | |||
} | |||
|
|||
// Parse the container ID to get the actual ID string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comment on exported method PodContainerStatus.GetContainerID should be of the form "GetContainerID ..."
db2cf2b
to
37cfd21
Compare
ok to test |
Awesome work @vjsamuel, as usual, thank you! 🎉 |
I see that docs are added in #6065, so I'm removing the needs_docs label from this PR. Please open an issue against docs if additional changes are required. |
This PR adds auto discovery for kubernetes. Some sample specs for using this feature:
Filebeat: (discover all workloads in default namespace)
Metricbeat: query for pods with label
project: prometheus