Client-go: How to use `FieldSelector`? How to filter Services service-side?

Created on 14 Nov 2019  路  1Comment  路  Source: kubernetes/client-go

After scouring the documentation (unsuccessfully), I found the example in #326 and tried to filter Services by NodePort:

opts := metav1.ListOptions{
    LabelSelector: "app=...",
    FieldSelector: fmt.Sprintf("spec.ports[0].nodePort=%s", port),
}
_, err := c.CoreV1().Services(namespace).List(opts)

But this appears to not work. I'm possibly writing the filter incorrectly but this errors saying that you can only use metadata.namespace or metadata.name as FieldSelector.

  • Am I using this incorrectly?
  • Is there a way to filter (e.g. Services) service-side like this?

I've resorted to filtering the returned Services client-side but would prefer to not do so.

Most helpful comment

Am I using this incorrectly?

Your "key=value" strings for selectors are correct. Although, if you want more type-safety, you can use a method LabelSelectorAsSelector in a package k8s.io/apimachinery/pkg/apis/meta/v1 for LabelSelector and methods in a package k8s.io/apimachinery/pkg/fields for FieldSelector.

Is there a way to filter (e.g. Services) service-side like this?

The short answer is "no, sorry".
The long story is this: according to the discussion in kubernetes/#53459 and similar issues, it is not possible to filter by random fields. The set of fields is highly restricted, you can dig it here https://github.com/kubernetes/kubernetes/blob/v1.16.1/pkg/registry in strategy.go files (i collect them all to the fancy table for another project: table).

>All comments

Am I using this incorrectly?

Your "key=value" strings for selectors are correct. Although, if you want more type-safety, you can use a method LabelSelectorAsSelector in a package k8s.io/apimachinery/pkg/apis/meta/v1 for LabelSelector and methods in a package k8s.io/apimachinery/pkg/fields for FieldSelector.

Is there a way to filter (e.g. Services) service-side like this?

The short answer is "no, sorry".
The long story is this: according to the discussion in kubernetes/#53459 and similar issues, it is not possible to filter by random fields. The set of fields is highly restricted, you can dig it here https://github.com/kubernetes/kubernetes/blob/v1.16.1/pkg/registry in strategy.go files (i collect them all to the fancy table for another project: table).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

narasago picture narasago  路  3Comments

yashbhutwala picture yashbhutwala  路  4Comments

prestonvanloon picture prestonvanloon  路  6Comments

jgrobbel picture jgrobbel  路  3Comments

h0tbird picture h0tbird  路  3Comments