Quiero enumerar mis trabajos de k8s con un selector de etiquetas por cliente, vaya como este comando:
$ kubectl get jobs -l 'hello-world in (London, China, NewYork)'Revisé el código fuente de client-go, luego escribí un código como este:
func listJobs(cli *kubernetes.Clientset) (*batchv1.JobList, error) { label := metav1.LabelSelector{ MatchExpressions: []metav1.LabelSelectorRequirement{ { Key: "hello-world", Operator: metav1.LabelSelectorOpIn, Values: []string{ "London", "China", "NewYork", }, }, }, } fmt.Println(label.String()) return cli.BatchV1().Jobs("default").List(context.TODO(), metav1.ListOptions{ LabelSelector: label.String(), }) }y luego obtuve el error:
&LabelSelector{MatchLabels:map[string]string{},MatchExpressions:[]LabelSelectorRequirement{LabelSelectorRequirement{Key:hello-world,Operator:In,Values:[London China NewYork],},},} 2021/01/28 17:58:07 unable to parse requirement: invalid label key "&LabelSelector{MatchLabels:map[string]string{}": name part must consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character (eg 'MyName', or 'my.name', or '123-abc', regex used for validation is '([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]')¿Qué hice mal? ¿Cómo enumero mis trabajos con un selector de etiquetas que es una expresión compleja?
Con la biblioteca client-go Kubernetes, simplemente puede escribir selectores de etiquetas de la misma manera que lo haría con kubectl .
Escribir hello-world in (London, China, NewYork) debería funcionar bien.
func listJobs(cli *kubernetes.Clientset) (*batchv1.JobList, error) { return cli.BatchV1().Jobs("default").List(context.TODO(), metav1.ListOptions{ LabelSelector: "hello-world in (London, China, NewYork)", }) } Sin embargo, si lo que desea es generar dinámicamente hello-world in (London, China, NewYork) a partir de un objeto programático, esa es otra pregunta, que ya está respondida en StackOverflow aquí .