/
client.go
231 lines (194 loc) · 6.53 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
/*
* Copyright (c) 2020 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
*
* 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 client
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"net/http"
"strings"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack"
"github.com/gophercloud/utils/openstack/clientconfig"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
os "github.com/gardener/gardener-extension-provider-openstack/pkg/openstack"
)
// NewOpenstackClientFromCredentials returns a Factory implementation that can be used to create clients for OpenStack services.
// TODO: respect CloudProfile's requestTimeout for the OpenStack client.
// see https://github.com/kubernetes/cloud-provider-openstack/blob/c44d941cdb5c7fe651f5cb9191d0af23e266c7cb/pkg/openstack/openstack.go#L257
func NewOpenstackClientFromCredentials(credentials *os.Credentials) (Factory, error) {
opts := &clientconfig.ClientOpts{
AuthInfo: &clientconfig.AuthInfo{
AuthURL: credentials.AuthURL,
Username: credentials.Username,
Password: credentials.Password,
ProjectName: credentials.TenantName,
DomainName: credentials.DomainName,
ApplicationCredentialID: credentials.ApplicationCredentialID,
ApplicationCredentialName: credentials.ApplicationCredentialName,
ApplicationCredentialSecret: credentials.ApplicationCredentialSecret,
},
}
config := &tls.Config{
InsecureSkipVerify: credentials.Insecure,
}
if len(credentials.CACert) > 0 {
pool := x509.NewCertPool()
ok := pool.AppendCertsFromPEM([]byte(credentials.CACert))
if !ok {
return nil, fmt.Errorf("failed to load CA Bundle for KeyStone")
}
config.RootCAs = pool
}
if opts.AuthInfo.ApplicationCredentialSecret != "" {
opts.AuthType = clientconfig.AuthV3ApplicationCredential
}
transport := &http.Transport{Proxy: http.ProxyFromEnvironment, TLSClientConfig: config}
opts.HTTPClient = &http.Client{
Transport: transport,
}
authOpts, err := clientconfig.AuthOptions(opts)
if err != nil {
return nil, err
}
// AllowReauth should be set to true if you grant permission for Gophercloud to
// cache your credentials in memory, and to allow Gophercloud to attempt to
// re-authenticate automatically if/when your token expires.
authOpts.AllowReauth = true
provider, err := openstack.NewClient(authOpts.IdentityEndpoint)
if err != nil {
return nil, err
}
provider.HTTPClient = *opts.HTTPClient
err = openstack.Authenticate(provider, *authOpts)
if err != nil {
return nil, err
}
return &OpenstackClientFactory{
providerClient: provider,
}, nil
}
// NewOpenStackClientFromSecretRef returns a Factory implementation that can be used to create clients for OpenStack services.
// The credentials are fetched from the Kubernetes secret referenced by <secretRef>.
func NewOpenStackClientFromSecretRef(ctx context.Context, c client.Client, secretRef corev1.SecretReference, keyStoneUrl *string) (Factory, error) {
creds, err := os.GetCredentials(ctx, c, secretRef, false)
if err != nil {
return nil, err
}
if len(strings.TrimSpace(creds.AuthURL)) == 0 && keyStoneUrl != nil {
creds.AuthURL = *keyStoneUrl
}
return NewOpenstackClientFromCredentials(creds)
}
// WithRegion returns an Option that can modify the region a client targets.
func WithRegion(region string) Option {
return func(opts gophercloud.EndpointOpts) gophercloud.EndpointOpts {
opts.Region = region
return opts
}
}
// Storage returns a Storage client. The client uses Swift v1 API for issuing calls.
func (oc *OpenstackClientFactory) Storage(options ...Option) (Storage, error) {
eo := gophercloud.EndpointOpts{}
for _, opt := range options {
eo = opt(eo)
}
storageClient, err := openstack.NewObjectStorageV1(oc.providerClient, eo)
if err != nil {
return nil, err
}
return &StorageClient{
client: storageClient,
}, nil
}
// Compute returns a Compute client. The client uses Nova v2 API for issuing calls.
func (oc *OpenstackClientFactory) Compute(options ...Option) (Compute, error) {
eo := gophercloud.EndpointOpts{}
for _, opt := range options {
eo = opt(eo)
}
client, err := openstack.NewComputeV2(oc.providerClient, eo)
if err != nil {
return nil, err
}
return &ComputeClient{
client: client,
}, nil
}
// DNS returns a DNS client. The client uses Designate v2 API for issuing calls.
func (oc *OpenstackClientFactory) DNS(options ...Option) (DNS, error) {
eo := gophercloud.EndpointOpts{}
for _, opt := range options {
eo = opt(eo)
}
client, err := openstack.NewDNSV2(oc.providerClient, eo)
if err != nil {
return nil, err
}
return &DNSClient{
client: client,
}, nil
}
// Networking returns a Networking client. The client uses Neutron v2 API for issuing calls.
func (oc *OpenstackClientFactory) Networking(options ...Option) (Networking, error) {
eo := gophercloud.EndpointOpts{}
for _, opt := range options {
eo = opt(eo)
}
client, err := openstack.NewNetworkV2(oc.providerClient, eo)
if err != nil {
return nil, err
}
return &NetworkingClient{
client: client,
}, nil
}
// Loadbalancing creates a Loadbalancing client.
func (oc *OpenstackClientFactory) Loadbalancing(options ...Option) (Loadbalancing, error) {
eo := gophercloud.EndpointOpts{}
for _, opt := range options {
eo = opt(eo)
}
client, err := openstack.NewLoadBalancerV2(oc.providerClient, eo)
if err != nil {
return nil, err
}
return &LoadbalancingClient{
client: client,
}, nil
}
// IsNotFoundError checks if an error returned by OpenStack is caused by HTTP 404 status code.
func IsNotFoundError(err error) bool {
if err == nil {
return false
}
if _, ok := err.(gophercloud.ErrDefault404); ok {
return true
}
if _, ok := err.(gophercloud.Err404er); ok {
return true
}
return false
}
// IgnoreNotFoundError ignore not found error
func IgnoreNotFoundError(err error) error {
if IsNotFoundError(err) {
return nil
}
return err
}