forked from k3s-io/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
openstack.go
221 lines (183 loc) · 5.57 KB
/
openstack.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
/*
Copyright 2014 Google Inc. 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 openstack
import (
"errors"
"fmt"
"io"
"net"
"net/url"
"regexp"
"code.google.com/p/gcfg"
"github.com/rackspace/gophercloud"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/cloudprovider"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
)
var ErrServerNotFound = errors.New("Server not found")
var ErrMultipleServersFound = errors.New("Multiple servers matched query")
var ErrFlavorNotFound = errors.New("Flavor not found")
// OpenStack is an implementation of cloud provider Interface for OpenStack.
type OpenStack struct {
provider string
authOpt gophercloud.AuthOptions
region string
access *gophercloud.Access
}
type Config struct {
Global struct {
AuthUrl string
Username, Password string
ApiKey string
TenantId, TenantName string
Region string
}
}
func init() {
cloudprovider.RegisterCloudProvider("openstack", func(config io.Reader) (cloudprovider.Interface, error) {
cfg, err := readConfig(config)
if err != nil {
return nil, err
}
return newOpenStack(cfg)
})
}
func (cfg Config) toAuthOptions() gophercloud.AuthOptions {
return gophercloud.AuthOptions{
Username: cfg.Global.Username,
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 OpenStack cloud provider config file given")
return Config{}, err
}
var cfg Config
err := gcfg.ReadInto(&cfg, config)
return cfg, err
}
func newOpenStack(cfg Config) (*OpenStack, error) {
os := OpenStack{
provider: cfg.Global.AuthUrl,
authOpt: cfg.toAuthOptions(),
region: cfg.Global.Region,
}
access, err := gophercloud.Authenticate(os.provider, os.authOpt)
os.access = access
return &os, err
}
type Instances struct {
servers gophercloud.CloudServersProvider
flavor_to_resource map[string]*api.NodeResources // keyed by flavor id
}
// Instances returns an implementation of Instances for OpenStack.
func (os *OpenStack) Instances() (cloudprovider.Instances, bool) {
servers, err := gophercloud.ServersApi(os.access, gophercloud.ApiCriteria{
Type: "compute",
UrlChoice: gophercloud.PublicURL,
Region: os.region,
})
if err != nil {
return nil, false
}
flavors, err := servers.ListFlavors()
if err != nil {
return nil, false
}
flavor_to_resource := make(map[string]*api.NodeResources, len(flavors))
for _, flavor := range flavors {
rsrc := api.NodeResources{
Capacity: api.ResourceList{
"cpu": util.NewIntOrStringFromInt(flavor.VCpus),
"memory": util.NewIntOrStringFromString(fmt.Sprintf("%dMiB", flavor.Ram)),
"openstack.org/disk": util.NewIntOrStringFromString(fmt.Sprintf("%dGB", flavor.Disk)),
"openstack.org/rxTxFactor": util.NewIntOrStringFromInt(int(flavor.RxTxFactor * 1000)),
"openstack.org/swap": util.NewIntOrStringFromString(fmt.Sprintf("%dMiB", flavor.Swap)),
},
}
flavor_to_resource[flavor.Id] = &rsrc
}
return &Instances{servers, flavor_to_resource}, true
}
func (i *Instances) List(name_filter string) ([]string, error) {
filter := url.Values{}
filter.Set("name", name_filter)
filter.Set("status", "ACTIVE")
servers, err := i.servers.ListServersByFilter(filter)
if err != nil {
return nil, err
}
ret := make([]string, len(servers))
for idx, srv := range servers {
ret[idx] = srv.Name
}
return ret, nil
}
func getServerByName(api gophercloud.CloudServersProvider, name string) (*gophercloud.Server, error) {
filter := url.Values{}
filter.Set("name", fmt.Sprintf("^%s$", regexp.QuoteMeta(name)))
filter.Set("status", "ACTIVE")
servers, err := api.ListServersByFilter(filter)
if err != nil {
return nil, err
}
if len(servers) == 0 {
return nil, ErrServerNotFound
} else if len(servers) > 1 {
return nil, ErrMultipleServersFound
}
return &servers[0], nil
}
func (i *Instances) IPAddress(name string) (net.IP, error) {
srv, err := getServerByName(i.servers, name)
if err != nil {
return nil, err
}
var s string
if len(srv.Addresses.Private) > 0 {
s = srv.Addresses.Private[0].Addr
} else if len(srv.Addresses.Public) > 0 {
s = srv.Addresses.Public[0].Addr
} else if srv.AccessIPv4 != "" {
s = srv.AccessIPv4
} else {
s = srv.AccessIPv6
}
return net.ParseIP(s), nil
}
func (i *Instances) GetNodeResources(name string) (*api.NodeResources, error) {
srv, err := getServerByName(i.servers, name)
if err != nil {
return nil, err
}
rsrc, ok := i.flavor_to_resource[srv.Flavor.Id]
if !ok {
return nil, ErrFlavorNotFound
}
return rsrc, nil
}
func (aws *OpenStack) Clusters() (cloudprovider.Clusters, bool) {
return nil, false
}
func (os *OpenStack) TCPLoadBalancer() (cloudprovider.TCPLoadBalancer, bool) {
return nil, false
}
func (os *OpenStack) Zones() (cloudprovider.Zones, bool) {
return nil, false
}