-
Notifications
You must be signed in to change notification settings - Fork 0
/
kube2vulcand.go
288 lines (259 loc) · 8.49 KB
/
kube2vulcand.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
/*
Copyright 2014 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.
*/
// kube2vulcand is a bridge between Kubernetes and SkyDNS. It watches the
// Kubernetes master for changes in Services and manifests them into etcd for
// SkyDNS to serve as DNS records.
package main
import (
"errors"
"flag"
"fmt"
"hash/fnv"
"net/url"
"os"
"time"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
kclient "github.com/GoogleCloudPlatform/kubernetes/pkg/client"
kcache "github.com/GoogleCloudPlatform/kubernetes/pkg/client/cache"
kclientcmd "github.com/GoogleCloudPlatform/kubernetes/pkg/client/clientcmd"
kframework "github.com/GoogleCloudPlatform/kubernetes/pkg/controller/framework"
kSelector "github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
kLabel "github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/golang/glog"
vulcandapi "github.com/mailgun/vulcand/api"
vulcandng "github.com/mailgun/vulcand/engine"
)
var (
argKubecfgFile = flag.String("kubecfg_file", "", "Location of kubecfg file for access to kubernetes service")
argKubeMasterURL = flag.String("kube_master_url", "http://127.0.0.1:8080", "URL to reach kubernetes master. Env variables in this flag will be expanded.")
argVulcandURL = flag.String("vulcand_url", "http://127.0.0.1:8182", "Url to one of the vulcand server")
)
const (
// Resync period for the kube controller loop.
resyncPeriod = 30 * time.Minute
)
type nameNamespace struct {
name string
namespace string
}
type kube2vulcand struct {
//VulcandClient
vulcandClient *vulcandapi.Client
// A cache that contains all the endpoints in the system.
endpointsStore kcache.Store
// A cache that contains all the servicess in the system.
servicesStore kcache.Store
// Lock for controlling access to headless services.
//mlock sync.Mutex
}
// Returns a cache.ListWatch that gets all changes to services.
func createServiceLW(kubeClient *kclient.Client) *kcache.ListWatch {
return kcache.NewListWatchFromClient(kubeClient, "services", kapi.NamespaceAll, kSelector.Everything())
}
func (kv *kube2vulcand) addVulcandBackend(i instance) error {
backend, err := vulcandng.NewHTTPBackend(i.Name, vulcandng.HTTPBackendSettings{})
if err != nil {
return err
}
return kv.vulcandClient.UpsertBackend(*backend)
}
func (kv *kube2vulcand) addVulcandFrontend(i instance) error {
route := fmt.Sprintf("Host(`%s.<whatever>`)", i.Name)
frontend, err := vulcandng.NewHTTPFrontend(i.Name, i.Name, route, vulcandng.HTTPFrontendSettings{})
if err != nil {
glog.Errorf("addVulcandFrontend: Failed to create HTTPFrontend : ", err)
return err
}
return kv.vulcandClient.UpsertFrontend(*frontend, 0)
}
type vulcandServer struct {
URL string
Name string
}
type instance struct {
Name string // admin.os-identity or os-identity
Servers []vulcandServer
}
func (kv *kube2vulcand) newService(obj interface{}) {
if s, ok := obj.(*kapi.Service); ok {
// We only care about external services, which are declared as LoadBalancer (because they cloudprovider to make one)
if s.Spec.Type == kapi.ServiceTypeLoadBalancer {
// TODO: refactor to use the same KubeClient as the service poller ?
kubeClient, err := newKubeClient()
if err != nil {
glog.Errorf("Failed to create a kubernetes client: %v", err)
return
}
nodesList, err := kubeClient.Nodes().List(kLabel.Everything(), kSelector.Everything())
if err != nil {
glog.Errorf("Failed get nodes list: %v", err)
return
}
nodes := nodesList.Items
instances := []instance{}
for _, port := range s.Spec.Ports {
if port.Protocol != kapi.ProtocolTCP {
continue
}
var instanceName string
if port.Name == "" {
instanceName = s.Name
} else {
instanceName = port.Name + "." + s.Name
}
currentInstance := instance{
Name: instanceName,
}
for _, node := range nodes {
// POPO
url := fmt.Sprintf("http://%s:%d", node.Status.Addresses[0].Address, port.NodePort)
s := vulcandServer{
URL: url,
Name: getHash(url),
}
currentInstance.Servers = append(currentInstance.Servers, s)
}
instances = append(instances, currentInstance)
}
for _, i := range instances {
if err := kv.addVulcandBackend(i); err != nil {
glog.Errorf("Failed to add Vulcand Backend : ", err)
}
if err := kv.addVulcandFrontend(i); err != nil {
glog.Errorf("Failed to add Vulcand Frontend : ", err)
}
for _, server := range i.Servers {
srv, err := vulcandng.NewServer(server.Name, server.URL)
if err != nil {
glog.Errorf("Failed to create a Vulcand server", err)
continue
}
if err := kv.vulcandClient.UpsertServer(vulcandng.BackendKey{Id: i.Name}, *srv, 0); err != nil {
glog.Errorf("Failed to create Vulcand Server : ", err)
}
}
}
}
}
}
func (kv *kube2vulcand) removeService(obj interface{}) {
if s, ok := obj.(*kapi.Service); ok {
if s.Spec.Type == kapi.ServiceTypeLoadBalancer {
for _, port := range s.Spec.Ports {
if port.Protocol != kapi.ProtocolTCP {
continue
}
var instanceName string
if port.Name == "" {
instanceName = s.Name
} else {
instanceName = port.Name + "." + s.Name
}
kv.vulcandClient.DeleteFrontend(vulcandng.FrontendKey{Id: instanceName})
kv.vulcandClient.DeleteBackend(vulcandng.BackendKey{Id: instanceName})
}
}
}
}
func (kv *kube2vulcand) updateService(oldObj, newObj interface{}) {
// TODO: Avoid unwanted updates.
kv.removeService(oldObj)
kv.newService(newObj)
}
func newVulcandClient(vulcandURL string) (*vulcandapi.Client, error) {
client := vulcandapi.NewClient(vulcandURL, nil)
if client != nil {
return client, nil
}
return nil, errors.New("Couldn't create VulcandClient !")
}
func getKubeMasterURL() (string, error) {
parsedURL, err := url.Parse(os.ExpandEnv(*argKubeMasterURL))
if err != nil {
return "", fmt.Errorf("failed to parse --kube_master_url %s - %v", *argKubeMasterURL, err)
}
if parsedURL.Scheme == "" || parsedURL.Host == "" || parsedURL.Host == ":" {
return "", fmt.Errorf("invalid --kube_master_url specified %s", *argKubeMasterURL)
}
return parsedURL.String(), nil
}
// TODO: evaluate using pkg/client/clientcmd
func newKubeClient() (*kclient.Client, error) {
var (
config *kclient.Config
err error
masterURL string
)
if *argKubeMasterURL != "" {
masterURL, err = getKubeMasterURL()
if err != nil {
return nil, err
}
}
if *argKubecfgFile == "" {
if masterURL == "" {
return nil, fmt.Errorf("--kube_master_url must be set when --kubecfg_file is not set")
}
config = &kclient.Config{
Host: masterURL,
Version: "v1",
}
} else {
overrides := &kclientcmd.ConfigOverrides{}
if masterURL != "" {
overrides.ClusterInfo.Server = masterURL
}
if config, err = kclientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&kclientcmd.ClientConfigLoadingRules{ExplicitPath: *argKubecfgFile},
overrides).ClientConfig(); err != nil {
return nil, err
}
}
glog.Infof("Using %s for kubernetes master", config.Host)
glog.Infof("Using kubernetes API %s", config.Version)
return kclient.New(config)
}
func watchForServices(kubeClient *kclient.Client, kv *kube2vulcand) kcache.Store {
serviceStore, serviceController := kframework.NewInformer(
createServiceLW(kubeClient),
&kapi.Service{},
resyncPeriod,
kframework.ResourceEventHandlerFuncs{
AddFunc: kv.newService,
DeleteFunc: kv.removeService,
UpdateFunc: kv.updateService,
},
)
go serviceController.Run(util.NeverStop)
return serviceStore
}
func getHash(text string) string {
h := fnv.New32a()
h.Write([]byte(text))
return fmt.Sprintf("%x", h.Sum32())
}
func main() {
flag.Parse()
var err error
kv := kube2vulcand{}
if kv.vulcandClient, err = newVulcandClient(*argVulcandURL); err != nil {
glog.Fatalf("Failed to create vulcand client - %v", err)
}
kubeClient, err := newKubeClient()
if err != nil {
glog.Fatalf("Failed to create a kubernetes client: %v", err)
}
kv.servicesStore = watchForServices(kubeClient, &kv)
select {}
}