diff --git a/api/v1alpha1/linodemachine_types.go b/api/v1alpha1/linodemachine_types.go index e3ff82473..bc0b27edf 100644 --- a/api/v1alpha1/linodemachine_types.go +++ b/api/v1alpha1/linodemachine_types.go @@ -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"` diff --git a/cloud/services/volumes.go b/cloud/services/volumes.go index b45078f55..e8a120d69 100644 --- a/cloud/services/volumes.go +++ b/cloud/services/volumes.go @@ -2,7 +2,6 @@ package services import ( "context" - "encoding/json" "fmt" "net/http" @@ -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, @@ -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 @@ -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 } } diff --git a/config/crd/bases/infrastructure.cluster.x-k8s.io_linodemachines.yaml b/config/crd/bases/infrastructure.cluster.x-k8s.io_linodemachines.yaml index 539a38f5d..70cc50148 100644 --- a/config/crd/bases/infrastructure.cluster.x-k8s.io_linodemachines.yaml +++ b/config/crd/bases/infrastructure.cluster.x-k8s.io_linodemachines.yaml @@ -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 diff --git a/controller/linodemachine_controller.go b/controller/linodemachine_controller.go index 26940bc75..e23e69489 100644 --- a/controller/linodemachine_controller.go +++ b/controller/linodemachine_controller.go @@ -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")