Skip to content

Commit f6f92ee

Browse files
committed
feat: implement proper context in scope methods
Signed-off-by: Carlos Salas <carlos.salas@suse.com>
1 parent 958d55b commit f6f92ee

13 files changed

Lines changed: 42 additions & 44 deletions

cloud/interfaces.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ type MachineGetter interface {
9090
ControlPlaneGroupName() string
9191
GetInstanceID() *string
9292
GetProviderID() string
93-
GetBootstrapData() (string, error)
93+
GetBootstrapData(ctx context.Context) (string, error)
9494
GetInstanceStatus() *infrav1.InstanceStatus
9595
}
9696

cloud/scope/cluster.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -421,11 +421,11 @@ func (s *ClusterScope) TargetTCPProxySpec() *compute.TargetTcpProxy {
421421
// ANCHOR_END: ClusterControlPlaneSpec
422422

423423
// PatchObject persists the cluster configuration and status.
424-
func (s *ClusterScope) PatchObject() error {
425-
return s.patchHelper.Patch(context.TODO(), s.GCPCluster)
424+
func (s *ClusterScope) PatchObject(ctx context.Context) error {
425+
return s.patchHelper.Patch(ctx, s.GCPCluster)
426426
}
427427

428428
// Close closes the current scope persisting the cluster configuration and status.
429-
func (s *ClusterScope) Close() error {
430-
return s.PatchObject()
429+
func (s *ClusterScope) Close(ctx context.Context) error {
430+
return s.PatchObject(ctx)
431431
}

cloud/scope/machine.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ func (m *MachineScope) SetAddresses(addressList []corev1.NodeAddress) {
223223
// ANCHOR: MachineInstanceSpec
224224

225225
// InstanceImageSpec returns compute instance image attched-disk spec.
226-
func (m *MachineScope) InstanceImageSpec() *compute.AttachedDisk {
226+
func (m *MachineScope) InstanceImageSpec(ctx context.Context) *compute.AttachedDisk {
227227
version := m.Machine.Spec.Version
228228
image := "capi-ubuntu-1804-k8s-" + strings.ReplaceAll(semver.MajorMinor(version), ".", "-")
229229
sourceImage := path.Join("projects", m.ClusterGetter.Project(), "global", "images", "family", image)
@@ -244,7 +244,7 @@ func (m *MachineScope) InstanceImageSpec() *compute.AttachedDisk {
244244
InitializeParams: &compute.AttachedDiskInitializeParams{
245245
DiskSizeGb: m.GCPMachine.Spec.RootDeviceSize,
246246
DiskType: path.Join("zones", m.Zone(), "diskTypes", string(diskType)),
247-
ResourceManagerTags: shared.ResourceTagConvert(context.TODO(), m.GCPMachine.Spec.ResourceManagerTags),
247+
ResourceManagerTags: shared.ResourceTagConvert(ctx, m.GCPMachine.Spec.ResourceManagerTags),
248248
SourceImage: sourceImage,
249249
Labels: m.ClusterGetter.AdditionalLabels().AddLabels(m.GCPMachine.Spec.AdditionalLabels),
250250
},
@@ -406,9 +406,7 @@ func instanceGuestAcceleratorsSpec(guestAccelerators []infrav1.Accelerator) []*c
406406
}
407407

408408
// InstanceSpec returns instance spec.
409-
func (m *MachineScope) InstanceSpec(log logr.Logger) *compute.Instance {
410-
ctx := context.TODO()
411-
409+
func (m *MachineScope) InstanceSpec(ctx context.Context, log logr.Logger) *compute.Instance {
412410
instance := &compute.Instance{
413411
Name: m.Name(),
414412
Zone: m.Zone(),
@@ -421,7 +419,7 @@ func (m *MachineScope) InstanceSpec(log logr.Logger) *compute.Instance {
421419
),
422420
},
423421
Params: &compute.InstanceParams{
424-
ResourceManagerTags: shared.ResourceTagConvert(context.TODO(), m.ResourceManagerTags()),
422+
ResourceManagerTags: shared.ResourceTagConvert(ctx, m.ResourceManagerTags()),
425423
},
426424
Labels: infrav1.Build(infrav1.BuildParams{
427425
ClusterName: m.ClusterGetter.Name(),
@@ -494,7 +492,7 @@ func (m *MachineScope) InstanceSpec(log logr.Logger) *compute.Instance {
494492
}
495493
}
496494

497-
instance.Disks = append(instance.Disks, m.InstanceImageSpec())
495+
instance.Disks = append(instance.Disks, m.InstanceImageSpec(ctx))
498496
instance.Disks = append(instance.Disks, instanceAdditionalDiskSpec(ctx, m.GCPMachine.Spec.AdditionalDisks, m.GCPMachine.Spec.RootDiskEncryptionKey, m.Zone(), m.ResourceManagerTags())...)
499497
instance.Metadata = m.InstanceAdditionalMetadataSpec()
500498
instance.ServiceAccounts = append(instance.ServiceAccounts, instanceServiceAccountsSpec(m.GCPMachine.Spec.ServiceAccount))
@@ -510,14 +508,14 @@ func (m *MachineScope) InstanceSpec(log logr.Logger) *compute.Instance {
510508
// ANCHOR_END: MachineInstanceSpec
511509

512510
// GetBootstrapData returns the bootstrap data from the secret in the Machine's bootstrap.dataSecretName.
513-
func (m *MachineScope) GetBootstrapData() (string, error) {
511+
func (m *MachineScope) GetBootstrapData(ctx context.Context) (string, error) {
514512
if m.Machine.Spec.Bootstrap.DataSecretName == nil {
515513
return "", errors.New("error retrieving bootstrap data: linked Machine's bootstrap.dataSecretName is nil")
516514
}
517515

518516
secret := &corev1.Secret{}
519517
key := types.NamespacedName{Namespace: m.Namespace(), Name: *m.Machine.Spec.Bootstrap.DataSecretName}
520-
if err := m.client.Get(context.TODO(), key, secret); err != nil {
518+
if err := m.client.Get(ctx, key, secret); err != nil {
521519
return "", errors.Wrapf(err, "failed to retrieve bootstrap data secret for GCPMachine %s/%s", m.Namespace(), m.Name())
522520
}
523521

@@ -530,13 +528,13 @@ func (m *MachineScope) GetBootstrapData() (string, error) {
530528
}
531529

532530
// PatchObject persists the cluster configuration and status.
533-
func (m *MachineScope) PatchObject() error {
534-
return m.patchHelper.Patch(context.TODO(), m.GCPMachine)
531+
func (m *MachineScope) PatchObject(ctx context.Context) error {
532+
return m.patchHelper.Patch(ctx, m.GCPMachine)
535533
}
536534

537535
// Close closes the current scope persisting the cluster configuration and status.
538-
func (m *MachineScope) Close() error {
539-
return m.PatchObject()
536+
func (m *MachineScope) Close(ctx context.Context) error {
537+
return m.PatchObject(ctx)
540538
}
541539

542540
// ResourceManagerTags merges ResourceManagerTags from the scope's GCPCluster and GCPMachine. If the same key is present in both,

cloud/scope/managedcluster.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,11 +327,11 @@ func (s *ManagedClusterScope) FirewallRulesSpec() []*compute.Firewall {
327327
// ANCHOR_END: ClusterFirewallSpec
328328

329329
// PatchObject persists the cluster configuration and status.
330-
func (s *ManagedClusterScope) PatchObject() error {
331-
return s.patchHelper.Patch(context.TODO(), s.GCPManagedCluster)
330+
func (s *ManagedClusterScope) PatchObject(ctx context.Context) error {
331+
return s.patchHelper.Patch(ctx, s.GCPManagedCluster)
332332
}
333333

334334
// Close closes the current scope persisting the cluster configuration and status.
335-
func (s *ManagedClusterScope) Close() error {
336-
return s.PatchObject()
335+
func (s *ManagedClusterScope) Close(ctx context.Context) error {
336+
return s.PatchObject(ctx)
337337
}

cloud/scope/managedcontrolplane.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ type ManagedControlPlaneScope struct {
128128
}
129129

130130
// PatchObject persists the managed control plane configuration and status.
131-
func (s *ManagedControlPlaneScope) PatchObject() error {
131+
func (s *ManagedControlPlaneScope) PatchObject(ctx context.Context) error {
132132
return s.patchHelper.Patch(
133-
context.TODO(),
133+
ctx,
134134
s.GCPManagedControlPlane,
135135
v1beta1patch.WithOwnedConditions{Conditions: []clusterv1beta1.ConditionType{
136136
infrav1exp.GKEControlPlaneReadyCondition,
@@ -141,11 +141,11 @@ func (s *ManagedControlPlaneScope) PatchObject() error {
141141
}
142142

143143
// Close closes the current scope persisting the managed control plane configuration and status.
144-
func (s *ManagedControlPlaneScope) Close() error {
144+
func (s *ManagedControlPlaneScope) Close(ctx context.Context) error {
145145
s.mcClient.Close()
146146
s.tagBindingsClient.Close()
147147
s.credentialsClient.Close()
148-
return s.PatchObject()
148+
return s.PatchObject(ctx)
149149
}
150150

151151
// ConditionSetter return a condition setter (which is GCPManagedControlPlane itself).

cloud/scope/managedmachinepool.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ type ManagedMachinePoolScope struct {
117117
}
118118

119119
// PatchObject persists the managed control plane configuration and status.
120-
func (s *ManagedMachinePoolScope) PatchObject() error {
120+
func (s *ManagedMachinePoolScope) PatchObject(ctx context.Context) error {
121121
return s.patchHelper.Patch(
122-
context.TODO(),
122+
ctx,
123123
s.GCPManagedMachinePool,
124124
v1beta1patch.WithOwnedConditions{Conditions: []clusterv1beta1.ConditionType{
125125
infrav1exp.GKEMachinePoolReadyCondition,
@@ -130,10 +130,10 @@ func (s *ManagedMachinePoolScope) PatchObject() error {
130130
}
131131

132132
// Close closes the current scope persisting the managed control plane configuration and status.
133-
func (s *ManagedMachinePoolScope) Close() error {
133+
func (s *ManagedMachinePoolScope) Close(ctx context.Context) error {
134134
s.mcClient.Close()
135135
s.migClient.Close()
136-
return s.PatchObject()
136+
return s.PatchObject(ctx)
137137
}
138138

139139
// ConditionSetter return a condition setter (which is GCPManagedMachinePool itself).

cloud/services/compute/instances/reconcile.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func (s *Service) Reconcile(ctx context.Context) error {
101101
func (s *Service) Delete(ctx context.Context) error {
102102
log := log.FromContext(ctx)
103103
log.Info("Deleting instance resources")
104-
instanceSpec := s.scope.InstanceSpec(log)
104+
instanceSpec := s.scope.InstanceSpec(ctx, log)
105105
instanceName := instanceSpec.Name
106106
instanceKey := meta.ZonalKey(instanceName, s.scope.Zone())
107107
log.V(2).Info("Looking for instance before deleting", "name", instanceName, "zone", s.scope.Zone())
@@ -128,13 +128,13 @@ func (s *Service) Delete(ctx context.Context) error {
128128
func (s *Service) createOrGetInstance(ctx context.Context) (*compute.Instance, error) {
129129
log := log.FromContext(ctx)
130130
log.V(2).Info("Getting bootstrap data for machine")
131-
bootstrapData, err := s.scope.GetBootstrapData()
131+
bootstrapData, err := s.scope.GetBootstrapData(ctx)
132132
if err != nil {
133133
log.Error(err, "Error getting bootstrap data for machine")
134134
return nil, errors.Wrap(err, "failed to retrieve bootstrap data")
135135
}
136136

137-
instanceSpec := s.scope.InstanceSpec(log)
137+
instanceSpec := s.scope.InstanceSpec(ctx, log)
138138
instanceName := instanceSpec.Name
139139
instanceKey := meta.ZonalKey(instanceName, s.scope.Zone())
140140
instanceSpec.Metadata.Items = append(instanceSpec.Metadata.Items, &compute.MetadataItems{

cloud/services/compute/instances/service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ type instancegroupsInterface interface {
4444
// Scope is an interfaces that hold used methods.
4545
type Scope interface {
4646
cloud.Machine
47-
InstanceSpec(log logr.Logger) *compute.Instance
47+
InstanceSpec(ctx context.Context, log logr.Logger) *compute.Instance
4848
}
4949

5050
// Service implements instances reconciler.

controllers/gcpcluster_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func (r *GCPClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
146146

147147
// Always close the scope when exiting this function so we can persist any GCPMachine changes.
148148
defer func() {
149-
if err := clusterScope.Close(); err != nil && reterr == nil {
149+
if err := clusterScope.Close(ctx); err != nil && reterr == nil {
150150
reterr = err
151151
}
152152
}()
@@ -165,7 +165,7 @@ func (r *GCPClusterReconciler) reconcile(ctx context.Context, clusterScope *scop
165165
log.Info("Reconciling GCPCluster")
166166

167167
controllerutil.AddFinalizer(clusterScope.GCPCluster, infrav1.ClusterFinalizer)
168-
if err := clusterScope.PatchObject(); err != nil {
168+
if err := clusterScope.PatchObject(ctx); err != nil {
169169
return ctrl.Result{}, err
170170
}
171171

controllers/gcpmachine_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ func (r *GCPMachineReconciler) Reconcile(ctx context.Context, req ctrl.Request)
201201

202202
// Always close the scope when exiting this function so we can persist any GCPMachine changes.
203203
defer func() {
204-
if err := machineScope.Close(); err != nil && reterr == nil {
204+
if err := machineScope.Close(ctx); err != nil && reterr == nil {
205205
reterr = err
206206
}
207207
}()
@@ -220,7 +220,7 @@ func (r *GCPMachineReconciler) reconcile(ctx context.Context, machineScope *scop
220220
log.Info("Reconciling GCPMachine")
221221

222222
controllerutil.AddFinalizer(machineScope.GCPMachine, infrav1.MachineFinalizer)
223-
if err := machineScope.PatchObject(); err != nil {
223+
if err := machineScope.PatchObject(ctx); err != nil {
224224
return ctrl.Result{}, err
225225
}
226226

0 commit comments

Comments
 (0)