Skip to content

Commit

Permalink
remove list instances call on create
Browse files Browse the repository at this point in the history
  • Loading branch information
AshleyDumaine committed Aug 21, 2024
1 parent 0cd01ba commit e4236a5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 279 deletions.
74 changes: 12 additions & 62 deletions controller/linodemachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,74 +234,24 @@ func (r *LinodeMachineReconciler) reconcileCreate(
return ctrl.Result{}, err
}

tags := []string{machineScope.LinodeCluster.Name}
var instanceID *int
if machineScope.LinodeMachine.Spec.ProviderID != nil {
instID, err := util.GetInstanceID(machineScope.LinodeMachine.Spec.ProviderID)
if err != nil {
logger.Error(err, "Failed to parse instance ID from provider ID")
return ctrl.Result{}, err
}
instanceID = util.Pointer(instID)
}

// TODO: switch this to a get or just list all machines by tag (cluster name) if using caching
listFilter := util.Filter{
ID: instanceID,
Label: machineScope.LinodeMachine.Name,
Tags: tags,
}
filter, err := listFilter.String()
// get the bootstrap data for the Linode instance and set it for create config
createOpts, err := r.newCreateConfig(ctx, machineScope, logger)
if err != nil {
return ctrl.Result{}, err
}
linodeInstances, err := machineScope.LinodeClient.ListInstances(ctx, linodego.NewListOptions(1, filter))
if err != nil {
logger.Error(err, "Failed to list Linode machine instances")

return ctrl.Result{RequeueAfter: reconciler.DefaultMachineControllerWaitForRunningDelay}, nil
logger.Error(err, "Failed to create Linode machine InstanceCreateOptions")
return retryIfTransient(err)
}

var linodeInstance *linodego.Instance
switch len(linodeInstances) {
case 1:
logger.Info("Linode instance already exists")
linodeInstance = &linodeInstances[0]
case 0:
// get the bootstrap data for the Linode instance and set it for create config
createOpts, err := r.newCreateConfig(ctx, machineScope, tags, logger)
if err != nil {
logger.Error(err, "Failed to create Linode machine InstanceCreateOptions")
return retryIfTransient(err)
}
linodeInstance, err = machineScope.LinodeClient.CreateInstance(ctx, *createOpts)
if err != nil {
if util.IsRetryableError(err) {
logger.Error(err, "Failed to create Linode instance due to API error, requeing")
if linodego.ErrHasStatus(err, http.StatusTooManyRequests) {
return ctrl.Result{RequeueAfter: reconciler.DefaultLinodeTooManyRequestsErrorRetryDelay}, nil
}
return ctrl.Result{RequeueAfter: reconciler.DefaultMachineControllerRetryDelay}, nil
}
logger.Error(err, "Failed to create Linode machine instance")

if reconciler.RecordDecayingCondition(machineScope.LinodeMachine,
ConditionPreflightCreated, string(cerrs.CreateMachineError), err.Error(),
reconciler.DefaultTimeout(r.ReconcileTimeout, reconciler.DefaultMachineControllerWaitForPreflightTimeout)) {
return ctrl.Result{}, err
}

return ctrl.Result{RequeueAfter: reconciler.DefaultMachineControllerWaitForRunningDelay}, nil
linodeInstance, err := machineScope.LinodeClient.CreateInstance(ctx, *createOpts)
if err != nil {
logger.Error(err, "Failed to create Linode machine instance")
if reconciler.RecordDecayingCondition(machineScope.LinodeMachine,
ConditionPreflightCreated, string(cerrs.CreateMachineError), err.Error(),
reconciler.DefaultTimeout(r.ReconcileTimeout, reconciler.DefaultMachineControllerWaitForPreflightTimeout)) {
return ctrl.Result{}, err
}
default:
err = errors.New("multiple instances")
logger.Error(err, "multiple instances found", "tags", tags)

return ctrl.Result{}, err
return retryIfTransient(err)
}

conditions.MarkTrue(machineScope.LinodeMachine, ConditionPreflightCreated)

return r.reconcileInstanceCreate(ctx, logger, machineScope, linodeInstance)
}

Expand Down
4 changes: 2 additions & 2 deletions controller/linodemachine_controller_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ var (
errNoPublicIPv6SLAACAddrs = errors.New("no public SLAAC address set")
)

func (r *LinodeMachineReconciler) newCreateConfig(ctx context.Context, machineScope *scope.MachineScope, tags []string, logger logr.Logger) (*linodego.InstanceCreateOptions, error) {
func (r *LinodeMachineReconciler) newCreateConfig(ctx context.Context, machineScope *scope.MachineScope, logger logr.Logger) (*linodego.InstanceCreateOptions, error) {
var err error

createConfig := linodeMachineSpecToInstanceCreateConfig(machineScope.LinodeMachine.Spec)
Expand All @@ -86,7 +86,7 @@ func (r *LinodeMachineReconciler) newCreateConfig(ctx context.Context, machineSc
if createConfig.Tags == nil {
createConfig.Tags = []string{}
}
createConfig.Tags = append(createConfig.Tags, tags...)
createConfig.Tags = append(createConfig.Tags, machineScope.LinodeCluster.Name)

if createConfig.Label == "" {
createConfig.Label = machineScope.LinodeMachine.Name
Expand Down
Loading

0 comments on commit e4236a5

Please sign in to comment.