diff --git a/cloud/interfaces.go b/cloud/interfaces.go index 58aeda69d..349579777 100644 --- a/cloud/interfaces.go +++ b/cloud/interfaces.go @@ -90,7 +90,7 @@ type MachineGetter interface { ControlPlaneGroupName() string GetInstanceID() *string GetProviderID() string - GetBootstrapData() (string, error) + GetBootstrapData(ctx context.Context) (string, error) GetInstanceStatus() *infrav1.InstanceStatus } diff --git a/cloud/scope/cluster.go b/cloud/scope/cluster.go index 59b7c291f..cdbdbe680 100644 --- a/cloud/scope/cluster.go +++ b/cloud/scope/cluster.go @@ -421,11 +421,11 @@ func (s *ClusterScope) TargetTCPProxySpec() *compute.TargetTcpProxy { // ANCHOR_END: ClusterControlPlaneSpec // PatchObject persists the cluster configuration and status. -func (s *ClusterScope) PatchObject() error { - return s.patchHelper.Patch(context.TODO(), s.GCPCluster) +func (s *ClusterScope) PatchObject(ctx context.Context) error { + return s.patchHelper.Patch(ctx, s.GCPCluster) } // Close closes the current scope persisting the cluster configuration and status. -func (s *ClusterScope) Close() error { - return s.PatchObject() +func (s *ClusterScope) Close(ctx context.Context) error { + return s.PatchObject(ctx) } diff --git a/cloud/scope/machine.go b/cloud/scope/machine.go index 9c29d0ae1..e6ab99d7d 100644 --- a/cloud/scope/machine.go +++ b/cloud/scope/machine.go @@ -223,7 +223,7 @@ func (m *MachineScope) SetAddresses(addressList []corev1.NodeAddress) { // ANCHOR: MachineInstanceSpec // InstanceImageSpec returns compute instance image attched-disk spec. -func (m *MachineScope) InstanceImageSpec() *compute.AttachedDisk { +func (m *MachineScope) InstanceImageSpec(ctx context.Context) *compute.AttachedDisk { version := m.Machine.Spec.Version image := "capi-ubuntu-1804-k8s-" + strings.ReplaceAll(semver.MajorMinor(version), ".", "-") sourceImage := path.Join("projects", m.ClusterGetter.Project(), "global", "images", "family", image) @@ -244,7 +244,7 @@ func (m *MachineScope) InstanceImageSpec() *compute.AttachedDisk { InitializeParams: &compute.AttachedDiskInitializeParams{ DiskSizeGb: m.GCPMachine.Spec.RootDeviceSize, DiskType: path.Join("zones", m.Zone(), "diskTypes", string(diskType)), - ResourceManagerTags: shared.ResourceTagConvert(context.TODO(), m.GCPMachine.Spec.ResourceManagerTags), + ResourceManagerTags: shared.ResourceTagConvert(ctx, m.GCPMachine.Spec.ResourceManagerTags), SourceImage: sourceImage, Labels: m.ClusterGetter.AdditionalLabels().AddLabels(m.GCPMachine.Spec.AdditionalLabels), }, @@ -406,9 +406,7 @@ func instanceGuestAcceleratorsSpec(guestAccelerators []infrav1.Accelerator) []*c } // InstanceSpec returns instance spec. -func (m *MachineScope) InstanceSpec(log logr.Logger) *compute.Instance { - ctx := context.TODO() - +func (m *MachineScope) InstanceSpec(ctx context.Context, log logr.Logger) *compute.Instance { instance := &compute.Instance{ Name: m.Name(), Zone: m.Zone(), @@ -421,7 +419,7 @@ func (m *MachineScope) InstanceSpec(log logr.Logger) *compute.Instance { ), }, Params: &compute.InstanceParams{ - ResourceManagerTags: shared.ResourceTagConvert(context.TODO(), m.ResourceManagerTags()), + ResourceManagerTags: shared.ResourceTagConvert(ctx, m.ResourceManagerTags()), }, Labels: infrav1.Build(infrav1.BuildParams{ ClusterName: m.ClusterGetter.Name(), @@ -494,7 +492,7 @@ func (m *MachineScope) InstanceSpec(log logr.Logger) *compute.Instance { } } - instance.Disks = append(instance.Disks, m.InstanceImageSpec()) + instance.Disks = append(instance.Disks, m.InstanceImageSpec(ctx)) instance.Disks = append(instance.Disks, instanceAdditionalDiskSpec(ctx, m.GCPMachine.Spec.AdditionalDisks, m.GCPMachine.Spec.RootDiskEncryptionKey, m.Zone(), m.ResourceManagerTags())...) instance.Metadata = m.InstanceAdditionalMetadataSpec() instance.ServiceAccounts = append(instance.ServiceAccounts, instanceServiceAccountsSpec(m.GCPMachine.Spec.ServiceAccount)) @@ -510,14 +508,14 @@ func (m *MachineScope) InstanceSpec(log logr.Logger) *compute.Instance { // ANCHOR_END: MachineInstanceSpec // GetBootstrapData returns the bootstrap data from the secret in the Machine's bootstrap.dataSecretName. -func (m *MachineScope) GetBootstrapData() (string, error) { +func (m *MachineScope) GetBootstrapData(ctx context.Context) (string, error) { if m.Machine.Spec.Bootstrap.DataSecretName == nil { return "", errors.New("error retrieving bootstrap data: linked Machine's bootstrap.dataSecretName is nil") } secret := &corev1.Secret{} key := types.NamespacedName{Namespace: m.Namespace(), Name: *m.Machine.Spec.Bootstrap.DataSecretName} - if err := m.client.Get(context.TODO(), key, secret); err != nil { + if err := m.client.Get(ctx, key, secret); err != nil { return "", errors.Wrapf(err, "failed to retrieve bootstrap data secret for GCPMachine %s/%s", m.Namespace(), m.Name()) } @@ -530,13 +528,13 @@ func (m *MachineScope) GetBootstrapData() (string, error) { } // PatchObject persists the cluster configuration and status. -func (m *MachineScope) PatchObject() error { - return m.patchHelper.Patch(context.TODO(), m.GCPMachine) +func (m *MachineScope) PatchObject(ctx context.Context) error { + return m.patchHelper.Patch(ctx, m.GCPMachine) } // Close closes the current scope persisting the cluster configuration and status. -func (m *MachineScope) Close() error { - return m.PatchObject() +func (m *MachineScope) Close(ctx context.Context) error { + return m.PatchObject(ctx) } // ResourceManagerTags merges ResourceManagerTags from the scope's GCPCluster and GCPMachine. If the same key is present in both, diff --git a/cloud/scope/managedcluster.go b/cloud/scope/managedcluster.go index 03de2c201..485d24d1d 100644 --- a/cloud/scope/managedcluster.go +++ b/cloud/scope/managedcluster.go @@ -327,11 +327,11 @@ func (s *ManagedClusterScope) FirewallRulesSpec() []*compute.Firewall { // ANCHOR_END: ClusterFirewallSpec // PatchObject persists the cluster configuration and status. -func (s *ManagedClusterScope) PatchObject() error { - return s.patchHelper.Patch(context.TODO(), s.GCPManagedCluster) +func (s *ManagedClusterScope) PatchObject(ctx context.Context) error { + return s.patchHelper.Patch(ctx, s.GCPManagedCluster) } // Close closes the current scope persisting the cluster configuration and status. -func (s *ManagedClusterScope) Close() error { - return s.PatchObject() +func (s *ManagedClusterScope) Close(ctx context.Context) error { + return s.PatchObject(ctx) } diff --git a/cloud/scope/managedcontrolplane.go b/cloud/scope/managedcontrolplane.go index 5833694d6..ef39e3457 100644 --- a/cloud/scope/managedcontrolplane.go +++ b/cloud/scope/managedcontrolplane.go @@ -128,9 +128,9 @@ type ManagedControlPlaneScope struct { } // PatchObject persists the managed control plane configuration and status. -func (s *ManagedControlPlaneScope) PatchObject() error { +func (s *ManagedControlPlaneScope) PatchObject(ctx context.Context) error { return s.patchHelper.Patch( - context.TODO(), + ctx, s.GCPManagedControlPlane, v1beta1patch.WithOwnedConditions{Conditions: []clusterv1beta1.ConditionType{ infrav1exp.GKEControlPlaneReadyCondition, @@ -141,11 +141,11 @@ func (s *ManagedControlPlaneScope) PatchObject() error { } // Close closes the current scope persisting the managed control plane configuration and status. -func (s *ManagedControlPlaneScope) Close() error { +func (s *ManagedControlPlaneScope) Close(ctx context.Context) error { s.mcClient.Close() s.tagBindingsClient.Close() s.credentialsClient.Close() - return s.PatchObject() + return s.PatchObject(ctx) } // ConditionSetter return a condition setter (which is GCPManagedControlPlane itself). diff --git a/cloud/scope/managedmachinepool.go b/cloud/scope/managedmachinepool.go index c08f20c51..d7665e7be 100644 --- a/cloud/scope/managedmachinepool.go +++ b/cloud/scope/managedmachinepool.go @@ -117,9 +117,9 @@ type ManagedMachinePoolScope struct { } // PatchObject persists the managed control plane configuration and status. -func (s *ManagedMachinePoolScope) PatchObject() error { +func (s *ManagedMachinePoolScope) PatchObject(ctx context.Context) error { return s.patchHelper.Patch( - context.TODO(), + ctx, s.GCPManagedMachinePool, v1beta1patch.WithOwnedConditions{Conditions: []clusterv1beta1.ConditionType{ infrav1exp.GKEMachinePoolReadyCondition, @@ -130,10 +130,10 @@ func (s *ManagedMachinePoolScope) PatchObject() error { } // Close closes the current scope persisting the managed control plane configuration and status. -func (s *ManagedMachinePoolScope) Close() error { +func (s *ManagedMachinePoolScope) Close(ctx context.Context) error { s.mcClient.Close() s.migClient.Close() - return s.PatchObject() + return s.PatchObject(ctx) } // ConditionSetter return a condition setter (which is GCPManagedMachinePool itself). diff --git a/cloud/services/compute/instances/reconcile.go b/cloud/services/compute/instances/reconcile.go index 76277d072..760815d8a 100644 --- a/cloud/services/compute/instances/reconcile.go +++ b/cloud/services/compute/instances/reconcile.go @@ -101,7 +101,7 @@ func (s *Service) Reconcile(ctx context.Context) error { func (s *Service) Delete(ctx context.Context) error { log := log.FromContext(ctx) log.Info("Deleting instance resources") - instanceSpec := s.scope.InstanceSpec(log) + instanceSpec := s.scope.InstanceSpec(ctx, log) instanceName := instanceSpec.Name instanceKey := meta.ZonalKey(instanceName, s.scope.Zone()) 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 { func (s *Service) createOrGetInstance(ctx context.Context) (*compute.Instance, error) { log := log.FromContext(ctx) log.V(2).Info("Getting bootstrap data for machine") - bootstrapData, err := s.scope.GetBootstrapData() + bootstrapData, err := s.scope.GetBootstrapData(ctx) if err != nil { log.Error(err, "Error getting bootstrap data for machine") return nil, errors.Wrap(err, "failed to retrieve bootstrap data") } - instanceSpec := s.scope.InstanceSpec(log) + instanceSpec := s.scope.InstanceSpec(ctx, log) instanceName := instanceSpec.Name instanceKey := meta.ZonalKey(instanceName, s.scope.Zone()) instanceSpec.Metadata.Items = append(instanceSpec.Metadata.Items, &compute.MetadataItems{ diff --git a/cloud/services/compute/instances/service.go b/cloud/services/compute/instances/service.go index c31b8306a..8cb37b55a 100644 --- a/cloud/services/compute/instances/service.go +++ b/cloud/services/compute/instances/service.go @@ -44,7 +44,7 @@ type instancegroupsInterface interface { // Scope is an interfaces that hold used methods. type Scope interface { cloud.Machine - InstanceSpec(log logr.Logger) *compute.Instance + InstanceSpec(ctx context.Context, log logr.Logger) *compute.Instance } // Service implements instances reconciler. diff --git a/controllers/gcpcluster_controller.go b/controllers/gcpcluster_controller.go index e84281a5c..710443d71 100644 --- a/controllers/gcpcluster_controller.go +++ b/controllers/gcpcluster_controller.go @@ -146,7 +146,7 @@ func (r *GCPClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) // Always close the scope when exiting this function so we can persist any GCPMachine changes. defer func() { - if err := clusterScope.Close(); err != nil && reterr == nil { + if err := clusterScope.Close(ctx); err != nil && reterr == nil { reterr = err } }() @@ -165,7 +165,7 @@ func (r *GCPClusterReconciler) reconcile(ctx context.Context, clusterScope *scop log.Info("Reconciling GCPCluster") controllerutil.AddFinalizer(clusterScope.GCPCluster, infrav1.ClusterFinalizer) - if err := clusterScope.PatchObject(); err != nil { + if err := clusterScope.PatchObject(ctx); err != nil { return ctrl.Result{}, err } diff --git a/controllers/gcpmachine_controller.go b/controllers/gcpmachine_controller.go index d08a45ad7..a235a5cf4 100644 --- a/controllers/gcpmachine_controller.go +++ b/controllers/gcpmachine_controller.go @@ -201,7 +201,7 @@ func (r *GCPMachineReconciler) Reconcile(ctx context.Context, req ctrl.Request) // Always close the scope when exiting this function so we can persist any GCPMachine changes. defer func() { - if err := machineScope.Close(); err != nil && reterr == nil { + if err := machineScope.Close(ctx); err != nil && reterr == nil { reterr = err } }() @@ -220,7 +220,7 @@ func (r *GCPMachineReconciler) reconcile(ctx context.Context, machineScope *scop log.Info("Reconciling GCPMachine") controllerutil.AddFinalizer(machineScope.GCPMachine, infrav1.MachineFinalizer) - if err := machineScope.PatchObject(); err != nil { + if err := machineScope.PatchObject(ctx); err != nil { return ctrl.Result{}, err } diff --git a/exp/controllers/gcpmanagedcluster_controller.go b/exp/controllers/gcpmanagedcluster_controller.go index 8096b9fbb..c6ea3f029 100644 --- a/exp/controllers/gcpmanagedcluster_controller.go +++ b/exp/controllers/gcpmanagedcluster_controller.go @@ -125,7 +125,7 @@ func (r *GCPManagedClusterReconciler) Reconcile(ctx context.Context, req ctrl.Re // Always close the scope when exiting this function so we can persist any GCPMachine changes. defer func() { - if err := clusterScope.Close(); err != nil && reterr == nil { + if err := clusterScope.Close(ctx); err != nil && reterr == nil { reterr = err } }() @@ -173,7 +173,7 @@ func (r *GCPManagedClusterReconciler) reconcile(ctx context.Context, clusterScop log.Info("Reconciling GCPManagedCluster") controllerutil.AddFinalizer(clusterScope.GCPManagedCluster, infrav1exp.ClusterFinalizer) - if err := clusterScope.PatchObject(); err != nil { + if err := clusterScope.PatchObject(ctx); err != nil { return err } diff --git a/exp/controllers/gcpmanagedcontrolplane_controller.go b/exp/controllers/gcpmanagedcontrolplane_controller.go index 5fd6b68c7..be8f18243 100644 --- a/exp/controllers/gcpmanagedcontrolplane_controller.go +++ b/exp/controllers/gcpmanagedcontrolplane_controller.go @@ -137,7 +137,7 @@ func (r *GCPManagedControlPlaneReconciler) Reconcile(ctx context.Context, req ct // Always close the scope when exiting this function so we can persist any GCPMachine changes. defer func() { - if err := managedControlPlaneScope.Close(); err != nil && reterr == nil { + if err := managedControlPlaneScope.Close(ctx); err != nil && reterr == nil { reterr = err } }() @@ -156,7 +156,7 @@ func (r *GCPManagedControlPlaneReconciler) reconcile(ctx context.Context, manage log.Info("Reconciling GCPManagedControlPlane") controllerutil.AddFinalizer(managedControlPlaneScope.GCPManagedControlPlane, infrav1exp.ManagedControlPlaneFinalizer) - if err := managedControlPlaneScope.PatchObject(); err != nil { + if err := managedControlPlaneScope.PatchObject(ctx); err != nil { return ctrl.Result{}, err } diff --git a/exp/controllers/gcpmanagedmachinepool_controller.go b/exp/controllers/gcpmanagedmachinepool_controller.go index 4f81f85c9..3ee9b5167 100644 --- a/exp/controllers/gcpmanagedmachinepool_controller.go +++ b/exp/controllers/gcpmanagedmachinepool_controller.go @@ -305,7 +305,7 @@ func (r *GCPManagedMachinePoolReconciler) Reconcile(ctx context.Context, req ctr // Always close the scope when exiting this function so we can persist any GCPMachine changes. defer func() { - if err := managedMachinePoolScope.Close(); err != nil && reterr == nil { + if err := managedMachinePoolScope.Close(ctx); err != nil && reterr == nil { log.Error(err, "Failed to patch GCPManagedMachinePool object", "GCPManagedMachinePool", managedMachinePoolScope.GCPManagedMachinePool.Name) reterr = err } @@ -326,7 +326,7 @@ func (r *GCPManagedMachinePoolReconciler) reconcile(ctx context.Context, managed controllerutil.AddFinalizer(managedMachinePoolScope.GCPManagedMachinePool, infrav1exp.ManagedMachinePoolFinalizer) managedMachinePoolScope.SetInfrastructureMachineKind() - if err := managedMachinePoolScope.PatchObject(); err != nil { + if err := managedMachinePoolScope.PatchObject(ctx); err != nil { return ctrl.Result{}, err }