-
Notifications
You must be signed in to change notification settings - Fork 5
/
restclientgetter.go
76 lines (64 loc) · 2.62 KB
/
restclientgetter.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package resourcebuilder
import (
"github.com/pkg/errors"
"io/ioutil"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/client-go/discovery"
"k8s.io/client-go/discovery/cached/disk"
"k8s.io/client-go/rest"
"k8s.io/client-go/restmapper"
"k8s.io/client-go/tools/clientcmd"
"path/filepath"
"time"
)
// PersistentRESTClientGetter implements RESTClientGetter with persistent clients
type PersistentRESTClientGetter struct {
RESTConfig *rest.Config
CachedDiscoveryInterface discovery.CachedDiscoveryInterface
RESTMapper meta.RESTMapper
}
// ToRESTConfig implements RESTClientGetter
func (g *PersistentRESTClientGetter) ToRESTConfig() (*rest.Config, error) {
return g.RESTConfig, nil
}
// ToDiscoveryClient implements RESTClientGetter
func (g *PersistentRESTClientGetter) ToDiscoveryClient() (discovery.CachedDiscoveryInterface, error) {
return g.CachedDiscoveryInterface, nil
}
// ToRESTMapper implements RESTClientGetter
func (g *PersistentRESTClientGetter) ToRESTMapper() (meta.RESTMapper, error) {
return g.RESTMapper, nil
}
// NewPersistentRESTClientGetter creates new PersistentRESTClientGetter
func NewPersistentRESTClientGetter(kubeconfig string) (*PersistentRESTClientGetter, error) {
restConfig, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
return nil, errors.Wrap(err, "clientcmd.BuildConfigFromFlags error")
}
discoveryConfig, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
return nil, errors.Wrap(err, "clientcmd.BuildConfigFromFlags error")
}
// The more groups you have, the more discovery requests you need to make.
// given 25 groups (our groups + a few custom resources) with one-ish version each, discovery needs to make 50 requests
// double it just so we don't end up here again for a while. This config is only used for discovery.
discoveryConfig.Burst = 100
cacheDir, err := ioutil.TempDir("", "kubebigbrother")
if err != nil {
return nil, errors.Wrap(err, "ioutil.TempDir error")
}
httpCacheDir := filepath.Join(cacheDir, "http")
discoveryCacheDir := filepath.Join(cacheDir, "discovery")
cachedDiscoveryClient, err := disk.NewCachedDiscoveryClientForConfig(
restConfig, discoveryCacheDir, httpCacheDir, 10*time.Minute)
if err != nil {
return nil, errors.Wrap(err, "disk.NewCachedDiscoveryClientForConfig error")
}
mapper := restmapper.NewDeferredDiscoveryRESTMapper(cachedDiscoveryClient)
expander := restmapper.NewShortcutExpander(mapper, cachedDiscoveryClient)
return &PersistentRESTClientGetter{
RESTConfig: restConfig,
CachedDiscoveryInterface: cachedDiscoveryClient,
RESTMapper: expander,
}, nil
}