aboutsummaryrefslogtreecommitdiffstats
path: root/kube2consul/src/kube2consul/consul_work.go
blob: 2e5927aa2de21c82787ade392155d98d087813a7 (plain)
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
Copyright 2017 ZTE, Inc. and others.

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.
*/
// consul_work.go
package main

import (
	"log"
	"strings"
	"sync"

	consulapi "github.com/hashicorp/consul/api"
	kapi "k8s.io/kubernetes/pkg/api"
)

type ConsulWorker interface {
	AddService(config DnsInfo, service *kapi.Service)
	RemoveService(config DnsInfo)
	AddPod(config DnsInfo, pod *kapi.Pod)
	RemovePod(config DnsInfo)
	SyncDNS()
}

type ConsulAgentWorker struct {
	sync.Mutex
	ConsulWorker
	ids   map[string][]*consulapi.AgentServiceRegistration
	agent *consulapi.Client
}

func newConsulAgentWorker(client *consulapi.Client) *ConsulAgentWorker {
	return &ConsulAgentWorker{
		agent: client,
		ids:   make(map[string][]*consulapi.AgentServiceRegistration),
	}
}

func (client *ConsulAgentWorker) AddService(config DnsInfo, service *kapi.Service) {
	client.Lock()
	defer client.Unlock()
	log.Println("Starting Add Service for: ", config.BaseID)

	if config.IPAddress == "" || config.BaseID == "" {
		log.Println("Service Info is not valid for AddService")
		return
	}

	annos := service.ObjectMeta.Annotations
	//only register service with annotations
	if len(annos) == 0 {
		log.Println("no Annotation defined for this service:", service.Name)
		return
	}

	for _, port := range service.Spec.Ports {
		kubeService := newKubeService()
		kubeService.BuildServiceInfoMap(&port, &annos)

		if len(kubeService.sInfos) == 0 {
			log.Println("no Service defined for this port:", port.Port)
			continue
		}

		//remove service with no name
		for id, s := range kubeService.sInfos {
			if len(s.Name) == 0 {
				if id == "0" {
					log.Printf("SERVICE_%d_NAME not set, ignore this service.", port.Port)
				} else {
					log.Printf("SERVICE_%d_NAME_%s not set, ignore this service.", port.Port, id)
				}
				delete(kubeService.sInfos, id)
			}
		}

		//test again
		if len(kubeService.sInfos) == 0 {
			log.Println("no Service defined for this port:", port.Port)
			continue
		}

		for _, s := range kubeService.sInfos {
			asr := kubeService.BuildAgentService(s, config, &port)
			if *argChecks && port.Protocol == "TCP" && config.ServiceType != kapi.ServiceTypeClusterIP {
				//Create Check if neeeded
				asr.Check = kubeService.BuildAgentServiceCheck(config, &port)
			}

			if client.agent != nil {
				//Registers with DNS
				if err := client.agent.Agent().ServiceRegister(asr); err != nil {
					log.Println("Error creating service record: ", asr.ID)
				}
			}

			//Add to IDS
			client.ids[config.BaseID] = append(client.ids[config.BaseID], asr)
		}
	}
	log.Println("Added Service: ", service.Name)
}

func (client *ConsulAgentWorker) RemoveService(config DnsInfo) {
	client.Lock()
	defer client.Unlock()
	if ids, ok := client.ids[config.BaseID]; ok {
		for _, asr := range ids {
			if client.agent != nil {
				if err := client.agent.Agent().ServiceDeregister(asr.ID); err != nil {
					log.Println("Error removing service: ", err)
				}
			}
		}
		delete(client.ids, config.BaseID)
	} else {
		log.Println("Requested to remove non-existant BaseID DNS of:", config.BaseID)
	}
}

