@@ -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"
@@ -322,12 +321,12 @@ func (m *MachineScope) InstanceAdditionalDiskSpec() []*compute.AttachedDisk {
322321}
323322
324323// InstanceNetworkInterfaceSpec returns compute network interface spec.
325- func ( m * MachineScope ) InstanceNetworkInterfaceSpec ( ) * compute.NetworkInterface {
324+ func InstanceNetworkInterfaceSpec ( cluster cloud. ClusterGetter , publicIP * bool , subnet * string ) * compute.NetworkInterface {
326325 networkInterface := & compute.NetworkInterface {
327- Network : path .Join ("projects" , m . ClusterGetter . NetworkProject (), "global" , "networks" , m . ClusterGetter .NetworkName ()),
326+ Network : path .Join ("projects" , cluster . NetworkProject (), "global" , "networks" , cluster .NetworkName ()),
328327 }
329328
330- if m . GCPMachine . Spec . PublicIP != nil && * m . GCPMachine . Spec . PublicIP {
329+ if publicIP != nil && * publicIP {
331330 networkInterface .AccessConfigs = []* compute.AccessConfig {
332331 {
333332 Type : "ONE_TO_ONE_NAT" ,
@@ -336,34 +335,34 @@ func (m *MachineScope) InstanceNetworkInterfaceSpec() *compute.NetworkInterface
336335 }
337336 }
338337
339- if m . GCPMachine . Spec . Subnet != nil {
340- networkInterface .Subnetwork = path .Join ("projects" , m . ClusterGetter . NetworkProject (), "regions" , m . ClusterGetter . Region (), "subnetworks" , * m . GCPMachine . Spec . Subnet )
338+ if subnet != nil {
339+ networkInterface .Subnetwork = path .Join ("projects" , cluster . NetworkProject (), "regions" , cluster . Region (), "subnetworks" , * subnet )
341340 }
342341
343342 return networkInterface
344343}
345344
346345// InstanceServiceAccountsSpec returns service-account spec.
347- func ( m * MachineScope ) InstanceServiceAccountsSpec ( ) * compute.ServiceAccount {
346+ func InstanceServiceAccountsSpec ( spec * infrav1. ServiceAccount ) * compute.ServiceAccount {
348347 serviceAccount := & compute.ServiceAccount {
349348 Email : "default" ,
350349 Scopes : []string {
351350 compute .CloudPlatformScope ,
352351 },
353352 }
354353
355- if m . GCPMachine . Spec . ServiceAccount != nil {
356- serviceAccount .Email = m . GCPMachine . Spec . ServiceAccount .Email
357- serviceAccount .Scopes = m . GCPMachine . Spec . ServiceAccount .Scopes
354+ if spec != nil {
355+ serviceAccount .Email = spec .Email
356+ serviceAccount .Scopes = spec .Scopes
358357 }
359358
360359 return serviceAccount
361360}
362361
363362// InstanceAdditionalMetadataSpec returns additional metadata spec.
364- func ( m * MachineScope ) InstanceAdditionalMetadataSpec () * compute.Metadata {
363+ func InstanceAdditionalMetadataSpec (spec []infrav1. MetadataItem ) * compute.Metadata {
365364 metadata := new (compute.Metadata )
366- for _ , additionalMetadata := range m . GCPMachine . Spec . AdditionalMetadata {
365+ for _ , additionalMetadata := range spec {
367366 metadata .Items = append (metadata .Items , & compute.MetadataItems {
368367 Key : additionalMetadata .Key ,
369368 Value : additionalMetadata .Value ,
@@ -462,24 +461,29 @@ func (m *MachineScope) InstanceSpec(log logr.Logger) *compute.Instance {
462461
463462 instance .Disks = append (instance .Disks , m .InstanceImageSpec ())
464463 instance .Disks = append (instance .Disks , m .InstanceAdditionalDiskSpec ()... )
465- instance .Metadata = m . InstanceAdditionalMetadataSpec ()
466- instance .ServiceAccounts = append (instance .ServiceAccounts , m . InstanceServiceAccountsSpec ())
467- instance .NetworkInterfaces = append (instance .NetworkInterfaces , m . InstanceNetworkInterfaceSpec ())
464+ instance .Metadata = InstanceAdditionalMetadataSpec (m . GCPMachine . Spec . AdditionalMetadata )
465+ instance .ServiceAccounts = append (instance .ServiceAccounts , InstanceServiceAccountsSpec (m . GCPMachine . Spec . ServiceAccount ))
466+ instance .NetworkInterfaces = append (instance .NetworkInterfaces , InstanceNetworkInterfaceSpec (m . ClusterGetter , m . GCPMachine . Spec . PublicIP , m . GCPMachine . Spec . Subnet ))
468467 return instance
469468}
470469
471470// ANCHOR_END: MachineInstanceSpec
472471
473472// GetBootstrapData returns the bootstrap data from the secret in the Machine's bootstrap.dataSecretName.
474- func (m * MachineScope ) GetBootstrapData () (string , error ) {
475- if m .Machine .Spec .Bootstrap .DataSecretName == nil {
473+ func (m * MachineScope ) GetBootstrapData (ctx context.Context ) (string , error ) {
474+ return GetBootstrapData (ctx , m .client , m .Machine , m .Machine .Spec .Bootstrap )
475+ }
476+
477+ // GetBootstrapData returns the bootstrap data from the secret in the Machine's bootstrap.dataSecretName.
478+ func GetBootstrapData (ctx context.Context , client client.Client , parent client.Object , bootstrap clusterv1.Bootstrap ) (string , error ) {
479+ if bootstrap .DataSecretName == nil {
476480 return "" , errors .New ("error retrieving bootstrap data: linked Machine's bootstrap.dataSecretName is nil" )
477481 }
478482
479483 secret := & corev1.Secret {}
480- key := types.NamespacedName {Namespace : m . Namespace (), Name : * m . Machine . Spec . Bootstrap .DataSecretName }
481- if err := m . client .Get (context . TODO () , key , secret ); err != nil {
482- return "" , errors .Wrapf (err , "failed to retrieve bootstrap data secret for GCPMachine %s/%s" , m .Namespace (), m .Name () )
484+ key := types.NamespacedName {Namespace : parent . GetNamespace (), Name : * bootstrap .DataSecretName }
485+ if err := client .Get (ctx , key , secret ); err != nil {
486+ return "" , errors .Wrapf (err , "failed to retrieve bootstrap data secret %s/%s" , key .Namespace , key .Name )
483487 }
484488
485489 value , ok := secret .Data ["value" ]
0 commit comments