-
Notifications
You must be signed in to change notification settings - Fork 672
Expand file tree
/
Copy pathinstances.go
More file actions
293 lines (244 loc) · 8.27 KB
/
Copy pathinstances.go
File metadata and controls
293 lines (244 loc) · 8.27 KB
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
/*
Copyright 2024 The Kubernetes Authors.
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 (
"context"
"fmt"
sysos "os"
"regexp"
"strings"
"github.com/gophercloud/gophercloud/v2"
"github.com/gophercloud/gophercloud/v2/openstack/compute/v2/flavors"
"github.com/gophercloud/gophercloud/v2/openstack/compute/v2/servers"
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/ports"
"github.com/gophercloud/gophercloud/v2/pagination"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/validation"
cloudprovider "k8s.io/cloud-provider"
"k8s.io/cloud-provider-openstack/pkg/client"
"k8s.io/cloud-provider-openstack/pkg/metrics"
"k8s.io/cloud-provider-openstack/pkg/util"
"k8s.io/cloud-provider-openstack/pkg/util/errors"
"k8s.io/klog/v2"
)
const (
RegionalProviderIDEnv = "OS_CCM_REGIONAL"
instanceShutoff = "SHUTOFF"
)
// InstancesV2 encapsulates an implementation of InstancesV2 for OpenStack.
type InstancesV2 struct {
compute *gophercloud.ServiceClient
network *gophercloud.ServiceClient
region string
regionProviderID bool
networkingOpts NetworkingOpts
}
// InstancesV2 returns an implementation of InstancesV2 for OpenStack.
func (os *OpenStack) InstancesV2() (cloudprovider.InstancesV2, bool) {
klog.V(4).Info("openstack.Instancesv2() called")
compute, err := client.NewComputeV2(os.provider, os.epOpts)
if err != nil {
klog.Errorf("unable to access compute v2 API : %v", err)
return nil, false
}
network, err := client.NewNetworkV2(os.provider, os.epOpts)
if err != nil {
klog.Errorf("unable to access network v2 API : %v", err)
return nil, false
}
regionalProviderID := false
if isRegionalProviderID := sysos.Getenv(RegionalProviderIDEnv); isRegionalProviderID == "true" {
regionalProviderID = true
}
return &InstancesV2{
compute: compute,
network: network,
region: os.epOpts.Region,
regionProviderID: regionalProviderID,
networkingOpts: os.networkingOpts,
}, true
}
// InstanceExists indicates whether a given node exists according to the cloud provider
func (i *InstancesV2) InstanceExists(ctx context.Context, node *v1.Node) (bool, error) {
_, err := i.getInstance(ctx, node)
if err == cloudprovider.InstanceNotFound {
klog.V(6).Infof("instance not found for node: %s", node.Name)
return false, nil
}
if err != nil {
return false, err
}
return true, nil
}
// InstanceShutdown returns true if the instance is shutdown according to the cloud provider.
func (i *InstancesV2) InstanceShutdown(ctx context.Context, node *v1.Node) (bool, error) {
server, err := i.getInstance(ctx, node)
if err != nil {
return false, err
}
// SHUTOFF is the only state where we can detach volumes immediately
if server.Status == instanceShutoff {
return true, nil
}
return false, nil
}
// InstanceMetadata returns the instance's metadata.
func (i *InstancesV2) InstanceMetadata(ctx context.Context, node *v1.Node) (*cloudprovider.InstanceMetadata, error) {
srv, err := i.getInstance(ctx, node)
if err != nil {
return nil, err
}
var server servers.Server
if srv != nil {
server = *srv
}
instanceType, err := srvInstanceType(ctx, i.compute, &server)
if err != nil {
return nil, err
}
ports, err := getAttachedPorts(ctx, i.network, server.ID)
if err != nil {
return nil, err
}
addresses, err := nodeAddresses(ctx, &server, ports, i.network, i.networkingOpts)
if err != nil {
return nil, err
}
availabilityZone := util.SanitizeLabel(server.AvailabilityZone)
return &cloudprovider.InstanceMetadata{
ProviderID: i.makeInstanceID(&server),
InstanceType: instanceType,
NodeAddresses: addresses,
Zone: availabilityZone,
Region: i.region,
}, nil
}
func (i *InstancesV2) makeInstanceID(srv *servers.Server) string {
if i.regionProviderID {
return fmt.Sprintf("%s://%s/%s", ProviderName, i.region, srv.ID)
}
return fmt.Sprintf("%s:///%s", ProviderName, srv.ID)
}
func (i *InstancesV2) getInstance(ctx context.Context, node *v1.Node) (*servers.Server, error) {
if node.Spec.ProviderID == "" {
return getServerByName(ctx, i.compute, node.Name)
}
instanceID, instanceRegion, err := instanceIDFromProviderID(node.Spec.ProviderID)
if err != nil {
return nil, err
}
if instanceRegion != "" && instanceRegion != i.region {
return nil, fmt.Errorf("ProviderID \"%s\" didn't match supported region \"%s\"", node.Spec.ProviderID, i.region)
}
mc := metrics.NewMetricContext("server", "get")
server, err := servers.Get(ctx, i.compute, instanceID).Extract()
if mc.ObserveRequest(err) != nil {
if errors.IsNotFound(err) {
return nil, cloudprovider.InstanceNotFound
}
return nil, err
}
return server, nil
}
func getServerByName(ctx context.Context, client *gophercloud.ServiceClient, name string) (*servers.Server, error) {
opts := servers.ListOpts{
Name: fmt.Sprintf("^%s$", regexp.QuoteMeta(name)),
}
serverList := make([]servers.Server, 0, 1)
mc := metrics.NewMetricContext("server", "list")
pager := servers.List(client, opts)
err := pager.EachPage(ctx, func(_ context.Context, 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, errors.ErrMultipleResults
}
return true, nil
})
if mc.ObserveRequest(err) != nil {
return nil, err
}
if len(serverList) == 0 {
return nil, cloudprovider.InstanceNotFound
}
return &serverList[0], nil
}
// If Instances.InstanceID or cloudprovider.GetInstanceProviderID is changed, the regexp should be changed too.
var providerIDRegexp = regexp.MustCompile(`^` + ProviderName + `://([^/]*)/([^/]+)$`)
// instanceIDFromProviderID splits a provider's id and return instanceID.
// A providerID is build out of '${ProviderName}:///${instance-id}' which contains ':///'.
// or '${ProviderName}://${region}/${instance-id}' which contains '://'.
// See cloudprovider.GetInstanceProviderID and Instances.InstanceID.
func instanceIDFromProviderID(providerID string) (instanceID string, region string, err error) {
// https://github.com/kubernetes/kubernetes/issues/85731
if providerID != "" && !strings.Contains(providerID, "://") {
providerID = ProviderName + "://" + providerID
}
matches := providerIDRegexp.FindStringSubmatch(providerID)
if len(matches) != 3 {
return "", "", fmt.Errorf("ProviderID \"%s\" didn't match expected format \"openstack://region/InstanceID\"", providerID)
}
return matches[2], matches[1], nil
}
// getAttachedPorts returns a list of ports attached to a server.
func getAttachedPorts(ctx context.Context, client *gophercloud.ServiceClient, serverID string) ([]PortWithTrunkDetails, error) {
var allPorts []PortWithTrunkDetails
listOpts := ports.ListOpts{
DeviceID: serverID,
}
allPages, err := ports.List(client, listOpts).AllPages(ctx)
if err != nil {
return allPorts, err
}
err = ports.ExtractPortsInto(allPages, &allPorts)
if err != nil {
return allPorts, err
}
return allPorts, nil
}
func srvInstanceType(ctx context.Context, client *gophercloud.ServiceClient, srv *servers.Server) (string, error) {
keys := []string{"original_name", "id"}
for _, key := range keys {
val, found := srv.Flavor[key]
if !found {
continue
}
flavor, ok := val.(string)
if !ok {
continue
}
if key == "original_name" && isValidLabelValue(flavor) {
return flavor, nil
}
// get flavor name by id
mc := metrics.NewMetricContext("flavor", "get")
f, err := flavors.Get(ctx, client, flavor).Extract()
if mc.ObserveRequest(err) == nil {
if isValidLabelValue(f.Name) {
return f.Name, nil
}
// fallback on flavor id
return f.ID, nil
}
}
return "", fmt.Errorf("flavor original_name/id not found")
}
func isValidLabelValue(v string) bool {
if errs := validation.IsValidLabelValue(v); len(errs) != 0 {
return false
}
return true
}