Skip to content
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

Merged
merged 4 commits into from
Jan 12, 2018

Conversation

vjsamuel
Copy link
Contributor

This PR adds auto discovery for kubernetes. Some sample specs for using this feature:

Filebeat: (discover all workloads in default namespace)

filebeat.autodiscover:
  providers:
    - type: kubernetes
      in_cluster: false
      kube_config: /home/docker/config
      host: minikube
      templates:
        - condition:
            equals:
              kubernetes.namespace: default
          config:
            - type: docker
              containers.ids:
                - "${data.kubernetes.container.id}"


output.console.pretty: true

Metricbeat: query for pods with label project: prometheus

metricbeat.autodiscover:
  providers:
    - type: kubernetes
      in_cluster: false
      kube_config: /home/docker/config
      host: minikube
      templates:
        - condition:
            equals:
              kubernetes.labels.project: prometheus
          config:
            - module: prometheus
              metricsets: ["stats"]
              hosts: "${data.host}:9090"

output.console.pretty: true

@elasticmachine
Copy link
Collaborator

Can one of the admins verify this patch?

return client, nil
}

func DiscoverKubernetesNode(host string, client *k8s.Client) string {

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

"github.com/elastic/beats/libbeat/logp"
)

func GetKubernetesClient(in_cluster bool, kube_config string) (client *k8s.Client, err error) {

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

@@ -107,6 +108,17 @@ type Pod struct {
Status PodStatus `json:"status"`
}

func (s *PodContainerStatus) GetContainerID() string {

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() {

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() {

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

Copy link
Contributor

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Contributor

@ruflin ruflin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Would like to also get a review from @exekias o this.

One part that is missing is docs. @exekias Should we open a meta issue to track the changes to make sure we don't forget about docs afterwards?

@@ -47,6 +47,9 @@ func (g *metaGenerator) PodMetadata(pod *Pod) common.MapStr {
"pod": common.MapStr{
"name": pod.Metadata.Name,
},
"node": common.MapStr{
Copy link
Contributor

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?

)

func GetKubernetesClient(in_cluster bool, kube_config string) (client *k8s.Client, err error) {
if in_cluster == true {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We normally use inClusterfor naming, same for kubeConfig

if podName == "localhost" {
host = "localhost"
} else {
pod, error := client.CoreV1().GetPod(ctx, podName, client.Namespace)
Copy link
Contributor

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

@ruflin
Copy link
Contributor

ruflin commented Jan 12, 2018

jenkins, test it


}
}
config.Host = kubernetes.DiscoverKubernetesNode(config.Host, client)
Copy link
Contributor

@walktall walktall Jan 12, 2018

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Contributor

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.

@vjsamuel vjsamuel force-pushed the kubernetes_autodiscover branch 3 times, most recently from 61a916a to e487576 Compare January 12, 2018 05:45
@@ -98,3 +107,32 @@ type Pod struct {
Spec PodSpec `json:"spec"`
Status PodStatus `json:"status"`
}

func (s *PodContainerStatus) GetContainerID() string {

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

}
}

func NewModule(base mb.BaseModule) (mb.Module, error) {

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

@@ -98,3 +107,32 @@ type Pod struct {
Spec PodSpec `json:"spec"`
Status PodStatus `json:"status"`
}

func (s *PodContainerStatus) GetContainerID() string {

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

}
}

func NewModule(base mb.BaseModule) (mb.Module, error) {

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

@vjsamuel vjsamuel force-pushed the kubernetes_autodiscover branch from e487576 to 8415b40 Compare January 12, 2018 06:22
Copy link
Contributor

@exekias exekias left a 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

@exekias
Copy link
Contributor

exekias commented Jan 12, 2018

ok to test

@@ -107,6 +108,18 @@ type Pod struct {
Status PodStatus `json:"status"`
}

// Parse the container ID to get the actual ID string

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 ..."

@@ -107,6 +108,18 @@ type Pod struct {
Status PodStatus `json:"status"`
}

// Parse the container ID to get the actual ID string

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 ..."

@vjsamuel vjsamuel force-pushed the kubernetes_autodiscover branch from db2cf2b to 37cfd21 Compare January 12, 2018 17:02
@exekias
Copy link
Contributor

exekias commented Jan 12, 2018

ok to test

@exekias exekias merged commit 7fbd75d into elastic:master Jan 12, 2018
@exekias
Copy link
Contributor

exekias commented Jan 12, 2018

Awesome work @vjsamuel, as usual, thank you! 🎉

@vjsamuel vjsamuel deleted the kubernetes_autodiscover branch January 12, 2018 22:10
@dedemorton dedemorton mentioned this pull request Jan 17, 2018
37 tasks
@dedemorton
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants