How can I get k8s annotation in a pod by golang?

huangapple go评论132阅读模式
英文:

How can I get k8s annotation in a pod by golang?

问题

当我在 Kubernetes 中运行一个 Golang 应用程序时,我想要获取它所在的 Pod 的注解。

有没有任何库可以帮助我实现这个功能?

英文:

When I'm running a Golang application in a Kubernetes, I want to get the annotations of the pod where it's running.

Is there any library can help me to do that?

答案1

得分: 0

你正在寻找库 https://github.com/kubernetes/apimachinery。它有以下方法对你的用例可能很有用。

  1. func (meta *ObjectMeta) SetAnnotations(annotations map[string]string)
  2. func HasAnnotation(obj ObjectMeta, ann string) bool

参考链接:

  1. https://pkg.go.dev/k8s.io/apimachinery@v0.17.0/pkg/apis/meta/v1#HasAnnotation
  2. https://pkg.go.dev/k8s.io/apimachinery@v0.17.0/pkg/apis/meta/v1#ObjectMeta.SetAnnotations
英文:

You are looking for the library

https://github.com/kubernetes/apimachinery. It has methods like below which can be usefull for your usecase.

  1. func (meta *ObjectMeta) SetAnnotations(annotations map[string]string)
  2. func HasAnnotation(obj ObjectMeta, ann string) bool

Ref:

  1. https://pkg.go.dev/k8s.io/apimachinery@v0.17.0/pkg/apis/meta/v1#HasAnnotation
  2. https://pkg.go.dev/k8s.io/apimachinery@v0.17.0/pkg/apis/meta/v1#ObjectMeta.SetAnnotations

答案2

得分: 0

假设您知道要检索注释的 Pod 的名称,使用下面的示例 Pod nginx-6799fc88d8-mzmcj

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  6. "k8s.io/client-go/kubernetes"
  7. "k8s.io/client-go/rest"
  8. )
  9. func main() {
  10. config, _ := rest.InClusterConfig()
  11. clientset, _ := kubernetes.NewForConfig(config)
  12. pod, _ := clientset.CoreV1().Pods("default").Get(context.TODO(), "nginx-6799fc88d8-mzmcj", metav1.GetOptions{})
  13. for annotation_name, annotation_value := range pod.GetAnnotations(){
  14. fmt.Println(annotation_name, annotation_value)
  15. }
  16. }

结果如下:

  1. $ kubectl logs incluster-app-6dc44ddcf5-dxj8p
  2. cni.projectcalico.org/containerID 7a63f9befd1174d68384adc05735fbcb1482dfe0d312839736531e90fa9fe790
  3. cni.projectcalico.org/podIP 10.0.124.193/32
  4. cni.projectcalico.org/podIPs 10.0.124.193/32
英文:

Assuming you know the name of the pod of which you want to retrieve the annotations, using example pod nginx-6799fc88d8-mzmcj below:

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  6. "k8s.io/client-go/kubernetes"
  7. "k8s.io/client-go/rest"
  8. )
  9. func main() {
  10. config, _ := rest.InClusterConfig()
  11. clientset, _ := kubernetes.NewForConfig(config)
  12. pod, _ := clientset.CoreV1().Pods("default").Get(context.TODO(), "nginx-6799fc88d8-mzmcj", metav1.GetOptions{})
  13. for annotation_name, annotation_value := range pod.GetAnnotations(){
  14. fmt.Println(annotation_name, annotation_value)
  15. }
  16. }

and the result:

  1. $ kubectl logs incluster-app-6dc44ddcf5-dxj8p
  2. cni.projectcalico.org/containerID 7a63f9befd1174d68384adc05735fbcb1482dfe0d312839736531e90fa9fe790
  3. cni.projectcalico.org/podIP 10.0.124.193/32
  4. cni.projectcalico.org/podIPs 10.0.124.193/32

huangapple
  • 本文由 发表于 2021年9月14日 18:00:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/69175719.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定