/
informers.go
101 lines (84 loc) · 2.8 KB
/
informers.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package openshift_sdn_node
import (
"net"
"net/http"
"time"
osdnclient "github.com/openshift/client-go/network/clientset/versioned"
osdninformers "github.com/openshift/client-go/network/informers/externalversions"
kinformers "k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
var defaultInformerResyncPeriod = 30 * time.Minute
// sdnInformers is a small bag of data that holds our informers
type sdnInformers struct {
kubeClient kubernetes.Interface
osdnClient osdnclient.Interface
kubeInformers kinformers.SharedInformerFactory
osdnInformers osdninformers.SharedInformerFactory
}
// buildInformers creates all the informer factories.
func (sdn *openShiftSDN) buildInformers() error {
kubeConfig, err := getInClusterConfig()
if err != nil {
return err
}
protoKubeConfig := rest.CopyConfig(kubeConfig)
protoKubeConfig.AcceptContentTypes = "application/vnd.kubernetes.protobuf,application/json"
protoKubeConfig.ContentType = "application/vnd.kubernetes.protobuf"
kubeClient, err := kubernetes.NewForConfig(protoKubeConfig)
if err != nil {
return err
}
osdnClient, err := osdnclient.NewForConfig(kubeConfig)
if err != nil {
return err
}
kubeInformers := kinformers.NewSharedInformerFactory(kubeClient, defaultInformerResyncPeriod)
osdnInformers := osdninformers.NewSharedInformerFactory(osdnClient, defaultInformerResyncPeriod)
sdn.informers = &sdnInformers{
kubeClient: kubeClient,
osdnClient: osdnClient,
kubeInformers: kubeInformers,
osdnInformers: osdnInformers,
}
return nil
}
// start starts the informers.
func (i *sdnInformers) start(stopCh <-chan struct{}) {
i.kubeInformers.Start(stopCh)
i.osdnInformers.Start(stopCh)
}
// getInClusterConfig loads in-cluster config, then applies default overrides.
func getInClusterConfig() (*rest.Config, error) {
clientConfig, err := rest.InClusterConfig()
if err != nil {
return nil, err
}
applyClientConnectionOverrides(clientConfig)
clientConfig.WrapTransport = defaultClientTransport
return clientConfig, nil
}
// defaultClientTransport sets defaults for a client Transport that are suitable
// for use by infrastructure components.
func defaultClientTransport(rt http.RoundTripper) http.RoundTripper {
transport, ok := rt.(*http.Transport)
if !ok {
return rt
}
// TODO: this should be configured by the caller, not in this method.
dialer := &net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}
transport.Dial = dialer.Dial
// Hold open more internal idle connections
// TODO: this should be configured by the caller, not in this method.
transport.MaxIdleConnsPerHost = 100
return transport
}
// applyClientConnectionOverrides updates a kubeConfig with default overrides
func applyClientConnectionOverrides(kubeConfig *rest.Config) {
kubeConfig.QPS = 10.0
kubeConfig.Burst = 20
}