如何使用client-go在service.yaml中访问metadata.labels.{custom_field}?

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

How to access metadata.labels.{custom_field} in service.yaml with client-go?

问题

我目前正在使用Go语言进行一个边缘项目的开发。我想要获取集群上运行的Pod的信息。

根据命名空间的值,我可以访问到Pods,但是为了访问到service.yaml文件中metadata.labels.applicationGroup值对应的工作中的Pods,我首先需要获取这个值。

我在下面添加了service.yaml文件的一部分内容。

  1. ---
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: metadata-name
  6. labels:
  7. service: service-name
  8. applicationGroup: beta --> 这个字段
  9. spec:
  10. replicas: 1
  11. selector:
  12. matchLabels:
  13. service: service-name
  14. template:
  15. metadata:
  16. labels:
  17. service: service-name
  18. spec:
  19. containers:
  20. - name: nginx
  21. image: nginx:1.15.8
  22. ports:
  23. - containerPort: 80
  24. ...

目前,我可以访问"default"命名空间下的Pods信息。

  1. func getPodsInfo() (string, error) {
  2. var kubeConfig *string
  3. if home := homedir.HomeDir(); home != "" {
  4. kubeConfig = flag.String("kubeConfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeConfig file")
  5. } else {
  6. kubeConfig = flag.String("kubeConfig", "", "absolute path to the kubeConfig file")
  7. }
  8. flag.Parse()
  9. config, err := clientcmd.BuildConfigFromFlags("", *kubeConfig)
  10. if err != nil {
  11. err = fmt.Errorf("error occured when build config from flags: %s", err.Error())
  12. return "", err
  13. }
  14. clientSet, err := kubernetes.NewForConfig(config)
  15. if err != nil {
  16. err = fmt.Errorf("error occured when build client set from config: %s", err.Error())
  17. return "", err
  18. }
  19. /*
  20. listOptions := meta.ListOptions{
  21. FieldSelector: "metadata.labels.applicationGroup=alpha",
  22. }
  23. */
  24. pods, err := clientSet.CoreV1().Pods("default").List(context.TODO(), metav1.ListOptions{})
  25. if err != nil {
  26. err = fmt.Errorf("error occured when get pods info from k8s: %s", err.Error())
  27. return "", err
  28. }
  29. podsInfo, err := json.Marshal(&pods)
  30. if err != nil {
  31. err = fmt.Errorf("error occured when json marshal: %s", err.Error())
  32. return "", err
  33. }
  34. return string(podsInfo), nil
  35. }

我尝试使用FieldSelector,但是FieldSelector只支持一些字段。

  1. listOptions := meta.ListOptions{
  2. FieldSelector: "metadata.labels.applicationGroup=alpha",
  3. }

然后我得到了以下错误。

field label not supported: metadata.labels.applicationGroup

你可以查看这个链接来获取支持的字段列表。

对于我的问题,我该如何使用client-go来获取metadata.labels.applicationGroup的值,并且使用哪个函数来获取使用该值的Pods?

英文:

I am currently working on a side project with Go. I'm trying to get the information about the pods running on the cluster.

I can reach the pods according to the namespace value, but in order to reach the working pods with the metadata.labels.applicationGroup value in the service.yaml file, I need to obtain this value first.

I added below a part of my service.yaml file.

  1. ---
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: metadata-name
  6. labels:
  7. service: service-name
  8. applicationGroup: beta --> this field
  9. spec:
  10. replicas: 1
  11. selector:
  12. matchLabels:
  13. service: service-name
  14. template:
  15. metadata:
  16. labels:
  17. service: service-name
  18. spec:
  19. containers:
  20. - name: nginx
  21. image: nginx:1.15.8
  22. ports:
  23. - containerPort: 80
  24. ...

Right now, I can access information pods with "default" namespaces.

  1. func getPodsInfo() (string, error) {
  2. var kubeConfig *string
  3. if home := homedir.HomeDir(); home != "" {
  4. kubeConfig = flag.String("kubeConfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeConfig file")
  5. } else {
  6. kubeConfig = flag.String("kubeConfig", "", "absolute path to the kubeConfig file")
  7. }
  8. flag.Parse()
  9. config, err := clientcmd.BuildConfigFromFlags("", *kubeConfig)
  10. if err != nil {
  11. err = fmt.Errorf("error occured when build config from flags: %s", err.Error())
  12. return "", err
  13. }
  14. clientSet, err := kubernetes.NewForConfig(config)
  15. if err != nil {
  16. err = fmt.Errorf("error occured when build client set from config: %s", err.Error())
  17. return "", err
  18. }
  19. /*
  20. listOptions := meta.ListOptions{
  21. FieldSelector: "metadata.labels.applicationGroup=alpha",
  22. }
  23. */
  24. pods, err := clientSet.CoreV1().Pods("default").List(context.TODO(), metav1.ListOptions{})
  25. if err != nil {
  26. err = fmt.Errorf("error occured when get pods info from k8s: %s", err.Error())
  27. return "", err
  28. }
  29. podsInfo, err := json.Marshal(&pods)
  30. if err != nil {
  31. err = fmt.Errorf("error occured when json marshal: %s", err.Error())
  32. return "", err
  33. }
  34. return string(podsInfo), nil
  35. }

I tried to FieldSelector, but FieldSelector has supported some fields.

  1. listOptions := meta.ListOptions{
  2. FieldSelector: "metadata.labels.applicationGroup=alpha",
  3. }

And I got the below error.

field label not supported: metadata.labels.applicationGroup

You can check this link for the supported fields.

To my question, how can I reach the metadata.labels.applicationGroup value with cilent_go, and which function should I use to reach pods working with this value?

答案1

得分: 1

只需使用LabelSelector而不是FieldSelector。

  1. listOptions := meta.ListOptions{
  2. LabelSelector: "applicationGroup=alpha",
  3. }

更新:如果您想在Pod上使用它,您应该在Pod模板上设置applicationGroup。

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: metadata-name
  5. labels:
  6. service: service-name
  7. spec:
  8. replicas: 1
  9. selector:
  10. matchLabels:
  11. service: service-name
  12. template:
  13. metadata:
  14. labels:
  15. service: service-name
  16. applicationGroup: beta
  17. spec:
  18. containers:
  19. - name: nginx
  20. image: nginx:1.15.8
  21. ports:
  22. - containerPort: 80
英文:

Just use LabelSelector instead of FieldSelector

  1. listOptions := meta.ListOptions{
  2. LabelSelector: "applicationGroup=alpha",
  3. }

UPD: If you want to use it for pods you should set applicationGroup on pod template

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: metadata-name
  5. labels:
  6. service: service-name
  7. spec:
  8. replicas: 1
  9. selector:
  10. matchLabels:
  11. service: service-name
  12. template:
  13. metadata:
  14. labels:
  15. service: service-name
  16. applicationGroup: beta
  17. spec:
  18. containers:
  19. - name: nginx
  20. image: nginx:1.15.8
  21. ports:
  22. - containerPort: 80

huangapple
  • 本文由 发表于 2022年4月13日 08:18:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/71850457.html
匿名

发表评论

匿名网友

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

确定