Skip to content

Commit

Permalink
Add Finalizers on LinodeCluster resource for each ControlPlane Linode…
Browse files Browse the repository at this point in the history
…Machine resource (#434)

* add finalizers for control plane machines if using dns LB
  • Loading branch information
amold1 authored Aug 6, 2024
1 parent 568ee38 commit d0fcbd7
Show file tree
Hide file tree
Showing 6 changed files with 568 additions and 64 deletions.
73 changes: 62 additions & 11 deletions cloud/scope/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import (
"fmt"

corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/util/retry"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
kutil "sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

Expand All @@ -26,7 +29,8 @@ type MachineScopeParams struct {

type MachineScope struct {
Client K8sClient
PatchHelper *patch.Helper
MachinePatchHelper *patch.Helper
ClusterPatchHelper *patch.Helper
Cluster *clusterv1.Cluster
Machine *clusterv1.Machine
LinodeClient LinodeClient
Expand Down Expand Up @@ -111,14 +115,20 @@ func NewMachineScope(ctx context.Context, linodeClientConfig, dnsClientConfig Cl
return nil, fmt.Errorf("failed to create akamai dns client: %w", err)
}

helper, err := patch.NewHelper(params.LinodeMachine, params.Client)
machineHelper, err := patch.NewHelper(params.LinodeMachine, params.Client)
if err != nil {
return nil, fmt.Errorf("failed to init patch helper: %w", err)
return nil, fmt.Errorf("failed to init machine patch helper: %w", err)
}

clusterHelper, err := patch.NewHelper(params.LinodeCluster, params.Client)
if err != nil {
return nil, fmt.Errorf("failed to init cluster patch helper: %w", err)
}

return &MachineScope{
Client: params.Client,
PatchHelper: helper,
MachinePatchHelper: machineHelper,
ClusterPatchHelper: clusterHelper,
Cluster: params.Cluster,
Machine: params.Machine,
LinodeClient: linodeClient,
Expand All @@ -129,21 +139,62 @@ func NewMachineScope(ctx context.Context, linodeClientConfig, dnsClientConfig Cl
}, nil
}

// PatchObject persists the machine configuration and status.
func (s *MachineScope) PatchObject(ctx context.Context) error {
return s.PatchHelper.Patch(ctx, s.LinodeMachine)
// CloseAll persists the linodemachine and linodecluster configuration and status.
func (s *MachineScope) CloseAll(ctx context.Context) error {
if err := s.MachineClose(ctx); err != nil {
return err
}
if err := s.ClusterClose(ctx); err != nil {
return err
}
return nil
}

// MachineClose persists the linodemachine configuration and status.
func (s *MachineScope) MachineClose(ctx context.Context) error {
return retry.OnError(retry.DefaultRetry, apierrors.IsConflict, func() error {
return s.MachinePatchHelper.Patch(ctx, s.LinodeMachine)
})
}

// Close closes the current scope persisting the machine configuration and status.
func (s *MachineScope) Close(ctx context.Context) error {
return s.PatchObject(ctx)
// ClusterClose persists the linodecluster configuration and status.
func (s *MachineScope) ClusterClose(ctx context.Context) error {
return retry.OnError(retry.DefaultRetry, apierrors.IsConflict, func() error {
return s.ClusterPatchHelper.Patch(ctx, s.LinodeCluster)
})
}

// AddFinalizer adds a finalizer if not present and immediately patches the
// object to avoid any race conditions.
func (s *MachineScope) AddFinalizer(ctx context.Context) error {
if controllerutil.AddFinalizer(s.LinodeMachine, infrav1alpha2.MachineFinalizer) {
return s.Close(ctx)
return s.MachineClose(ctx)
}

return nil
}

// AddLinodeClusterFinalizer adds a finalizer if not present and immediately patches the
// object to avoid any race conditions.
func (s *MachineScope) AddLinodeClusterFinalizer(ctx context.Context) error {
if !kutil.IsControlPlaneMachine(s.Machine) {
return nil
}
if controllerutil.AddFinalizer(s.LinodeCluster, s.LinodeMachine.Name) {
return s.ClusterClose(ctx)
}

return nil
}

// RemoveLinodeClusterFinalizer adds a finalizer if not present and immediately patches the
// object to avoid any race conditions.
func (s *MachineScope) RemoveLinodeClusterFinalizer(ctx context.Context) error {
if !kutil.IsControlPlaneMachine(s.Machine) {
return nil
}
if controllerutil.RemoveFinalizer(s.LinodeCluster, s.LinodeMachine.Name) {
return s.ClusterClose(ctx)
}

return nil
Expand Down
Loading

0 comments on commit d0fcbd7

Please sign in to comment.