Skip to content

Commit

Permalink
allow for retrying provider container run
Browse files Browse the repository at this point in the history
Signed-off-by: Emily McMullan <[email protected]>
  • Loading branch information
eemcmullan committed Apr 23, 2024
1 parent e2f483e commit 3cc9870
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions cmd/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ func NewAnalyzeCmd(log logr.Logger) *cobra.Command {
log.Error(err, "failed to create container volume")
return err
}
providerPorts, err := analyzeCmd.RunProviders(cmd.Context(), containerNetworkName, containerVolName, foundProviders)
// allow for 5 retries of running provider in the case of port in use
providerPorts, err := analyzeCmd.RunProviders(cmd.Context(), containerNetworkName, containerVolName, foundProviders, 5)
if err != nil {
log.Error(err, "failed to run provider")
return err
Expand Down Expand Up @@ -763,7 +764,20 @@ func (a *analyzeCommand) createContainerVolume(sourceInput string) (string, erro
return volName, nil
}

func (a *analyzeCommand) RunProviders(ctx context.Context, networkName string, volName string, providers []string) (map[string]int, error) {
func (a *analyzeCommand) retryProviderContainer(ctx context.Context, networkName string, volName string, providers []string, retry int) error {
if retry == 0 {
return fmt.Errorf("too many provider container retry attempts")
}
retry--

_, err := a.RunProviders(ctx, networkName, volName, providers, retry)
if err != nil && err.Error() != "bind: address already in use" {
return fmt.Errorf("error retrying run provider %v", err)
}
return nil
}

func (a *analyzeCommand) RunProviders(ctx context.Context, networkName string, volName string, providers []string, retry int) (map[string]int, error) {
providerPorts := map[string]int{}
port, err := freeport.GetFreePort()
if err != nil {
Expand Down Expand Up @@ -801,7 +815,14 @@ func (a *analyzeCommand) RunProviders(ctx context.Context, networkName string, v
container.WithNetwork(networkName),
)
if err != nil {
return nil, err
if err.Error() == "bind: address already in use" {
err := a.retryProviderContainer(ctx, networkName, volName, providers, retry)
if err != nil {
return nil, err
}
} else {
return nil, err
}
}
a.providerContainerNames = append(a.providerContainerNames, con.Name)

Expand Down

0 comments on commit 3cc9870

Please sign in to comment.