Skip to content

Commit

Permalink
handle case where instance already exists
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulait committed Nov 7, 2024
1 parent 4a9c8bf commit f1eb5ed
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions controller/linodemachine_controller_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,27 @@ func createInstance(ctx context.Context, logger logr.Logger, machineScope *scope

machineScope.LinodeClient.OnAfterResponse(ctr.ApiResponseRatelimitCounter)
inst, err := machineScope.LinodeClient.CreateInstance(ctx, *createOpts)

// if instance already exists, we get 400 response. get respective instance and return
if linodego.ErrHasStatus(err, http.StatusBadRequest) {
logger.Error(err, "Failed to create instance, received [400 BadRequest] response.")

// check if instance already exists
listFilter := util.Filter{Label: createOpts.Label}
filter, errFilter := listFilter.String()
if errFilter != nil {
logger.Error(err, "Failed to create filter to list instance")
return nil, ctr.RetryAfter(), err
}
instances, listErr := machineScope.LinodeClient.ListInstances(ctx, linodego.NewListOptions(1, filter))
if listErr != nil {
return nil, ctr.RetryAfter(), listErr
}
if len(instances) > 0 {
return &instances[0], ctr.RetryAfter(), nil
}

Check warning on line 742 in controller/linodemachine_controller_helpers.go

View check run for this annotation

Codecov / codecov/patch

controller/linodemachine_controller_helpers.go#L727-L742

Added lines #L727 - L742 were not covered by tests
}

return inst, ctr.RetryAfter(), err
}

Expand Down

0 comments on commit f1eb5ed

Please sign in to comment.