-
Notifications
You must be signed in to change notification settings - Fork 57
/
service_listener.go
338 lines (295 loc) · 9 KB
/
service_listener.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
329
330
331
332
333
334
335
336
337
338
package main
import (
"errors"
"flag"
"fmt"
"os"
"strings"
"time"
"strconv"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/credentials/ec2rolecreds"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/elb"
"github.com/aws/aws-sdk-go/service/route53"
"github.com/golang/glog"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/client/restclient"
"k8s.io/kubernetes/pkg/client/transport"
client "k8s.io/kubernetes/pkg/client/unversioned"
"k8s.io/kubernetes/pkg/labels"
)
// Don't actually commit the changes to route53 records, just print out what we would have done.
var dryRun bool
// Sleep time in secs before checking
var sleepTime time.Duration
func init() {
dryRunStr := os.Getenv("DRY_RUN")
if dryRunStr != "" {
dryRun = true
}
sleepTimeString := os.Getenv("SLEEP_TIME")
if (sleepTimeString != "") {
i64, err := strconv.ParseInt(sleepTimeString, 10, 32)
if (err != nil) {
fmt.Println("Error while trying to parse SLEEP_TIME env var")
os.Exit(1)
}
sleepTime = time.Duration(int32(i64))
} else {
sleepTime = 30
}
}
func main() {
flag.Parse()
glog.Info("Route53 Update Service")
config, err := restclient.InClusterConfig()
if err != nil {
kubernetesService := os.Getenv("KUBERNETES_SERVICE_HOST")
kubernetesServicePort := os.Getenv("KUBERNETES_SERVICE_PORT")
if kubernetesService == "" {
glog.Fatal("Please specify the Kubernetes server via KUBERNETES_SERVICE_HOST")
}
if kubernetesServicePort == "" {
kubernetesServicePort = "443"
}
apiServer := fmt.Sprintf("https://%s:%s", kubernetesService, kubernetesServicePort)
caFilePath := os.Getenv("CA_FILE_PATH")
certFilePath := os.Getenv("CERT_FILE_PATH")
keyFilePath := os.Getenv("KEY_FILE_PATH")
if caFilePath == "" || certFilePath == "" || keyFilePath == "" {
glog.Fatal("You must provide paths for CA, Cert, and Key files")
}
tls := transport.TLSConfig{
CAFile: caFilePath,
CertFile: certFilePath,
KeyFile: keyFilePath,
}
// tlsTransport := transport.New(transport.Config{TLS: tls})
tlsTransport, err := transport.New(&transport.Config{TLS: tls})
if err != nil {
glog.Fatalf("Couldn't set up tls transport: %s", err)
}
config = &restclient.Config{
Host: apiServer,
Transport: tlsTransport,
}
}
c, err := client.New(config)
if err != nil {
glog.Fatalf("Failed to make client: %v", err)
}
glog.Infof("Connected to kubernetes @ %s", config.Host)
metadata := ec2metadata.New(session.New())
creds := credentials.NewChainCredentials(
[]credentials.Provider{
&credentials.EnvProvider{},
&credentials.SharedCredentialsProvider{},
&ec2rolecreds.EC2RoleProvider{Client: metadata},
})
region, err := metadata.Region()
if err != nil {
glog.Fatalf("Unable to retrieve the region from the EC2 instance %v\n", err)
}
awsConfig := aws.NewConfig()
awsConfig.WithCredentials(creds)
awsConfig.WithRegion(region)
sess := session.New(awsConfig)
r53Api := route53.New(sess)
elbAPI := elb.New(sess)
if r53Api == nil || elbAPI == nil {
glog.Fatal("Failed to make AWS connection")
}
selector := "dns=route53"
l, err := labels.Parse(selector)
if err != nil {
glog.Fatalf("Failed to parse selector %q: %v", selector, err)
}
listOptions := api.ListOptions{
LabelSelector: l,
}
glog.Infof("Starting Service Polling every 30s")
awsCallFailed := false
for {
if awsCallFailed {
glog.Info("Noticed failed calls to AWS services, refreshing creds")
sess.Config.Credentials.Expire()
awsCallFailed = false
}
services, err := c.Services(api.NamespaceAll).List(listOptions)
if err != nil {
glog.Fatalf("Failed to list pods: %v", err)
}
glog.Infof("Found %d DNS services in all namespaces with selector %q", len(services.Items), selector)
for i := range services.Items {
s := &services.Items[i]
hn, err := serviceHostname(s)
if err != nil {
glog.Warningf("Couldn't find hostname for %s: %s", s.Name, err)
continue
}
annotation, ok := s.ObjectMeta.Annotations["domainName"]
if !ok {
glog.Warningf("Domain name not set for %s", s.Name)
continue
}
domains := strings.Split(annotation, ",")
for j := range domains {
domain := domains[j]
glog.Infof("Creating DNS for %s service: %s -> %s", s.Name, hn, domain)
elbZoneID, err := hostedZoneID(elbAPI, hn)
if err != nil {
glog.Warningf("Couldn't get zone ID: %s", err)
awsCallFailed = true
continue
}
zone, err := getDestinationZone(domain, r53Api)
if err != nil {
glog.Warningf("Couldn't find destination zone: %s", err)
awsCallFailed = true
continue
}
zoneID := *zone.Id
zoneParts := strings.Split(zoneID, "/")
zoneID = zoneParts[len(zoneParts)-1]
if err = updateDNS(r53Api, hn, elbZoneID, strings.TrimLeft(domain, "."), zoneID); err != nil {
glog.Warning(err)
awsCallFailed = true
continue
}
glog.Infof("Created dns record set: domain=%s, zoneID=%s", domain, zoneID)
}
}
time.Sleep(sleepTime * time.Second)
}
}
func getDestinationZone(domain string, r53Api *route53.Route53) (*route53.HostedZone, error) {
tld, err := getTLD(domain)
if err != nil {
return nil, err
}
listHostedZoneInput := route53.ListHostedZonesByNameInput{
DNSName: &tld,
}
hzOut, err := r53Api.ListHostedZonesByName(&listHostedZoneInput)
if err != nil {
return nil, fmt.Errorf("No zone found for %s: %v", tld, err)
}
// TODO: The AWS API may return multiple pages, we should parse them all
return findMostSpecificZoneForDomain(domain, hzOut.HostedZones)
}
func findMostSpecificZoneForDomain(domain string, zones []*route53.HostedZone) (*route53.HostedZone, error) {
domain = domainWithTrailingDot(domain)
if len(zones) < 1 {
return nil, fmt.Errorf("No zone found for %s", domain)
}
var mostSpecific *route53.HostedZone
curLen := 0
for i := range zones {
zone := zones[i]
zoneName := *zone.Name
if (domain == zoneName || strings.HasSuffix(domain, "." + zoneName)) && curLen < len(zoneName) {
curLen = len(zoneName)
mostSpecific = zone
}
}
if mostSpecific == nil {
return nil, fmt.Errorf("Zone found %s does not match domain given %s", *zones[0].Name, domain)
}
return mostSpecific, nil
}
func getTLD(domain string) (string, error) {
domainParts := strings.Split(domain, ".")
segments := len(domainParts)
if segments < 3 {
return "", fmt.Errorf("Domain %s is invalid - it should be a fully qualified domain name and subdomain (i.e. test.example.com)", domain)
}
return strings.Join(domainParts[segments-2:], "."), nil
}
func domainWithTrailingDot(withoutDot string) string {
if withoutDot[len(withoutDot)-1:] == "." {
return withoutDot
}
return fmt.Sprint(withoutDot, ".")
}
func serviceHostname(service *api.Service) (string, error) {
ingress := service.Status.LoadBalancer.Ingress
if len(ingress) < 1 {
return "", errors.New("No ingress defined for ELB")
}
if len(ingress) > 1 {
return "", errors.New("Multiple ingress points found for ELB, not supported")
}
return ingress[0].Hostname, nil
}
func loadBalancerNameFromHostname(hostname string) (string, error) {
var name string
hostnameSegments := strings.Split(hostname, "-")
if len(hostnameSegments) < 2 {
return name, fmt.Errorf("%s is not a valid ELB hostname", hostname)
}
name = hostnameSegments[0]
// handle internal load balancer naming
if name == "internal" {
name = hostnameSegments[1]
}
return name, nil
}
func hostedZoneID(elbAPI *elb.ELB, hostname string) (string, error) {
elbName, err := loadBalancerNameFromHostname(hostname)
if err != nil {
return "", fmt.Errorf("Couldn't parse ELB hostname: %v", err)
}
lbInput := &elb.DescribeLoadBalancersInput{
LoadBalancerNames: []*string{
&elbName,
},
}
resp, err := elbAPI.DescribeLoadBalancers(lbInput)
if err != nil {
return "", fmt.Errorf("Could not describe load balancer: %v", err)
}
descs := resp.LoadBalancerDescriptions
if len(descs) < 1 {
return "", fmt.Errorf("No lb found: %v", err)
}
if len(descs) > 1 {
return "", fmt.Errorf("Multiple lbs found: %v", err)
}
return *descs[0].CanonicalHostedZoneNameID, nil
}
func updateDNS(r53Api *route53.Route53, hn, hzID, domain, zoneID string) error {
at := route53.AliasTarget{
DNSName: &hn,
EvaluateTargetHealth: aws.Bool(false),
HostedZoneId: &hzID,
}
rrs := route53.ResourceRecordSet{
AliasTarget: &at,
Name: &domain,
Type: aws.String("A"),
}
change := route53.Change{
Action: aws.String("UPSERT"),
ResourceRecordSet: &rrs,
}
batch := route53.ChangeBatch{
Changes: []*route53.Change{&change},
Comment: aws.String("Kubernetes Update to Service"),
}
crrsInput := route53.ChangeResourceRecordSetsInput{
ChangeBatch: &batch,
HostedZoneId: &zoneID,
}
if dryRun {
glog.Infof("DRY RUN: We normally would have updated %s to point to %s (%s)", zoneID, hzID, hn)
return nil
}
_, err := r53Api.ChangeResourceRecordSets(&crrsInput)
if err != nil {
return fmt.Errorf("Failed to update record set: %v", err)
}
return nil
}