@@ -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"
@@ -41,6 +40,13 @@ import (
4140 "sigs.k8s.io/controller-runtime/pkg/client"
4241)
4342
43+ // Constants for GCP OnHostMaintenance values.
44+ // These are not exported, because they are not _our_ API, but they are used in multiple places.
45+ const (
46+ onHostMaintenanceTerminate = "TERMINATE"
47+ onHostMaintenanceMigrate = "MIGRATE"
48+ )
49+
4450// MachineScopeParams defines the input parameters used to create a new MachineScope.
4551type MachineScopeParams struct {
4652 Client client.Client
@@ -320,12 +326,12 @@ func instanceAdditionalDiskSpec(ctx context.Context, spec []infrav1.AttachedDisk
320326}
321327
322328// InstanceNetworkInterfaceSpec returns compute network interface spec.
323- func ( m * MachineScope ) InstanceNetworkInterfaceSpec ( ) * compute.NetworkInterface {
329+ func InstanceNetworkInterfaceSpec ( cluster cloud. ClusterGetter , publicIP * bool , subnet * string ) * compute.NetworkInterface {
324330 networkInterface := & compute.NetworkInterface {
325- Network : path .Join ("projects" , m . ClusterGetter . NetworkProject (), "global" , "networks" , m . ClusterGetter .NetworkName ()),
331+ Network : path .Join ("projects" , cluster . NetworkProject (), "global" , "networks" , cluster .NetworkName ()),
326332 }
327333
328- if m . GCPMachine . Spec . PublicIP != nil && * m . GCPMachine . Spec . PublicIP {
334+ if publicIP != nil && * publicIP {
329335 networkInterface .AccessConfigs = []* compute.AccessConfig {
330336 {
331337 Type : "ONE_TO_ONE_NAT" ,
@@ -334,8 +340,8 @@ func (m *MachineScope) InstanceNetworkInterfaceSpec() *compute.NetworkInterface
334340 }
335341 }
336342
337- if m . GCPMachine . Spec . Subnet != nil {
338- 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 )
339345 }
340346
341347 networkInterface .AliasIpRanges = m .InstanceNetworkInterfaceAliasIPRangesSpec ()
@@ -377,9 +383,9 @@ func instanceServiceAccountsSpec(serviceAccount *infrav1.ServiceAccount) *comput
377383}
378384
379385// InstanceAdditionalMetadataSpec returns additional metadata spec.
380- func ( m * MachineScope ) InstanceAdditionalMetadataSpec () * compute.Metadata {
386+ func InstanceAdditionalMetadataSpec (spec []infrav1. MetadataItem ) * compute.Metadata {
381387 metadata := new (compute.Metadata )
382- for _ , additionalMetadata := range m . GCPMachine . Spec . AdditionalMetadata {
388+ for _ , additionalMetadata := range spec {
383389 metadata .Items = append (metadata .Items , & compute.MetadataItems {
384390 Key : additionalMetadata .Key ,
385391 Value : additionalMetadata .Value ,
@@ -469,9 +475,9 @@ func (m *MachineScope) InstanceSpec(log logr.Logger) *compute.Instance {
469475 if m .GCPMachine .Spec .OnHostMaintenance != nil {
470476 switch * m .GCPMachine .Spec .OnHostMaintenance {
471477 case infrav1 .HostMaintenancePolicyMigrate :
472- instance .Scheduling .OnHostMaintenance = "MIGRATE"
478+ instance .Scheduling .OnHostMaintenance = onHostMaintenanceMigrate
473479 case infrav1 .HostMaintenancePolicyTerminate :
474- instance .Scheduling .OnHostMaintenance = "TERMINATE"
480+ instance .Scheduling .OnHostMaintenance = onHostMaintenanceTerminate
475481 default :
476482 log .Error (errors .New ("Invalid value" ), "Unknown OnHostMaintenance value" , "Spec.OnHostMaintenance" , * m .GCPMachine .Spec .OnHostMaintenance )
477483 }
@@ -496,12 +502,13 @@ func (m *MachineScope) InstanceSpec(log logr.Logger) *compute.Instance {
496502
497503 instance .Disks = append (instance .Disks , m .InstanceImageSpec ())
498504 instance .Disks = append (instance .Disks , instanceAdditionalDiskSpec (ctx , m .GCPMachine .Spec .AdditionalDisks , m .GCPMachine .Spec .RootDiskEncryptionKey , m .Zone (), m .ResourceManagerTags ())... )
499- instance .Metadata = m .InstanceAdditionalMetadataSpec ()
505+
506+ instance .Metadata = InstanceAdditionalMetadataSpec (m .GCPMachine .Spec .AdditionalMetadata )
500507 instance .ServiceAccounts = append (instance .ServiceAccounts , instanceServiceAccountsSpec (m .GCPMachine .Spec .ServiceAccount ))
501- instance .NetworkInterfaces = append (instance .NetworkInterfaces , m . InstanceNetworkInterfaceSpec ())
508+ instance .NetworkInterfaces = append (instance .NetworkInterfaces , InstanceNetworkInterfaceSpec (m . ClusterGetter , m . GCPMachine . Spec . PublicIP , m . GCPMachine . Spec . Subnet ))
502509 instance .GuestAccelerators = instanceGuestAcceleratorsSpec (m .GCPMachine .Spec .GuestAccelerators )
503510 if len (instance .GuestAccelerators ) > 0 {
504- instance .Scheduling .OnHostMaintenance = "TERMINATE"
511+ instance .Scheduling .OnHostMaintenance = onHostMaintenanceTerminate
505512 }
506513
507514 return instance
@@ -510,15 +517,20 @@ func (m *MachineScope) InstanceSpec(log logr.Logger) *compute.Instance {
510517// ANCHOR_END: MachineInstanceSpec
511518
512519// GetBootstrapData returns the bootstrap data from the secret in the Machine's bootstrap.dataSecretName.
513- func (m * MachineScope ) GetBootstrapData () (string , error ) {
514- if m .Machine .Spec .Bootstrap .DataSecretName == nil {
520+ func (m * MachineScope ) GetBootstrapData (ctx context.Context ) (string , error ) {
521+ return GetBootstrapData (ctx , m .client , m .Machine , m .Machine .Spec .Bootstrap )
522+ }
523+
524+ // GetBootstrapData returns the bootstrap data from the secret in the Machine's bootstrap.dataSecretName.
525+ func GetBootstrapData (ctx context.Context , client client.Client , parent client.Object , bootstrap clusterv1.Bootstrap ) (string , error ) {
526+ if bootstrap .DataSecretName == nil {
515527 return "" , errors .New ("error retrieving bootstrap data: linked Machine's bootstrap.dataSecretName is nil" )
516528 }
517529
518530 secret := & corev1.Secret {}
519- key := types.NamespacedName {Namespace : m . Namespace (), Name : * m . Machine . Spec . Bootstrap .DataSecretName }
520- if err := m . client .Get (context . TODO () , key , secret ); err != nil {
521- return "" , errors .Wrapf (err , "failed to retrieve bootstrap data secret for GCPMachine %s/%s" , m .Namespace (), m .Name () )
531+ key := types.NamespacedName {Namespace : parent . GetNamespace (), Name : * bootstrap .DataSecretName }
532+ if err := client .Get (ctx , key , secret ); err != nil {
533+ return "" , errors .Wrapf (err , "failed to retrieve bootstrap data secret %s/%s" , key .Namespace , key .Name )
522534 }
523535
524536 value , ok := secret .Data ["value" ]
0 commit comments