Skip to content

Commit

Permalink
argocd_application: added retry configuration block to sync policy (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
oboukili committed Jan 23, 2021
1 parent 56bc0a7 commit 3c8ab5c
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 5 deletions.
28 changes: 28 additions & 0 deletions argocd/resource_argocd_application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,26 @@ ingress:
"spec.0.sync_policy.0.automated.self_heal",
"true",
),
resource.TestCheckResourceAttr(
"argocd_application.sync_policy",
"spec.0.sync_policy.0.retry.0.backoff.duration",
"30s",
),
resource.TestCheckResourceAttr(
"argocd_application.sync_policy",
"spec.0.sync_policy.0.retry.0.backoff.max_duration",
"2m",
),
resource.TestCheckResourceAttr(
"argocd_application.sync_policy",
"spec.0.sync_policy.0.retry.0.backoff.factor",
"2",
),
resource.TestCheckResourceAttr(
"argocd_application.sync_policy",
"spec.0.sync_policy.0.retry.0.limit",
"5",
),
),
},
{
Expand Down Expand Up @@ -354,6 +374,14 @@ resource "argocd_application" "sync_policy" {
prune = true
self_heal = true
}
retry {
limit = "5"
backoff = {
duration = "30s"
max_duration = "2m"
factor = "2"
}
}
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions argocd/schema_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,26 @@ func applicationSpecSchema() *schema.Schema {
// TODO: add a validator
},
},
"retry": {
Type: schema.TypeList,
MinItems: 1,
MaxItems: 1,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"limit": {
Type: schema.TypeString,
Description: "Max number of allowed sync retries, as a string",
Optional: true,
},
"backoff": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
},
},
},
Expand Down
64 changes: 59 additions & 5 deletions argocd/structure_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ func expandApplicationSpec(d *schema.ResourceData) (
spec.IgnoreDifferences = expandApplicationIgnoreDifferences(v.([]interface{}))
}
if v, ok := s["sync_policy"]; ok {
spec.SyncPolicy = expandApplicationSyncPolicy(v.([]interface{}))
spec.SyncPolicy, err = expandApplicationSyncPolicy(v.([]interface{}))
}
if v, ok := s["destination"]; ok {
spec.Destination = expandApplicationDestination(v.(*schema.Set).List()[0])
}
if v, ok := s["source"]; ok {
spec.Source = expandApplicationSource(v.([]interface{})[0])
}
return spec, nil
return spec, err
}

func expandApplicationSource(_as interface{}) (
Expand Down Expand Up @@ -244,13 +244,15 @@ func expandApplicationSourceHelm(in []interface{}) *application.ApplicationSourc
return result
}

func expandApplicationSyncPolicy(_sp []interface{}) *application.SyncPolicy {
func expandApplicationSyncPolicy(_sp []interface{}) (*application.SyncPolicy, error) {
if len(_sp) == 0 {
return nil
return nil, nil
}
sp := _sp[0]
var automated = &application.SyncPolicyAutomated{}
var syncOptions application.SyncOptions
var retry = &application.RetryStrategy{}
var err error

if a, ok := sp.(map[string]interface{})["automated"]; ok {
for k, v := range a.(map[string]interface{}) {
Expand All @@ -268,10 +270,43 @@ func expandApplicationSyncPolicy(_sp []interface{}) *application.SyncPolicy {
syncOptions = append(syncOptions, sOpt.(string))
}
}
if _retry, ok := sp.(map[string]interface{})["retry"].([]interface{}); ok {
if len(_retry) > 0 {
r := _retry[0]
for k, v := range r.(map[string]interface{}) {
if k == "limit" {
retry.Limit, err = convertStringToInt64(v.(string))
if err != nil {
return nil, err
}
}
if k == "backoff" {
retry.Backoff = &application.Backoff{}
for kb, vb := range v.(map[string]interface{}) {
if kb == "duration" {
retry.Backoff.Duration = vb.(string)
}
if kb == "max_duration" {
retry.Backoff.MaxDuration = vb.(string)
}
if kb == "factor" {
var pFactor int64
pFactor, err = convertStringToInt64(vb.(string))
if err != nil {
return nil, fmt.Errorf("%s: not a valid int64: %s", kb, vb.(string))
}
retry.Backoff.Factor = &pFactor
}
}
}
}
}
}
return &application.SyncPolicy{
Automated: automated,
SyncOptions: syncOptions,
}
Retry: retry,
}, nil
}

func expandApplicationIgnoreDifferences(ids []interface{}) (
Expand Down Expand Up @@ -370,13 +405,32 @@ func flattenApplicationSyncPolicy(sp *application.SyncPolicy) []map[string]inter
return nil
}
result := make(map[string]interface{}, 0)
backoff := make(map[string]string, 0)
if sp.Automated != nil {
result["automated"] = map[string]bool{
"prune": sp.Automated.Prune,
"self_heal": sp.Automated.SelfHeal,
}
}
result["sync_options"] = []string(sp.SyncOptions)
if sp.Retry != nil {
limit := convertInt64ToString(sp.Retry.Limit)
if sp.Retry.Backoff != nil {
backoff = map[string]string{
"duration": sp.Retry.Backoff.Duration,
"max_duration": sp.Retry.Backoff.MaxDuration,
}
if sp.Retry.Backoff.Factor != nil {
backoff["factor"] = convertInt64PointerToString(sp.Retry.Backoff.Factor)
}
}
result["retry"] = []map[string]interface{}{
{
"limit": limit,
"backoff": backoff,
},
}
}
return []map[string]interface{}{result}
}

Expand Down
4 changes: 4 additions & 0 deletions argocd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ func convertInt64ToString(i int64) string {
return strconv.FormatInt(i, 10)
}

func convertInt64PointerToString(i *int64) string {
return strconv.FormatInt(*i, 10)
}

func isKeyInMap(key string, d map[string]interface{}) bool {
if d == nil {
return false
Expand Down
10 changes: 10 additions & 0 deletions docs/resources/application.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,21 @@ The `destination` block has the following attributes:
The `sync_policy` block has the following attributes:
* `automated` - (Optional) map(string) of strings, will keep an application synced to the target revision. Structure is documented below
* `sync_options` - (Optional) list of sync options, allow you to specify whole app sync-options (only available from ArgoCD 1.5.0 onwards).
* `retry` - (Optional) controls failed sync retry behavior, structure is documented below

The `sync_policy/automated` map has the following attributes:
* `prune` - (Optional), boolean, will prune resources automatically as part of automated sync. Defaults to `false`.
* `self_heal` - (Optional), boolean, enables auto-syncing if the live resources differ from the targeted revision manifests. Defaults to `false`.

The `sync_policy/retry` block has the following attributes:
* `limit` - (Optional), max number of allowed sync retries, as a string.
* `backoff` - (Optional), retry backoff strategy, structure is documented below

The `sync_policy/retry/backoff` map has the following attributes:
* `duration` - (Optional), Duration is the amount to back off. Default unit is seconds, but could also be a duration (e.g. "2m", "1h"), as a string.
* `factor` - (Optional), Factor is a factor to multiply the base duration after each failed retry, as a string.
* `max_duration` - (Optional), is the maximum amount of time allowed for the backoff strategy. Default unit is seconds, but could also be a duration (e.g. "2m", "1h"), as a string.

Each `ignore_difference` block can have the following attributes:
* `group` - (Optional) The targeted Kubernetes resource kind.
* `kind` - (Optional) The targeted Kubernetes resource kind.
Expand Down

0 comments on commit 3c8ab5c

Please sign in to comment.