-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add retry to helm installation and knative manifests apply
- Loading branch information
Showing
6 changed files
with
115 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package retry | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"os/exec" | ||
"time" | ||
|
||
"github.com/avast/retry-go/v4" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
const ( | ||
retryCount = 10 | ||
retryWait = 3 * time.Second | ||
) | ||
|
||
type Doer interface { | ||
Do(ctx context.Context) error | ||
DoWithErrorHandling(ctx context.Context, errorFunc ErrorFunc) error | ||
} | ||
|
||
type ErrorFunc func(error, *bytes.Buffer, *bytes.Buffer) error | ||
|
||
type commandDoer struct { | ||
cmd string | ||
args []string | ||
} | ||
|
||
func Command(cmd string, args ...string) Doer { | ||
return commandDoer{ | ||
cmd: cmd, | ||
args: args, | ||
} | ||
} | ||
|
||
func (c commandDoer) Do(ctx context.Context) error { | ||
return retry.Do(func() error { | ||
cmd, stdout, stderr := c.createCmd(ctx) | ||
err := cmd.Run() | ||
if err != nil { | ||
return fmt.Errorf("command %q with args [%v] failed STDOUT=(%s) STDERR=(%s): %w", | ||
c.cmd, c.args, stdout.String(), stderr.String(), err, | ||
) | ||
} | ||
return nil | ||
}, | ||
c.createOpts(ctx)..., | ||
) | ||
} | ||
|
||
func (c commandDoer) DoWithErrorHandling(ctx context.Context, errorFunc ErrorFunc) error { | ||
return retry.Do(func() error { | ||
cmd, stdout, stderr := c.createCmd(ctx) | ||
err := cmd.Run() | ||
return errorFunc(err, stdout, stderr) | ||
}, | ||
c.createOpts(ctx)..., | ||
) | ||
} | ||
|
||
func (c commandDoer) createCmd(ctx context.Context) (*exec.Cmd, *bytes.Buffer, *bytes.Buffer) { | ||
stdout := new(bytes.Buffer) | ||
stderr := new(bytes.Buffer) | ||
cmd := exec.CommandContext(ctx, c.cmd, c.args...) //nolint:gosec | ||
cmd.Stdout = stdout | ||
cmd.Stderr = stderr | ||
return cmd, stdout, stderr | ||
} | ||
|
||
func (c commandDoer) createOpts(ctx context.Context) []retry.Option { | ||
return []retry.Option{ | ||
retry.Context(ctx), | ||
retry.Delay(retryWait), | ||
retry.Attempts(retryCount), | ||
retry.DelayType(retry.FixedDelay), | ||
retry.OnRetry(func(_ uint, err error) { | ||
logrus.WithError(err). | ||
WithField("args", c.args). | ||
Errorf("failed running %s", c.cmd) | ||
}), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters