forked from kubernetes-retired/contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
328 lines (274 loc) · 8.28 KB
/
controller.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
Copyright 2015 The Kubernetes Authors All rights reserved.
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 main
import (
"crypto/md5"
"encoding/hex"
"fmt"
"io"
"os"
"sort"
"time"
"github.com/golang/glog"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/client/cache"
"k8s.io/kubernetes/pkg/client/unversioned"
"k8s.io/kubernetes/pkg/controller/framework"
"k8s.io/kubernetes/pkg/fields"
utildbus "k8s.io/kubernetes/pkg/util/dbus"
"k8s.io/kubernetes/pkg/util/exec"
"k8s.io/kubernetes/pkg/util/flowcontrol"
"k8s.io/kubernetes/pkg/util/intstr"
utiliptables "k8s.io/kubernetes/pkg/util/iptables"
)
const (
reloadQPS = 10.0
resyncPeriod = 10 * time.Second
)
type service struct {
IP string
Port int
}
type serviceByIPPort []service
func (c serviceByIPPort) Len() int { return len(c) }
func (c serviceByIPPort) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
func (c serviceByIPPort) Less(i, j int) bool {
iIP := c[i].IP
jIP := c[j].IP
if iIP != jIP {
return iIP < jIP
}
iPort := c[i].Port
jPort := c[j].Port
return iPort < jPort
}
type vip struct {
Name string
IP string
Port int
Protocol string
LVSMethod string
Backends []service
}
type vipByNameIPPort []vip
func (c vipByNameIPPort) Len() int { return len(c) }
func (c vipByNameIPPort) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
func (c vipByNameIPPort) Less(i, j int) bool {
iName := c[i].Name
jName := c[j].Name
if iName != jName {
return iName < jName
}
iIP := c[i].IP
jIP := c[j].IP
if iIP != jIP {
return iIP < jIP
}
iPort := c[i].Port
jPort := c[j].Port
return iPort < jPort
}
// ipvsControllerController watches the kubernetes api and adds/removes
// services from LVS throgh ipvsadmin.
type ipvsControllerController struct {
client *unversioned.Client
epController *framework.Controller
svcController *framework.Controller
svcLister cache.StoreToServiceLister
epLister cache.StoreToEndpointsLister
reloadRateLimiter flowcontrol.RateLimiter
keepalived *keepalived
configMapName string
ruCfg []vip
ruMD5 string
}
// getEndpoints returns a list of <endpoint ip>:<port> for a given service/target port combination.
func (ipvsc *ipvsControllerController) getEndpoints(
s *api.Service, servicePort *api.ServicePort) []service {
ep, err := ipvsc.epLister.GetServiceEndpoints(s)
if err != nil {
glog.Warningf("unexpected error getting service endpoints: %v", err)
return []service{}
}
var endpoints []service
// The intent here is to create a union of all subsets that match a targetPort.
// We know the endpoint already matches the service, so all pod ips that have
// the target port are capable of service traffic for it.
for _, ss := range ep.Subsets {
for _, epPort := range ss.Ports {
var targetPort int
switch servicePort.TargetPort.Type {
case intstr.Int:
if int(epPort.Port) == servicePort.TargetPort.IntValue() {
targetPort = int(epPort.Port)
}
case intstr.String:
if epPort.Name == servicePort.TargetPort.StrVal {
targetPort = int(epPort.Port)
}
}
if targetPort == 0 {
continue
}
for _, epAddress := range ss.Addresses {
endpoints = append(endpoints, service{IP: epAddress.IP, Port: targetPort})
}
}
}
return endpoints
}
// getServices returns a list of services and their endpoints.
func (ipvsc *ipvsControllerController) getServices() []vip {
svcs := []vip{}
ns, name, err := parseNsName(ipvsc.configMapName)
if err != nil {
glog.Warningf("%v", err)
return []vip{}
}
cfgMap, err := ipvsc.getConfigMap(ns, name)
if err != nil {
glog.Warningf("%v", err)
return []vip{}
}
// k -> IP to use
// v -> <namespace>/<service name>:<lvs method>
for externalIP, nsSvcLvs := range cfgMap.Data {
ns, svc, lvsm, err := parseNsSvcLVS(nsSvcLvs)
if err != nil {
glog.Warningf("%v", err)
continue
}
nsSvc := fmt.Sprintf("%v/%v", ns, svc)
svcObj, svcExists, err := ipvsc.svcLister.Store.GetByKey(nsSvc)
if err != nil {
glog.Warningf("error getting service %v: %v", nsSvc, err)
continue
}
if !svcExists {
glog.Warningf("service %v not found", nsSvc)
continue
}
s := svcObj.(*api.Service)
for _, servicePort := range s.Spec.Ports {
ep := ipvsc.getEndpoints(s, &servicePort)
if len(ep) == 0 {
glog.Warningf("no endpoints found for service %v, port %+v", s.Name, servicePort)
continue
}
sort.Sort(serviceByIPPort(ep))
svcs = append(svcs, vip{
Name: fmt.Sprintf("%v/%v", s.Namespace, s.Name),
IP: externalIP,
Port: int(servicePort.Port),
LVSMethod: lvsm,
Backends: ep,
Protocol: fmt.Sprintf("%v", servicePort.Protocol),
})
glog.V(2).Infof("found service: %v:%v", s.Name, servicePort.Port)
}
}
sort.Sort(vipByNameIPPort(svcs))
return svcs
}
func (ipvsc *ipvsControllerController) getConfigMap(ns, name string) (*api.ConfigMap, error) {
return ipvsc.client.ConfigMaps(ns).Get(name)
}
// sync all services with the
func (ipvsc *ipvsControllerController) sync() {
ipvsc.reloadRateLimiter.Accept()
if !ipvsc.epController.HasSynced() || !ipvsc.svcController.HasSynced() {
time.Sleep(100 * time.Millisecond)
return
}
svc := ipvsc.getServices()
ipvsc.ruCfg = svc
err := ipvsc.keepalived.WriteCfg(svc)
if err != nil {
return
}
glog.V(2).Infof("services: %v", svc)
md5, err := checksum(keepalivedCfg)
if err == nil && md5 == ipvsc.ruMD5 {
return
}
ipvsc.ruMD5 = md5
err = ipvsc.keepalived.Reload()
if err != nil {
glog.Errorf("error reloading keepalived: %v", err)
}
}
// newIPVSController creates a new controller from the given config.
func newIPVSController(kubeClient *unversioned.Client, namespace string, useUnicast bool, configMapName string) *ipvsControllerController {
ipvsc := ipvsControllerController{
client: kubeClient,
reloadRateLimiter: flowcontrol.NewTokenBucketRateLimiter(reloadQPS, int(reloadQPS)),
ruCfg: []vip{},
configMapName: configMapName,
}
podInfo, err := getPodDetails(kubeClient)
if err != nil {
glog.Fatalf("Error getting POD information: %v", err)
}
pod, err := kubeClient.Pods(podInfo.PodNamespace).Get(podInfo.PodName)
if err != nil {
glog.Fatalf("Error getting %v: %v", podInfo.PodName, err)
}
selector := parseNodeSelector(pod.Spec.NodeSelector)
clusterNodes := getClusterNodesIP(kubeClient, selector)
nodeInfo, err := getNetworkInfo(podInfo.NodeIP)
if err != nil {
glog.Fatalf("Error getting local IP from nodes in the cluster: %v", err)
}
neighbors := getNodeNeighbors(nodeInfo, clusterNodes)
execer := exec.New()
dbus := utildbus.New()
iptInterface := utiliptables.New(execer, dbus, utiliptables.ProtocolIpv4)
ipvsc.keepalived = &keepalived{
iface: nodeInfo.iface,
ip: nodeInfo.ip,
netmask: nodeInfo.netmask,
nodes: clusterNodes,
neighbors: neighbors,
priority: getNodePriority(nodeInfo.ip, clusterNodes),
useUnicast: useUnicast,
ipt: iptInterface,
}
err = ipvsc.keepalived.loadTemplate()
if err != nil {
glog.Fatalf("Error loading keepalived template: %v", err)
}
eventHandlers := framework.ResourceEventHandlerFuncs{}
ipvsc.svcLister.Store, ipvsc.svcController = framework.NewInformer(
cache.NewListWatchFromClient(
ipvsc.client, "services", namespace, fields.Everything()),
&api.Service{}, resyncPeriod, eventHandlers)
ipvsc.epLister.Store, ipvsc.epController = framework.NewInformer(
cache.NewListWatchFromClient(
ipvsc.client, "endpoints", namespace, fields.Everything()),
&api.Endpoints{}, resyncPeriod, eventHandlers)
return &ipvsc
}
func checksum(filename string) (string, error) {
var result []byte
file, err := os.Open(filename)
if err != nil {
return "", err
}
defer file.Close()
hash := md5.New()
_, err = io.Copy(hash, file)
if err != nil {
return "", err
}
return hex.EncodeToString(hash.Sum(result)), nil
}