@@ -25,7 +25,6 @@ import (
25
25
"strings"
26
26
27
27
"github.com/go-logr/logr"
28
-
29
28
"github.com/pkg/errors"
30
29
"golang.org/x/mod/semver"
31
30
"google.golang.org/api/compute/v1"
@@ -322,12 +321,12 @@ func (m *MachineScope) InstanceAdditionalDiskSpec() []*compute.AttachedDisk {
322
321
}
323
322
324
323
// 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 {
326
325
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 ()),
328
327
}
329
328
330
- if m . GCPMachine . Spec . PublicIP != nil && * m . GCPMachine . Spec . PublicIP {
329
+ if publicIP != nil && * publicIP {
331
330
networkInterface .AccessConfigs = []* compute.AccessConfig {
332
331
{
333
332
Type : "ONE_TO_ONE_NAT" ,
@@ -336,34 +335,34 @@ func (m *MachineScope) InstanceNetworkInterfaceSpec() *compute.NetworkInterface
336
335
}
337
336
}
338
337
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 )
341
340
}
342
341
343
342
return networkInterface
344
343
}
345
344
346
345
// InstanceServiceAccountsSpec returns service-account spec.
347
- func ( m * MachineScope ) InstanceServiceAccountsSpec ( ) * compute.ServiceAccount {
346
+ func InstanceServiceAccountsSpec ( spec * infrav1. ServiceAccount ) * compute.ServiceAccount {
348
347
serviceAccount := & compute.ServiceAccount {
349
348
Email : "default" ,
350
349
Scopes : []string {
351
350
compute .CloudPlatformScope ,
352
351
},
353
352
}
354
353
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
358
357
}
359
358
360
359
return serviceAccount
361
360
}
362
361
363
362
// InstanceAdditionalMetadataSpec returns additional metadata spec.
364
- func ( m * MachineScope ) InstanceAdditionalMetadataSpec () * compute.Metadata {
363
+ func InstanceAdditionalMetadataSpec (spec []infrav1. MetadataItem ) * compute.Metadata {
365
364
metadata := new (compute.Metadata )
366
- for _ , additionalMetadata := range m . GCPMachine . Spec . AdditionalMetadata {
365
+ for _ , additionalMetadata := range spec {
367
366
metadata .Items = append (metadata .Items , & compute.MetadataItems {
368
367
Key : additionalMetadata .Key ,
369
368
Value : additionalMetadata .Value ,
@@ -462,24 +461,28 @@ func (m *MachineScope) InstanceSpec(log logr.Logger) *compute.Instance {
462
461
463
462
instance .Disks = append (instance .Disks , m .InstanceImageSpec ())
464
463
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 ))
468
467
return instance
469
468
}
470
469
471
470
// ANCHOR_END: MachineInstanceSpec
472
471
472
+ func (m * MachineScope ) GetBootstrapData (ctx context.Context ) (string , error ) {
473
+ return GetBootstrapData (ctx , m .client , m .Machine , m .Machine .Spec .Bootstrap )
474
+ }
475
+
473
476
// 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 {
477
+ func GetBootstrapData ( ctx context. Context , client client. Client , parent client. Object , bootstrap clusterv1. Bootstrap ) (string , error ) {
478
+ if bootstrap .DataSecretName == nil {
476
479
return "" , errors .New ("error retrieving bootstrap data: linked Machine's bootstrap.dataSecretName is nil" )
477
480
}
478
481
479
482
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 () )
483
+ key := types.NamespacedName {Namespace : parent . GetNamespace (), Name : * bootstrap .DataSecretName }
484
+ if err := client .Get (ctx , key , secret ); err != nil {
485
+ return "" , errors .Wrapf (err , "failed to retrieve bootstrap data secret %s/%s" , key .Namespace , key .Name )
483
486
}
484
487
485
488
value , ok := secret .Data ["value" ]
0 commit comments