Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cloud/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
8 changes: 4 additions & 4 deletions cloud/scope/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
24 changes: 11 additions & 13 deletions cloud/scope/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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),
},
Expand Down Expand Up @@ -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(),
Expand All @@ -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(),
Expand Down Expand Up @@ -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))
Expand All @@ -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())
}

Expand All @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions cloud/scope/managedcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
8 changes: 4 additions & 4 deletions cloud/scope/managedcontrolplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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).
Expand Down
8 changes: 4 additions & 4 deletions cloud/scope/managedmachinepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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).
Expand Down
6 changes: 3 additions & 3 deletions cloud/services/compute/instances/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion cloud/services/compute/instances/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions controllers/gcpcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}()
Expand All @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions controllers/gcpmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}()
Expand All @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions exp/controllers/gcpmanagedcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}()
Expand Down Expand Up @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions exp/controllers/gcpmanagedcontrolplane_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}()
Expand All @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions exp/controllers/gcpmanagedmachinepool_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}

Expand Down