Get current node of a pod in Kubernetes source code

5/29/2019

I am doing with Kubernetes.

Now, I am doing with leaderelection in K8S.

Because I want to develop something here. So, I need to get the node which current pod belongs to.

For example,in this file leaderelection.go, after a pod becomes leader, I want to know which node this pod belongs to.

Can someone help me? Can you give me an example of code!

Thank you!

-- nguyennd
go
kubernetes

1 Answer

5/29/2019

First of all the codebase you are referring to is outdated.

You can use kubernetes/client-go library to query Kubernetes API. Refer to examples

Just a minimal code:

  pod, - := ks.clientSet.CoreV1().Pods(apiV1.NamespaceDefault).Get("your_pod_name", metaV1.GetOptions{})
  fmt.Printf("node name: %s", pod.Spec.NodeName)
-- diyoda_
Source: StackOverflow