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.
I've resorted to filtering the returned Services client-side but would prefer to not do so.
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).
Most helpful comment
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/v1for LabelSelector and methods in a packagek8s.io/apimachinery/pkg/fieldsfor FieldSelector.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.gofiles (i collect them all to the fancy table for another project: table).