Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add applications sync to application set sync policies #337

Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ARGOCD_INSECURE?=true
ARGOCD_SERVER?=127.0.0.1:8080
ARGOCD_AUTH_USERNAME?=admin
ARGOCD_AUTH_PASSWORD?=acceptancetesting
ARGOCD_VERSION?=v2.7.1
ARGOCD_VERSION?=v2.8.3

export

Expand Down
4 changes: 2 additions & 2 deletions argocd/resource_argocd_application_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func resourceArgoCDApplicationSetCreate(ctx context.Context, d *schema.ResourceD
return featureNotSupported(features.ApplicationSet)
}

objectMeta, spec, err := expandApplicationSet(d, si.IsFeatureSupported(features.MultipleApplicationSources))
objectMeta, spec, err := expandApplicationSet(d, si.IsFeatureSupported(features.MultipleApplicationSources), si.IsFeatureSupported(features.ApplicationSetApplicationsSyncPolicy))
if err != nil {
return errorToDiagnostics("failed to expand application set", err)
}
Expand Down Expand Up @@ -118,7 +118,7 @@ func resourceArgoCDApplicationSetUpdate(ctx context.Context, d *schema.ResourceD
return nil
}

objectMeta, spec, err := expandApplicationSet(d, si.IsFeatureSupported(features.MultipleApplicationSources))
objectMeta, spec, err := expandApplicationSet(d, si.IsFeatureSupported(features.MultipleApplicationSources), si.IsFeatureSupported(features.ApplicationSetApplicationsSyncPolicy))
if err != nil {
return errorToDiagnostics(fmt.Sprintf("failed to expand application set %s", d.Id()), err)
}
Expand Down
76 changes: 76 additions & 0 deletions argocd/resource_argocd_application_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,43 @@ func TestAccArgoCDApplicationSet_syncPolicy(t *testing.T) {
})
}

func TestAccArgoCDApplicationSet_syncPolicyWithApplicationsSyncPolicy(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckFeatureSupported(t, features.ApplicationSetApplicationsSyncPolicy)
},
ProviderFactories: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccArgoCDApplicationSet_syncPolicyWithApplicationsSync(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(
"argocd_application_set.applications_sync_policy",
"metadata.0.uid",
),
resource.TestCheckResourceAttr(
"argocd_application_set.applications_sync_policy",
"spec.0.sync_policy.0.preserve_resources_on_deletion",
"true",
),
resource.TestCheckResourceAttr(
"argocd_application_set.applications_sync_policy",
"spec.0.sync_policy.0.applications_sync",
"create-update",
),
),
},
{
ResourceName: "argocd_application_set.applications_sync_policy",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"metadata.0.resource_version"},
},
},
})
}

