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 all 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
8 changes: 8 additions & 0 deletions argocd/resource_argocd_application_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func resourceArgoCDApplicationSetCreate(ctx context.Context, d *schema.ResourceD
return featureNotSupported(features.ApplicationSetProgressiveSync)
}

if !si.IsFeatureSupported(features.ApplicationSetApplicationsSyncPolicy) && spec.SyncPolicy != nil && spec.SyncPolicy.ApplicationsSync != nil {
return featureNotSupported(features.ApplicationSetApplicationsSyncPolicy)
}

as, err := si.ApplicationSetClient.Create(ctx, &applicationset.ApplicationSetCreateRequest{
Applicationset: &application.ApplicationSet{
ObjectMeta: objectMeta,
Expand Down Expand Up @@ -127,6 +131,10 @@ func resourceArgoCDApplicationSetUpdate(ctx context.Context, d *schema.ResourceD
return featureNotSupported(features.ApplicationSetProgressiveSync)
}

if !si.IsFeatureSupported(features.ApplicationSetApplicationsSyncPolicy) && spec.SyncPolicy != nil && spec.SyncPolicy.ApplicationsSync != nil {
return featureNotSupported(features.ApplicationSetApplicationsSyncPolicy)
}

_, err = si.ApplicationSetClient.Create(ctx, &applicationset.ApplicationSetCreateRequest{
Applicationset: &application.ApplicationSet{
ObjectMeta: objectMeta,
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
31 changes: 28 additions & 3 deletions argocd/structure_application_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -768,10 +768,34 @@ 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{}) (assp *application.ApplicationSetSyncPolicy) {
assp = &application.ApplicationSetSyncPolicy{}

if v, ok := sp["applications_sync"].(string); ok && len(v) > 0 {
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 +1461,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