client-go: Fake client doesn't filter Pods on FieldSelector

The fake client is ignoring the FieldSelector when listing Pods.

package fake_test

import (
	"k8s.io/api/core/v1"
	metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes/fake"
	"testing"
)

func TestPodFieldSelectors(t *testing.T) {
	client := fake.NewSimpleClientset(&v1.Pod{
		TypeMeta: metaV1.TypeMeta{
			Kind:       "Pod",
			APIVersion: "v1",
		},
		ObjectMeta: metaV1.ObjectMeta{
			Name:      "tls-app-579f7cd745-t6fdg",
			Namespace: "default",
			Labels: map[string]string{
				"tag": "",
			},
		},
		Status: v1.PodStatus{
			PodIP: "172.1.0.3",
		},
	})

	podList, _ := client.CoreV1().Pods("default").List(metaV1.ListOptions{FieldSelector: "status.podIp=10.1.2.3"})
	if len(podList.Items) != 0 {
		t.Errorf("Expected no items in podList, got %d", len(podList.Items))
	}
}

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 1
  • Comments: 18 (10 by maintainers)

Most upvoted comments

@liggitt Field selection behavior seems generic to me.

I understand it appears that way, but it is not. Each resource’s field selector is manually implemented server-side, using code unavailable to client-go. For example:

https://github.com/kubernetes/kubernetes/blob/d0439d417b0563d44c65b6e400e2070964dea7d1/pkg/registry/core/pod/strategy.go#L235-L248

@liggitt Field selection behavior seems generic to me.

The existing behavior is astonishing in that the fake client, when called with constraints it does not support, does not panic or otherwise call attention to the unsupported request but instead silently returns incorrect data.

The suggestion of using reactors means that either one has to in the test implement the generic matching behavior that would be better implemented in the fake client itself or make the test brittle by asserting the particular filter passed by the present implementation under test (and failing to catch any subtle errors in the filter).

Not to mention the reactor facility of the fake client is under-documented. At least I wasn’t able to figure it out at the time. Instead, I left that functionality untested.