forked from kubernetes-sigs/cloud-provider-equinix-metal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevices.go
234 lines (199 loc) · 7 KB
/
devices.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
package metal
import (
"context"
"errors"
"fmt"
"strings"
"github.com/packethost/packngo"
"github.com/packethost/packngo/metadata"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
cloudprovider "k8s.io/cloud-provider"
cpapi "k8s.io/cloud-provider/api"
"k8s.io/klog/v2"
)
type instances struct {
client *packngo.Client
project string
}
var _ cloudprovider.InstancesV2 = (*instances)(nil)
func newInstances(client *packngo.Client, projectID string) *instances {
return &instances{client: client, project: projectID}
}
// InstanceShutdown returns true if the node is shutdown in cloudprovider
func (i *instances) InstanceShutdown(ctx context.Context, node *v1.Node) (bool, error) {
klog.V(2).Infof("called InstanceShutdown for node %s with providerID %s", node.GetName(), node.Spec.ProviderID)
device, err := i.deviceFromProviderID(node.Spec.ProviderID)
if err != nil {
return false, err
}
return device.State == "inactive", nil
}
// InstanceExists returns true if the node exists in cloudprovider
func (i *instances) InstanceExists(ctx context.Context, node *v1.Node) (bool, error) {
klog.V(2).Infof("called InstanceExists for node %s with providerID %s", node.GetName(), node.Spec.ProviderID)
_, err := i.deviceFromProviderID(node.Spec.ProviderID)
switch {
case errors.Is(err, cloudprovider.InstanceNotFound):
return false, nil
case err != nil:
return false, err
}
return true, nil
}
// InstanceMetadata returns instancemetadata for the node according to the cloudprovider
func (i *instances) InstanceMetadata(ctx context.Context, node *v1.Node) (*cloudprovider.InstanceMetadata, error) {
device, err := i.deviceByNode(node)
if err != nil {
return nil, err
}
// was a node IP provided
var providedNodeIP string
if node.Annotations != nil {
providedNodeIP = node.Annotations[cpapi.AnnotationAlphaProvidedIPAddr]
}
nodeAddresses, err := nodeAddresses(device, providedNodeIP)
if err != nil {
// TODO(displague) we error on missing private and public ip. is that restrictive?
// TODO(displague) should we return the public addresses DNS name as the Type=Hostname NodeAddress type too?
return nil, err
}
var p, r, z string
if device.Plan != nil {
p = device.Plan.Slug
}
// "A zone represents a logical failure domain"
// "A region represents a larger domain, made up of one or more zones"
//
// Equinix Metal metros are made up of one or more facilities, so we treat
// metros as K8s topology regions. EM facilities are then equated to zones.
//
// https://kubernetes.io/docs/reference/labels-annotations-taints/#topologykubernetesiozone
if device.Facility != nil {
z = device.Facility.Code
}
if device.Metro != nil {
r = device.Metro.Code
}
return &cloudprovider.InstanceMetadata{
ProviderID: providerIDFromDevice(device),
InstanceType: p,
NodeAddresses: nodeAddresses,
Zone: z,
Region: r,
}, nil
}
func nodeAddresses(device *packngo.Device, providedNodeIP string) ([]v1.NodeAddress, error) {
var (
addresses []v1.NodeAddress
unique = map[string]bool{}
privateIP, publicIP string
)
addr := v1.NodeAddress{Type: v1.NodeHostName, Address: device.Hostname}
unique[addr.Address] = true
addresses = append(addresses, addr)
// if the kubelet was started with --node-ip, that must be the first address to use
if providedNodeIP != "" {
privateIP = providedNodeIP
addr = v1.NodeAddress{Type: v1.NodeInternalIP, Address: providedNodeIP}
if _, ok := unique[addr.Address]; !ok {
unique[addr.Address] = true
addresses = append(addresses, addr)
}
}
for _, address := range device.Network {
if address.AddressFamily == int(metadata.IPv4) {
var addrType v1.NodeAddressType
if address.Public {
publicIP = address.Address
addrType = v1.NodeExternalIP
} else {
privateIP = address.Address
addrType = v1.NodeInternalIP
}
addr = v1.NodeAddress{Type: addrType, Address: address.Address}
if _, ok := unique[addr.Address]; !ok {
unique[addr.Address] = true
addresses = append(addresses, addr)
}
}
}
if privateIP == "" {
return nil, errors.New("could not get at least one private ip")
}
if publicIP == "" {
return nil, errors.New("could not get at least one public ip")
}
return addresses, nil
}
func (i *instances) deviceByNode(node *v1.Node) (*packngo.Device, error) {
if node.Spec.ProviderID != "" {
return i.deviceFromProviderID(node.Spec.ProviderID)
}
return deviceByName(i.client, i.project, types.NodeName(node.GetName()))
}
func deviceByID(client *packngo.Client, id string) (*packngo.Device, error) {
klog.V(2).Infof("called deviceByID with ID %s", id)
device, _, err := client.Devices.Get(id, nil)
if isNotFound(err) {
return nil, cloudprovider.InstanceNotFound
}
return device, err
}
// deviceByName returns an instance whose hostname matches the kubernetes node.Name
func deviceByName(client *packngo.Client, projectID string, nodeName types.NodeName) (*packngo.Device, error) {
klog.V(2).Infof("called deviceByName with projectID %s nodeName %s", projectID, nodeName)
if string(nodeName) == "" {
return nil, errors.New("node name cannot be empty string")
}
devices, _, err := client.Devices.List(projectID, nil)
if err != nil {
klog.V(2).Infof("error listing devices for project %s: %v", projectID, err)
return nil, err
}
for _, device := range devices {
if device.Hostname == string(nodeName) {
klog.V(2).Infof("Found device %s for nodeName %s", device.ID, nodeName)
return &device, nil
}
}
klog.V(2).Infof("No device found for nodeName %s", nodeName)
return nil, cloudprovider.InstanceNotFound
}
// deviceIDFromProviderID returns a device's ID from providerID.
//
// The providerID spec should be retrievable from the Kubernetes
// node object. The expected format is: equinixmetal://device-id or just device-id
func deviceIDFromProviderID(providerID string) (string, error) {
klog.V(2).Infof("called deviceIDFromProviderID with providerID %s", providerID)
if providerID == "" {
return "", errors.New("providerID cannot be empty string")
}
split := strings.Split(providerID, "://")
var deviceID string
switch len(split) {
case 2:
deviceID = split[1]
if split[0] != ProviderName && split[0] != deprecatedProviderName {
return "", fmt.Errorf("provider name from providerID should be %s, was %s", ProviderName, split[0])
}
case 1:
deviceID = providerID
default:
return "", fmt.Errorf("unexpected providerID format: %s, format should be: 'device-id' or 'equinixmetal://device-id'", providerID)
}
return deviceID, nil
}
// deviceFromProviderID uses providerID to get the device id and return the device
func (i *instances) deviceFromProviderID(providerID string) (*packngo.Device, error) {
klog.V(2).Infof("called deviceFromProviderID with providerID %s", providerID)
id, err := deviceIDFromProviderID(providerID)
if err != nil {
return nil, err
}
return deviceByID(i.client, id)
}
// providerIDFromDevice returns a providerID from a device
func providerIDFromDevice(device *packngo.Device) string {
return fmt.Sprintf("%s://%s", ProviderName, device.ID)
}