Skip to content

Commit 9503821

Browse files
authored
argocd_project: handles orphaned_resources/ignore, signature_keys (#50)
1 parent 5fb1481 commit 9503821

File tree

6 files changed

+154
-15
lines changed

6 files changed

+154
-15
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
strategy:
1515
fail-fast: false
1616
matrix:
17-
argocd_version: ["v.1.8.3", "v1.7.11", "v1.6.2"]
17+
argocd_version: ["v.1.8.3", "v1.7.11"]
1818
steps:
1919
- uses: actions/checkout@v2
2020
- uses: actions/setup-go@v1

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,19 @@ resource "argocd_project" "myproject" {
138138
group = "networking.k8s.io"
139139
kind = "Ingress"
140140
}
141-
orphaned_resources = {
141+
orphaned_resources {
142142
warn = true
143+
144+
ignore {
145+
group = "apps/v1"
146+
kind = "Deployment"
147+
name = "ignored1"
148+
}
149+
ignore {
150+
group = "apps/v1"
151+
kind = "Deployment"
152+
name = "ignored2"
153+
}
143154
}
144155
role {
145156
name = "testrole"
@@ -173,6 +184,10 @@ resource "argocd_project" "myproject" {
173184
schedule = "22 1 5 * *"
174185
manual_sync = false
175186
}
187+
signature_keys = [
188+
"4AEE18F83AFDEB23",
189+
"07E34825A909B250"
190+
]
176191
}
177192
}
178193

argocd/resource_argocd_project_test.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,18 @@ resource "argocd_project" "simple" {
151151
group = "networking.k8s.io"
152152
kind = "Ingress"
153153
}
154-
orphaned_resources = {
154+
orphaned_resources {
155155
warn = true
156+
ignore {
157+
group = "apps/v1"
158+
kind = "Deployment"
159+
name = "ignored1"
160+
}
161+
ignore {
162+
group = "apps/v1"
163+
kind = "Deployment"
164+
name = "ignored2"
165+
}
156166
}
157167
sync_window {
158168
kind = "allow"
@@ -172,6 +182,10 @@ resource "argocd_project" "simple" {
172182
schedule = "22 1 5 * *"
173183
manual_sync = false
174184
}
185+
signature_keys = [
186+
"4AEE18F83AFDEB23",
187+
"07E34825A909B250"
188+
]
175189
}
176190
}
177191
`, name)

argocd/schema_project.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,38 @@ func projectSpecSchema() *schema.Schema {
7070
},
7171
},
7272
"orphaned_resources": {
73-
Type: schema.TypeMap,
73+
Type: schema.TypeSet,
7474
Optional: true,
75-
Elem: &schema.Schema{Type: schema.TypeBool},
75+
MaxItems: 1,
76+
Elem: &schema.Resource{
77+
Schema: map[string]*schema.Schema{
78+
"warn": {
79+
Type: schema.TypeBool,
80+
Optional: true,
81+
},
82+
"ignore": {
83+
Type: schema.TypeSet,
84+
Optional: true,
85+
Elem: &schema.Resource{
86+
Schema: map[string]*schema.Schema{
87+
"group": {
88+
Type: schema.TypeString,
89+
ValidateFunc: validateGroupName,
90+
Optional: true,
91+
},
92+
"kind": {
93+
Type: schema.TypeString,
94+
Optional: true,
95+
},
96+
"name": {
97+
Type: schema.TypeString,
98+
Optional: true,
99+
},
100+
},
101+
},
102+
},
103+
},
104+
},
76105
},
77106
"role": {
78107
Type: schema.TypeList,
@@ -106,6 +135,11 @@ func projectSpecSchema() *schema.Schema {
106135
Required: true,
107136
Elem: &schema.Schema{Type: schema.TypeString},
108137
},
138+
"signature_keys": {
139+
Type: schema.TypeList,
140+
Optional: true,
141+
Elem: &schema.Schema{Type: schema.TypeString},
142+
},
109143
"sync_window": {
110144
Type: schema.TypeList,
111145
Optional: true,

argocd/structure_project.go

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,24 @@ func expandProjectSpec(d *schema.ResourceData) (
5555
spec.SourceRepos = append(spec.SourceRepos, sr.(string))
5656
}
5757
}
58+
if v, ok := s["signature_keys"]; ok {
59+
for _, sk := range v.([]interface{}) {
60+
spec.SignatureKeys = append(spec.SignatureKeys, application.SignatureKey{
61+
KeyID: sk.(string),
62+
})
63+
}
64+
}
5865
if v, ok := s["orphaned_resources"]; ok {
59-
if _warn, ok := v.(map[string]interface{})["warn"]; ok {
60-
warn := _warn.(bool)
61-
spec.OrphanedResources = &application.OrphanedResourcesMonitorSettings{
62-
Warn: &warn,
66+
spec.OrphanedResources = &application.OrphanedResourcesMonitorSettings{}
67+
orphanedResources := v.(*schema.Set).List()
68+
if len(orphanedResources) > 0 {
69+
if _warn, _ok := orphanedResources[0].(map[string]interface{})["warn"]; _ok {
70+
warn := _warn.(bool)
71+
spec.OrphanedResources.Warn = &warn
72+
}
73+
if _ignore, _ok := orphanedResources[0].(map[string]interface{})["ignore"]; _ok {
74+
ignore := expandOrphanedResourcesIgnore(_ignore.(*schema.Set))
75+
spec.OrphanedResources.Ignore = ignore
6376
}
6477
}
6578
}
@@ -91,6 +104,19 @@ func expandProjectSpec(d *schema.ResourceData) (
91104
return spec, nil
92105
}
93106

107+
func expandOrphanedResourcesIgnore(ignore *schema.Set) (
108+
result []application.OrphanedResourceKey) {
109+
for _, _i := range ignore.List() {
110+
i := _i.(map[string]interface{})
111+
result = append(result, application.OrphanedResourceKey{
112+
Group: i["group"].(string),
113+
Kind: i["kind"].(string),
114+
Name: i["name"].(string),
115+
})
116+
}
117+
return
118+
}
119+
94120
// Flatten
95121

96122
func flattenProject(p *application.AppProject, d *schema.ResourceData) error {
@@ -118,16 +144,42 @@ func flattenProjectSpec(s application.AppProjectSpec) []map[string]interface{} {
118144
"sync_window": flattenSyncWindows(s.SyncWindows),
119145
"description": s.Description,
120146
"source_repos": s.SourceRepos,
147+
"signature_keys": flattenProjectSignatureKeys(s.SignatureKeys),
121148
}
122149
return []map[string]interface{}{spec}
123150
}
124151

152+
func flattenProjectSignatureKeys(keys []application.SignatureKey) (
153+
result []string) {
154+
for _, key := range keys {
155+
result = append(result, key.KeyID)
156+
}
157+
return
158+
}
159+
125160
func flattenProjectOrphanedResources(ors *application.OrphanedResourcesMonitorSettings) (
126-
result map[string]bool) {
161+
result []map[string]interface{}) {
162+
r := make(map[string]interface{}, 0)
127163
if ors != nil {
128-
result = map[string]bool{
129-
"warn": *ors.Warn,
164+
if ors.Warn != nil {
165+
r["warn"] = *ors.Warn
130166
}
167+
if ors.Ignore != nil {
168+
r["ignore"] = flattenProjectOrphanedResourcesIgnore(ors.Ignore)
169+
result = append(result, r)
170+
}
171+
}
172+
return
173+
}
174+
175+
func flattenProjectOrphanedResourcesIgnore(ignore []application.OrphanedResourceKey) (
176+
result []map[string]string) {
177+
for _, i := range ignore {
178+
result = append(result, map[string]string{
179+
"group": i.Group,
180+
"kind": i.Kind,
181+
"name": i.Name,
182+
})
131183
}
132184
return
133185
}

docs/resources/project.md

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,20 @@ resource "argocd_project" "myproject" {
4545
group = "networking.k8s.io"
4646
kind = "Ingress"
4747
}
48-
orphaned_resources = {
48+
orphaned_resources {
4949
warn = true
50+
51+
ignore {
52+
group = "apps/v1"
53+
kind = "Deployment"
54+
name = "ignored1"
55+
}
56+
57+
ignore {
58+
group = "apps/v1"
59+
kind = "Deployment"
60+
name = "ignored2"
61+
}
5062
}
5163
role {
5264
name = "testrole"
@@ -80,6 +92,10 @@ resource "argocd_project" "myproject" {
8092
schedule = "22 1 5 * *"
8193
manual_sync = false
8294
}
95+
signature_keys = [
96+
"4AEE18F83AFDEB23",
97+
"07E34825A909B250"
98+
]
8399
}
84100
}
85101
@@ -106,13 +122,20 @@ The `spec` block can have the following attributes:
106122
* `namespace_resource_blacklist` - (Optional) Namespaced-scoped resources allowed to be managed by the project applications, can be repeated multiple times.
107123
* `role` - (Optional) can be repeated multiple times.
108124
* `sync_window` - (Optional) can be repeated multiple times.
125+
* `signature_keys` - (Optional) list of PGP key IDs strings that commits to be synced to must be signed with.
109126

110127
Each `cluster_resource_whitelist` block can have the following attributes:
111128
* `group` - (Optional) The Kubernetes resource Group to match for.
112129
* `kind` - (Optional) The Kubernetes resource Kind to match for.
113130

114-
The `orphaned_resources` map can have the following attributes:
115-
* `warn` - Boolean, defaults to `false`.
131+
The `orphaned_resources` block can have the following attributes:
132+
* `warn` - (Optional) Boolean, defaults to `false`.
133+
* `ignore` - (Optional), set of map of strings, specifies which Group/Kind/Name resource(s) to ignore. Can be repeated multiple times. Structure is documented below.
134+
135+
Each `orphaned_resources/ignore` block can have the following attributes:
136+
* `group` - (Optional) The Kubernetes resource Group to match for.
137+
* `kind` - (Optional) The Kubernetes resource Kind to match for.
138+
* `name` - (Optional) The Kubernetes resource name to match for.
116139

117140
Each `namespace_resource_blacklist` block can have the following attributes:
118141
* `group` - (Optional) The Kubernetes resource Group to match for.
@@ -133,6 +156,7 @@ Each `sync_window` block can have the following attributes:
133156
* `namespaces` - (Optional) List of namespaces that the window will apply to.
134157
* `schedule` - (Optional) Time the window will begin, specified in cron format.
135158

159+
136160
## Import
137161

138162
ArgoCD projects can be imported using an id consisting of `{name}`, e.g.

0 commit comments

Comments
 (0)