Skip to content

Commit

Permalink
address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
AshleyDumaine authored and eljohnson92 committed Feb 1, 2024
1 parent d59ca2a commit 2616a1d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
43 changes: 30 additions & 13 deletions cloud/scope/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@ package scope

import (
"context"
b64 "encoding/base64"
"errors"
"fmt"

"github.com/linode/linodego"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"

infrav1 "github.com/linode/cluster-api-provider-linode/api/v1alpha1"
"github.com/linode/linodego"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

infrav1alpha1 "github.com/linode/cluster-api-provider-linode/api/v1alpha1"
)

type MachineScopeParams struct {
Client client.Client
Cluster *clusterv1.Cluster
Machine *clusterv1.Machine
LinodeCluster *infrav1.LinodeCluster
LinodeMachine *infrav1.LinodeMachine
LinodeCluster *infrav1alpha1.LinodeCluster
LinodeMachine *infrav1alpha1.LinodeMachine
}

type MachineScope struct {
Expand All @@ -31,8 +31,8 @@ type MachineScope struct {
Cluster *clusterv1.Cluster
Machine *clusterv1.Machine
LinodeClient *linodego.Client
LinodeCluster *infrav1.LinodeCluster
LinodeMachine *infrav1.LinodeMachine
LinodeCluster *infrav1alpha1.LinodeCluster
LinodeMachine *infrav1alpha1.LinodeMachine
}

func validateMachineScopeParams(params MachineScopeParams) error {
Expand Down Expand Up @@ -75,10 +75,27 @@ func NewMachineScope(apiKey string, params MachineScopeParams) (*MachineScope, e
}, nil
}

// PatchObject persists the machine configuration and status.
func (s *MachineScope) PatchObject(ctx context.Context) error {
return s.PatchHelper.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)
}

// AddFinalizer adds a finalizer and immediately patches the object to avoid any race conditions
func (s *MachineScope) AddFinalizer(ctx context.Context) error {
controllerutil.AddFinalizer(s.LinodeMachine, infrav1alpha1.GroupVersion.String())

return s.Close(ctx)
}

// GetBootstrapData returns the bootstrap data from the secret in the Machine's bootstrap.dataSecretName.
func (m *MachineScope) GetBootstrapData(ctx context.Context) (string, error) {
func (m *MachineScope) GetBootstrapData(ctx context.Context) ([]byte, error) {
if m.Machine.Spec.Bootstrap.DataSecretName == nil {
return "", fmt.Errorf(
return []byte{}, fmt.Errorf(
"bootstrap data secret is nil for LinodeMachine %s/%s",
m.LinodeMachine.Namespace,
m.LinodeMachine.Name,
Expand All @@ -88,7 +105,7 @@ func (m *MachineScope) GetBootstrapData(ctx context.Context) (string, error) {
secret := &corev1.Secret{}
key := types.NamespacedName{Namespace: m.LinodeMachine.Namespace, Name: *m.Machine.Spec.Bootstrap.DataSecretName}
if err := m.client.Get(ctx, key, secret); err != nil {
return "", fmt.Errorf(
return []byte{}, fmt.Errorf(
"failed to retrieve bootstrap data secret for LinodeMachine %s/%s",
m.LinodeMachine.Namespace,
m.LinodeMachine.Name,
Expand All @@ -97,12 +114,12 @@ func (m *MachineScope) GetBootstrapData(ctx context.Context) (string, error) {

value, ok := secret.Data["value"]
if !ok {
return "", fmt.Errorf(
return []byte{}, fmt.Errorf(
"bootstrap data secret value key is missing for LinodeMachine %s/%s",
m.LinodeMachine.Namespace,
m.LinodeMachine.Name,
)
}

return b64.StdEncoding.EncodeToString(value), nil
return value, nil
}
10 changes: 7 additions & 3 deletions controller/linodemachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package controller

import (
"context"
b64 "encoding/base64"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -202,15 +203,18 @@ func (r *LinodeMachineReconciler) reconcile(
r.Recorder.Event(machineScope.LinodeMachine, corev1.EventTypeWarning, string(failureReason), err.Error())
}

if patchErr := machineScope.PatchHelper.Patch(ctx, machineScope.LinodeMachine); patchErr != nil && client.IgnoreNotFound(patchErr) != nil {
// Always close the scope when exiting this function so we can persist any LinodeMachine changes.
if patchErr := machineScope.Close(ctx); patchErr != nil && client.IgnoreNotFound(patchErr) != nil {
logger.Error(patchErr, "failed to patch LinodeMachine")

err = errors.Join(err, patchErr)
}
}()

// Add the finalizer if not already there
controllerutil.AddFinalizer(machineScope.LinodeMachine, infrav1.GroupVersion.String())
if err := machineScope.AddFinalizer(ctx); err != nil {
return res, err
}

// Delete
if !machineScope.LinodeMachine.ObjectMeta.DeletionTimestamp.IsZero() {
Expand Down Expand Up @@ -295,7 +299,7 @@ func (*LinodeMachineReconciler) reconcileCreate(ctx context.Context, machineScop
return nil, err
}
createConfig.Metadata = &linodego.InstanceMetadataOptions{
UserData: bootstrapData,
UserData: b64.StdEncoding.EncodeToString(bootstrapData),
}

if linodeInstance, err = machineScope.LinodeClient.CreateInstance(ctx, *createConfig); err != nil {
Expand Down

0 comments on commit 2616a1d

Please sign in to comment.