Skip to content

Commit

Permalink
store the volumeID to save some API calls
Browse files Browse the repository at this point in the history
  • Loading branch information
AshleyDumaine committed Feb 26, 2024
1 parent b6c5bf1 commit e637b9a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 31 deletions.
5 changes: 5 additions & 0 deletions api/v1alpha1/linodemachine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ type LinodeMachineStatus struct {
// Addresses contains the Linode instance associated addresses.
Addresses []clusterv1.MachineAddress `json:"addresses,omitempty"`

// EtcdVolumeID contains the ID for the Linode volume associated the node
// (control plane nodes only)
// +optional
EtcdVolumeID int `json:"etcdVolumeID,omitempty"`

// InstanceState is the state of the Linode instance for this machine.
// +optional
InstanceState *linodego.InstanceStatus `json:"instanceState,omitempty"`
Expand Down
45 changes: 15 additions & 30 deletions cloud/services/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package services

import (
"context"
"encoding/json"
"fmt"
"net/http"

Expand All @@ -21,12 +20,12 @@ func CreateEtcdVolume(ctx context.Context,
linodeID int,
logger logr.Logger,
machineScope *scope.MachineScope,
) error {
) (volume *linodego.Volume, err error) {
// if the instance is part of the control plane, create an etcd volume to attach
if !kutil.IsControlPlaneMachine(machineScope.Machine) {
return nil
return nil, nil
}
_, err := machineScope.LinodeClient.CreateVolume(ctx, linodego.VolumeCreateOptions{
volume, err = machineScope.LinodeClient.CreateVolume(ctx, linodego.VolumeCreateOptions{
Label: fmt.Sprintf("%s-%s", machineScope.LinodeMachine.Name, "etcd"),
Region: machineScope.LinodeMachine.Spec.Region,
LinodeID: linodeID,
Expand All @@ -36,10 +35,10 @@ func CreateEtcdVolume(ctx context.Context,
if err != nil {
logger.Error(err, "Failed to create etcd volume")

return err
return nil, err
}

return nil
return volume, nil
}

// DeleteEtcdVolume deletes the etcd Volume for a given linode instance
Expand All @@ -50,34 +49,20 @@ func DeleteEtcdVolume(ctx context.Context,
if !kutil.IsControlPlaneMachine(machineScope.Machine) {
return nil
}
filter := map[string]string{
"label": fmt.Sprintf("%s-%s", machineScope.LinodeMachine.Name, "etcd"),
}
rawFilter, err := json.Marshal(filter)
volumes, err := machineScope.LinodeClient.ListInstanceVolumes(
ctx,
*machineScope.LinodeMachine.Spec.InstanceID,
&linodego.ListOptions{Filter: string(rawFilter)},
)
if err != nil {
logger.Error(err, "Failed to list instance volumes")

return err
}
for _, volume := range volumes {
if err := machineScope.LinodeClient.DetachVolume(ctx, volume.ID); err != nil {
if util.IgnoreLinodeAPIError(err, http.StatusNotFound) != nil {
logger.Error(err, "Failed to detach volume")
volumeID := machineScope.LinodeMachine.Status.EtcdVolumeID
if err := machineScope.LinodeClient.DetachVolume(ctx, volumeID); err != nil {
if util.IgnoreLinodeAPIError(err, http.StatusNotFound) != nil {
logger.Error(err, "Failed to detach volume")

return err
}
return err
}
if err := machineScope.LinodeClient.DeleteVolume(ctx, volume.ID); err != nil {
if util.IgnoreLinodeAPIError(err, http.StatusNotFound) != nil {
logger.Error(err, "Failed to delete volume")
}
if err := machineScope.LinodeClient.DeleteVolume(ctx, volumeID); err != nil {
if util.IgnoreLinodeAPIError(err, http.StatusNotFound) != nil {
logger.Error(err, "Failed to delete volume")

return err
}
return err
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@ spec:
- type
type: object
type: array
etcdVolumeID:
description: |-
EtcdVolumeID contains the ID for the Linode volume associated the node
(control plane nodes only)
type: integer
failureMessage:
description: |-
FailureMessage will be set in the event that there is a terminal problem
Expand Down
4 changes: 3 additions & 1 deletion controller/linodemachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,11 +342,13 @@ func (r *LinodeMachineReconciler) reconcileCreate(
return nil, err
}

if err := services.CreateEtcdVolume(ctx, linodeInstance.ID, logger, machineScope); err != nil {
volume, err := services.CreateEtcdVolume(ctx, linodeInstance.ID, logger, machineScope)
if err != nil {
logger.Error(err, "Failed to create etcd volume")

return nil, err
}
machineScope.LinodeMachine.Status.EtcdVolumeID = volume.ID

default:
err = errors.New("multiple instances")
Expand Down

0 comments on commit e637b9a

Please sign in to comment.