@@ -460,12 +460,12 @@ func TestService_createOrGetInstance(t *testing.T) {
460
460
ResourceManagerTags : map [string ]string {},
461
461
},
462
462
SelfLink : "https://www.googleapis.com/compute/v1/projects/proj-id/zones/us-central1-c/instances/my-machine" ,
463
- Scheduling : & compute.Scheduling {
464
- OnHostMaintenance : strings .ToUpper (string (infrav1 .HostMaintenancePolicyTerminate )),
465
- },
466
463
ConfidentialInstanceConfig : & compute.ConfidentialInstanceConfig {
467
464
EnableConfidentialCompute : true ,
468
465
},
466
+ Scheduling : & compute.Scheduling {
467
+ OnHostMaintenance : strings .ToUpper (string (infrav1 .HostMaintenancePolicyTerminate )),
468
+ },
469
469
ServiceAccounts : []* compute.ServiceAccount {
470
470
{
471
471
Email : "default" ,
@@ -609,6 +609,152 @@ func TestService_createOrGetInstance(t *testing.T) {
609
609
Zone : "us-central1-a" ,
610
610
},
611
611
},
612
+ {
613
+ name : "instance does not exist (should create instance) with Customer-Managed boot DiskEncryption" ,
614
+ scope : func () Scope {
615
+ machineScope .GCPMachine = getFakeGCPMachine ()
616
+ diskEncryption := infrav1.CustomerEncryptionKey {
617
+ KeyType : infrav1 .CustomerManagedKey ,
618
+ ManagedKey : & infrav1.ManagedKey {
619
+ KmsKeyName : "projects/my-project/locations/us-central1/keyRings/us-central1/cryptoKeys/some-key" ,
620
+ },
621
+ }
622
+ machineScope .GCPMachine .Spec .RootDiskEncryptionKey = & diskEncryption
623
+ return machineScope
624
+ },
625
+ mockInstance : & cloud.MockInstances {
626
+ ProjectRouter : & cloud.SingleProjectRouter {ID : "proj-id" },
627
+ Objects : map [meta.Key ]* cloud.MockInstancesObj {},
628
+ },
629
+ want : & compute.Instance {
630
+ Name : "my-machine" ,
631
+ CanIpForward : true ,
632
+ Disks : []* compute.AttachedDisk {
633
+ {
634
+ AutoDelete : true ,
635
+ Boot : true ,
636
+ InitializeParams : & compute.AttachedDiskInitializeParams {
637
+ DiskType : "zones/us-central1-c/diskTypes/pd-standard" ,
638
+ SourceImage : "projects/my-proj/global/images/family/capi-ubuntu-1804-k8s-v1-19" ,
639
+ ResourceManagerTags : map [string ]string {},
640
+ },
641
+ DiskEncryptionKey : & compute.CustomerEncryptionKey {
642
+ KmsKeyName : "projects/my-project/locations/us-central1/keyRings/us-central1/cryptoKeys/some-key" ,
643
+ },
644
+ },
645
+ },
646
+ Labels : map [string ]string {
647
+ "capg-role" : "node" ,
648
+ "capg-cluster-my-cluster" : "owned" ,
649
+ "foo" : "bar" ,
650
+ },
651
+ MachineType : "zones/us-central1-c/machineTypes" ,
652
+ Metadata : & compute.Metadata {
653
+ Items : []* compute.MetadataItems {
654
+ {
655
+ Key : "user-data" ,
656
+ Value : ptr.To [string ]("Zm9vCg==" ),
657
+ },
658
+ },
659
+ },
660
+ NetworkInterfaces : []* compute.NetworkInterface {
661
+ {
662
+ Network : "projects/my-proj/global/networks/default" ,
663
+ },
664
+ },
665
+ Params : & compute.InstanceParams {
666
+ ResourceManagerTags : map [string ]string {},
667
+ },
668
+ SelfLink : "https://www.googleapis.com/compute/v1/projects/proj-id/zones/us-central1-c/instances/my-machine" ,
669
+ Scheduling : & compute.Scheduling {},
670
+ ServiceAccounts : []* compute.ServiceAccount {
671
+ {
672
+ Email : "default" ,
673
+ Scopes : []string {"https://www.googleapis.com/auth/cloud-platform" },
674
+ },
675
+ },
676
+ Tags : & compute.Tags {
677
+ Items : []string {
678
+ "my-cluster-node" ,
679
+ "my-cluster" ,
680
+ },
681
+ },
682
+ Zone : "us-central1-c" ,
683
+ },
684
+ },
685
+ {
686
+ name : "instance does not exist (should create instance) with Customer-Supplied boot DiskEncryption" ,
687
+ scope : func () Scope {
688
+ machineScope .GCPMachine = getFakeGCPMachine ()
689
+ diskEncryption := infrav1.CustomerEncryptionKey {
690
+ KeyType : infrav1 .CustomerSuppliedKey ,
691
+ SuppliedKey : & infrav1.SuppliedKey {
692
+ RawKey : "SGVsbG8gZnJvbSBHb29nbGUgQ2xvdWQgUGxhdGZvcm0=" ,
693
+ },
694
+ }
695
+ machineScope .GCPMachine .Spec .RootDiskEncryptionKey = & diskEncryption
696
+ return machineScope
697
+ },
698
+ mockInstance : & cloud.MockInstances {
699
+ ProjectRouter : & cloud.SingleProjectRouter {ID : "proj-id" },
700
+ Objects : map [meta.Key ]* cloud.MockInstancesObj {},
701
+ },
702
+ want : & compute.Instance {
703
+ Name : "my-machine" ,
704
+ CanIpForward : true ,
705
+ Disks : []* compute.AttachedDisk {
706
+ {
707
+ AutoDelete : true ,
708
+ Boot : true ,
709
+ InitializeParams : & compute.AttachedDiskInitializeParams {
710
+ DiskType : "zones/us-central1-c/diskTypes/pd-standard" ,
711
+ SourceImage : "projects/my-proj/global/images/family/capi-ubuntu-1804-k8s-v1-19" ,
712
+ ResourceManagerTags : map [string ]string {},
713
+ },
714
+ DiskEncryptionKey : & compute.CustomerEncryptionKey {
715
+ RawKey : "SGVsbG8gZnJvbSBHb29nbGUgQ2xvdWQgUGxhdGZvcm0=" ,
716
+ },
717
+ },
718
+ },
719
+ Labels : map [string ]string {
720
+ "capg-role" : "node" ,
721
+ "capg-cluster-my-cluster" : "owned" ,
722
+ "foo" : "bar" ,
723
+ },
724
+ MachineType : "zones/us-central1-c/machineTypes" ,
725
+ Metadata : & compute.Metadata {
726
+ Items : []* compute.MetadataItems {
727
+ {
728
+ Key : "user-data" ,
729
+ Value : ptr.To [string ]("Zm9vCg==" ),
730
+ },
731
+ },
732
+ },
733
+ NetworkInterfaces : []* compute.NetworkInterface {
734
+ {
735
+ Network : "projects/my-proj/global/networks/default" ,
736
+ },
737
+ },
738
+ Params : & compute.InstanceParams {
739
+ ResourceManagerTags : map [string ]string {},
740
+ },
741
+ SelfLink : "https://www.googleapis.com/compute/v1/projects/proj-id/zones/us-central1-c/instances/my-machine" ,
742
+ Scheduling : & compute.Scheduling {},
743
+ ServiceAccounts : []* compute.ServiceAccount {
744
+ {
745
+ Email : "default" ,
746
+ Scopes : []string {"https://www.googleapis.com/auth/cloud-platform" },
747
+ },
748
+ },
749
+ Tags : & compute.Tags {
750
+ Items : []string {
751
+ "my-cluster-node" ,
752
+ "my-cluster" ,
753
+ },
754
+ },
755
+ Zone : "us-central1-c" ,
756
+ },
757
+ },
612
758
}
613
759
for _ , tt := range tests {
614
760
t .Run (tt .name , func (t * testing.T ) {
0 commit comments