forked from confidential-containers/cloud-api-adaptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider.go
439 lines (358 loc) · 13.1 KB
/
provider.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
// (C) Copyright Confidential Containers Contributors
// SPDX-License-Identifier: Apache-2.0
package ibmcloud
import (
"context"
"errors"
"fmt"
"log"
"net/netip"
"os"
"time"
"github.com/IBM/go-sdk-core/v5/core"
"github.com/IBM/vpc-go-sdk/vpcv1"
provider "github.com/confidential-containers/cloud-api-adaptor/src/cloud-providers"
"github.com/confidential-containers/cloud-api-adaptor/src/cloud-providers/util"
"github.com/confidential-containers/cloud-api-adaptor/src/cloud-providers/util/cloudinit"
)
const (
maxRetries = 10
queryInterval = 2
)
var logger = log.New(log.Writer(), "[adaptor/cloud/ibmcloud] ", log.LstdFlags|log.Lmsgprefix)
var errNotReady = errors.New("address not ready")
const maxInstanceNameLen = 63
type vpcV1 interface {
CreateInstanceWithContext(context.Context, *vpcv1.CreateInstanceOptions) (*vpcv1.Instance, *core.DetailedResponse, error)
GetInstanceWithContext(context.Context, *vpcv1.GetInstanceOptions) (*vpcv1.Instance, *core.DetailedResponse, error)
DeleteInstanceWithContext(context.Context, *vpcv1.DeleteInstanceOptions) (*core.DetailedResponse, error)
GetInstanceProfileWithContext(context.Context, *vpcv1.GetInstanceProfileOptions) (*vpcv1.InstanceProfile, *core.DetailedResponse, error)
GetImageWithContext(ctx context.Context, getImageOptions *vpcv1.GetImageOptions) (*vpcv1.Image, *core.DetailedResponse, error)
}
type ibmcloudVPCProvider struct {
vpc vpcV1
serviceConfig *Config
}
func NewProvider(config *Config) (provider.Provider, error) {
var authenticator core.Authenticator
if config.ApiKey != "" {
authenticator = &core.IamAuthenticator{
ApiKey: config.ApiKey,
URL: config.IamServiceURL,
}
} else if config.IAMProfileID != "" {
authenticator = &core.ContainerAuthenticator{
URL: config.IamServiceURL,
IAMProfileID: config.IAMProfileID,
CRTokenFilename: config.CRTokenFileName,
}
} else {
return nil, fmt.Errorf("either an IAM API Key or Profile ID needs to be set")
}
nodeName, ok := os.LookupEnv("NODE_NAME")
var nodeLabels map[string]string
if ok {
var err error
nodeLabels, err = util.NodeLabels(context.TODO(), nodeName)
if err != nil {
logger.Printf("warning, could not find node labels\ndue to: %v\n", err)
}
}
nodeRegion, ok := nodeLabels["topology.kubernetes.io/region"]
if config.VpcServiceURL == "" && ok {
// Assume in prod if fetching from labels for now
// TODO handle other environments
config.VpcServiceURL = fmt.Sprintf("https://%s.iaas.cloud.ibm.com/v1", nodeRegion)
}
vpcV1, err := vpcv1.NewVpcV1(&vpcv1.VpcV1Options{
Authenticator: authenticator,
URL: config.VpcServiceURL,
})
if err != nil {
return nil, err
}
// If this label exists assume we are in an IKS cluster
primarySubnetID, iks := nodeLabels["ibm-provider.kubernetes.io/subnet-id"]
if !iks {
primarySubnetID, iks = nodeLabels["ibm-cloud.kubernetes.io/subnet-id"]
}
if iks {
if config.ZoneName == "" {
config.ZoneName = nodeLabels["topology.kubernetes.io/zone"]
}
vpcID, rgID, sgID, err := fetchVPCDetails(vpcV1, primarySubnetID)
if err != nil {
logger.Printf("warning, unable to automatically populate VPC details\ndue to: %v\n", err)
} else {
if config.PrimarySubnetID == "" {
config.PrimarySubnetID = primarySubnetID
}
if config.VpcID == "" {
config.VpcID = vpcID
}
if config.ResourceGroupID == "" {
config.ResourceGroupID = rgID
}
if config.PrimarySecurityGroupID == "" {
config.PrimarySecurityGroupID = sgID
}
}
}
provider := &ibmcloudVPCProvider{
vpc: vpcV1,
serviceConfig: config,
}
if err = provider.updateInstanceProfileSpecList(); err != nil {
return nil, err
}
if err = provider.updateImageList(context.TODO()); err != nil {
return nil, err
}
logger.Printf("ibmcloud-vpc config: %#v", config.Redact())
return provider, nil
}
func fetchVPCDetails(vpcV1 *vpcv1.VpcV1, subnetID string) (vpcID string, resourceGroupID string, securityGroupID string, e error) {
subnet, response, err := vpcV1.GetSubnet(&vpcv1.GetSubnetOptions{
ID: &subnetID,
})
if err != nil {
e = fmt.Errorf("VPC error with:\n %w\nfurther details:\n %v", err, response)
return
}
sg, response, err := vpcV1.GetVPCDefaultSecurityGroup(&vpcv1.GetVPCDefaultSecurityGroupOptions{
ID: subnet.VPC.ID,
})
if err != nil {
e = fmt.Errorf("VPC error with:\n %w\nfurther details:\n %v", err, response)
return
}
securityGroupID = *sg.ID
vpcID = *subnet.VPC.ID
resourceGroupID = *subnet.ResourceGroup.ID
return
}
func (p *ibmcloudVPCProvider) getInstancePrototype(instanceName, userData, instanceProfile, imageId string) *vpcv1.InstancePrototype {
prototype := &vpcv1.InstancePrototype{
Name: &instanceName,
Image: &vpcv1.ImageIdentity{ID: &imageId},
UserData: &userData,
Profile: &vpcv1.InstanceProfileIdentity{Name: &instanceProfile},
Zone: &vpcv1.ZoneIdentity{Name: &p.serviceConfig.ZoneName},
Keys: []vpcv1.KeyIdentityIntf{},
VPC: &vpcv1.VPCIdentity{ID: &p.serviceConfig.VpcID},
PrimaryNetworkInterface: &vpcv1.NetworkInterfacePrototype{
Subnet: &vpcv1.SubnetIdentity{ID: &p.serviceConfig.PrimarySubnetID},
SecurityGroups: []vpcv1.SecurityGroupIdentityIntf{
&vpcv1.SecurityGroupIdentityByID{ID: &p.serviceConfig.PrimarySecurityGroupID},
},
},
}
if p.serviceConfig.KeyID != "" {
prototype.Keys = append(prototype.Keys, &vpcv1.KeyIdentity{ID: &p.serviceConfig.KeyID})
}
if p.serviceConfig.ResourceGroupID != "" {
prototype.ResourceGroup = &vpcv1.ResourceGroupIdentity{ID: &p.serviceConfig.ResourceGroupID}
}
if p.serviceConfig.SecondarySubnetID != "" {
var allowIPSpoofing bool = true
prototype.NetworkInterfaces = []vpcv1.NetworkInterfacePrototype{
{
AllowIPSpoofing: &allowIPSpoofing,
Subnet: &vpcv1.SubnetIdentity{ID: &p.serviceConfig.SecondarySubnetID},
SecurityGroups: []vpcv1.SecurityGroupIdentityIntf{
&vpcv1.SecurityGroupIdentityByID{ID: &p.serviceConfig.SecondarySecurityGroupID},
},
},
}
}
return prototype
}
func getIPs(instance *vpcv1.Instance, instanceID string, numInterfaces int) ([]netip.Addr, error) {
interfaces := []*vpcv1.NetworkInterfaceInstanceContextReference{instance.PrimaryNetworkInterface}
for i, nic := range instance.NetworkInterfaces {
if *nic.ID != *instance.PrimaryNetworkInterface.ID {
interfaces = append(interfaces, &instance.NetworkInterfaces[i])
}
}
var ips []netip.Addr
for i, nic := range interfaces {
if nic.PrimaryIP == nil {
return nil, errNotReady
}
addr := nic.PrimaryIP.Address
if addr == nil || *addr == "" || *addr == "0.0.0.0" {
return nil, errNotReady
}
ip, err := netip.ParseAddr(*addr)
if err != nil {
return nil, fmt.Errorf("failed to parse pod node IP %q: %w", *addr, err)
}
ips = append(ips, ip)
logger.Printf("podNodeIP[%d]=%s", i, ip.String())
}
if len(ips) < numInterfaces {
return nil, errNotReady
}
return ips, nil
}
func (p *ibmcloudVPCProvider) CreateInstance(ctx context.Context, podName, sandboxID string, cloudConfig cloudinit.CloudConfigGenerator, spec provider.InstanceTypeSpec) (*provider.Instance, error) {
instanceName := util.GenerateInstanceName(podName, sandboxID, maxInstanceNameLen)
userData, err := cloudConfig.Generate()
if err != nil {
return nil, err
}
instanceProfile, err := p.selectInstanceProfile(ctx, spec)
if err != nil {
return nil, err
}
imageID, err := p.selectImage(ctx, spec, instanceProfile)
if err != nil {
return nil, err
}
prototype := p.getInstancePrototype(instanceName, userData, instanceProfile, imageID)
logger.Printf("CreateInstance: name: %q", instanceName)
vpcInstance, resp, err := p.vpc.CreateInstanceWithContext(ctx, &vpcv1.CreateInstanceOptions{InstancePrototype: prototype})
if err != nil {
logger.Printf("failed to create an instance : %v and the response is %s", err, resp)
return nil, err
}
instanceID := *vpcInstance.ID
numInterfaces := len(prototype.NetworkInterfaces)
var ips []netip.Addr
for retries := 0; retries < maxRetries; retries++ {
ips, err = getIPs(vpcInstance, instanceID, numInterfaces)
if err == nil {
break
}
if err != errNotReady {
return nil, err
}
time.Sleep(time.Duration(queryInterval) * time.Second)
result, resp, err := p.vpc.GetInstanceWithContext(ctx, &vpcv1.GetInstanceOptions{ID: &instanceID})
if err != nil {
logger.Printf("failed to get an instance : %v and the response is %s", err, resp)
return nil, err
}
vpcInstance = result
}
instance := &provider.Instance{
ID: instanceID,
Name: instanceName,
IPs: ips,
}
return instance, nil
}
// Select an instance profile based on the memory and vcpu requirements
func (p *ibmcloudVPCProvider) selectInstanceProfile(ctx context.Context, spec provider.InstanceTypeSpec) (string, error) {
return provider.SelectInstanceTypeToUse(spec, p.serviceConfig.InstanceProfileSpecList, p.serviceConfig.InstanceProfiles, p.serviceConfig.ProfileName)
}
// Populate instanceProfileSpecList for all the instanceProfiles
func (p *ibmcloudVPCProvider) updateInstanceProfileSpecList() error {
// Get the instance types from the service config
instanceProfiles := p.serviceConfig.InstanceProfiles
// If instanceProfiles is empty then populate it with the default instance type
if len(instanceProfiles) == 0 {
instanceProfiles = append(instanceProfiles, p.serviceConfig.ProfileName)
}
// Create a list of instanceProfileSpec
var instanceProfileSpecList []provider.InstanceTypeSpec
// Iterate over the instance types and populate the instanceProfileSpecList
for _, profileType := range instanceProfiles {
vcpus, memory, arch, err := p.getProfileNameInformation(profileType)
if err != nil {
return err
}
instanceProfileSpecList = append(instanceProfileSpecList, provider.InstanceTypeSpec{InstanceType: profileType, VCPUs: vcpus, Memory: memory, Arch: arch})
}
// Sort the instanceProfileSpecList by Memory and update the serviceConfig
p.serviceConfig.InstanceProfileSpecList = provider.SortInstanceTypesOnMemory(instanceProfileSpecList)
logger.Printf("instanceProfileSpecList (%v)", p.serviceConfig.InstanceProfileSpecList)
return nil
}
// Add a method to retrieve cpu, memory, and arch from the profile name
func (p *ibmcloudVPCProvider) getProfileNameInformation(profileName string) (vcpu int64, memory int64, arch string, err error) {
// Get the profile information from the instance type using IBMCloud API
result, details, err := p.vpc.GetInstanceProfileWithContext(context.Background(),
&vpcv1.GetInstanceProfileOptions{
Name: &profileName,
},
)
if err != nil {
return 0, 0, "", fmt.Errorf("instance profile name %s not found, due to %w\nFurther Details:\n%v", profileName, err, details)
}
vcpu = int64(*result.VcpuCount.(*vpcv1.InstanceProfileVcpu).Value)
// Value returned is in GiB, convert to MiB
memory = int64(*result.Memory.(*vpcv1.InstanceProfileMemory).Value) * 1024
arch = string(*result.VcpuArchitecture.Value)
return vcpu, memory, arch, nil
}
// Select Image from list, invalid image IDs should have already been removed
func (p *ibmcloudVPCProvider) selectImage(ctx context.Context, spec provider.InstanceTypeSpec, selectedInstanceProfile string) (string, error) {
specArch := spec.Arch
if specArch == "" {
for _, instanceProfileSpec := range p.serviceConfig.InstanceProfileSpecList {
if instanceProfileSpec.InstanceType == selectedInstanceProfile {
specArch = instanceProfileSpec.Arch
break
}
}
}
for _, image := range p.serviceConfig.Images {
if specArch != "" && image.Arch != specArch {
continue
}
logger.Printf("selected image with ID <%s> out of %d images", image.ID, len(p.serviceConfig.Images))
return image.ID, nil
}
return "", fmt.Errorf("unable to find matching image to use")
}
// Remove Images that are not valid (e.g. not found in this region)
func (p *ibmcloudVPCProvider) updateImageList(ctx context.Context) error {
i := 0
for _, image := range p.serviceConfig.Images {
arch, os, err := p.getImageDetails(ctx, image.ID)
if err != nil {
logger.Printf("skipping image (%s), due to %v", image.ID, err)
continue
}
image.Arch = arch
image.OS = os
p.serviceConfig.Images[i] = image
i++
}
if i == 0 {
return fmt.Errorf("no images valid images found")
}
p.serviceConfig.Images = p.serviceConfig.Images[:i]
return nil
}
func (p *ibmcloudVPCProvider) getImageDetails(ctx context.Context, imageID string) (arch, os string, err error) {
result, _, err := p.vpc.GetImageWithContext(ctx, &vpcv1.GetImageOptions{
ID: &imageID,
})
if err != nil {
return "", "", err
}
return *result.OperatingSystem.Architecture, *result.OperatingSystem.Name, nil
}
func (p *ibmcloudVPCProvider) DeleteInstance(ctx context.Context, instanceID string) error {
options := &vpcv1.DeleteInstanceOptions{}
options.SetID(instanceID)
resp, err := p.vpc.DeleteInstanceWithContext(ctx, options)
if err != nil {
logger.Printf("failed to delete an instance: %v and the response is %v", err, resp)
return err
}
logger.Printf("deleted an instance %s", instanceID)
return nil
}
func (p *ibmcloudVPCProvider) Teardown() error {
return nil
}
func (p *ibmcloudVPCProvider) ConfigVerifier() error {
images := p.serviceConfig.Images.String()
if len(images) == 0 {
return fmt.Errorf("image-id is empty")
}
return nil
}