-
Notifications
You must be signed in to change notification settings - Fork 0
/
kubernetes.go
173 lines (149 loc) · 4.16 KB
/
kubernetes.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package k8pool
import (
"context"
"fmt"
"reflect"
api_v1 "k8s.io/api/core/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
)
type PeerInfo struct {
// (Optional) The name of the data center this peer is in. Leave blank if not using multi data center support.
DataCenter string
// (Required) The ip address of the peer
IPAddress string
// (Optional) The http address:port of the peer
HTTPAddress string
// (Optional) The grpc address:port of the peer
GRPCAddress string
// (Optional) Is true if PeerInfo is for this instance of app
IsOwner bool
}
type UpdateFunc func([]PeerInfo)
type Pool struct {
informer cache.SharedIndexInformer
client *kubernetes.Clientset
log logger
conf Config
done chan struct{}
}
type Config struct {
Logger logger
OnUpdate UpdateFunc
Namespace string
Selector string
PodIP string
PodPort string
}
func New(conf Config) (*Pool, error) {
config, err := rest.InClusterConfig()
if err != nil {
return nil, err
}
// creates the client
client, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
pool := &Pool{
done: make(chan struct{}),
log: conf.Logger,
client: client,
conf: conf,
}
return pool, pool.start()
}
func (e *Pool) start() error {
e.informer = cache.NewSharedIndexInformer(
&cache.ListWatch{
ListFunc: func(options meta_v1.ListOptions) (runtime.Object, error) {
options.LabelSelector = e.conf.Selector
return e.client.CoreV1().Endpoints(e.conf.Namespace).List(context.TODO(), options)
},
WatchFunc: func(options meta_v1.ListOptions) (watch.Interface, error) {
options.LabelSelector = e.conf.Selector
return e.client.CoreV1().Endpoints(e.conf.Namespace).Watch(context.TODO(), options)
},
},
&api_v1.Endpoints{},
0, //Skip resync
cache.Indexers{},
)
e.informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
key, err := cache.MetaNamespaceKeyFunc(obj)
e.log.Debugf("Queue (Add) '%s' - %s", key, err)
if err != nil {
e.log.Errorf("while calling MetaNamespaceKeyFunc(): %s", err)
return
}
},
UpdateFunc: func(obj, new interface{}) {
key, err := cache.MetaNamespaceKeyFunc(obj)
e.log.Debugf("Queue (Update) '%s' - %s", key, err)
if err != nil {
e.log.Errorf("while calling MetaNamespaceKeyFunc(): %s", err)
return
}
e.updatePeers()
},
DeleteFunc: func(obj interface{}) {
key, err := cache.MetaNamespaceKeyFunc(obj)
e.log.Debugf("Queue (Delete) '%s' - %s", key, err)
if err != nil {
e.log.Errorf("while calling MetaNamespaceKeyFunc(): %s", err)
return
}
e.updatePeers()
},
})
go e.informer.Run(e.done)
if !cache.WaitForCacheSync(e.done, e.informer.HasSynced) {
close(e.done)
return fmt.Errorf("timed out waiting for caches to sync")
}
return nil
}
func (e *Pool) updatePeers() {
e.log.Debug("Fetching peer list from endpoints API")
var peers []PeerInfo
for _, obj := range e.informer.GetStore().List() {
endpoint, ok := obj.(*api_v1.Endpoints)
if !ok {
e.log.Errorf("expected type v1.Endpoints got '%s' instead", reflect.TypeOf(obj).String())
}
for _, s := range endpoint.Subsets {
for _, addr := range s.Addresses {
// TODO(thrawn01): Might consider using the `namespace` as the `DataCenter`. We should
// do what ever k8s convention is for identifying a k8s cluster within a federated multi-data
// center setup.
peer := PeerInfo{
IPAddress: fmt.Sprintf("%s", addr.IP),
HTTPAddress: fmt.Sprintf("http://%s:%s", addr.IP, e.conf.PodPort),
GRPCAddress: fmt.Sprintf("%s:%s", addr.IP, e.conf.PodPort),
}
if addr.IP == e.conf.PodIP {
peer.IsOwner = true
}
peers = append(peers, peer)
e.log.Debugf("Peer: %+v\n", peer)
}
}
}
e.conf.OnUpdate(peers)
}
func (e *Pool) Close() {
close(e.done)
}
func isPodReady(pod *api_v1.Pod) bool {
for _, condition := range pod.Status.Conditions {
if condition.Type == api_v1.PodReady && condition.Status == api_v1.ConditionTrue {
return true
}
}
return false
}