@@ -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,8 +340,8 @@ 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
@@ -366,9 +365,9 @@ func instanceServiceAccountsSpec(serviceAccount *infrav1.ServiceAccount) *comput
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 ,
@@ -485,9 +484,10 @@ 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 ()
487+
488+ instance .Metadata = InstanceAdditionalMetadataSpec (m .GCPMachine .Spec .AdditionalMetadata )
489489 instance .ServiceAccounts = append (instance .ServiceAccounts , instanceServiceAccountsSpec (m .GCPMachine .Spec .ServiceAccount ))
490- instance .NetworkInterfaces = append (instance .NetworkInterfaces , m . InstanceNetworkInterfaceSpec ())
490+ instance .NetworkInterfaces = append (instance .NetworkInterfaces , InstanceNetworkInterfaceSpec (m . ClusterGetter , m . GCPMachine . Spec . PublicIP , m . GCPMachine . Spec . Subnet ))
491491 instance .GuestAccelerators = instanceGuestAcceleratorsSpec (m .GCPMachine .Spec .GuestAccelerators )
492492 if len (instance .GuestAccelerators ) > 0 {
493493 instance .Scheduling .OnHostMaintenance = "TERMINATE"
@@ -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