Skip to content

Commit 2475548

Browse files
authored
feat: Support ApplicationSets in any Namespace (#636)
Signed-off-by: VerhovnikU <[email protected]>
1 parent 7bd8a0d commit 2475548

File tree

4 files changed

+118
-10
lines changed

4 files changed

+118
-10
lines changed

argocd/resource_argocd_application_set.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func resourceArgoCDApplicationSetCreate(ctx context.Context, d *schema.ResourceD
8383
}
8484
}
8585

86-
d.SetId(as.Name)
86+
d.SetId(fmt.Sprintf("%s:%s", as.Name, objectMeta.Namespace))
8787

8888
return resourceArgoCDApplicationSetRead(ctx, d, meta)
8989
}
@@ -94,23 +94,26 @@ func resourceArgoCDApplicationSetRead(ctx context.Context, d *schema.ResourceDat
9494
return pluginSDKDiags(diags)
9595
}
9696

97-
name := d.Id()
97+
ids := strings.Split(d.Id(), ":")
98+
appSetName := ids[0]
99+
namespace := ids[1]
98100

99101
appSet, err := si.ApplicationSetClient.Get(ctx, &applicationset.ApplicationSetGetQuery{
100-
Name: name,
102+
Name: appSetName,
103+
AppsetNamespace: namespace,
101104
})
102105
if err != nil {
103106
if strings.Contains(err.Error(), "NotFound") {
104107
d.SetId("")
105108
return diag.Diagnostics{}
106109
}
107110

108-
return argoCDAPIError("read", "application set", name, err)
111+
return argoCDAPIError("read", "application set", appSetName, err)
109112
}
110113

111114
err = flattenApplicationSet(appSet, d)
112115
if err != nil {
113-
return errorToDiagnostics(fmt.Sprintf("failed to flatten application set %s", name), err)
116+
return errorToDiagnostics(fmt.Sprintf("failed to flatten application set %s", appSetName), err)
114117
}
115118

116119
return nil
@@ -172,12 +175,15 @@ func resourceArgoCDApplicationSetDelete(ctx context.Context, d *schema.ResourceD
172175
return pluginSDKDiags(diags)
173176
}
174177

175-
_, err := si.ApplicationSetClient.Delete(ctx, &applicationset.ApplicationSetDeleteRequest{
176-
Name: d.Id(),
177-
})
178+
ids := strings.Split(d.Id(), ":")
179+
appSetName := ids[0]
180+
namespace := ids[1]
178181

179-
if err != nil && !strings.Contains(err.Error(), "NotFound") {
180-
return argoCDAPIError("delete", "application set", d.Id(), err)
182+
if _, err := si.ApplicationSetClient.Delete(ctx, &applicationset.ApplicationSetDeleteRequest{
183+
Name: appSetName,
184+
AppsetNamespace: namespace,
185+
}); err != nil && !strings.Contains(err.Error(), "NotFound") {
186+
return argoCDAPIError("delete", "application set", appSetName, err)
181187
}
182188

183189
d.SetId("")

argocd/resource_argocd_application_set_test.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package argocd
22

33
import (
4+
"fmt"
45
"regexp"
56
"testing"
67

78
"github.com/argoproj-labs/terraform-provider-argocd/internal/features"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
810
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
911
)
1012

@@ -1042,6 +1044,32 @@ func TestAccArgoCDApplicationSet_templatePatch(t *testing.T) {
10421044
})
10431045
}
10441046

1047+
func TestAccArgoCDApplicationSet_CustomNamespace(t *testing.T) {
1048+
name := acctest.RandomWithPrefix("appset-ns")
1049+
1050+
resource.ParallelTest(t, resource.TestCase{
1051+
PreCheck: func() { testAccPreCheck(t); testAccPreCheckFeatureSupported(t, features.ApplicationSet) },
1052+
ProviderFactories: testAccProviders,
1053+
Steps: []resource.TestStep{
1054+
{
1055+
Config: testAccArgoCDApplicationSetCustomNamespace(name),
1056+
Check: resource.ComposeTestCheckFunc(
1057+
resource.TestCheckResourceAttrSet(
1058+
"argocd_application_set.custom_namespace",
1059+
"metadata.0.uid",
1060+
),
1061+
),
1062+
},
1063+
{
1064+
ResourceName: "argocd_application_set.custom_namespace",
1065+
ImportState: true,
1066+
ImportStateVerify: true,
1067+
ImportStateVerifyIgnore: []string{"wait", "cascade", "status", "validate"},
1068+
},
1069+
},
1070+
})
1071+
}
1072+
10451073
func testAccArgoCDApplicationSet_clusters() string {
10461074
return `
10471075
resource "argocd_application_set" "clusters" {
@@ -3202,3 +3230,74 @@ resource "argocd_application_set" "template_patch" {
32023230
}
32033231
}`
32043232
}
3233+
3234+
func testAccArgoCDApplicationSetCustomNamespace(name string) string {
3235+
return fmt.Sprintf(`
3236+
resource "argocd_project" "custom_namespace" {
3237+
metadata {
3238+
name = "%[1]s"
3239+
namespace = "argocd"
3240+
}
3241+
3242+
spec {
3243+
description = "project with source namespace"
3244+
source_repos = ["*"]
3245+
source_namespaces = ["mynamespace-1"]
3246+
3247+
destination {
3248+
server = "https://kubernetes.default.svc"
3249+
namespace = "default"
3250+
}
3251+
}
3252+
}
3253+
3254+
resource "argocd_application_set" "custom_namespace" {
3255+
metadata {
3256+
name = "%[1]s"
3257+
namespace = "mynamespace-1"
3258+
}
3259+
3260+
spec {
3261+
generator {
3262+
list {
3263+
elements = [
3264+
{
3265+
cluster = "in-cluster"
3266+
}
3267+
]
3268+
}
3269+
}
3270+
3271+
template {
3272+
metadata {
3273+
name = "test"
3274+
}
3275+
spec {
3276+
project = argocd_project.custom_namespace.metadata[0].name
3277+
source {
3278+
repo_url = "https://raw.githubusercontent.com/bitnami/charts/archive-full-index/bitnami"
3279+
chart = "redis"
3280+
target_revision = "16.9.11"
3281+
helm {
3282+
parameter {
3283+
name = "image.tag"
3284+
value = "6.2.5"
3285+
}
3286+
parameter {
3287+
name = "architecture"
3288+
value = "standalone"
3289+
}
3290+
release_name = "testing"
3291+
}
3292+
}
3293+
3294+
destination {
3295+
server = "https://kubernetes.default.svc"
3296+
namespace = "mynamespace-1"
3297+
}
3298+
}
3299+
}
3300+
}
3301+
}
3302+
`, name)
3303+
}

manifests/install/cluster-rbac.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ rules:
1616
- argoproj.io
1717
resources:
1818
- applications
19+
- applicationsets
1920
---
2021
apiVersion: rbac.authorization.k8s.io/v1
2122
kind: ClusterRoleBinding

manifests/install/patches/argocd-cmd-params-cm.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ metadata:
44
name: argocd-cmd-params-cm
55
data:
66
application.namespaces: mynamespace-*
7+
applicationsetcontroller.enable.scm.providers: "false"
8+
applicationsetcontroller.namespaces: "*"

0 commit comments

Comments
 (0)