@@ -25,7 +25,6 @@ import (
2525 "strings"
2626
2727 "github.com/go-logr/logr"
28-
2928 "github.com/pkg/errors"
3029 "golang.org/x/mod/semver"
3130 "google.golang.org/api/compute/v1"
@@ -327,12 +326,12 @@ func instanceAdditionalDiskSpec(ctx context.Context, spec []infrav1.AttachedDisk
327326}
328327
329328// InstanceNetworkInterfaceSpec returns compute network interface spec.
330- func ( m * MachineScope ) InstanceNetworkInterfaceSpec ( ) * compute.NetworkInterface {
329+ func InstanceNetworkInterfaceSpec ( cluster cloud. ClusterGetter , publicIP * bool , subnet * string ) * compute.NetworkInterface {
331330 networkInterface := & compute.NetworkInterface {
332- Network : path .Join ("projects" , m . ClusterGetter . NetworkProject (), "global" , "networks" , m . ClusterGetter .NetworkName ()),
331+ Network : path .Join ("projects" , cluster . NetworkProject (), "global" , "networks" , cluster .NetworkName ()),
333332 }
334333
335- if m . GCPMachine . Spec . PublicIP != nil && * m . GCPMachine . Spec . PublicIP {
334+ if publicIP != nil && * publicIP {
336335 networkInterface .AccessConfigs = []* compute.AccessConfig {
337336 {
338337 Type : "ONE_TO_ONE_NAT" ,
@@ -341,34 +340,34 @@ func (m *MachineScope) InstanceNetworkInterfaceSpec() *compute.NetworkInterface
341340 }
342341 }
343342
344- if m . GCPMachine . Spec . Subnet != nil {
345- networkInterface .Subnetwork = path .Join ("projects" , m . ClusterGetter . NetworkProject (), "regions" , m . ClusterGetter . Region (), "subnetworks" , * m . GCPMachine . Spec . Subnet )
343+ if subnet != nil {
344+ networkInterface .Subnetwork = path .Join ("projects" , cluster . NetworkProject (), "regions" , cluster . Region (), "subnetworks" , * subnet )
346345 }
347346
348347 return networkInterface
349348}
350349
351350// InstanceServiceAccountsSpec returns service-account spec.
352- func ( m * MachineScope ) InstanceServiceAccountsSpec ( ) * compute.ServiceAccount {
351+ func InstanceServiceAccountsSpec ( spec * infrav1. ServiceAccount ) * compute.ServiceAccount {
353352 serviceAccount := & compute.ServiceAccount {
354353 Email : "default" ,
355354 Scopes : []string {
356355 compute .CloudPlatformScope ,
357356 },
358357 }
359358
360- if m . GCPMachine . Spec . ServiceAccount != nil {
361- serviceAccount .Email = m . GCPMachine . Spec . ServiceAccount .Email
362- serviceAccount .Scopes = m . GCPMachine . Spec . ServiceAccount .Scopes
359+ if spec != nil {
360+ serviceAccount .Email = spec .Email
361+ serviceAccount .Scopes = spec .Scopes
363362 }
364363
365364 return serviceAccount
366365}
367366
368367// InstanceAdditionalMetadataSpec returns additional metadata spec.
369- func ( m * MachineScope ) InstanceAdditionalMetadataSpec () * compute.Metadata {
368+ func InstanceAdditionalMetadataSpec (spec []infrav1. MetadataItem ) * compute.Metadata {
370369 metadata := new (compute.Metadata )
371- for _ , additionalMetadata := range m . GCPMachine . Spec . AdditionalMetadata {
370+ for _ , additionalMetadata := range spec {
372371 metadata .Items = append (metadata .Items , & compute.MetadataItems {
373372 Key : additionalMetadata .Key ,
374373 Value : additionalMetadata .Value ,
@@ -379,12 +378,12 @@ func (m *MachineScope) InstanceAdditionalMetadataSpec() *compute.Metadata {
379378}
380379
381380// InstanceGuestAcceleratorsSpec returns a slice of Guest Accelerator Config specs.
382- func ( m * MachineScope ) InstanceGuestAcceleratorsSpec () []* compute.AcceleratorConfig {
383- if len (m . GCPMachine . Spec . GuestAccelerators ) == 0 {
381+ func InstanceGuestAcceleratorsSpec (guestAccelerators []infrav1. GuestAccelerator ) []* compute.AcceleratorConfig {
382+ if len (guestAccelerators ) == 0 {
384383 return nil
385384 }
386- accelConfigs := make ([]* compute.AcceleratorConfig , 0 , len (m . GCPMachine . Spec . GuestAccelerators ))
387- for _ , accel := range m . GCPMachine . Spec . GuestAccelerators {
385+ accelConfigs := make ([]* compute.AcceleratorConfig , 0 , len (guestAccelerators ))
386+ for _ , accel := range guestAccelerators {
388387 accelConfig := & compute.AcceleratorConfig {
389388 AcceleratorType : accel .Type ,
390389 AcceleratorCount : accel .Count ,
@@ -485,10 +484,11 @@ func (m *MachineScope) InstanceSpec(log logr.Logger) *compute.Instance {
485484
486485 instance .Disks = append (instance .Disks , m .InstanceImageSpec ())
487486 instance .Disks = append (instance .Disks , instanceAdditionalDiskSpec (ctx , m .GCPMachine .Spec .AdditionalDisks , m .GCPMachine .Spec .RootDiskEncryptionKey , m .Zone (), m .ResourceManagerTags ())... )
488- instance .Metadata = m .InstanceAdditionalMetadataSpec ()
489- instance .ServiceAccounts = append (instance .ServiceAccounts , m .InstanceServiceAccountsSpec ())
490- instance .NetworkInterfaces = append (instance .NetworkInterfaces , m .InstanceNetworkInterfaceSpec ())
491- instance .GuestAccelerators = m .InstanceGuestAcceleratorsSpec ()
487+
488+ instance .Metadata = InstanceAdditionalMetadataSpec (m .GCPMachine .Spec .AdditionalMetadata )
489+ instance .ServiceAccounts = append (instance .ServiceAccounts , InstanceServiceAccountsSpec (m .GCPMachine .Spec .ServiceAccount ))
490+ instance .NetworkInterfaces = append (instance .NetworkInterfaces , InstanceNetworkInterfaceSpec (m .ClusterGetter , m .GCPMachine .Spec .PublicIP , m .GCPMachine .Spec .Subnet ))
491+ instance .GuestAccelerators = InstanceGuestAcceleratorsSpec (m .GCPMachine .Spec .GuestAccelerators )
492492 if len (instance .GuestAccelerators ) > 0 {
493493 instance .Scheduling .OnHostMaintenance = "TERMINATE"
494494 }
@@ -499,15 +499,20 @@ func (m *MachineScope) InstanceSpec(log logr.Logger) *compute.Instance {
499499// ANCHOR_END: MachineInstanceSpec
500500
501501// GetBootstrapData returns the bootstrap data from the secret in the Machine's bootstrap.dataSecretName.
502- func (m * MachineScope ) GetBootstrapData () (string , error ) {
503- if m .Machine .Spec .Bootstrap .DataSecretName == nil {
502+ func (m * MachineScope ) GetBootstrapData (ctx context.Context ) (string , error ) {
503+ return GetBootstrapData (ctx , m .client , m .Machine , m .Machine .Spec .Bootstrap )
504+ }
505+
506+ // GetBootstrapData returns the bootstrap data from the secret in the Machine's bootstrap.dataSecretName.
507+ func GetBootstrapData (ctx context.Context , client client.Client , parent client.Object , bootstrap clusterv1.Bootstrap ) (string , error ) {
508+ if bootstrap .DataSecretName == nil {
504509 return "" , errors .New ("error retrieving bootstrap data: linked Machine's bootstrap.dataSecretName is nil" )
505510 }
506511
507512 secret := & corev1.Secret {}
508- key := types.NamespacedName {Namespace : m . Namespace (), Name : * m . Machine . Spec . Bootstrap .DataSecretName }
509- if err := m . client .Get (context . TODO () , key , secret ); err != nil {
510- return "" , errors .Wrapf (err , "failed to retrieve bootstrap data secret for GCPMachine %s/%s" , m .Namespace (), m .Name () )
513+ key := types.NamespacedName {Namespace : parent . GetNamespace (), Name : * bootstrap .DataSecretName }
514+ if err := client .Get (ctx , key , secret ); err != nil {
515+ return "" , errors .Wrapf (err , "failed to retrieve bootstrap data secret %s/%s" , key .Namespace , key .Name )
511516 }
512517
513518 value , ok := secret .Data ["value" ]
0 commit comments