@@ -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 instanceAdditionalDiskSpec(ctx context.Context, spec []infrav1.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 ,
@@ -464,24 +463,29 @@ func (m *MachineScope) InstanceSpec(log logr.Logger) *compute.Instance {
464463
465464 instance .Disks = append (instance .Disks , m .InstanceImageSpec ())
466465 instance .Disks = append (instance .Disks , instanceAdditionalDiskSpec (ctx , m .GCPMachine .Spec .AdditionalDisks , m .GCPMachine .Spec .RootDiskEncryptionKey , m .Zone (), m .ResourceManagerTags ())... )
467- instance .Metadata = m . InstanceAdditionalMetadataSpec ()
468- instance .ServiceAccounts = append (instance .ServiceAccounts , m . InstanceServiceAccountsSpec ())
469- instance .NetworkInterfaces = append (instance .NetworkInterfaces , m . InstanceNetworkInterfaceSpec ())
466+ instance .Metadata = InstanceAdditionalMetadataSpec (m . GCPMachine . Spec . AdditionalMetadata )
467+ instance .ServiceAccounts = append (instance .ServiceAccounts , InstanceServiceAccountsSpec (m . GCPMachine . Spec . ServiceAccount ))
468+ instance .NetworkInterfaces = append (instance .NetworkInterfaces , InstanceNetworkInterfaceSpec (m . ClusterGetter , m . GCPMachine . Spec . PublicIP , m . GCPMachine . Spec . Subnet ))
470469 return instance
471470}
472471
473472// ANCHOR_END: MachineInstanceSpec
474473
475474// GetBootstrapData returns the bootstrap data from the secret in the Machine's bootstrap.dataSecretName.
476- func (m * MachineScope ) GetBootstrapData () (string , error ) {
477- if m .Machine .Spec .Bootstrap .DataSecretName == nil {
475+ func (m * MachineScope ) GetBootstrapData (ctx context.Context ) (string , error ) {
476+ return GetBootstrapData (ctx , m .client , m .Machine , m .Machine .Spec .Bootstrap )
477+ }
478+
479+ // GetBootstrapData returns the bootstrap data from the secret in the Machine's bootstrap.dataSecretName.
480+ func GetBootstrapData (ctx context.Context , client client.Client , parent client.Object , bootstrap clusterv1.Bootstrap ) (string , error ) {
481+ if bootstrap .DataSecretName == nil {
478482 return "" , errors .New ("error retrieving bootstrap data: linked Machine's bootstrap.dataSecretName is nil" )
479483 }
480484
481485 secret := & corev1.Secret {}
482- key := types.NamespacedName {Namespace : m . Namespace (), Name : * m . Machine . Spec . Bootstrap .DataSecretName }
483- if err := m . client .Get (context . TODO () , key , secret ); err != nil {
484- return "" , errors .Wrapf (err , "failed to retrieve bootstrap data secret for GCPMachine %s/%s" , m .Namespace (), m .Name () )
486+ key := types.NamespacedName {Namespace : parent . GetNamespace (), Name : * bootstrap .DataSecretName }
487+ if err := client .Get (ctx , key , secret ); err != nil {
488+ return "" , errors .Wrapf (err , "failed to retrieve bootstrap data secret %s/%s" , key .Namespace , key .Name )
485489 }
486490
487491 value , ok := secret .Data ["value" ]
0 commit comments