-
Notifications
You must be signed in to change notification settings - Fork 5
/
client.go
334 lines (283 loc) · 9.42 KB
/
client.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
package openstack
import (
"context"
"crypto/tls"
"fmt"
"net/http"
"os"
"strings"
"time"
netutil "k8s.io/apimachinery/pkg/util/net"
certutil "k8s.io/client-go/util/cert"
"github.com/gophercloud/gophercloud/v2"
"github.com/gophercloud/gophercloud/v2/openstack"
"github.com/gophercloud/utils/v2/openstack/clientconfig"
"github.com/prometheus/client_golang/prometheus"
"gopkg.in/ini.v1"
)
type GetOSClientFunc func(iniData []byte, overwrite OSClientOverwrite) (Client, error)
type OSClientOverwrite struct {
ProjectID *string
}
// OSClient is an implementation of Client. It must be configured by calling Configure().
// When requesting any specific client the required resources will be created on first call. Mind, that you should
// not call Configure() again, because the created resources will not be invalidated.
type OSClient struct {
networkV2 *gophercloud.ServiceClient
computeV2 *gophercloud.ServiceClient
ini []byte
overwrite OSClientOverwrite
timeout time.Duration
promCounter *prometheus.CounterVec
}
// Configures the OSClient with the data of an os auth ini file.
// Used and required flags are auth-url, username, password, domain-name, tenant-name, region in the [global] directive.
//
// Example ini file:
//
// [Global]
// auth-url="https://this-is-my-keystone-ep:5000/v3"
// domain-name="default"
// tenant-name="mycooltenant"
// username="itmyuser"
// password="suupersecret"
// region="eu01"
func (r *OSClient) Configure(
iniBytes []byte,
overwrite OSClientOverwrite,
timeout time.Duration,
promCounter *prometheus.CounterVec,
) error {
r.ini = iniBytes
r.timeout = timeout
r.promCounter = promCounter
r.overwrite = overwrite
return nil
}
// Returns a configured OSFloatingIPClient as FipClient.
// Make sure that you invoked Configure() before this.
func (r *OSClient) FipClient(ctx context.Context) (FipClient, error) {
if r.networkV2 == nil {
var sc *gophercloud.ServiceClient
sc, err := createNetworkV2FromIni(ctx, r.ini, r.overwrite, r.timeout)
if err != nil {
return nil, err
}
r.networkV2 = sc
}
client := &OSFloatingIPClient{}
return client.Configure(r.networkV2, r.timeout, r.promCounter), nil
}
// Returns a configured OSPortClient as PortClient.
// Make sure that you invoked Configure() before this.
func (r *OSClient) PortClient(ctx context.Context) (PortClient, error) {
if r.networkV2 == nil {
var sc *gophercloud.ServiceClient
sc, err := createNetworkV2FromIni(ctx, r.ini, r.overwrite, r.timeout)
if err != nil {
return nil, err
}
r.networkV2 = sc
}
client := &OSPortClient{}
return client.Configure(r.networkV2, r.timeout, r.promCounter), nil
}
// Returns a configured OSGroupClient as GroupClient.
// Make sure that you invoked Configure() before this.
func (r *OSClient) GroupClient(ctx context.Context) (GroupClient, error) {
if r.networkV2 == nil {
var sc *gophercloud.ServiceClient
sc, err := createNetworkV2FromIni(ctx, r.ini, r.overwrite, r.timeout)
if err != nil {
return nil, err
}
r.networkV2 = sc
}
client := &OSGroupClient{}
return client.Configure(r.networkV2, r.timeout, r.promCounter), nil
}
// Returns a configured OSRuleClient as RuleClient.
// Make sure that you invoked Configure() before this.
func (r *OSClient) RuleClient(ctx context.Context) (RuleClient, error) {
if r.networkV2 == nil {
var sc *gophercloud.ServiceClient
sc, err := createNetworkV2FromIni(ctx, r.ini, r.overwrite, r.timeout)
if err != nil {
return nil, err
}
r.networkV2 = sc
}
client := &OSRuleClient{}
return client.Configure(r.networkV2, r.timeout, r.promCounter), nil
}
// Returns a configured OSServerClient as ServerClient.
// Make sure that you invoked Configure() before this.
func (r *OSClient) ServerClient(ctx context.Context) (ServerClient, error) {
if r.computeV2 == nil {
var sc *gophercloud.ServiceClient
sc, err := createComputeV2FromIni(ctx, r.ini, r.overwrite, r.timeout)
if err != nil {
return nil, err
}
r.computeV2 = sc
}
client := &OSServerClient{}
return client.Configure(r.computeV2, r.timeout, r.promCounter), nil
}
// Returns a configured OSServerGroupClient as ServerGroupClient.
// Make sure that you invoked Configure() before this.
func (r *OSClient) ServerGroupClient(ctx context.Context) (ServerGroupClient, error) {
if r.computeV2 == nil {
var sc *gophercloud.ServiceClient
sc, err := createComputeV2FromIni(ctx, r.ini, r.overwrite, r.timeout)
if err != nil {
return nil, err
}
r.computeV2 = sc
}
client := &OSServerGroupClient{}
return client.Configure(r.computeV2, r.timeout, r.promCounter), nil
}
// Returns a configured OSKeypairClient as KeyPairClient.
// Make sure that you invoked Configure() before this.
func (r *OSClient) KeyPairClient(ctx context.Context) (KeyPairClient, error) {
if r.computeV2 == nil {
var sc *gophercloud.ServiceClient
sc, err := createComputeV2FromIni(ctx, r.ini, r.overwrite, r.timeout)
if err != nil {
return nil, err
}
r.computeV2 = sc
}
client := &OSKeypairClient{}
return client.Configure(r.computeV2, r.timeout, r.promCounter), nil
}
func createNetworkV2FromIni(
ctx context.Context,
iniData []byte,
overwrite OSClientOverwrite,
timeout time.Duration,
) (*gophercloud.ServiceClient, error) {
provider, opts, err := getProvider(ctx, iniData, overwrite, timeout)
if err != nil {
return nil, err
}
client, err := openstack.NewNetworkV2(provider, *opts)
if err != nil {
return nil, err
}
return client, nil
}
func createComputeV2FromIni(
ctx context.Context,
iniData []byte,
overwrite OSClientOverwrite,
timeout time.Duration,
) (*gophercloud.ServiceClient, error) {
provider, opts, err := getProvider(ctx, iniData, overwrite, timeout)
if err != nil {
return nil, err
}
client, err := openstack.NewComputeV2(provider, *opts)
if err != nil {
return nil, err
}
return client, nil
}
func getProvider(
ctx context.Context,
iniData []byte,
overwrite OSClientOverwrite,
timeout time.Duration,
) (*gophercloud.ProviderClient, *gophercloud.EndpointOpts, error) {
cfg, err := ini.LoadSources(
ini.LoadOptions{IgnoreInlineComment: true},
iniData,
)
if err != nil {
return nil, nil, err
}
var authInfo clientconfig.AuthInfo
authURL := strings.TrimSpace(cfg.Section("Global").Key("auth-url").String())
authInfo.AuthURL = authURL
authInfo.Username = strings.TrimSpace(cfg.Section("Global").Key("username").String())
authInfo.Password = strings.TrimSpace(cfg.Section("Global").Key("password").String())
authInfo.DomainName = strings.TrimSpace(cfg.Section("Global").Key("domain-name").String())
authInfo.DomainID = strings.TrimSpace(cfg.Section("Global").Key("domain-id").String())
legacyProjectName := strings.TrimSpace(cfg.Section("Global").Key("tenant-name").String())
projectName := strings.TrimSpace(cfg.Section("Global").Key("project-name").String())
authInfo.ProjectID = strings.TrimSpace(cfg.Section("Global").Key("project-id").String())
caFile := strings.TrimSpace(cfg.Section("Global").Key("ca-file").String())
// TODO: remove legacyProjectName once openstack-cloud-controller has dropped tenant-name support. Link to ccm args:
//nolint:lll // link
// https://github.com/kubernetes/cloud-provider-openstack/blob/master/docs/openstack-cloud-controller-manager/using-openstack-cloud-controller-manager.md
if projectName != "" {
authInfo.ProjectName = projectName
} else {
authInfo.ProjectName = legacyProjectName
}
region := strings.TrimSpace(cfg.Section("Global").Key("region").String())
if overwrite.ProjectID != nil {
authInfo.ProjectName = ""
authInfo.ProjectID = *overwrite.ProjectID
}
// construct transport that trusts the configured CA bundle
var transport http.RoundTripper
// If OS_CACERT env var is set it takes precedence over the configuration.
// This is useful for running yawol-controller locally where the configured file name in the cloud-provider config
// might not match with the local environment.
if caFileEnv := os.Getenv("OS_CACERT"); caFileEnv != "" {
caFile = caFileEnv
}
if caFile != "" {
roots, err := certutil.NewPool(caFile)
if err != nil {
return nil, nil, err
}
config := &tls.Config{MinVersion: tls.VersionTLS12}
config.RootCAs = roots
transport = netutil.SetOldTransportDefaults(&http.Transport{TLSClientConfig: config})
}
clientOpts := new(clientconfig.ClientOpts)
clientOpts.AuthInfo = &authInfo
ao, err := clientconfig.AuthOptions(clientOpts)
if err != nil {
return nil, nil, fmt.Errorf("failed to create client auth options: %+v", err)
}
provider, err := openstack.NewClient(ao.IdentityEndpoint)
if err != nil {
return nil, nil, err
}
if transport != nil {
provider.HTTPClient.Transport = transport
}
actx, acancel := context.WithTimeout(ctx, timeout)
defer acancel()
err = openstack.Authenticate(actx, provider, *ao)
if err != nil {
return nil, nil, err
}
authProvider := *provider
authProvider.SetThrowaway(true)
authProvider.ReauthFunc = nil
authProvider.SetTokenAndAuthResult(nil)
authOpts := *ao
authOpts.AllowReauth = false
provider.ReauthFunc = func(context.Context) error {
pctx, pcancel := context.WithTimeout(ctx, timeout)
defer pcancel()
eo := gophercloud.EndpointOpts{}
if strings.Contains(authURL, "v2") {
if err := openstack.AuthenticateV2(pctx, &authProvider, authOpts, eo); err != nil {
return err
}
} else {
if err := openstack.AuthenticateV3(pctx, &authProvider, &authOpts, eo); err != nil {
return err
}
}
provider.CopyTokenFrom(&authProvider)
return nil
}
return provider, &gophercloud.EndpointOpts{Region: region}, nil
}