Skip to content

Commit 6b4e5de

Browse files
committed
✨ Allow using ListOptions as ListOption
1 parent d90bbc6 commit 6b4e5de

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

pkg/client/options.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,24 @@ type ListOptions struct {
273273
Raw *metav1.ListOptions
274274
}
275275

276+
var _ ListOption = &ListOptions{}
277+
278+
// ApplyToList implements ListOption for ListOptions
279+
func (o *ListOptions) ApplyToList(lo *ListOptions) {
280+
if o.LabelSelector != nil {
281+
lo.LabelSelector = o.LabelSelector
282+
}
283+
if o.FieldSelector != nil {
284+
lo.FieldSelector = o.FieldSelector
285+
}
286+
if o.Namespace != "" {
287+
lo.Namespace = o.Namespace
288+
}
289+
if o.Raw != nil {
290+
lo.Raw = o.Raw
291+
}
292+
}
293+
276294
// AsListOptions returns these options as a flattened metav1.ListOptions.
277295
// This may mutate the Raw field.
278296
func (o *ListOptions) AsListOptions() *metav1.ListOptions {

pkg/client/options_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package client_test
18+
19+
import (
20+
. "github.com/onsi/ginkgo"
21+
. "github.com/onsi/gomega"
22+
23+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24+
"k8s.io/apimachinery/pkg/fields"
25+
"k8s.io/apimachinery/pkg/labels"
26+
"sigs.k8s.io/controller-runtime/pkg/client"
27+
)
28+
29+
var _ = Describe("ListOptions", func() {
30+
It("Should set LabelSelector", func() {
31+
labelSelector, err := labels.Parse("a=b")
32+
Expect(err).NotTo(HaveOccurred())
33+
o := &client.ListOptions{LabelSelector: labelSelector}
34+
newListOpts := &client.ListOptions{}
35+
o.ApplyToList(newListOpts)
36+
Expect(newListOpts).To(Equal(o))
37+
})
38+
It("Should set FieldSelector", func() {
39+
o := &client.ListOptions{FieldSelector: fields.Nothing()}
40+
newListOpts := &client.ListOptions{}
41+
o.ApplyToList(newListOpts)
42+
Expect(newListOpts).To(Equal(o))
43+
})
44+
It("Should set Namespace", func() {
45+
o := &client.ListOptions{Namespace: "my-ns"}
46+
newListOpts := &client.ListOptions{}
47+
o.ApplyToList(newListOpts)
48+
Expect(newListOpts).To(Equal(o))
49+
})
50+
It("Should set Raw", func() {
51+
o := &client.ListOptions{Raw: &metav1.ListOptions{FieldSelector: "Hans"}}
52+
newListOpts := &client.ListOptions{}
53+
o.ApplyToList(newListOpts)
54+
Expect(newListOpts).To(Equal(o))
55+
})
56+
It("Should not set anything", func() {
57+
o := &client.ListOptions{}
58+
newListOpts := &client.ListOptions{}
59+
o.ApplyToList(newListOpts)
60+
Expect(newListOpts).To(Equal(o))
61+
})
62+
})

0 commit comments

Comments
 (0)