forked from aws/amazon-vpc-cni-k8s
/
discovery.go
300 lines (251 loc) · 8.93 KB
/
discovery.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Package k8sapi contains logic to retrieve pods running on local node
package k8sapi
import (
"fmt"
"os"
"sync"
"time"
"github.com/pkg/errors"
log "github.com/cihub/seelog"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type controller struct {
indexer cache.Indexer
queue workqueue.RateLimitingInterface
informer cache.Controller
}
// K8SAPIs defines interface to use kubelet introspection API
type K8SAPIs interface {
K8SGetLocalPodIPs() ([]*K8SPodInfo, error)
}
// K8SPodInfo provides pod info
type K8SPodInfo struct {
// Name is pod's name
Name string
// Namespace is pod's namespace
Namespace string
// Container is pod's container id
Container string
// IP is pod's ipv4 address
IP string
UID string
}
// ErrInformerNotSynced indicates that it has not synced with API server yet
var ErrInformerNotSynced = errors.New("discovery: informer not synced")
// Controller defines global context for discovery controller
type Controller struct {
workerPods map[string]*K8SPodInfo
workerPodsLock sync.RWMutex
controller *controller
kubeClient kubernetes.Interface
myNodeName string
synced bool
}
// NewController creates a new DiscoveryController
func NewController(clientset kubernetes.Interface) *Controller {
return &Controller{kubeClient: clientset,
myNodeName: os.Getenv("MY_NODE_NAME"),
workerPods: make(map[string]*K8SPodInfo)}
}
// CreateKubeClient creates a k8s client
func CreateKubeClient(apiserver string, kubeconfig string) (clientset.Interface, error) {
var config *rest.Config
var err error
if apiserver == "" && kubeconfig == "" {
config, err = rest.InClusterConfig()
} else {
config, err = clientcmd.BuildConfigFromFlags(apiserver, kubeconfig)
}
if err != nil {
return nil, err
}
kubeClient, err := clientset.NewForConfig(config)
if err != nil {
return nil, err
}
// Informers don't seem to do a good job logging error messages when it
// can't reach the server, making debugging hard. This makes it easier to
// figure out if apiserver is configured incorrectly.
log.Infof("Testing communication with server")
v, err := kubeClient.Discovery().ServerVersion()
if err != nil {
errMsg := "Failed to communicate with K8S Server. Please check instance security groups or http proxy setting"
log.Infof(errMsg)
fmt.Printf(errMsg)
return nil, fmt.Errorf("error communicating with apiserver: %v", err)
}
log.Infof("Running with Kubernetes cluster version: v%s.%s. git version: %s. git tree state: %s. commit: %s. platform: %s",
v.Major, v.Minor, v.GitVersion, v.GitTreeState, v.GitCommit, v.Platform)
log.Info("Communication with server successful")
return kubeClient, nil
}
// DiscoverK8SPods discovers Pods running in the cluster
func (d *Controller) DiscoverK8SPods() {
// create the pod watcher
podListWatcher := cache.NewListWatchFromClient(d.kubeClient.CoreV1().RESTClient(), "pods", metav1.NamespaceAll, fields.Everything())
// create the workqueue
queue := workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter())
// Bind the workqueue to a cache with the help of an informer. This way we make sure that
// whenever the cache is updated, the pod key is added to the workqueue.
// Note that when we finally process the item from the workqueue, we might see a newer version
// of the Pod than the version which was responsible for triggering the update.
indexer, informer := cache.NewIndexerInformer(podListWatcher, &v1.Pod{}, 0, cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
key, err := cache.MetaNamespaceKeyFunc(obj)
if err == nil {
queue.Add(key)
}
},
UpdateFunc: func(old interface{}, new interface{}) {
key, err := cache.MetaNamespaceKeyFunc(new)
if err == nil {
queue.Add(key)
}
},
DeleteFunc: func(obj interface{}) {
// IndexerInformer uses a delta queue, therefore for deletes we have to use this
// key function.
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
if err == nil {
queue.Add(key)
}
},
}, cache.Indexers{})
d.controller = newController(queue, indexer, informer)
// Now let's start the controller
stop := make(chan struct{})
defer close(stop)
go d.run(1, stop)
// Wait forever
select {}
}
// K8SGetLocalPodIPs return the list of pods running on the local nodes
func (d *Controller) K8SGetLocalPodIPs() ([]*K8SPodInfo, error) {
var localPods []*K8SPodInfo
if !d.synced {
log.Info("GetLocalPods: informer not synced yet")
return nil, ErrInformerNotSynced
}
log.Debug("GetLocalPods start ...")
d.workerPodsLock.Lock()
defer d.workerPodsLock.Unlock()
for _, pod := range d.workerPods {
log.Infof("K8SGetLocalPodIPs discovered local Pods: %s %s %s %s",
pod.Name, pod.Namespace, pod.IP, pod.UID)
localPods = append(localPods, pod)
}
return localPods, nil
}
// The rest of logic/code are taken from kubernetes/client-go/examples/workqueue
func newController(queue workqueue.RateLimitingInterface, indexer cache.Indexer, informer cache.Controller) *controller {
return &controller{
informer: informer,
indexer: indexer,
queue: queue,
}
}
func (d *Controller) processNextItem() bool {
// Wait until there is a new item in the working queue
key, quit := d.controller.queue.Get()
if quit {
return false
}
// Tell the queue that we are done with processing this key. This unblocks the key for other workers
// This allows safe parallel processing because two pods with the same key are never processed in
// parallel.
defer d.controller.queue.Done(key)
// Invoke the method containing the business logic
err := d.handlePodUpdate(key.(string))
// Handle the error if something went wrong during the execution of the business logic
d.controller.handleErr(err, key)
return true
}
func (d *Controller) handlePodUpdate(key string) error {
obj, exists, err := d.controller.indexer.GetByKey(key)
if err != nil {
log.Errorf("fetching object with key %s from store failed with %v", key, err)
return err
}
if !exists {
log.Infof(" Pods deleted on my node: %v", key)
d.workerPodsLock.Lock()
defer d.workerPodsLock.Unlock()
delete(d.workerPods, key)
return nil
}
pod, ok := obj.(*v1.Pod)
if !ok || pod == nil {
log.Errorf("updated object received was not a pod: %+v", obj)
return errors.New("received a non-pod object update")
}
// Note that you also have to check the uid if you have a local controlled resource, which
// is dependent on the actual instance, to detect that a Pod was recreated with the same name
podName := pod.GetName()
// check to see if this is one of worker pod on my nodes
if d.myNodeName == pod.Spec.NodeName && !pod.Spec.HostNetwork {
d.workerPodsLock.Lock()
defer d.workerPodsLock.Unlock()
d.workerPods[key] = &K8SPodInfo{
Name: podName,
Namespace: pod.GetNamespace(),
UID: string(pod.GetUID()),
IP: pod.Status.PodIP,
}
log.Infof(" Add/Update for Pod %s on my node, namespace = %s, IP = %s", podName, d.workerPods[key].Namespace, d.workerPods[key].IP)
}
return nil
}
// handleErr checks if an error happened and makes sure we will retry later.
func (c *controller) handleErr(err error, key interface{}) {
if err == nil {
// Forget about the #AddRateLimited history of the key on every successful synchronization.
// This ensures that future processing of updates for this key is not delayed because of
// an outdated error history.
c.queue.Forget(key)
return
}
// This controller retries 5 times if something goes wrong. After that, it stops trying.
if c.queue.NumRequeues(key) < 5 {
log.Infof("Error syncing pod %v: %v", key, err)
// Re-enqueue the key rate limited. Based on the rate limiter on the
// queue and the re-enqueue history, the key will be processed later again.
c.queue.AddRateLimited(key)
return
}
c.queue.Forget(key)
// Report to an external entity that, even after several retries, we could not successfully process this key
runtime.HandleError(err)
log.Infof("Dropping pod %q out of the queue: %v", key, err)
}
func (d *Controller) run(threadiness int, stopCh chan struct{}) {
// Let the workers stop when we are done
defer d.controller.queue.ShutDown()
log.Info("Starting Pod controller")
go d.controller.informer.Run(stopCh)
// Wait for all involved caches to be synced, before processing items from the queue is started
if !cache.WaitForCacheSync(stopCh, d.controller.informer.HasSynced) {
runtime.HandleError(fmt.Errorf("timed out waiting for caches to sync"))
return
}
log.Info("Synced successfully with APIServer")
d.synced = true
for i := 0; i < threadiness; i++ {
go wait.Until(d.runWorker, time.Second, stopCh)
}
<-stopCh
log.Info("Stopping Pod controller")
}
func (d *Controller) runWorker() {
for d.processNextItem() {
}
}