Como listar meus trabalhos k8s com um LabelSelector complexo por client-go?
 Jan 28 2021
Eu quero listar meus trabalhos k8s com um seletor de rótulo por client-go como este comando:
$ kubectl get jobs -l 'hello-world in (London, China, NewYork)'
 
    Eu olhei o código-fonte do client-go, então escrevi alguns códigos 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(),
    })
}
 
    e então recebi o erro:
&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 (e.g. 'MyName',  or 'my.name',  or '123-abc', regex used for validation is '([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]')
 
    Onde é que eu me enganei? Como faço para listar minhas tarefas com um seletor de rótulo que é uma expressão complexa?
Respostas
3  Ullaakut Jan 28 2021 at 17:36
Usando a client-gobiblioteca Kubernetes , você pode simplesmente escrever seletores de rótulo da mesma forma que faria com kubectl.
Escrever hello-world in (London, China, NewYork)deve funcionar bem.
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)",
    })
}
 
     Se o que você deseja é gerar dinamicamente a hello-world in (London, China, NewYork)partir de um objeto programático, essa é outra questão, que já foi respondida no StackOverflow aqui .
O que significa um erro “Não é possível encontrar o símbolo” ou “Não é possível resolver o símbolo”?
George Harrison ficou chateado por suas letras de 'Hurdy Gurdy Man' de Donovan não terem sido usadas