This repository has been archived by the owner on Apr 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 99
/
kubernetes.go
247 lines (216 loc) · 6.76 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
Copyright 2018-2022 Mailgun Technologies Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gubernator
import (
"context"
"fmt"
"reflect"
"github.com/mailgun/holster/v4/setter"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
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/tools/cache"
)
type K8sPool struct {
informer cache.SharedIndexInformer
client *kubernetes.Clientset
log FieldLogger
conf K8sPoolConfig
watchCtx context.Context
watchCancel func()
done chan struct{}
}
type WatchMechanism string
const (
WatchEndpoints WatchMechanism = "endpoints"
WatchPods WatchMechanism = "pods"
)
func WatchMechanismFromString(mechanism string) (WatchMechanism, error) {
switch WatchMechanism(mechanism) {
case "": // keep default behavior
return WatchEndpoints, nil
case WatchEndpoints:
return WatchEndpoints, nil
case WatchPods:
return WatchPods, nil
default:
return "", fmt.Errorf("unknown watch mechanism specified: %s", mechanism)
}
}
type K8sPoolConfig struct {
Logger FieldLogger
Mechanism WatchMechanism
OnUpdate UpdateFunc
Namespace string
Selector string
PodIP string
PodPort string
}
func NewK8sPool(conf K8sPoolConfig) (*K8sPool, error) {
config, err := RestConfig()
if err != nil {
return nil, errors.Wrap(err, "during InClusterConfig()")
}
// creates the client
client, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, errors.Wrap(err, "during NewForConfig()")
}
ctx, cancel := context.WithCancel(context.Background())
pool := &K8sPool{
done: make(chan struct{}),
log: conf.Logger,
client: client,
conf: conf,
watchCtx: ctx,
watchCancel: cancel,
}
setter.SetDefault(&pool.log, logrus.WithField("category", "gubernator"))
return pool, pool.start()
}
func (e *K8sPool) start() error {
switch e.conf.Mechanism {
case WatchEndpoints:
return e.startEndpointWatch()
case WatchPods:
return e.startPodWatch()
default:
return fmt.Errorf("unknown value for watch mechanism: %s", e.conf.Mechanism)
}
}
func (e *K8sPool) startGenericWatch(objType runtime.Object, listWatch *cache.ListWatch, updateFunc func()) error {
e.informer = cache.NewSharedIndexInformer(
listWatch,
objType,
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' - %v", key, err)
if err != nil {
e.log.Errorf("while calling MetaNamespaceKeyFunc(): %s", err)
return
}
updateFunc()
},
UpdateFunc: func(obj, new interface{}) {
key, err := cache.MetaNamespaceKeyFunc(obj)
e.log.Debugf("Queue (Update) '%s' - %v", key, err)
if err != nil {
e.log.Errorf("while calling MetaNamespaceKeyFunc(): %s", err)
return
}
updateFunc()
},
DeleteFunc: func(obj interface{}) {
key, err := cache.MetaNamespaceKeyFunc(obj)
e.log.Debugf("Queue (Delete) '%s' - %v", key, err)
if err != nil {
e.log.Errorf("while calling MetaNamespaceKeyFunc(): %s", err)
return
}
updateFunc()
},
})
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 *K8sPool) startPodWatch() error {
listWatch := &cache.ListWatch{
ListFunc: func(options meta_v1.ListOptions) (runtime.Object, error) {
options.LabelSelector = e.conf.Selector
return e.client.CoreV1().Pods(e.conf.Namespace).List(context.Background(), options)
},
WatchFunc: func(options meta_v1.ListOptions) (watch.Interface, error) {
options.LabelSelector = e.conf.Selector
return e.client.CoreV1().Pods(e.conf.Namespace).Watch(e.watchCtx, options)
},
}
return e.startGenericWatch(&api_v1.Pod{}, listWatch, e.updatePeersFromPods)
}
func (e *K8sPool) startEndpointWatch() error {
listWatch := &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.Background(), options)
},
WatchFunc: func(options meta_v1.ListOptions) (watch.Interface, error) {
options.LabelSelector = e.conf.Selector
return e.client.CoreV1().Endpoints(e.conf.Namespace).Watch(e.watchCtx, options)
},
}
return e.startGenericWatch(&api_v1.Endpoints{}, listWatch, e.updatePeersFromEndpoints)
}
func (e *K8sPool) updatePeersFromPods() {
e.log.Debug("Fetching peer list from pods API")
var peers []PeerInfo
main:
for _, obj := range e.informer.GetStore().List() {
pod, ok := obj.(*api_v1.Pod)
if !ok {
e.log.Errorf("expected type v1.Endpoints got '%s' instead", reflect.TypeOf(obj).String())
}
peer := PeerInfo{GRPCAddress: fmt.Sprintf("%s:%s", pod.Status.PodIP, e.conf.PodPort)}
// if containers are not ready or not running then skip this peer
for _, status := range pod.Status.ContainerStatuses {
if !status.Ready || status.State.Running == nil {
e.log.Debugf("Skipping peer because it's not ready or not running: %+v\n", peer)
continue main
}
}
if pod.Status.PodIP == e.conf.PodIP {
peer.IsOwner = true
}
e.log.Debugf("Peer: %+v\n", peer)
peers = append(peers, peer)
}
e.conf.OnUpdate(peers)
}
func (e *K8sPool) updatePeersFromEndpoints() {
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{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 *K8sPool) Close() {
e.watchCancel()
close(e.done)
}