Skip to content

Commit

Permalink
Add configuration for retry mechanism (#11084)
Browse files Browse the repository at this point in the history
* Add configuration for retry mechanism

* Fix
  • Loading branch information
KacperMalachowski authored Jun 24, 2024
1 parent 67613d8 commit 9e32235
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
3 changes: 3 additions & 0 deletions configs/kaniko-build-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ ado-config:
ado-organization-url: https://dev.azure.com/hyperspace-pipelines
ado-project-name: kyma
ado-pipeline-id: 14902
ado-retry-strategy:
attempts: 3
delay: 5s
cache:
enabled: true
cache-repo: europe-docker.pkg.dev/kyma-project/cache/cache
Expand Down
29 changes: 22 additions & 7 deletions pkg/azuredevops/pipelines/pipelines.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ type TimelineTest struct {
Result string
}

// Retry strategy contains configuration for ADO request retry policy
//
// Fields:
// Attempts - Attempts to try before fail request
// Delay - Waiting time between next try
type RetryStrategy struct {
// Attempts to make before failing request
Attempts uint `yaml:"attempts" json:"attempts"`
// Delay between two request tries
Delay time.Duration `yaml:"delay" json:"delay"`
}

// Config is a struct that holds the configuration for Azure DevOps (ADO) pipelines.
// It includes the ADO organization URL, project name, pipeline ID, test pipeline ID, and pipeline version.
// These fields are used to trigger ADO pipelines and are required for the correct operation of the pipelines.
Expand All @@ -65,6 +77,7 @@ type TimelineTest struct {
// ADOPipelineID: The ID of the ADO oci-image-builder pipeline.
// ADOTestPipelineID: The ID of the ADO test pipeline.
// ADOPipelineVersion: The version of the ADO pipeline.
// ADORequestStrategy: Strategy for retrying failed requests to ADO API
type Config struct {
// ADO organization URL to call for triggering ADO pipeline
ADOOrganizationURL string `yaml:"ado-organization-url" json:"ado-organization-url"`
Expand All @@ -74,6 +87,8 @@ type Config struct {
ADOPipelineID int `yaml:"ado-pipeline-id" json:"ado-pipeline-id"`
// ADO pipeline version to call for triggering ADO pipeline
ADOPipelineVersion int `yaml:"ado-pipeline-version,omitempty" json:"ado-pipeline-version,omitempty"`
// ADO Retry strategy for requests
ADORetryStrategy RetryStrategy `yaml:"ado-retry-strategy" json:"ado-retry-strategy"`
}

func (c Config) GetADOConfig() Config {
Expand Down Expand Up @@ -135,8 +150,8 @@ func GetRunResult(ctx context.Context, adoClient Client, adoConfig Config, pipel
})
return pipelineRun, err
},
retry.Attempts(3),
retry.Delay(5*time.Second),
retry.Attempts(adoConfig.ADORetryStrategy.Attempts),
retry.Delay(adoConfig.ADORetryStrategy.Delay),
)
if err != nil {
return nil, fmt.Errorf("failed getting ADO pipeline run, err: %w", err)
Expand Down Expand Up @@ -168,16 +183,16 @@ func GetRunResult(ctx context.Context, adoClient Client, adoConfig Config, pipel
func GetRunLogs(ctx context.Context, buildClient BuildClient, httpClient HTTPClient, adoConfig Config, pipelineRunID *int, adoPAT string) (string, error) {
// Fetch the build logs metadata for the pipeline run. If an error occurs, retry three times with a delay of 5 seconds between each retry.
// We get the pipeline run status over network, so we need to handle network errors.
buildLogs, err := retry.DoWithData[*[]build.BuildLog](
buildLogs, err := retry.DoWithData(
func() (*[]build.BuildLog, error) {
buildLogs, err := buildClient.GetBuildLogs(ctx, build.GetBuildLogsArgs{
Project: &adoConfig.ADOProjectName,
BuildId: pipelineRunID,
})
return buildLogs, err
},
retry.Attempts(3),
retry.Delay(5*time.Second),
retry.Attempts(adoConfig.ADORetryStrategy.Attempts),
retry.Delay(adoConfig.ADORetryStrategy.Delay),
)
if err != nil {
return "", fmt.Errorf("failed getting build logs metadata, err: %w", err)
Expand All @@ -198,8 +213,8 @@ func GetRunLogs(ctx context.Context, buildClient BuildClient, httpClient HTTPCli
func() (*http.Response, error) {
return httpClient.Do(req)
},
retry.Attempts(3),
retry.Delay(5*time.Second),
retry.Attempts(adoConfig.ADORetryStrategy.Attempts),
retry.Delay(adoConfig.ADORetryStrategy.Delay),
)
if err != nil {
return "", fmt.Errorf("failed http request getting build log, err: %w", err)
Expand Down
4 changes: 4 additions & 0 deletions pkg/azuredevops/pipelines/pipelines_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ var _ = Describe("Pipelines", func() {
ADOProjectName: "example-project",
ADOPipelineID: 123,
ADOPipelineVersion: 1,
ADORetryStrategy: pipelines.RetryStrategy{
Attempts: 3,
Delay: 5 * time.Second,
},
}
})

Expand Down

0 comments on commit 9e32235

Please sign in to comment.