forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rackspace.go
388 lines (327 loc) · 9.35 KB
/
rackspace.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
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.
*/
package rackspace
import (
"errors"
"fmt"
"io"
"net"
"regexp"
"time"
"github.com/rackspace/gophercloud"
os_servers "github.com/rackspace/gophercloud/openstack/compute/v2/servers"
"github.com/rackspace/gophercloud/pagination"
"github.com/rackspace/gophercloud/rackspace"
"github.com/rackspace/gophercloud/rackspace/compute/v2/servers"
"github.com/scalingdata/gcfg"
"github.com/golang/glog"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/cloudprovider"
)
const ProviderName = "rackspace"
var ErrNotFound = errors.New("Failed to find object")
var ErrMultipleResults = errors.New("Multiple results where only one expected")
var ErrNoAddressFound = errors.New("No address found for host")
var ErrAttrNotFound = errors.New("Expected attribute not found")
// encoding.TextUnmarshaler interface for time.Duration
type MyDuration struct {
time.Duration
}
func (d *MyDuration) UnmarshalText(text []byte) error {
res, err := time.ParseDuration(string(text))
if err != nil {
return err
}
d.Duration = res
return nil
}
type LoadBalancerOpts struct {
SubnetId string `gcfg:"subnet-id"` // required
CreateMonitor bool `gcfg:"create-monitor"`
MonitorDelay MyDuration `gcfg:"monitor-delay"`
MonitorTimeout MyDuration `gcfg:"monitor-timeout"`
MonitorMaxRetries uint `gcfg:"monitor-max-retries"`
}
// Rackspace is an implementation of cloud provider Interface for Rackspace.
type Rackspace struct {
provider *gophercloud.ProviderClient
region string
lbOpts LoadBalancerOpts
}
type Config struct {
Global struct {
AuthUrl string `gcfg:"auth-url"`
Username string
UserId string `gcfg:"user-id"`
Password string
ApiKey string `gcfg:"api-key"`
TenantId string `gcfg:"tenant-id"`
TenantName string `gcfg:"tenant-name"`
DomainId string `gcfg:"domain-id"`
DomainName string `gcfg:"domain-name"`
Region string
}
LoadBalancer LoadBalancerOpts
}
func init() {
cloudprovider.RegisterCloudProvider(ProviderName, func(config io.Reader) (cloudprovider.Interface, error) {
cfg, err := readConfig(config)
if err != nil {
return nil, err
}
return newRackspace(cfg)
})
}
func (cfg Config) toAuthOptions() gophercloud.AuthOptions {
return gophercloud.AuthOptions{
IdentityEndpoint: cfg.Global.AuthUrl,
Username: cfg.Global.Username,
UserID: cfg.Global.UserId,
Password: cfg.Global.Password,
APIKey: cfg.Global.ApiKey,
TenantID: cfg.Global.TenantId,
TenantName: cfg.Global.TenantName,
// Persistent service, so we need to be able to renew tokens
AllowReauth: true,
}
}
func readConfig(config io.Reader) (Config, error) {
if config == nil {
err := fmt.Errorf("no Rackspace cloud provider config file given")
return Config{}, err
}
var cfg Config
err := gcfg.ReadInto(&cfg, config)
return cfg, err
}
func newRackspace(cfg Config) (*Rackspace, error) {
provider, err := rackspace.AuthenticatedClient(cfg.toAuthOptions())
if err != nil {
return nil, err
}
os := Rackspace{
provider: provider,
region: cfg.Global.Region,
lbOpts: cfg.LoadBalancer,
}
return &os, nil
}
type Instances struct {
compute *gophercloud.ServiceClient
}
// Instances returns an implementation of Instances for Rackspace.
func (os *Rackspace) Instances() (cloudprovider.Instances, bool) {
glog.V(2).Info("rackspace.Instances() called")
compute, err := rackspace.NewComputeV2(os.provider, gophercloud.EndpointOpts{
Region: os.region,
})
if err != nil {
glog.Warningf("Failed to find compute endpoint: %v", err)
return nil, false
}
glog.V(1).Info("Claiming to support Instances")
return &Instances{compute}, true
}
func (i *Instances) List(name_filter string) ([]string, error) {
glog.V(2).Infof("rackspace List(%v) called", name_filter)
opts := os_servers.ListOpts{
Name: name_filter,
Status: "ACTIVE",
}
pager := servers.List(i.compute, opts)
ret := make([]string, 0)
err := pager.EachPage(func(page pagination.Page) (bool, error) {
sList, err := servers.ExtractServers(page)
if err != nil {
return false, err
}
for _, server := range sList {
ret = append(ret, server.Name)
}
return true, nil
})
if err != nil {
return nil, err
}
glog.V(2).Infof("Found %v entries: %v", len(ret), ret)
return ret, nil
}
func serverHasAddress(srv os_servers.Server, ip string) bool {
if ip == firstAddr(srv.Addresses["private"]) {
return true
}
if ip == firstAddr(srv.Addresses["public"]) {
return true
}
if ip == srv.AccessIPv4 {
return true
}
if ip == srv.AccessIPv6 {
return true
}
return false
}
func getServerByAddress(client *gophercloud.ServiceClient, name string) (*os_servers.Server, error) {
pager := servers.List(client, nil)
serverList := make([]os_servers.Server, 0, 1)
err := pager.EachPage(func(page pagination.Page) (bool, error) {
s, err := servers.ExtractServers(page)
if err != nil {
return false, err
}
for _, v := range s {
if serverHasAddress(v, name) {
serverList = append(serverList, v)
}
}
if len(serverList) > 1 {
return false, ErrMultipleResults
}
return true, nil
})
if err != nil {
return nil, err
}
if len(serverList) == 0 {
return nil, ErrNotFound
} else if len(serverList) > 1 {
return nil, ErrMultipleResults
}
return &serverList[0], nil
}
func getServerByName(client *gophercloud.ServiceClient, name string) (*os_servers.Server, error) {
if net.ParseIP(name) != nil {
// we're an IP, so we'll have to walk the full list of servers to
// figure out which one we are.
return getServerByAddress(client, name)
}
opts := os_servers.ListOpts{
Name: fmt.Sprintf("^%s$", regexp.QuoteMeta(name)),
Status: "ACTIVE",
}
pager := servers.List(client, opts)
serverList := make([]os_servers.Server, 0, 1)
err := pager.EachPage(func(page pagination.Page) (bool, error) {
s, err := servers.ExtractServers(page)
if err != nil {
return false, err
}
serverList = append(serverList, s...)
if len(serverList) > 1 {
return false, ErrMultipleResults
}
return true, nil
})
if err != nil {
return nil, err
}
if len(serverList) == 0 {
return nil, ErrNotFound
} else if len(serverList) > 1 {
return nil, ErrMultipleResults
}
return &serverList[0], nil
}
func firstAddr(netblob interface{}) string {
// Run-time types for the win :(
list, ok := netblob.([]interface{})
if !ok || len(list) < 1 {
return ""
}
props, ok := list[0].(map[string]interface{})
if !ok {
return ""
}
tmp, ok := props["addr"]
if !ok {
return ""
}
addr, ok := tmp.(string)
if !ok {
return ""
}
return addr
}
func getAddressByName(api *gophercloud.ServiceClient, name string) (string, error) {
srv, err := getServerByName(api, name)
if err != nil {
return "", err
}
var s string
if s == "" {
s = firstAddr(srv.Addresses["private"])
}
if s == "" {
s = firstAddr(srv.Addresses["public"])
}
if s == "" {
s = srv.AccessIPv4
}
if s == "" {
s = srv.AccessIPv6
}
if s == "" {
return "", ErrNoAddressFound
}
return s, nil
}
func (i *Instances) NodeAddresses(name string) ([]api.NodeAddress, error) {
glog.V(2).Infof("NodeAddresses(%v) called", name)
ip, err := getAddressByName(i.compute, name)
if err != nil {
return nil, err
}
glog.V(2).Infof("NodeAddresses(%v) => %v", name, ip)
// net.ParseIP().String() is to maintain compatibility with the old code
return []api.NodeAddress{{Type: api.NodeLegacyHostIP, Address: net.ParseIP(ip).String()}}, nil
}
// ExternalID returns the cloud provider ID of the specified instance (deprecated).
func (i *Instances) ExternalID(name string) (string, error) {
return "", fmt.Errorf("unimplemented")
}
// InstanceID returns the cloud provider ID of the specified instance.
func (i *Instances) InstanceID(name string) (string, error) {
return "", nil
}
func (i *Instances) AddSSHKeyToAllInstances(user string, keyData []byte) error {
return errors.New("unimplemented")
}
// Implementation of Instances.CurrentNodeName
func (i *Instances) CurrentNodeName(hostname string) (string, error) {
return hostname, nil
}
func (os *Rackspace) Clusters() (cloudprovider.Clusters, bool) {
return nil, false
}
// ProviderName returns the cloud provider ID.
func (os *Rackspace) ProviderName() string {
return ProviderName
}
// ScrubDNS filters DNS settings for pods.
func (os *Rackspace) ScrubDNS(nameservers, searches []string) (nsOut, srchOut []string) {
return nameservers, searches
}
func (os *Rackspace) TCPLoadBalancer() (cloudprovider.TCPLoadBalancer, bool) {
return nil, false
}
func (os *Rackspace) Zones() (cloudprovider.Zones, bool) {
glog.V(1).Info("Claiming to support Zones")
return os, true
}
func (os *Rackspace) Routes() (cloudprovider.Routes, bool) {
return nil, false
}
func (os *Rackspace) GetZone() (cloudprovider.Zone, error) {
glog.V(1).Infof("Current zone is %v", os.region)
return cloudprovider.Zone{Region: os.region}, nil
}