-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add field selector enabled fake client
- Loading branch information
1 parent
0fcf28e
commit d7e5b8e
Showing
2 changed files
with
275 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
Copyright 2020 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package fake | ||
|
||
import ( | ||
"context" | ||
|
||
"k8s.io/apimachinery/pkg/api/meta" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"sigs.k8s.io/controller-runtime/pkg/client/apiutil" | ||
) | ||
|
||
var _ client.Client = &fieldSelectorEnableClient{} | ||
|
||
// NewFieldSelectorEnableClient wrapper a fake client that List method enables field selector | ||
func NewFieldSelectorEnableClient(client client.Client, scheme *runtime.Scheme, obj runtime.Object, field string, indexerFunc client.IndexerFunc) (client.Client, error) { | ||
gvk, err := apiutil.GVKForObject(obj, scheme) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &fieldSelectorEnableClient{ | ||
Client: client, | ||
scheme: scheme, | ||
gvk: gvk, | ||
fieldName: field, | ||
fieldValue: indexerFunc, | ||
}, nil | ||
} | ||
|
||
type fieldSelectorEnableClient struct { | ||
client.Client | ||
scheme *runtime.Scheme | ||
|
||
gvk schema.GroupVersionKind | ||
|
||
fieldName string | ||
fieldValue client.IndexerFunc | ||
} | ||
|
||
func (f *fieldSelectorEnableClient) List(ctx context.Context, list runtime.Object, opts ...client.ListOption) error { | ||
if err := f.Client.List(ctx, list, opts...); err != nil { | ||
return err | ||
} | ||
|
||
if len(opts) == 0 { | ||
return nil | ||
} | ||
|
||
objs, err := meta.ExtractList(list) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(objs) == 0 { | ||
return nil | ||
} | ||
|
||
if gvk, err := apiutil.GVKForObject(objs[0], f.scheme); err != nil { | ||
return err | ||
} else if gvk != f.gvk { | ||
return nil | ||
} | ||
|
||
options := client.ListOptions{} | ||
options.ApplyOptions(opts) | ||
|
||
if options.FieldSelector == nil { | ||
return nil | ||
} | ||
|
||
value, ok := options.FieldSelector.RequiresExactMatch(f.fieldName) | ||
if !ok { | ||
return meta.SetList(list, []runtime.Object{}) | ||
} | ||
|
||
filtered := make([]runtime.Object, 0) | ||
for i := range objs { | ||
values := f.fieldValue(objs[i]) | ||
if len(values) != 1 { | ||
continue | ||
} | ||
if values[0] == value { | ||
filtered = append(filtered, objs[i]) | ||
} | ||
} | ||
|
||
return meta.SetList(list, filtered) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
/* | ||
Copyright 2020 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package fake | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/meta" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/fields" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/client-go/kubernetes/scheme" | ||
|
||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
func TestListByFieldSelector(t *testing.T) { | ||
pod := &corev1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "foo", | ||
Namespace: "bar", | ||
}, | ||
Spec: corev1.PodSpec{ | ||
NodeName: "node1", | ||
}, | ||
} | ||
|
||
node := &corev1.Node{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "foo", | ||
}, | ||
Spec: corev1.NodeSpec{ | ||
PodCIDR: "bar", | ||
}, | ||
} | ||
|
||
fakeClient := NewFakeClientWithScheme(scheme.Scheme, pod, node) | ||
|
||
fieldSelectorClient, err := NewFieldSelectorEnableClient(fakeClient, scheme.Scheme, &corev1.Pod{}, "spec.nodeName", func(o runtime.Object) []string { | ||
po, ok := o.(*corev1.Pod) | ||
if !ok { | ||
return nil | ||
} | ||
|
||
return []string{po.Spec.NodeName} | ||
}) | ||
if err != nil { | ||
t.Fatalf("unexpect init client err: %v", err) | ||
} | ||
|
||
fieldSelectorClient, err = NewFieldSelectorEnableClient(fieldSelectorClient, scheme.Scheme, &corev1.Node{}, "spec.podCIDR", func(o runtime.Object) []string { | ||
no, ok := o.(*corev1.Node) | ||
if !ok { | ||
return nil | ||
} | ||
|
||
return []string{no.Spec.PodCIDR} | ||
}) | ||
if err != nil { | ||
t.Fatalf("unexpect init client err: %v", err) | ||
} | ||
|
||
cases := []struct { | ||
obj runtime.Object | ||
name string | ||
opts client.ListOption | ||
expectObjectsCount int | ||
}{ | ||
{ | ||
name: "list Pod using nil fieldSelector", | ||
obj: &corev1.PodList{}, | ||
opts: nil, | ||
expectObjectsCount: 1, | ||
}, | ||
{ | ||
name: "list Node using nil fieldSelector", | ||
obj: &corev1.NodeList{}, | ||
opts: nil, | ||
expectObjectsCount: 1, | ||
}, | ||
{ | ||
name: "list Pod does not match fieldSelector name", | ||
obj: &corev1.PodList{}, | ||
opts: client.MatchingFields(fields.Set{ | ||
"spec.nodeName1": "node1", | ||
}), | ||
expectObjectsCount: 0, | ||
}, | ||
{ | ||
name: "list Node does not match fieldSelector name", | ||
obj: &corev1.NodeList{}, | ||
opts: client.MatchingFields(fields.Set{ | ||
"spec.podCIDR1": "node1", | ||
}), | ||
expectObjectsCount: 0, | ||
}, | ||
{ | ||
name: "list Pod does not match fieldSelector value", | ||
obj: &corev1.PodList{}, | ||
opts: client.MatchingFields(fields.Set{ | ||
"spec.nodeName": "node2", | ||
}), | ||
expectObjectsCount: 0, | ||
}, | ||
{ | ||
name: "list Node does not match fieldSelector value", | ||
obj: &corev1.NodeList{}, | ||
opts: client.MatchingFields(fields.Set{ | ||
"spec.podCIDR": "bar2", | ||
}), | ||
expectObjectsCount: 0, | ||
}, | ||
{ | ||
name: "list Pod match fieldSelector name and value", | ||
obj: &corev1.PodList{}, | ||
opts: client.MatchingFields(fields.Set{ | ||
"spec.nodeName": "node1", | ||
}), | ||
expectObjectsCount: 1, | ||
}, | ||
{ | ||
name: "list Node match fieldSelector name and value", | ||
obj: &corev1.NodeList{}, | ||
opts: client.MatchingFields(fields.Set{ | ||
"spec.podCIDR": "bar", | ||
}), | ||
expectObjectsCount: 1, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
if tc.opts != nil { | ||
if err := fieldSelectorClient.List(context.TODO(), tc.obj, tc.opts); err != nil { | ||
t.Fatalf("unexpect list err: %v", err) | ||
} | ||
} else { | ||
if err := fieldSelectorClient.List(context.TODO(), tc.obj); err != nil { | ||
t.Fatalf("unexpect list err: %v", err) | ||
} | ||
} | ||
|
||
objs, err := meta.ExtractList(tc.obj) | ||
if err != nil { | ||
t.Fatalf("unexpect extra list err: %v", err) | ||
} | ||
|
||
if len(objs) != tc.expectObjectsCount { | ||
t.Errorf("expect list object result count: %d, got: %d", tc.expectObjectsCount, len(objs)) | ||
} | ||
}) | ||
} | ||
} |