forked from fairfaxmedia/area53
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharea53.rb
executable file
·74 lines (63 loc) · 1.88 KB
/
area53.rb
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
#!/usr/bin/env ruby
require_relative 'kube_client'
require_relative 'route53_client'
class Watcher
def run
@route53_client = Route53Client.new(ENV['HOSTED_ZONE_ID'])
logger.info(status: 'startup', hosted_zone_id: ENV['HOSTED_ZONE_ID'])
KubeClient.new.watch_dns.each do |notice|
new_notice(notice)
end
rescue => ex
logger.error(status: 'end_watch', error: ex)
end
def self.logger
@_logger ||= begin
require 'ffx/container_logging'
Ffx::ContainerLogging::ConfigurationService.new(app_name: 'area-53').logger
rescue LoadError
logger = Logger.new(STDOUT)
logger.info(status: 'setup_log', msg: 'ffx/container_logging not present, using stdout')
logger
end
end
private
def logger
self.class.logger
end
def new_notice(notice)
action = notice_action(notice)
return if action.nil?
logger.info(status: 'new_notice', action: action)
svc = notice.object
return if svc.metadata.annotations.domainName.nil?
resource_value = svc.spec.clusterIP
type = 'A'
if svc.spec.type == 'LoadBalancer'
if svc.status.loadBalancer.ingress.nil?
logger.error(status: 'No domain', service: svc.metadata.name)
return
end
resource_value = svc.status.loadBalancer.ingress[0]['hostname']
type = 'CNAME'
end
logger.info(status: 'change_dns', domain: get_domain(svc), resource_value: resource_value, type: type, action: action)
@route53_client.change_dns(get_domain(svc), resource_value, type, action)
end
def notice_action(notice)
case notice.type
when 'ADDED', 'MODIFIED'
'UPSERT'
when 'DELETED'
'DELETE'
else
nil
end
end
def get_domain(svc)
domain = svc.metadata.annotations.domainName.dup
domain << '.' unless domain[-1] == '.'
domain << @route53_client.get_hosted_zone_name
end
end
Watcher.new.run