-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinstances.go
More file actions
248 lines (211 loc) · 7.69 KB
/
instances.go
File metadata and controls
248 lines (211 loc) · 7.69 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
/*
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 ccm
import (
"context"
"errors"
"fmt"
"regexp"
"strings"
"github.com/stackitcloud/cloud-provider-stackit/pkg/labels"
"github.com/stackitcloud/cloud-provider-stackit/pkg/stackit"
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
corev1 "k8s.io/api/core/v1"
cloudprovider "k8s.io/cloud-provider"
"k8s.io/klog/v2"
)
const (
RegionalProviderIDEnv = "OS_CCM_REGIONAL"
// TODO: update the state with a more definitive one from the IaaS.
instanceStopping = "STOPPING"
)
// If makeInstanceID is changed, the regexp should be changed too.
var providerIDRegexp = regexp.MustCompile(`^` + ProviderName + `://([^/]+)$`)
// TODO(migration): remove old provider support after migration
var oldProviderIDRegexp = regexp.MustCompile(`^` + oldProviderName + `://([^/]*)/([^/]+)$`)
// Instances encapsulates an implementation of Instances for OpenStack.
type Instances struct {
regionProviderID bool
iaasClient stackit.NodeClient
projectID string
region string
}
func NewInstance(client stackit.NodeClient, projectID, region string) (*Instances, error) {
return &Instances{
iaasClient: client,
projectID: projectID,
region: region,
regionProviderID: false,
}, nil
}
// InstanceExists indicates whether a given node exists according to the cloud provider
func (i *Instances) InstanceExists(ctx context.Context, node *corev1.Node) (bool, error) {
_, err := i.getInstance(ctx, node)
if errors.Is(err, cloudprovider.InstanceNotFound) {
klog.V(6).Infof("Instance not found for node: %s", node.Name)
return false, nil
}
if err != nil {
return false, fmt.Errorf("failed to get instance: %w", err)
}
return true, nil
}
// InstanceShutdown returns true if the instance is shutdown according to the cloud provider.
func (i *Instances) InstanceShutdown(ctx context.Context, node *corev1.Node) (bool, error) {
server, err := i.getInstance(ctx, node)
if err != nil {
return false, fmt.Errorf("failed to get instance: %w", err)
}
// SHUTOFF is the only state where we can detach volumes immediately
if *server.Status == instanceStopping {
return true, nil
}
return false, nil
}
// InstanceMetadata returns the instance's metadata.
func (i *Instances) InstanceMetadata(ctx context.Context, node *corev1.Node) (*cloudprovider.InstanceMetadata, error) {
server, err := i.getInstance(ctx, node)
if errors.Is(err, cloudprovider.InstanceNotFound) {
klog.V(6).Infof("Instance not found for node: %s", node.Name)
return nil, nil
}
if err != nil {
return nil, fmt.Errorf("failed to get instance: %w", err)
}
var addresses []corev1.NodeAddress
if len(server.GetNics()) == 0 {
return nil, fmt.Errorf("server has no network interfaces")
}
for _, nic := range server.GetNics() {
if ipv4, ok := nic.GetIpv4Ok(); ok {
addToNodeAddresses(&addresses,
corev1.NodeAddress{
Address: ipv4,
Type: corev1.NodeInternalIP,
})
}
if ipv6, ok := nic.GetIpv6Ok(); ok {
addToNodeAddresses(&addresses,
corev1.NodeAddress{
Address: ipv6,
Type: corev1.NodeInternalIP,
})
}
if publicIP, ok := nic.GetPublicIpOk(); ok {
addToNodeAddresses(&addresses,
corev1.NodeAddress{
Address: publicIP,
Type: corev1.NodeExternalIP,
})
}
}
addToNodeAddresses(&addresses,
corev1.NodeAddress{
Type: corev1.NodeHostName,
Address: server.GetName(),
})
availabilityZone := labels.Sanitize(server.GetAvailabilityZone())
return &cloudprovider.InstanceMetadata{
ProviderID: i.makeInstanceID(server),
InstanceType: server.GetMachineType(),
NodeAddresses: addresses,
Zone: availabilityZone,
Region: i.region,
}, nil
}
func (i *Instances) makeInstanceID(server *iaas.Server) string {
return fmt.Sprintf("%s://%s", ProviderName, server.GetId())
}
// addToNodeAddresses appends the NodeAddresses to the passed-by-pointer slice,
// only if they do not already exist
func addToNodeAddresses(addresses *[]corev1.NodeAddress, addAddresses ...corev1.NodeAddress) {
for _, add := range addAddresses {
exists := false
for _, existing := range *addresses {
if existing.Address == add.Address && existing.Type == add.Type {
exists = true
break
}
}
if !exists {
*addresses = append(*addresses, add)
}
}
}
// 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.
// TODO(migration): rework function once openstack:/// is no longer used
func instanceIDFromProviderID(providerID string) (instanceID, region string, err error) {
// https://github.com/kubernetes/kubernetes/issues/85731
if providerID != "" && !strings.Contains(providerID, "://") {
providerID = ProviderName + "://" + providerID
}
switch {
// TODO(migration): remove old provider support after migration
case strings.HasPrefix(providerID, "openstack://"):
matches := oldProviderIDRegexp.FindStringSubmatch(providerID)
if len(matches) != 3 {
return "", "", fmt.Errorf("ProviderID \"%s\" didn't match expected format \"%s://region/InstanceID\"", oldProviderName, providerID)
}
return matches[2], matches[1], nil
case strings.HasPrefix(providerID, "stackit://"):
matches := providerIDRegexp.FindStringSubmatch(providerID)
if len(matches) != 2 {
return "", "", fmt.Errorf("ProviderID \"%s\" didn't match expected format \"%s://InstanceID\"", ProviderName, providerID)
}
// The new stackit:// doesn't use the old regional providerID anymore and strictly follows the spec
return matches[1], "", nil
default:
return "", "", fmt.Errorf("unknown ProviderName")
}
}
func getServerByName(ctx context.Context, client stackit.NodeClient, name, projectID, region string) (*iaas.Server, error) {
servers, err := client.ListServers(ctx, projectID, region)
if err != nil {
return nil, fmt.Errorf("failed to list servers: %w", err)
}
serverList := *servers
if len(serverList) == 0 {
return nil, cloudprovider.InstanceNotFound
}
// TODO: Implement field selector for ListServers so we don't have to do the following
for i := range serverList {
server := serverList[i]
if serverName, ok := server.GetNameOk(); ok && serverName == name {
return &server, nil
}
}
return nil, cloudprovider.InstanceNotFound
}
func (i *Instances) getInstance(ctx context.Context, node *corev1.Node) (*iaas.Server, error) {
if node.Spec.ProviderID == "" {
return getServerByName(ctx, i.iaasClient, node.Name, i.projectID, i.region)
}
instanceID, instanceRegion, err := instanceIDFromProviderID(node.Spec.ProviderID)
if err != nil {
return nil, fmt.Errorf("failed to get instance ID from Provider ID: %w", err)
}
if instanceRegion != "" && instanceRegion != i.region {
return nil, fmt.Errorf("ProviderID \"%s\" didn't match supported region \"%s\"", node.Spec.ProviderID, i.region)
}
server, err := i.iaasClient.GetServer(ctx, i.projectID, i.region, instanceID)
if stackit.IsNotFound(err) {
return nil, cloudprovider.InstanceNotFound
}
if err != nil {
return nil, fmt.Errorf("failed to get server: %w", err)
}
return server, nil
}