func (client *ConsulAgentWorker) AddPod(config DnsInfo, pod *kapi.Pod) {
	client.Lock()
	defer client.Unlock()
	log.Println("Starting Add Pod for: ", config.BaseID)

	//double check if have Pod IPs so far in Paas mode
	if *argPdmControllerUrl != "" {
		if _, ok := pdmPodIPsMap[pod.Name]; !ok {
			log.Println("In Paas mode, We don't have Pod IPs to register with for Pod: ", pod.Name)
			return
		}
	}

	containerIdMap := make(map[string]string)
	buildContainerIdMap(pod, &containerIdMap)

	for _, c := range pod.Spec.Containers {

		for _, p := range c.Ports {
			podService := newPodService()
			podService.BuildServiceInfoMap(&p, &c)
			if len(podService.sInfos) == 0 {
				log.Println("no Service defined for this port:", p.ContainerPort)
				continue
			}

			//remove service with no name
			for id, s := range podService.sInfos {
				if len(s.Name) == 0 {
					if id == "0" {
						log.Printf("SERVICE_%d_NAME not set, ignore this service.", p.ContainerPort)
					} else {
						log.Printf("SERVICE_%d_NAME_%s not set, ignore this service.", p.ContainerPort, id)
					}
					delete(podService.sInfos, id)
				}
			}

			//remove service if same service exist with different protocol
			for id, s := range podService.sInfos {
				services := []*consulapi.CatalogService{}
				sn := s.Name
				//append namespace if specified
				if len(s.NS) != 0 {
					sn = sn + "-" + s.NS
				}
				var tags []string
				var protocol string
				tags = strings.Split(s.Tags, ",")
				for _, v := range tags {
					var elems []string
					elems = strings.Split(v, ":")
					if elems[0] == PROTOCOL {
						protocol = elems[1]
						break
					}
				}
				//remove service with empty protocol
				if protocol == "" {
					delete(podService.sInfos, id)
					continue
				}
				
				protocol_field := "\"protocol\":\"" + protocol + "\""
				
				//query consul
				if client.agent != nil {
					same_protocol := false
					services, _, _ = client.agent.Catalog().Service(sn, "", nil)
					if services == nil || len(services) == 0 {
						continue
					}
					for _, v := range services[0].ServiceTags {
						if strings.Contains(v, protocol_field) {
							same_protocol = true
							break
						}
					}
					
					if !same_protocol {
						log.Printf("same service with different protocol already exist, ignore service:%s, protocol:%s", sn, protocol)
						delete(podService.sInfos, id)
					}
				}
			}			

			//remove service with no network plane type in tags
			if *argPdmControllerUrl != "" {
				for id, s := range podService.sInfos {
					if len(s.Tags) == 0 {
						if id == "0" {
							log.Printf("SERVICE_%d_TAGS not set, ignore this service in Paas mode.", p.ContainerPort)
						} else {
							log.Printf("SERVICE_%d_TAGS_%s not set, ignore this service in Paas mode.", p.ContainerPort, id)
						}
						delete(podService.sInfos, id)
					} else {
						var tags []string
						tags = strings.Split(s.Tags, ",")
						for _, v := range tags {
							var elems []string
							elems = strings.Split(v, ":")
							//this is saved in Tags, later will be moved to Labels??
							if elems[0] == NETWORK_PLANE_TYPE {
								types := strings.Split(elems[1], "|")
								for _, t := range types {
									ip := getIPFromPodIPsMap(t, pod.Name)
									if ip == "" {
										log.Printf("User defined network type:%s has no assigned ip for Pod:%s", t, pod.Name)
									} else {
										log.Printf("Found ip:%s for network type:%s", ip, t)
										s.IPs = append(s.IPs, PodIP{
											NetworkPlane: t,
											IPAddress:    ip,
										})
									}
								}
							}
						}

						if len(s.IPs) == 0 {
							log.Printf("In Paas mode, no IP assigned for Pod:ContainerPort->%s:%d", pod.Name, p.ContainerPort)
							delete(podService.sInfos, id)
						}
					}
				}
			}

			//test again
			if len(podService.sInfos) == 0 {
				log.Println("no Service defined for this port:", p.ContainerPort)
				continue
			}

			for _, s := range podService.sInfos {
				asrs := podService.BuildAgentService(s, config, &p, containerIdMap[c.Name])
				if client.agent != nil {
					for _, asr := range asrs {
						//Registers with DNS
						if err := client.agent.Agent().ServiceRegister(asr); err != nil {
							log.Println("Error creating service record: ", asr.ID)
						} else {
							log.Printf("Added service:instance ->%s:%s", asr.Name, asr.ID)
						}
						client.ids[config.BaseID] = append(client.ids[config.BaseID], asr)
					}
				} else {
					log.Println("Consul client is not available.")
				}
			}
		}
	}
	log.Println("Added Pod: ", pod.Name)
}

func (client *ConsulAgentWorker) RemovePod(config DnsInfo) {
	client.Lock()
	defer client.Unlock()
	if ids, ok := client.ids[config.BaseID]; ok {
		for _, asr := range ids {
			if client.agent != nil {
				if err := client.agent.Agent().ServiceDeregister(asr.ID); err != nil {
					log.Println("Error removing service: ", err)
				} else {
					log.Printf("removed service -> %s:%d", asr.Name, asr.Port)
				}
			}
		}
		delete(client.ids, config.BaseID)
		log.Println("Removed Pod: ", config.BaseID)
	} else {
		log.Println("Requested to remove non-existant BaseID DNS of:", config.BaseID)
	}
}

func (client *ConsulAgentWorker) SyncDNS() {
	if client.agent != nil {
		if services, err := client.agent.Agent().Services(); err == nil {
			for _, registered := range client.ids {
				for _, service := range registered {
					if !containsServiceId(service.ID, services) {
						log.Println("Regregistering missing service ID: ", service.ID)
						client.agent.Agent().ServiceRegister(service)
					}
				}
			}
			for _, service := range services {
				found := false
				for _, registered := range client.ids {
					for _, svc := range registered {
						if service.ID == svc.ID {
							found = true
							break
						}
					}
					if found {
						break
					}
				}
				if !found {
					log.Println("Degregistering dead service ID: ", service.ID)
					client.agent.Agent().ServiceDeregister(service.ID)
				}
			}
		} else {
			log.Println("Error retreiving services from consul during sync: ", err)
		}
	}
}

func buildContainerIdMap(pod *kapi.Pod, cmap *map[string]string) {
	for _, s := range pod.Status.ContainerStatuses {
		id := strings.TrimLeft(s.ContainerID, "docker://")
		(*cmap)[s.Name] = id
	}
}

func containsServiceId(id string, services map[string]*consulapi.AgentService) bool {
	for _, service := range services {
		if service.ID == id {
			return true
		}
	}
	return false
}

func getIPFromPodIPsMap(ptype string, podname string) string {
	ips := pdmPodIPsMap[podname]
	for _, v := range ips {
		if v.NetworkPlane == ptype {
			return v.IPAddress
		}
	}
	return ""
}