Skip to content

Commit

Permalink
Add retry to verifying patch command
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Eckert committed Nov 1, 2022
1 parent 8c02cf2 commit 168450c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
20 changes: 13 additions & 7 deletions cli/cmd/uninstall/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,11 +423,11 @@ func (c *Command) removeCustomResources(uiLogger action.DebugLog) error {
return err
}
if len(crs) != 0 {
return common.NewDeletionError(fmt.Sprintf("%d custom resources remain after deletion request. Retrying deletion", len(crs)))
return common.NewDanglingResourceError(fmt.Sprintf("%d custom resources remain after deletion request", len(crs)))
}
return nil
}, backoff.WithMaxRetries(backoff.NewConstantBackOff(time.Second), 5))
if !common.IsDeletionError(err) {
if !common.IsDanglingResourceError(err) {
return err
}

Expand All @@ -443,12 +443,18 @@ func (c *Command) removeCustomResources(uiLogger action.DebugLog) error {
return err
}

crs, err = c.fetchCustomResources(crds)
err = backoff.Retry(func() error {
crs, err := c.fetchCustomResources(crds)
if err != nil {
return err
}
if len(crs) != 0 {
return common.NewDanglingResourceError(fmt.Sprintf("%d custom resources remain after request to patch finalizers", len(crs)))
}
return nil
}, backoff.WithMaxRetries(backoff.NewConstantBackOff(time.Second), 5))
if err != nil {
return err
}
if len(crs) != 0 {
return fmt.Errorf("unable to remove all custom resources managed by Consul. %d custom resources remain and will need to be removed manually", len(crs))
return fmt.Errorf("unable to remove all custom resources managed by Consul. %d custom resources remain and will need to be removed manually. %v", len(crs), err)
}

return nil
Expand Down
25 changes: 13 additions & 12 deletions cli/common/error.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
package common

// DeletionError should be used when a request was made to delete a resource
// and that request failed.
type DeletionError struct {
// DanglingResourceError should be used when a request was made to remove
// a resource and the resource still remains after enough time has elapsed
// that it should have been removed by Kubernetes.
type DanglingResourceError struct {
message string
}

// NewDeletionError returns a new instance of DeletionError for handling
// failures in deletion requests.
func NewDeletionError(message string) *DeletionError {
return &DeletionError{message}
// NewDanglingResourceError returns a new instance of DanglingResourceError with
// the given message.
func NewDanglingResourceError(message string) *DanglingResourceError {
return &DanglingResourceError{message}
}

// Error returns a string representation of the deletion error.
func (d *DeletionError) Error() string {
// Error returns a string representation of the dangling resource error.
func (d *DanglingResourceError) Error() string {
return d.message
}

// IsDeletionError returns true if the error passed in is of type DeletionError.
func IsDeletionError(err error) bool {
_, ok := err.(*DeletionError)
// IsDanglingResourceError returns true if the error passed in is of type DanglingResourceError.
func IsDanglingResourceError(err error) bool {
_, ok := err.(*DanglingResourceError)
return ok
}

0 comments on commit 168450c

Please sign in to comment.