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

[Discuss&POC] refactor k8s client to k8s.io/client-go #6117

Closed
wants to merge 2 commits into from

Conversation

walktall
Copy link
Contributor

@walktall walktall commented Jan 19, 2018

Ref: #6068

I didn't update vendor and fully test it yet, just open this PR for discuss.

Will be very happy if you can take a look! @exekias @vjsamuel @ruflin

Maybe it's still too big for bin to change client, but I abstract the common/kubernetes pkg and make it easier to use hopefully.

And also, found 2 bugs while refactoring.

  • query pod meta won't refresh deleted map timeout
  • delete event for k8s autodiscovery will be delayed since watcher delay the event push

With one another enhancement: beat can auto discovery correct k8s node now. So host in config might only be useful when you are testing...

Now beat can detect host by itself when outside cluster

It also fixes 2 bugs:

 * query pod meta won't refrese deleted map timeout
 * delete event for k8s autodiscovery will be delayed since watcher delay the event push
@@ -0,0 +1,69 @@
package add_kubernetes_metadata

Choose a reason for hiding this comment

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

don't use an underscore in package name


return po
func (e *Event) GetMetadata() *ObjectMeta {

Choose a reason for hiding this comment

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

exported method Event.GetMetadata should have comment or be unexported

logp.Warn("Unable to marshal %v", pod.String())
return nil
}
type Event struct {

Choose a reason for hiding this comment

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

exported type Event should have comment or be unexported

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

func (p *Pod) GetMetadata() *ObjectMeta {

Choose a reason for hiding this comment

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

exported method Pod.GetMetadata should have comment or be unexported

)

type Resource interface {

Choose a reason for hiding this comment

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

exported type Resource should have comment or be unexported

@elasticmachine
Copy link
Collaborator

Since this is a community submitted pull request, a Jenkins build has not been kicked off automatically. Can an Elastic organization member please verify the contents of this patch and then kick off a build manually?

@ruflin
Copy link
Contributor

ruflin commented Jan 21, 2018

@walktall Thanks for taking a stab at this. I haven't looked much at the code yet but I saw you mentioned that you discovered / solved two issue. I'm wondering if these could go into a separate PR to have them in rather soon or these depend on changing the library?

@ruflin ruflin added in progress Pull request is currently in progress. discuss Issue needs further discussion. libbeat labels Jan 21, 2018
@ruflin ruflin requested a review from exekias January 21, 2018 21:58
@walktall
Copy link
Contributor Author

@ruflin Yes, I will make another PR about two issues only.

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.

Overall I like very much how this is looking, thank you for taking this!! 🎉

These changes are tackling different concerns, so I think they should go on different PRs, at least 4:

  • [bugfix] query pod meta won't refresh deleted map timeout
  • Switch to client-go (is the size still +30MB?)
  • Watcher API refactor
  • Node discovery improvements

About: "delete event for k8s autodiscovery will be delayed since watcher delay the event push", this is not a bug but a feature :)

Especially when handling logs, but also with some metrics, you want to delay the shutdown of the module to ensure you get the latest events from the pod. If we stop reading the log just after the pod dies, last lines won't be read.

func inClusterNamespace() string {
// This way assumes you've set the POD_NAMESPACE environment variable using the downward API.
// This check has to be done first for backwards compatibility with the way InClusterConfig was originally set up
if ns := os.Getenv("POD_NAMESPACE"); ns != "" {
Copy link
Contributor

Choose a reason for hiding this comment

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

We stopped using this, in favor of client.Namespace, what's the motivation for this change?

Copy link
Contributor Author

@walktall walktall Jan 23, 2018

Choose a reason for hiding this comment

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

Since k8s.io/client-go doesn't have a field Namespace in clientset, so client.Namespace is kind like a tricky in ericchiang/k8s I think. This func is borrowed from client-go, and after rethink about it, I think user should not need to set namespace by downward API since if beat is in cluster, then it should be auto detected.

return defaultNode
}
for _, n := range nodes.Items {
if n.Status.NodeInfo.MachineID == mid {
Copy link
Contributor

Choose a reason for hiding this comment

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

Cool addition, I guess all the fallback mechanisms make node discovery bulletproof :)


// ListenStop returns a bus listener to receive pod stopped events, with a `pod` key holding it
ListenStop() bus.Listener
AddEventHandler(ResourceEventHandler)
Copy link
Contributor

@exekias exekias Jan 22, 2018

Choose a reason for hiding this comment

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

💯 to this approach, I've been wanting to change it to this since I wrote the current one 😅

@walktall
Copy link
Contributor Author

walktall commented Jan 23, 2018

@exekias Thank you for your comments! I will make PRs step by step. And sorry for misunderstanding the feature in auto discovery as a bug 😅

@walktall
Copy link
Contributor Author

@exekias For switching client-go:

With go 1.9.2, build with go build -ldflags="-s -w", it grows 12mb, from 53mb to 65mb.

With go 1.9.2, build with go build, it grows 19mb. From 104mb to 123mb.

Anyway, I will put the switching pr the last one, since it is only imported by common/kubernetes.

@ruflin
Copy link
Contributor

ruflin commented Feb 27, 2018

@walktall Did you ever investigate if we could sligthly tweak the import to massively reduce the dependency. I had a similar experience with docker.

@vjsamuel
Copy link
Contributor

i think that might be a good exercise to see if it works but the Kube client as is does a lot of things in addition to just making API calls.

@walktall
Copy link
Contributor Author

@ruflin I tried it before but I can not find it out...

@vjsamuel If we want to use k8s informer, then it is not just making API calls I think.

@walktall
Copy link
Contributor Author

Close due to no further plan for this right now since library currently used works well.

@walktall walktall closed this Mar 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discuss Issue needs further discussion. in progress Pull request is currently in progress. libbeat
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants