英文:
How to access metadata.labels.{custom_field} in service.yaml with client-go?
问题
我目前正在使用Go语言进行一个边缘项目的开发。我想要获取集群上运行的Pod的信息。
根据命名空间的值,我可以访问到Pods,但是为了访问到service.yaml文件中metadata.labels.applicationGroup值对应的工作中的Pods,我首先需要获取这个值。
我在下面添加了service.yaml文件的一部分内容。
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: metadata-name
labels:
service: service-name
applicationGroup: beta --> 这个字段
spec:
replicas: 1
selector:
matchLabels:
service: service-name
template:
metadata:
labels:
service: service-name
spec:
containers:
- name: nginx
image: nginx:1.15.8
ports:
- containerPort: 80
...
目前,我可以访问"default"命名空间下的Pods信息。
func getPodsInfo() (string, error) {
var kubeConfig *string
if home := homedir.HomeDir(); home != "" {
kubeConfig = flag.String("kubeConfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeConfig file")
} else {
kubeConfig = flag.String("kubeConfig", "", "absolute path to the kubeConfig file")
}
flag.Parse()
config, err := clientcmd.BuildConfigFromFlags("", *kubeConfig)
if err != nil {
err = fmt.Errorf("error occured when build config from flags: %s", err.Error())
return "", err
}
clientSet, err := kubernetes.NewForConfig(config)
if err != nil {
err = fmt.Errorf("error occured when build client set from config: %s", err.Error())
return "", err
}
/*
listOptions := meta.ListOptions{
FieldSelector: "metadata.labels.applicationGroup=alpha",
}
*/
pods, err := clientSet.CoreV1().Pods("default").List(context.TODO(), metav1.ListOptions{})
if err != nil {
err = fmt.Errorf("error occured when get pods info from k8s: %s", err.Error())
return "", err
}
podsInfo, err := json.Marshal(&pods)
if err != nil {
err = fmt.Errorf("error occured when json marshal: %s", err.Error())
return "", err
}
return string(podsInfo), nil
}
我尝试使用FieldSelector,但是FieldSelector只支持一些字段。
listOptions := meta.ListOptions{
FieldSelector: "metadata.labels.applicationGroup=alpha",
}
然后我得到了以下错误。
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.
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: metadata-name
labels:
service: service-name
applicationGroup: beta --> this field
spec:
replicas: 1
selector:
matchLabels:
service: service-name
template:
metadata:
labels:
service: service-name
spec:
containers:
- name: nginx
image: nginx:1.15.8
ports:
- containerPort: 80
...
Right now, I can access information pods with "default" namespaces.
func getPodsInfo() (string, error) {
var kubeConfig *string
if home := homedir.HomeDir(); home != "" {
kubeConfig = flag.String("kubeConfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeConfig file")
} else {
kubeConfig = flag.String("kubeConfig", "", "absolute path to the kubeConfig file")
}
flag.Parse()
config, err := clientcmd.BuildConfigFromFlags("", *kubeConfig)
if err != nil {
err = fmt.Errorf("error occured when build config from flags: %s", err.Error())
return "", err
}
clientSet, err := kubernetes.NewForConfig(config)
if err != nil {
err = fmt.Errorf("error occured when build client set from config: %s", err.Error())
return "", err
}
/*
listOptions := meta.ListOptions{
FieldSelector: "metadata.labels.applicationGroup=alpha",
}
*/
pods, err := clientSet.CoreV1().Pods("default").List(context.TODO(), metav1.ListOptions{})
if err != nil {
err = fmt.Errorf("error occured when get pods info from k8s: %s", err.Error())
return "", err
}
podsInfo, err := json.Marshal(&pods)
if err != nil {
err = fmt.Errorf("error occured when json marshal: %s", err.Error())
return "", err
}
return string(podsInfo), nil
}
I tried to FieldSelector, but FieldSelector has supported some fields.
listOptions := meta.ListOptions{
FieldSelector: "metadata.labels.applicationGroup=alpha",
}
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。
listOptions := meta.ListOptions{
LabelSelector: "applicationGroup=alpha",
}
更新:如果您想在Pod上使用它,您应该在Pod模板上设置applicationGroup。
apiVersion: apps/v1
kind: Deployment
metadata:
name: metadata-name
labels:
service: service-name
spec:
replicas: 1
selector:
matchLabels:
service: service-name
template:
metadata:
labels:
service: service-name
applicationGroup: beta
spec:
containers:
- name: nginx
image: nginx:1.15.8
ports:
- containerPort: 80
英文:
Just use LabelSelector instead of FieldSelector
listOptions := meta.ListOptions{
LabelSelector: "applicationGroup=alpha",
}
UPD: If you want to use it for pods you should set applicationGroup on pod template
apiVersion: apps/v1
kind: Deployment
metadata:
name: metadata-name
labels:
service: service-name
spec:
replicas: 1
selector:
matchLabels:
service: service-name
template:
metadata:
labels:
service: service-name
applicationGroup: beta
spec:
containers:
- name: nginx
image: nginx:1.15.8
ports:
- containerPort: 80
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论