func TestAccArgoCDApplicationSet_progressiveSync(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckFeatureSupported(t, features.ApplicationSetProgressiveSync) },
Expand Down Expand Up @@ -2745,6 +2782,45 @@ resource "argocd_application_set" "sync_policy" {
}`
}

func testAccArgoCDApplicationSet_syncPolicyWithApplicationsSync() string {
return `
resource "argocd_application_set" "applications_sync_policy" {
metadata {
name = "applications-sync-policy"
}

spec {
generator {
clusters {} # Automatically use all clusters defined within Argo CD
}

sync_policy {
preserve_resources_on_deletion = true
applications_sync = "create-update"
}

template {
metadata {
name = "appset-sync-policy-{{name}}"
}

spec {
source {
repo_url = "https://github.com/argoproj/argocd-example-apps/"
target_revision = "HEAD"
path = "guestbook"
}

destination {
server = "{{server}}"
namespace = "default"
}
}
}
}
}`
}

func testAccArgoCDApplicationSet_progressiveSync() string {
return `
resource "argocd_application_set" "progressive_sync" {
Expand Down
5 changes: 5 additions & 0 deletions argocd/schema_application_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ func applicationSetSpecSchemaV0() *schema.Schema {
Description: "Label selector used to narrow the scope of targeted clusters.",
Optional: true,
},
"applications_sync": {
Type: schema.TypeString,
Description: "Represents the policy applied on the generated applications. Possible values are create-only, create-update, create-delete, and sync.",
Optional: true,
},
},
},
},
Expand Down
40 changes: 33 additions & 7 deletions argocd/structure_application_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import (
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func expandApplicationSet(d *schema.ResourceData, featureMultipleApplicationSourcesSupported bool) (metadata meta.ObjectMeta, spec application.ApplicationSetSpec, err error) {
func expandApplicationSet(d *schema.ResourceData, featureMultipleApplicationSourcesSupported bool, featureApplicationSetApplicationsSyncPolicy bool) (metadata meta.ObjectMeta, spec application.ApplicationSetSpec, err error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realise this is a bit confusing, but can you please drop this parameter (and it's usage in this file) and rather use the feature flag to implement a check on the user's provided configuration as per https://github.com/oboukili/terraform-provider-argocd/blob/d84d09101f8c1954cf613df69890a846540692b7/argocd/resource_argocd_application_set.go#L49-L51.

In essence, this is the pattern used by all other feature flags - we check the user's configuration of the Terraform resource and return an error if they try to configure something via Terraform that isn't supported by their version of ArgoCD (thereby preventing the configuration from being applied to the resulting CRUD call the provider makes). Multiple applications sources is the exception here in that we support versions that both have and do not have the feature and thus, need branching logic in what we set on the CRUD call.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, thanks for the explanation! I've removed this parameter and added a feature flag check in the resource.

metadata = expandMetadata(d)
spec, err = expandApplicationSetSpec(d, featureMultipleApplicationSourcesSupported)
spec, err = expandApplicationSetSpec(d, featureMultipleApplicationSourcesSupported, featureApplicationSetApplicationsSyncPolicy)

return
}

func expandApplicationSetSpec(d *schema.ResourceData, featureMultipleApplicationSourcesSupported bool) (spec application.ApplicationSetSpec, err error) {
func expandApplicationSetSpec(d *schema.ResourceData, featureMultipleApplicationSourcesSupported bool, featureApplicationSetApplicationsSyncPolicy bool) (spec application.ApplicationSetSpec, err error) {
s := d.Get("spec.0").(map[string]interface{})

if v, ok := s["generator"].([]interface{}); ok && len(v) > 0 {
Expand All @@ -39,7 +39,7 @@ func expandApplicationSetSpec(d *schema.ResourceData, featureMultipleApplication
}

if v, ok := s["sync_policy"].([]interface{}); ok && len(v) > 0 {
spec.SyncPolicy = expandApplicationSetSyncPolicy(v[0].(map[string]interface{}))
spec.SyncPolicy = expandApplicationSetSyncPolicy(v[0].(map[string]interface{}), featureApplicationSetApplicationsSyncPolicy)
}

if v, ok := s["template"].([]interface{}); ok && len(v) > 0 {
Expand Down Expand Up @@ -768,10 +768,35 @@ func expandApplicationMatchExpressions(mes []interface{}) []application.Applicat
return asrss
}

func expandApplicationSetSyncPolicy(sp map[string]interface{}) *application.ApplicationSetSyncPolicy {
return &application.ApplicationSetSyncPolicy{
PreserveResourcesOnDeletion: sp["preserve_resources_on_deletion"].(bool),
func expandApplicationSetSyncPolicyApplicationsSyncPolicy(p string) (asp application.ApplicationsSyncPolicy) {
switch {
case p == "create-only":
asp = application.ApplicationsSyncPolicyCreateOnly
case p == "create-update":
asp = application.ApplicationsSyncPolicyCreateUpdate
case p == "create-delete":
asp = application.ApplicationsSyncPolicyCreateDelete
case p == "sync":
asp = application.ApplicationsSyncPolicyCreateDelete
}

return asp
}

func expandApplicationSetSyncPolicy(sp map[string]interface{}, featureApplicationSetApplicationsSyncPolicy bool) (assp *application.ApplicationSetSyncPolicy) {
assp = &application.ApplicationSetSyncPolicy{}

if v, ok := sp["applications_sync"].(string); ok && len(v) > 0 && featureApplicationSetApplicationsSyncPolicy {
asp := expandApplicationSetSyncPolicyApplicationsSyncPolicy(v)

assp.ApplicationsSync = &asp
}

if v, ok := sp["preserve_resources_on_deletion"]; ok {
assp.PreserveResourcesOnDeletion = v.(bool)
}

return assp
}

func expandApplicationSetTemplate(temp interface{}, featureMultipleApplicationSourcesSupported bool) (template application.ApplicationSetTemplate, err error) {
Expand Down Expand Up @@ -1437,6 +1462,7 @@ func flattenApplicationMatchExpression(in []application.ApplicationMatchExpressi
func flattenApplicationSetSyncPolicy(assp application.ApplicationSetSyncPolicy) []map[string]interface{} {
p := map[string]interface{}{
"preserve_resources_on_deletion": assp.PreserveResourcesOnDeletion,
"applications_sync": assp.ApplicationsSync,
}

return []map[string]interface{}{p}
Expand Down
1 change: 1 addition & 0 deletions docs/resources/application_set.md
Original file line number Diff line number Diff line change
Expand Up @@ -15568,4 +15568,5 @@ Optional:

Optional:

- `applications_sync` (String) Represents the policy applied on the generated applications. Possible values are create-only, create-update, create-delete, and sync.
- `preserve_resources_on_deletion` (Boolean) Label selector used to narrow the scope of targeted clusters.
Loading