/
provision_common.go
1385 lines (1218 loc) · 36 KB
/
provision_common.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// (C) Copyright Confidential Containers Contributors
// SPDX-License-Identifier: Apache-2.0
package aws
import (
"context"
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"text/template"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
awsConfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/ec2"
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/aws/aws-sdk-go-v2/service/eks"
ekstypes "github.com/aws/aws-sdk-go-v2/service/eks/types"
"github.com/aws/aws-sdk-go-v2/service/iam"
"github.com/aws/aws-sdk-go-v2/service/s3"
pv "github.com/confidential-containers/cloud-api-adaptor/src/cloud-api-adaptor/test/provisioner"
log "github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
kconf "sigs.k8s.io/e2e-framework/klient/conf"
"sigs.k8s.io/e2e-framework/klient/k8s"
"sigs.k8s.io/e2e-framework/pkg/envconf"
)
const (
EksCniAddonVersion = "v1.12.5-eksbuild.2"
EksVersion = "1.26"
)
var AWSProps = &AWSProvisioner{}
// S3Bucket represents an S3 bucket where the podvm image should be uploaded
type S3Bucket struct {
Client *s3.Client
Name string // Bucket name
Key string // Object key
}
// AMIImage represents an AMI image
type AMIImage struct {
Client *ec2.Client
Description string // Image description
DiskDescription string // Disk description
DiskFormat string // Disk format
EBSSnapshotId string // EBS disk snapshot ID
ID string // AMI image ID
RootDeviceName string // Root device name
}
// Vpc represents an AWS VPC
type Vpc struct {
BaseName string
CidrBlock string
Client *ec2.Client
ID string
InternetGatewayId string
Region string
RouteTableId string
SecurityGroupId string
SubnetId string
SecondarySubnetId string
}
// Cluster defines create/delete/access interfaces to Kubernetes clusters
type Cluster interface {
CreateCluster() error // Create the Kubernetes cluster
DeleteCluster() error // Delete the Kubernetes cluster
GetKubeconfigFile() (string, error) // Get the path to the kubeconfig file
}
// EKSCluster represents an EKS cluster
type EKSCluster struct {
AwsConfig aws.Config
Client *eks.Client
ClusterRoleName string
IamClient *iam.Client
Name string
NodeGroupName string
NodesRoleName string
NumWorkers int32
SshKpName string
Version string
Vpc *Vpc
}
// OnPremCluster represents an existing and running cluster
type OnPremCluster struct {
}
// AWSProvisioner implements the CloudProvision interface.
type AWSProvisioner struct {
AwsConfig aws.Config
iamClient *iam.Client
Cluster Cluster
ec2Client *ec2.Client
s3Client *s3.Client
Bucket *S3Bucket
PauseImage string
Image *AMIImage
Vpc *Vpc
VxlanPort string
SshKpName string
}
// AwsInstallOverlay implements the InstallOverlay interface
type AwsInstallOverlay struct {
Overlay *pv.KustomizeOverlay
}
// NewAWSProvisioner instantiates the AWS provisioner
func NewAWSProvisioner(properties map[string]string) (pv.CloudProvisioner, error) {
var cluster Cluster
cfg, err := awsConfig.LoadDefaultConfig(context.TODO())
if err != nil {
log.Error("Failed to load AWS config")
return nil, err
}
if properties["aws_region"] != "" {
cfg.Region = properties["aws_region"]
} else {
properties["aws_region"] = cfg.Region
}
ec2Client := ec2.NewFromConfig(cfg)
vpc := NewVpc(ec2Client, properties)
if properties["cluster_type"] == "" ||
properties["cluster_type"] == "onprem" {
cluster = NewOnPremCluster()
} else if properties["cluster_type"] == "eks" {
cluster = NewEKSCluster(cfg, vpc, properties["ssh_kp_name"])
} else {
return nil, fmt.Errorf("Cluster type '%s' not implemented",
properties["cluster_type"])
}
AWSProps = &AWSProvisioner{
AwsConfig: cfg,
iamClient: iam.NewFromConfig(cfg),
ec2Client: ec2.NewFromConfig(cfg),
s3Client: s3.NewFromConfig(cfg),
Bucket: &S3Bucket{
Client: s3.NewFromConfig(cfg),
Name: "peer-pods-tests",
Key: "", // To be defined when the file is uploaded
},
Cluster: cluster,
Image: NewAMIImage(ec2Client, properties),
PauseImage: properties["pause_image"],
Vpc: vpc,
VxlanPort: properties["vxlan_port"],
SshKpName: properties["ssh_kp_name"],
}
return AWSProps, nil
}
func (a *AWSProvisioner) CreateCluster(ctx context.Context, cfg *envconf.Config) error {
err := a.Cluster.CreateCluster()
if err != nil {
return err
}
kubeconfigPath, err := a.Cluster.GetKubeconfigFile()
if err != nil {
return err
}
*cfg = *envconf.NewWithKubeConfig(kubeconfigPath)
return nil
}
func (a *AWSProvisioner) CreateVPC(ctx context.Context, cfg *envconf.Config) error {
var err error
if a.Vpc.ID == "" {
log.Infof("Create AWS VPC on region %s", a.AwsConfig.Region)
if err = a.Vpc.createVpc(); err != nil {
return err
}
log.Infof("VPC Id: %s", a.Vpc.ID)
}
if a.Vpc.SubnetId == "" {
log.Infof("Create subnet on VPC %s", a.Vpc.ID)
if err = a.Vpc.createSubnet(); err != nil {
return err
}
log.Infof("Subnet Id: %s", a.Vpc.SubnetId)
if err = a.Vpc.setupVpcNetworking(); err != nil {
return err
}
}
if a.Vpc.SecurityGroupId == "" {
log.Infof("Create security group on VPC %s", a.Vpc.ID)
if err = a.Vpc.setupSecurityGroup(); err != nil {
return err
}
log.Infof("Security groupd Id: %s", a.Vpc.SecurityGroupId)
}
return nil
}
func (aws *AWSProvisioner) DeleteCluster(ctx context.Context, cfg *envconf.Config) error {
return nil
}
func (a *AWSProvisioner) DeleteVPC(ctx context.Context, cfg *envconf.Config) error {
var err error
vpc := a.Vpc
if vpc.SecurityGroupId != "" {
log.Infof("Delete security group: %s", vpc.SecurityGroupId)
if err = vpc.deleteSecurityGroup(); err != nil {
return err
}
}
if vpc.SubnetId != "" {
log.Infof("Delete subnet: %s", vpc.SubnetId)
if err = vpc.deleteSubnet(); err != nil {
return err
}
}
if vpc.ID != "" {
log.Infof("Delete vpc: %s", vpc.ID)
if err = vpc.deleteVpc(); err != nil {
return err
}
}
return nil
}
func (a *AWSProvisioner) GetProperties(ctx context.Context, cfg *envconf.Config) map[string]string {
credentials, _ := a.AwsConfig.Credentials.Retrieve(context.TODO())
return map[string]string{
"pause_image": a.PauseImage,
"podvm_launchtemplate": "",
"podvm_ami": a.Image.ID,
"podvm_instance_type": "t3.small",
"sg_ids": a.Vpc.SecurityGroupId, // TODO: what other SG needed?
"subnet_id": a.Vpc.SubnetId,
"ssh_kp_name": a.SshKpName,
"region": a.AwsConfig.Region,
"access_key_id": credentials.AccessKeyID,
"secret_access_key": credentials.SecretAccessKey,
"vxlan_port": a.VxlanPort,
}
}
func (a *AWSProvisioner) UploadPodvm(imagePath string, ctx context.Context, cfg *envconf.Config) error {
// AWS EC2 image-import does not support qcow2 files so convert the image to raw format.
imageRawFile, err := os.CreateTemp("", "podvm.*.raw")
imageRawPath := imageRawFile.Name()
imageRawFile.Close()
if err != nil {
return err
}
defer func() {
_, err := os.Stat(imageRawPath)
if err == nil {
os.Remove(imageRawPath)
}
}()
log.Infof("Convert qcow2 image to raw")
if err = ConvertQcow2ToRaw(imagePath, imageRawPath); err != nil {
return err
}
// Create the S3 bucket
log.Infof("Create bucket '%s'", a.Bucket.Name)
if err = a.Bucket.createBucket(); err != nil {
return err
}
// Create the vmimport role
log.Infof("Create vmimport service role")
if err = createVmimportServiceRole(ctx, a.iamClient, a.Bucket.Name); err != nil {
return err
}
// Upload raw image to S3
log.Infof("Upload image %s to S3 bucket '%s'", imageRawPath, a.Bucket.Name)
if err = a.Bucket.uploadLargeFileWithCli(imageRawPath); err != nil {
return err
}
log.Infof("Import disk snapshot for S3 key '%s'", a.Bucket.Key)
if err = a.Image.importEBSSnapshot(a.Bucket); err != nil {
return err
}
imageName := strings.Replace(filepath.Base(imagePath), ".qcow2", ".raw", 1)
log.Infof("Register image with name: %s", imageName)
err = a.Image.registerImage(imageName)
if err != nil {
return err
}
log.Infof("New AMI ID: %s", a.Image.ID)
return nil
}
func NewVpc(client *ec2.Client, properties map[string]string) *Vpc {
// Initialize the VPC CidrBlock
cidrBlock := properties["aws_vpc_cidrblock"]
if cidrBlock == "" {
cidrBlock = "10.0.0.0/24"
}
return &Vpc{
BaseName: "caa-e2e-test",
CidrBlock: cidrBlock,
Client: client,
ID: properties["aws_vpc_id"],
Region: properties["aws_region"],
SecurityGroupId: properties["aws_vpc_sg_id"],
SubnetId: properties["aws_vpc_subnet_id"],
InternetGatewayId: properties["aws_vpc_igw_id"],
RouteTableId: properties["aws_vpc_rt_id"],
}
}
// createVpc creates the VPC
func (v *Vpc) createVpc() error {
vpc, err := v.Client.CreateVpc(context.TODO(), &ec2.CreateVpcInput{
CidrBlock: aws.String(v.CidrBlock),
TagSpecifications: []ec2types.TagSpecification{
{
ResourceType: ec2types.ResourceTypeVpc,
Tags: []ec2types.Tag{
{
Key: aws.String("Name"),
Value: aws.String(v.BaseName + "-vpc"),
},
},
},
},
})
if err != nil {
return err
}
v.ID = *vpc.Vpc.VpcId
return nil
}
// createSubnet creates the VPC subnet
func (v *Vpc) createSubnet() error {
subnet, err := v.Client.CreateSubnet(context.TODO(),
&ec2.CreateSubnetInput{
VpcId: aws.String(v.ID),
CidrBlock: aws.String("10.0.0.0/25"),
TagSpecifications: []ec2types.TagSpecification{
{
ResourceType: ec2types.ResourceTypeSubnet,
Tags: []ec2types.Tag{
{
Key: aws.String("Name"),
Value: aws.String(v.BaseName + "-subnet"),
},
},
},
},
})
if err != nil {
return err
}
v.SubnetId = *subnet.Subnet.SubnetId
// Allow for instances created on the subnet to have a public IP assigned
if _, err = v.Client.ModifySubnetAttribute(context.TODO(),
&ec2.ModifySubnetAttributeInput{
MapPublicIpOnLaunch: &ec2types.AttributeBooleanValue{
Value: aws.Bool(true),
},
SubnetId: aws.String(v.SubnetId),
}); err != nil {
return err
}
return nil
}
func (v *Vpc) createSecondarySubnet() error {
if v.SecondarySubnetId != "" {
return nil
}
// EKS requires at least two subnets and they should be on different
// Availability zones on the same region. So let's ensure this secondary
// subnet's AZ don't clash with the primary's.
subnets, err := v.Client.DescribeSubnets(context.TODO(),
&ec2.DescribeSubnetsInput{
SubnetIds: []string{v.SubnetId},
})
if err != nil {
return err
}
primarySubnetAz := *subnets.Subnets[0].AvailabilityZone
secondarySubnetAz := v.Region + "a"
if secondarySubnetAz == primarySubnetAz {
secondarySubnetAz = v.Region + "b"
}
subnet, err := v.Client.CreateSubnet(context.TODO(),
&ec2.CreateSubnetInput{
AvailabilityZone: aws.String(secondarySubnetAz),
VpcId: aws.String(v.ID),
CidrBlock: aws.String("10.0.0.128/25"),
TagSpecifications: []ec2types.TagSpecification{
{
ResourceType: ec2types.ResourceTypeSubnet,
Tags: []ec2types.Tag{
{
Key: aws.String("Name"),
Value: aws.String(v.BaseName + "-subnet-2"),
},
},
},
},
})
if err != nil {
return err
}
v.SecondarySubnetId = *subnet.Subnet.SubnetId
return nil
}
// setupInternetGateway creates an internet gateway and table of routes, and
// associated them with the VPC
func (v *Vpc) setupVpcNetworking() error {
var (
rtOutput *ec2.CreateRouteTableOutput
igwOutput *ec2.CreateInternetGatewayOutput
err error
)
if v.SubnetId == "" {
return fmt.Errorf("Missing subnet Id to setup the VPC %s network\n", v.ID)
}
if igwOutput, err = v.Client.CreateInternetGateway(context.TODO(),
&ec2.CreateInternetGatewayInput{
TagSpecifications: []ec2types.TagSpecification{
{
ResourceType: ec2types.ResourceTypeInternetGateway,
Tags: []ec2types.Tag{
{
Key: aws.String("Name"),
Value: aws.String(v.BaseName + "-igw"),
},
},
},
},
}); err != nil {
return err
}
v.InternetGatewayId = *igwOutput.InternetGateway.InternetGatewayId
if _, err = v.Client.AttachInternetGateway(context.TODO(),
&ec2.AttachInternetGatewayInput{
InternetGatewayId: igwOutput.InternetGateway.InternetGatewayId,
VpcId: aws.String(v.ID),
}); err != nil {
return err
}
if rtOutput, err = v.Client.CreateRouteTable(context.TODO(),
&ec2.CreateRouteTableInput{
VpcId: aws.String(v.ID),
TagSpecifications: []ec2types.TagSpecification{
{
ResourceType: ec2types.ResourceTypeRouteTable,
Tags: []ec2types.Tag{
{
Key: aws.String("Name"),
Value: aws.String(v.BaseName + "-rtb"),
},
},
},
},
}); err != nil {
return err
}
if _, err := v.Client.CreateRoute(context.TODO(),
&ec2.CreateRouteInput{
RouteTableId: rtOutput.RouteTable.RouteTableId,
DestinationCidrBlock: aws.String("0.0.0.0/0"),
GatewayId: igwOutput.InternetGateway.InternetGatewayId,
}); err != nil {
return err
}
v.RouteTableId = *rtOutput.RouteTable.RouteTableId
if _, err := v.Client.AssociateRouteTable(context.TODO(),
&ec2.AssociateRouteTableInput{
RouteTableId: rtOutput.RouteTable.RouteTableId,
SubnetId: aws.String(v.SubnetId),
}); err != nil {
return err
}
return nil
}
func (v *Vpc) setupSecurityGroup() error {
if sgOutput, err := v.Client.CreateSecurityGroup(context.TODO(),
&ec2.CreateSecurityGroupInput{
Description: aws.String("cloud-api-adaptor e2e tests"),
GroupName: aws.String(v.BaseName + "-sg"),
VpcId: aws.String(v.ID),
}); err != nil {
return err
} else {
v.SecurityGroupId = *sgOutput.GroupId
}
if _, err := v.Client.AuthorizeSecurityGroupIngress(context.TODO(),
&ec2.AuthorizeSecurityGroupIngressInput{
IpPermissions: []ec2types.IpPermission{
{
FromPort: aws.Int32(-1),
IpProtocol: aws.String("icmp"),
IpRanges: []ec2types.IpRange{
{
CidrIp: aws.String("0.0.0.0/0"),
Description: aws.String("ingress rule for icmp access"),
},
},
ToPort: aws.Int32(-1),
},
{
FromPort: aws.Int32(22),
IpProtocol: aws.String("tcp"),
IpRanges: []ec2types.IpRange{
{
CidrIp: aws.String("0.0.0.0/0"),
Description: aws.String("ingress rule for ssh access"),
},
},
ToPort: aws.Int32(22),
},
{
FromPort: aws.Int32(6443),
IpProtocol: aws.String("tcp"),
IpRanges: []ec2types.IpRange{
{
CidrIp: aws.String("0.0.0.0/0"),
Description: aws.String("ingress rule for https traffic"),
},
},
ToPort: aws.Int32(6443),
},
{
FromPort: aws.Int32(15150),
IpProtocol: aws.String("tcp"),
IpRanges: []ec2types.IpRange{
{
CidrIp: aws.String("0.0.0.0/0"),
Description: aws.String("ingress rule for CAA proxy traffic"),
},
},
ToPort: aws.Int32(15150),
},
},
GroupId: aws.String(v.SecurityGroupId),
}); err != nil {
return err
}
if _, err := v.Client.AuthorizeSecurityGroupEgress(context.TODO(),
&ec2.AuthorizeSecurityGroupEgressInput{
IpPermissions: []ec2types.IpPermission{
{
FromPort: aws.Int32(6443),
IpProtocol: aws.String("tcp"),
IpRanges: []ec2types.IpRange{
{
CidrIp: aws.String("0.0.0.0/0"),
Description: aws.String("egress rule for https traffic"),
},
},
ToPort: aws.Int32(6443),
},
},
GroupId: aws.String(v.SecurityGroupId),
}); err != nil {
return err
}
return nil
}
// deleteSecurityGroup deletes the security group.
func (v *Vpc) deleteSecurityGroup() error {
if v.SecurityGroupId == "" {
return nil
}
if _, err := v.Client.DeleteSecurityGroup(context.TODO(),
&ec2.DeleteSecurityGroupInput{
GroupId: aws.String(v.SecurityGroupId),
}); err != nil {
return err
}
return nil
}
// deleteSubnet deletes the subnet. Instances running on the subnet will
// be terminated before.
func (v *Vpc) deleteSubnet() error {
if v.SubnetId == "" {
return nil
}
// There will be needed to terminate all instances launched on this subnet
// before the attempt to delete the subnet.
describeInstances, err := v.Client.DescribeInstances(context.TODO(),
&ec2.DescribeInstancesInput{
Filters: []ec2types.Filter{
{
Name: aws.String("subnet-id"),
Values: []string{v.SubnetId},
},
},
})
if err != nil {
return err
}
// Getting the instances IDs
instanceIds := make([]string, 0)
for _, reservation := range describeInstances.Reservations {
for _, instance := range reservation.Instances {
instanceIds = append(instanceIds, *instance.InstanceId)
}
}
if len(instanceIds) > 0 {
// Delete all instances in a single step
if _, err = v.Client.TerminateInstances(context.TODO(),
&ec2.TerminateInstancesInput{
InstanceIds: instanceIds,
}); err != nil {
return err
}
}
// Finally delete the subnet
if _, err = v.Client.DeleteSubnet(context.TODO(),
&ec2.DeleteSubnetInput{
SubnetId: aws.String(v.SubnetId),
}); err != nil {
return err
}
return nil
}
// deleteVpc deletes the VPC. All resources attached to it will be
// deleted before.
func (v *Vpc) deleteVpc() error {
var err error
if v.ID == "" {
return nil
}
// Delete the networking resources first
if v.RouteTableId != "" {
if _, err = v.Client.DeleteRouteTable(context.TODO(),
&ec2.DeleteRouteTableInput{
RouteTableId: aws.String(v.RouteTableId),
}); err != nil {
return err
}
}
// The internet gateway time
if v.InternetGatewayId != "" {
if _, err = v.Client.DetachInternetGateway(context.TODO(),
&ec2.DetachInternetGatewayInput{
InternetGatewayId: aws.String(v.InternetGatewayId),
VpcId: aws.String(v.ID),
}); err != nil {
return err
}
if _, err = v.Client.DeleteInternetGateway(context.TODO(),
&ec2.DeleteInternetGatewayInput{
InternetGatewayId: aws.String(v.InternetGatewayId),
}); err != nil {
return err
}
}
// Then finally the VPC itself
if _, err := v.Client.DeleteVpc(context.TODO(),
&ec2.DeleteVpcInput{
VpcId: aws.String(v.ID),
}); err != nil {
return err
}
return nil
}
// createBucket Creates the S3 bucket
func (b *S3Bucket) createBucket() error {
// No harm creating a bucket that already exist.
_, err := b.Client.CreateBucket(context.TODO(), &s3.CreateBucketInput{
Bucket: &b.Name,
})
if err != nil {
return err
}
// Set the bucket policy
policy := fmt.Sprintf(`{
"Version": "2012-10-17",
"Statement": [{
"Sid": "AllowVMIE",
"Effect": "Allow",
"Principal": { "Service": "vmie.amazonaws.com" },
"Action": ["s3:GetBucketLocation", "s3:GetObject", "s3:ListBucket" ],
"Resource": ["arn:aws:s3:::%s", "arn:aws:s3:::%s/*"]}]
}`, b.Name, b.Name)
if _, err = b.Client.PutBucketPolicy(context.TODO(), &s3.PutBucketPolicyInput{
Bucket: &b.Name,
Policy: &policy,
}); err != nil {
return err
}
return nil
}
// createVmimportServiceRole Creates the vmimport service role as required to use the VM snaphot import feature.
//
// For further details see https://docs.aws.amazon.com/vm-import/latest/userguide/required-permissions.html
func createVmimportServiceRole(ctx context.Context, client *iam.Client, bucketName string) error {
const roleName = "vmimport"
_, err := client.GetRole(context.TODO(), &iam.GetRoleInput{
RoleName: aws.String(roleName),
})
if err == nil {
// The role exists, do nothing
return nil
}
// Create the service role
trustPolicy := `{
"Version":"2012-10-17",
"Statement":[
{
"Effect":"Allow",
"Principal":{ "Service":"vmie.amazonaws.com" },
"Action": "sts:AssumeRole",
"Condition":{"StringEquals":{"sts:Externalid":"vmimport"}}
}
]
}`
if _, err = client.CreateRole(context.TODO(), &iam.CreateRoleInput{
AssumeRolePolicyDocument: aws.String(trustPolicy),
RoleName: aws.String(roleName),
}); err != nil {
return err
}
// Set the role policy
rolePolicy := fmt.Sprintf(`{
"Version":"2012-10-17",
"Statement":[
{
"Effect":"Allow",
"Action":["s3:GetBucketLocation","s3:GetObject","s3:ListBucket"],
"Resource":["arn:aws:s3:::%s","arn:aws:s3:::%s/*"]
},
{
"Effect":"Allow",
"Action":["ec2:ModifySnapshotAttribute","ec2:CopySnapshot","ec2:RegisterImage","ec2:Describe*"],
"Resource":"*"
}
]
}`, bucketName, bucketName)
if _, err = client.PutRolePolicy(context.TODO(), &iam.PutRolePolicyInput{
PolicyDocument: aws.String(rolePolicy),
PolicyName: aws.String("vmimport"),
RoleName: aws.String(roleName),
}); err != nil {
return err
}
return nil
}
func NewAMIImage(client *ec2.Client, properties map[string]string) *AMIImage {
return &AMIImage{
Client: client,
Description: "Peer Pod VM image",
DiskDescription: "Peer Pod VM disk",
DiskFormat: "RAW",
EBSSnapshotId: "", // To be defined when the snapshot is created
ID: properties["podvm_aws_ami_id"],
RootDeviceName: "/dev/xvda",
}
}
// importEBSSnapshot Imports the disk image into the EBS
func (i *AMIImage) importEBSSnapshot(bucket *S3Bucket) error {
// Create the import snapshot task
importSnapshotOutput, err := i.Client.ImportSnapshot(context.TODO(), &ec2.ImportSnapshotInput{
Description: aws.String("Peer Pod VM disk snapshot"),
DiskContainer: &ec2types.SnapshotDiskContainer{
Description: aws.String(i.DiskDescription),
Format: aws.String(i.DiskFormat),
UserBucket: &ec2types.UserBucket{
S3Bucket: aws.String(bucket.Name),
S3Key: aws.String(bucket.Key),
},
},
})
if err != nil {
return err
}
//taskId := *importSnapshotOutput.ImportTaskId
describeTasksInput := &ec2.DescribeImportSnapshotTasksInput{
ImportTaskIds: []string{*importSnapshotOutput.ImportTaskId},
}
// Wait the import task to finish
waiter := ec2.NewSnapshotImportedWaiter(i.Client)
if err = waiter.Wait(context.TODO(), describeTasksInput, time.Minute*3); err != nil {
return err
}
// Finally get the snapshot ID
describeTasks, err := i.Client.DescribeImportSnapshotTasks(context.TODO(), describeTasksInput)
if err != nil {
return err
}
taskDetail := describeTasks.ImportSnapshotTasks[0].SnapshotTaskDetail
i.EBSSnapshotId = *taskDetail.SnapshotId
return nil
}
// registerImage Registers an AMI image
func (i *AMIImage) registerImage(imageName string) error {
if i.EBSSnapshotId == "" {
return fmt.Errorf("EBS Snapshot ID not found\n")
}
result, err := i.Client.RegisterImage(context.TODO(), &ec2.RegisterImageInput{
Name: aws.String(imageName),
Architecture: ec2types.ArchitectureValuesX8664,
BlockDeviceMappings: []ec2types.BlockDeviceMapping{{
DeviceName: aws.String(i.RootDeviceName),
Ebs: &ec2types.EbsBlockDevice{
DeleteOnTermination: aws.Bool(true),
SnapshotId: aws.String(i.EBSSnapshotId),
},
}},
Description: aws.String(i.Description),
EnaSupport: aws.Bool(true),
RootDeviceName: aws.String(i.RootDeviceName),
VirtualizationType: aws.String("hvm"),
})
if err != nil {
return err
}
// Save the AMI ID
i.ID = *result.ImageId
return nil
}
// uploadLargeFileWithCli Uploads large files (>5GB) using the AWS CLI
func (b *S3Bucket) uploadLargeFileWithCli(filepath string) error {
file, err := os.Open(filepath)
if err != nil {
return err
}
defer file.Close()
stat, err := file.Stat()
if err != nil {
return err
}
key := stat.Name()
defer func() {
if err == nil {
b.Key = key
}
}()
s3uri := "s3://" + b.Name + "/" + key
// TODO: region!
cmd := exec.Command("aws", "s3", "cp", filepath, s3uri)
out, err := cmd.CombinedOutput()
fmt.Printf("%s\n", out)
if err != nil {
return err
}
return nil
}
// ConvertQcow2ToRaw Converts an qcow2 image to raw. Requires `qemu-img` installed.
func ConvertQcow2ToRaw(qcow2 string, raw string) error {
cmd := exec.Command("qemu-img", "convert", "-O", "raw", qcow2, raw)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
return err
}
return nil
}
func NewAwsInstallOverlay(installDir, provider string) (pv.InstallOverlay, error) {
overlay, err := pv.NewKustomizeOverlay(filepath.Join(installDir, "overlays", provider))
if err != nil {
return nil, err
}
return &AwsInstallOverlay{
Overlay: overlay,
}, nil
}
func (a *AwsInstallOverlay) Apply(ctx context.Context, cfg *envconf.Config) error {
return a.Overlay.Apply(ctx, cfg)
}
func (a *AwsInstallOverlay) Delete(ctx context.Context, cfg *envconf.Config) error {
return a.Overlay.Delete(ctx, cfg)
}
func (a *AwsInstallOverlay) Edit(ctx context.Context, cfg *envconf.Config, properties map[string]string) error {
var err error
// Mapping the internal properties to ConfigMapGenerator properties.
mapProps := map[string]string{
"pause_image": "PAUSE_IMAGE",
"podvm_launchtemplate": "PODVM_LAUNCHTEMPLATE_NAME",
"podvm_ami": "PODVM_AMI_ID",
"podvm_instance_type": "PODVM_INSTANCE_TYPE",
"sg_ids": "AWS_SG_IDS",
"subnet_id": "AWS_SUBNET_ID",
"ssh_kp_name": "SSH_KP_NAME",
"region": "AWS_REGION",
"vxlan_port": "VXLAN_PORT",
}
for k, v := range mapProps {
if properties[k] != "" {
if err = a.Overlay.SetKustomizeConfigMapGeneratorLiteral("peer-pods-cm",
v, properties[k]); err != nil {
return err
}
}
}
// Mapping the internal properties to SecretGenerator properties.