forked from kubernetes/minikube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns.go
291 lines (246 loc) · 6.77 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
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
/*
Copyright 2016 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 localkube
import (
"fmt"
"net"
"os"
"time"
"k8s.io/minikube/pkg/localkube/kube2sky"
etcd "github.com/coreos/etcd/client"
"github.com/golang/glog"
backendetcd "github.com/skynetservices/skydns/backends/etcd"
"github.com/skynetservices/skydns/metrics"
skydns "github.com/skynetservices/skydns/server"
kube "k8s.io/kubernetes/pkg/api"
kubeclient "k8s.io/kubernetes/pkg/client/unversioned"
"k8s.io/kubernetes/pkg/util/intstr"
"golang.org/x/net/context"
"k8s.io/minikube/pkg/util"
)
const (
DNSName = "dns"
DNSServiceName = "kube-dns"
DNSServiceNamespace = "kube-system"
)
type DNSServer struct {
etcd *EtcdServer
sky runner
kube2sky func() error
dnsServerAddr *net.UDPAddr
clusterIP string
done chan struct{}
}
func (lk LocalkubeServer) NewDNSServer(rootDomain, clusterIP, kubeAPIServer string) (*DNSServer, error) {
// setup backing etcd store
peerURLs := []string{"http://localhost:9256"}
DNSEtcdURLs := []string{"http://localhost:49090"}
publicIP, err := lk.GetHostIP()
if err != nil {
return nil, err
}
serverAddress := fmt.Sprintf("%s:%d", publicIP.String(), 53)
etcdServer, err := lk.NewEtcd(DNSEtcdURLs, peerURLs, DNSName, lk.GetDNSDataDirectory())
if err != nil {
return nil, err
}
// setup skydns
cfg := etcd.Config{
Endpoints: DNSEtcdURLs,
Transport: etcd.DefaultTransport,
}
etcdClient, err := etcd.New(cfg)
if err != nil {
return nil, err
}
keysApi := etcd.NewKeysAPI(etcdClient)
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(
keysApi,
context.Background(),
&backendetcd.Config{
Ttl: skyConfig.Ttl,
Priority: skyConfig.Priority,
})
skyServer := skydns.New(backend, skyConfig)
// setup so prometheus doesn't run into nil
metrics.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, util.Pad("DNS server already started"))
return
}
dns.done = make(chan struct{})
dns.etcd.Start()
go util.Until(dns.kube2sky, os.Stderr, "kube2sky", 2*time.Second, dns.done)
go util.Until(dns.sky.Run, os.Stderr, "skydns", 1*time.Second, dns.done)
go func() {
var err error
client := kubeClient()
meta := kube.ObjectMeta{
Name: DNSServiceName,
Namespace: DNSServiceNamespace,
Labels: map[string]string{
"k8s-app": "kube-dns",
"kubernetes.io/name": "KubeDNS",
},
}
for {
if err != nil {
time.Sleep(2 * time.Second)
}
if _, err = client.Namespaces().Get(DNSServiceNamespace); notFoundErr(err) {
err = createKubeSystemIfNotPresent(client)
if err != nil {
glog.Infof("Failed to create the kube-system namespace: %v\n", err)
continue
}
} else if err != nil {
glog.Infof("Failed to check for kube-system namespace existence: %v\n", err)
continue
}
// setup service
if _, err = client.Services(meta.Namespace).Get(meta.Name); notFoundErr(err) {
// create service if doesn't exist
err = createService(client, meta, dns.clusterIP, dns.dnsServerAddr.Port)
if err != nil {
glog.Infof("Failed to create Service for DNS: %v\n", err)
continue
}
} else if err != nil {
// error if cannot check for Service
glog.Infof("Failed to check for DNS Service existence: %v\n", err)
continue
}
// setup endpoint
if _, err = client.Endpoints(meta.Namespace).Get(meta.Name); notFoundErr(err) {
// create endpoint if doesn't exist
err = createEndpoint(client, meta, dns.dnsServerAddr.IP.String(), int32(dns.dnsServerAddr.Port))
if err != nil {
glog.Infof("Failed to create Endpoint for DNS: %v\n", err)
continue
}
} else if err != nil {
// error if cannot check for Endpoint
glog.Infof("Failed to check for DNS Endpoint existence: %v\n", err)
continue
}
// setup successful
glog.Infof("DNS started succesfully.")
break
}
}()
}
func (dns *DNSServer) Stop() {
teardownService()
// closing chan will prevent servers from restarting but will not kill running server
close(dns.done)
dns.etcd.Stop()
}
// 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 createService(client *kubeclient.Client, meta kube.ObjectMeta, clusterIP string, dnsPort int) error {
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
}
return nil
}
func createEndpoint(client *kubeclient.Client, meta kube.ObjectMeta, dnsIP string, dnsPort int32) error {
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 createKubeSystemIfNotPresent(client *kubeclient.Client) error {
var ns = &kube.Namespace{
ObjectMeta: kube.ObjectMeta{
Name: DNSServiceNamespace,
},
}
_, err := client.Namespaces().Create(ns)
if err != nil {
return err
}
return nil
}
func teardownService() {
client := kubeClient()
client.Services(DNSServiceNamespace).Delete(DNSServiceName)
client.Endpoints(DNSServiceNamespace).Delete(DNSServiceName)
}