From 8def323ba445e1433dc1da491be47103fa374ab1 Mon Sep 17 00:00:00 2001 From: Ashley Dumaine Date: Tue, 27 Feb 2024 10:33:36 -0500 Subject: [PATCH] half the root disk and make an etcd disk with it --- api/v1alpha1/linodemachine_types.go | 5 -- cloud/services/volumes.go | 89 ------------------- ...cture.cluster.x-k8s.io_linodemachines.yaml | 5 -- controller/linodemachine_controller.go | 47 +++++++--- .../linodemachine_controller_helpers.go | 4 +- 5 files changed, 40 insertions(+), 110 deletions(-) delete mode 100644 cloud/services/volumes.go diff --git a/api/v1alpha1/linodemachine_types.go b/api/v1alpha1/linodemachine_types.go index bc0b27edf..e3ff82473 100644 --- a/api/v1alpha1/linodemachine_types.go +++ b/api/v1alpha1/linodemachine_types.go @@ -115,11 +115,6 @@ 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 deleted file mode 100644 index 35b71d437..000000000 --- a/cloud/services/volumes.go +++ /dev/null @@ -1,89 +0,0 @@ -package services - -import ( - "context" - "fmt" - "net/http" - - "github.com/go-logr/logr" - "github.com/linode/linodego" - kutil "sigs.k8s.io/cluster-api/util" - - "github.com/linode/cluster-api-provider-linode/cloud/scope" - "github.com/linode/cluster-api-provider-linode/util" -) - -const ( - defaultEtcdSizeGB = 10 - defaultVolumeDetachTimeoutSeconds = 10 -) - -// CreateEtcdVolume creates an etcd Volume for a given linode instance ID -func CreateEtcdVolume(ctx context.Context, - linodeID int, - logger logr.Logger, - machineScope *scope.MachineScope, -) (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 volume, err - } - // use a label that's shorter than 32 character - volume, err = machineScope.LinodeClient.CreateVolume(ctx, linodego.VolumeCreateOptions{ - Label: fmt.Sprintf("%d-%s", linodeID, "etcd"), - Region: machineScope.LinodeMachine.Spec.Region, - LinodeID: linodeID, - Size: defaultEtcdSizeGB, - Tags: machineScope.LinodeMachine.Spec.Tags, - }) - if err != nil { - logger.Error(err, "Failed to create etcd volume") - - return nil, err - } - - return volume, nil -} - -// DeleteEtcdVolume deletes the etcd Volume for a given linode instance -func DeleteEtcdVolume(ctx context.Context, - logger logr.Logger, - machineScope *scope.MachineScope) error { - // detach and delete the etcd volume only if control plane node - if !kutil.IsControlPlaneMachine(machineScope.Machine) { - return nil - } - - 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 - } - } - - // need to wait for volume to actually detach before we can proceed with - // deleting it - if _, err := machineScope.LinodeClient.WaitForVolumeLinodeID( - ctx, - volumeID, - nil, - defaultVolumeDetachTimeoutSeconds, - ); err != nil { - logger.Error(err, "Timed out waiting for volume to detach") - - return err - } - - // now that the volume is detached it can be properly deleted - 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 nil -} 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 70cc50148..539a38f5d 100644 --- a/config/crd/bases/infrastructure.cluster.x-k8s.io_linodemachines.yaml +++ b/config/crd/bases/infrastructure.cluster.x-k8s.io_linodemachines.yaml @@ -272,11 +272,6 @@ 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 0bf21453e..c3db5c8d7 100644 --- a/controller/linodemachine_controller.go +++ b/controller/linodemachine_controller.go @@ -342,14 +342,47 @@ func (r *LinodeMachineReconciler) reconcileCreate( return nil, err } - volume, err := services.CreateEtcdVolume(ctx, linodeInstance.ID, logger, machineScope) + // get the default instance config + configs, err := machineScope.LinodeClient.ListInstanceConfigs(ctx, linodeInstance.ID, &linodego.ListOptions{}) + if err != nil || len(configs) == 0 { + logger.Error(err, "Failed to list instance configs") + + return nil, err + } + instanceConfig := configs[0] + + // halve the root disk for etcd + rootDiskID := instanceConfig.Devices.SDA.DiskID + rootDisk, err := machineScope.LinodeClient.GetInstanceDisk(ctx, linodeInstance.ID, rootDiskID) + etcdSize := rootDisk.Size / 2 + if err = machineScope.LinodeClient.ResizeInstanceDisk(ctx, linodeInstance.ID, rootDiskID, rootDisk.Size/2); err != nil { + logger.Error(err, "Failed to resize root disk") + + return nil, err + } + + // create the etcd disk + etcdDisk, err := machineScope.LinodeClient.CreateInstanceDisk( + ctx, + linodeInstance.ID, + linodego.InstanceDiskCreateOptions{ + Label: "etcd-data", + Size: etcdSize, + Filesystem: string(linodego.FilesystemExt4), + }, + ) if err != nil { - logger.Error(err, "Failed to create etcd volume") + logger.Error(err, "Failed to create etcd disk") return nil, err } - if volume != nil { - machineScope.LinodeMachine.Status.EtcdVolumeID = volume.ID + + instanceConfig.Devices.SDC = &linodego.InstanceConfigDevice{DiskID: etcdDisk.ID} + err = machineScope.LinodeClient.BootInstance(ctx, linodeInstance.ID, instanceConfig.ID) + if err != nil { + logger.Error(err, "Failed to boot instance") + + return nil, err } default: @@ -468,12 +501,6 @@ func (r *LinodeMachineReconciler) reconcileDelete( return err } - if err := services.DeleteEtcdVolume(ctx, logger, machineScope); err != nil { - logger.Error(err, "Failed to delete etcd volume") - - return err - } - if err := machineScope.LinodeClient.DeleteInstance(ctx, *machineScope.LinodeMachine.Spec.InstanceID); err != nil { if util.IgnoreLinodeAPIError(err, http.StatusNotFound) != nil { logger.Error(err, "Failed to delete Linode machine instance") diff --git a/controller/linodemachine_controller_helpers.go b/controller/linodemachine_controller_helpers.go index 0bd8d8579..34bea11f2 100644 --- a/controller/linodemachine_controller_helpers.go +++ b/controller/linodemachine_controller_helpers.go @@ -57,6 +57,9 @@ func (*LinodeMachineReconciler) newCreateConfig(ctx context.Context, machineScop return nil, err } + + createConfig.Booted = util.Pointer(false) + createConfig.PrivateIP = true bootstrapData, err := machineScope.GetBootstrapData(ctx) @@ -87,7 +90,6 @@ func (*LinodeMachineReconciler) newCreateConfig(ctx context.Context, machineScop if createConfig.Image == "" { createConfig.Image = reconciler.DefaultMachineControllerLinodeImage } - if createConfig.RootPass == "" { createConfig.RootPass = uuid.NewString() }