-
Notifications
You must be signed in to change notification settings - Fork 11
/
dns.go
208 lines (173 loc) · 4.27 KB
/
dns.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
package localkube
import (
"fmt"
"net"
"os"
"time"
kube2sky "rsprd.com/localkube/k2s"
"github.com/coreos/go-etcd/etcd"
backendetcd "github.com/skynetservices/skydns/backends/etcd"
skydns "github.com/skynetservices/skydns/server"
kube "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/util/intstr"
)
const (
DNSName = "dns"
DNSServiceName = "kube-dns"
DNSServiceNamespace = "kube-system"
)
var (
DNSEtcdURLs = []string{"http://localhost:9090"}
DNSEtcdDataDirectory = "/var/dns/data"
)
type DNSServer struct {
etcd *EtcdServer
sky runner
kube2sky func() error
dnsServerAddr *net.UDPAddr
clusterIP string
done chan struct{}
}
func NewDNSServer(rootDomain, clusterIP, serverAddress, kubeAPIServer string) (*DNSServer, error) {
// setup backing etcd store
peerURLs := []string{"http://localhost:9256"}
etcdServer, err := NewEtcd(DNSEtcdURLs, peerURLs, DNSName, DNSEtcdDataDirectory)
if err != nil {
return nil, err
}
// setup skydns
etcdClient := etcd.NewClient(DNSEtcdURLs)
skyConfig := &skydns.Config{
DnsAddr: serverAddress,
Domain: rootDomain,
}
dnsAddress, err := net.ResolveUDPAddr("udp", serverAddress)
if err != nil {
return nil, err
}
skydns.SetDefaults(skyConfig)
backend := backendetcd.NewBackend(etcdClient, &backendetcd.Config{
Ttl: skyConfig.Ttl,
Priority: skyConfig.Priority,
})
skyServer := skydns.New(backend, skyConfig)
// setup so prometheus doesn't run into nil
skydns.Metrics()
// setup kube2sky
k2s := kube2sky.NewKube2Sky(rootDomain, DNSEtcdURLs[0], "", kubeAPIServer, 10*time.Second, 8081)
return &DNSServer{
etcd: etcdServer,
sky: skyServer,
kube2sky: k2s,
dnsServerAddr: dnsAddress,
clusterIP: clusterIP,
}, nil
}
func (dns *DNSServer) Start() {
if dns.done != nil {
fmt.Fprint(os.Stderr, pad("DNS server already started"))
return
}
dns.done = make(chan struct{})
dns.etcd.Start()
go until(dns.kube2sky, os.Stderr, "kube2sky", 2*time.Second, dns.done)
go until(dns.sky.Run, os.Stderr, "skydns", 1*time.Second, dns.done)
go func() {
var err error
for {
err = setupService(dns.clusterIP, dns.dnsServerAddr.IP.String(), dns.dnsServerAddr.Port)
if err == nil {
return
}
fmt.Printf("Failed to create service+endpoint for DNS: %v\n", err)
time.Sleep(2 * time.Second)
}
}()
}
func (dns *DNSServer) Stop() {
teardownService()
// closing chan will prevent servers from restarting but will not kill running server
close(dns.done)
dns.etcd.Stop()
}
// Status is currently not support by DNSServer
func (dns *DNSServer) Status() Status {
if dns.done == nil {
return Stopped
}
return Started
}
// Name returns the servers unique name
func (DNSServer) Name() string {
return DNSName
}
// runner starts a server returning an error if it stops.
type runner interface {
Run() error
}
func setupService(clusterIP, dnsIP string, dnsPort int) error {
client := kubeClient()
meta := kube.ObjectMeta{
Name: DNSServiceName,
Namespace: DNSServiceNamespace,
Labels: map[string]string{
"k8s-app": "kube-dns",
"kubernetes.io/cluster-service": "true",
"kubernetes.io/name": "KubeDNS",
},
}
service := &kube.Service{
ObjectMeta: meta,
Spec: kube.ServiceSpec{
ClusterIP: clusterIP,
Ports: []kube.ServicePort{
{
Name: "dns",
Port: 53,
TargetPort: intstr.FromInt(dnsPort),
Protocol: kube.ProtocolUDP,
},
{
Name: "dns-tcp",
Port: 53,
TargetPort: intstr.FromInt(dnsPort),
Protocol: kube.ProtocolTCP,
},
},
},
}
_, err := client.Services(meta.Namespace).Create(service)
if err != nil {
return err
}
endpoints := &kube.Endpoints{
ObjectMeta: meta,
Subsets: []kube.EndpointSubset{
{
Addresses: []kube.EndpointAddress{
{IP: dnsIP},
},
Ports: []kube.EndpointPort{
{
Name: "dns",
Port: dnsPort,
},
{
Name: "dns-tcp",
Port: dnsPort,
},
},
},
},
}
_, err = client.Endpoints(meta.Namespace).Create(endpoints)
if err != nil {
return err
}
return nil
}
func teardownService() {
client := kubeClient()
client.Services(DNSServiceNamespace).Delete(DNSServiceName)
client.Endpoints(DNSServiceNamespace).Delete(DNSServiceName)
}