-
Notifications
You must be signed in to change notification settings - Fork 49
/
retrieve.go
50 lines (43 loc) · 1.5 KB
/
retrieve.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package controller
import (
"context"
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/tools/cache"
)
// Retriever is how a controller will retrieve the events on the resources from
// the APÎ server.
//
// A Retriever is bound to a single type.
type Retriever interface {
List(ctx context.Context, options metav1.ListOptions) (runtime.Object, error)
Watch(ctx context.Context, options metav1.ListOptions) (watch.Interface, error)
}
type listerWatcherRetriever struct {
lw cache.ListerWatcher
}
// RetrieverFromListerWatcher returns a Retriever from a Kubernetes client-go cache.ListerWatcher.
// If the received lister watcher is nil it will error.
func RetrieverFromListerWatcher(lw cache.ListerWatcher) (Retriever, error) {
if lw == nil {
return nil, fmt.Errorf("listerWatcher can't be nil")
}
return listerWatcherRetriever{lw: lw}, nil
}
// MustRetrieverFromListerWatcher returns a Retriever from a Kubernetes client-go cache.ListerWatcher
// if there is an error it will panic.
func MustRetrieverFromListerWatcher(lw cache.ListerWatcher) Retriever {
r, err := RetrieverFromListerWatcher(lw)
if lw == nil {
panic(err)
}
return r
}
func (l listerWatcherRetriever) List(_ context.Context, options metav1.ListOptions) (runtime.Object, error) {
return l.lw.List(options)
}
func (l listerWatcherRetriever) Watch(_ context.Context, options metav1.ListOptions) (watch.Interface, error) {
return l.lw.Watch(options)
}