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

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

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。它有以下方法对你的用例可能很有用。

func (meta *ObjectMeta) SetAnnotations(annotations map[string]string)
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.

func (meta *ObjectMeta) SetAnnotations(annotations map[string]string)
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

package main

import (
	"context"
	"fmt"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/rest"
)

func main() {
	config, _ := rest.InClusterConfig()
	clientset, _ := kubernetes.NewForConfig(config)
	pod, _ := clientset.CoreV1().Pods("default").Get(context.TODO(), "nginx-6799fc88d8-mzmcj", metav1.GetOptions{})
	for annotation_name, annotation_value := range pod.GetAnnotations(){
		fmt.Println(annotation_name, annotation_value)
	}
}

结果如下:

$ kubectl logs incluster-app-6dc44ddcf5-dxj8p
cni.projectcalico.org/containerID 7a63f9befd1174d68384adc05735fbcb1482dfe0d312839736531e90fa9fe790
cni.projectcalico.org/podIP 10.0.124.193/32
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:

package main

import (
	"context"
	"fmt"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/rest"
)

func main() {
	config, _ := rest.InClusterConfig()
	clientset, _ := kubernetes.NewForConfig(config)
	pod, _ := clientset.CoreV1().Pods("default").Get(context.TODO(), "nginx-6799fc88d8-mzmcj", metav1.GetOptions{})
	for annotation_name, annotation_value := range pod.GetAnnotations(){
		fmt.Println(annotation_name, annotation_value)
	}
}

and the result:

$ kubectl logs incluster-app-6dc44ddcf5-dxj8p
cni.projectcalico.org/containerID 7a63f9befd1174d68384adc05735fbcb1482dfe0d312839736531e90fa9fe790
cni.projectcalico.org/podIP 10.0.124.193/32
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:

确定