Skip to content

Commit 59bb5dc

Browse files
support jq path expressions in ignore_difference block (argoproj-labs#108)
* Add support for jq path expressions in ignore_differences block * Proper tf formatting * Remove single quotes * Fixes for dev/ci environments * Add jq path expressions version contraint, skip acceptance test if not supported
1 parent eff2e35 commit 59bb5dc

13 files changed

+196
-25
lines changed

.github/workflows/tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
strategy:
1515
fail-fast: false
1616
matrix:
17-
argocd_version: ["v2.1.2", "v2.0.5", "v.1.8.7"]
17+
argocd_version: ["v2.1.3", "v2.0.5", "v1.8.7"]
1818
steps:
1919
- uses: actions/checkout@v2
2020
- uses: actions/setup-go@v1
@@ -42,8 +42,8 @@ jobs:
4242
- name: Set up ArgoCD ${{ matrix.argocd_version }}
4343
env:
4444
ARGOCD_VERSION: ${{ matrix.argocd_version }}
45+
ARGOCD_CI: true
4546
run: |
46-
curl https://raw.githubusercontent.com/argoproj/argo-cd/${ARGOCD_VERSION}/manifests/install.yaml > manifests/install/install.yml
4747
sh scripts/testacc_prepare_env.sh
4848
until $(nc -z 127.0.0.1 8080); do sleep 2;done
4949
netstat -tulpn

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
terraform-provider-argocd
2-
./manifests/install/install.yml
2+
/manifests/install/argocd.yml
3+
/bin
34
.idea
45
.vscode
56
# Env variables settings

argocd/features.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ import (
2020

2121
const (
2222
featureApplicationLevelSyncOptions = iota
23+
featureIgnoreDiffJQPathExpressions
2324
featureRepositoryGet
2425
featureTokenIDs
2526
)
2627

2728
var (
2829
featureVersionConstraintsMap = map[int]*semver.Version{
2930
featureApplicationLevelSyncOptions: semver.MustParse("1.5.0"),
31+
featureIgnoreDiffJQPathExpressions: semver.MustParse("2.1.0"),
3032
featureRepositoryGet: semver.MustParse("1.6.0"),
3133
featureTokenIDs: semver.MustParse("1.5.3"),
3234
}

argocd/resource_argocd_application.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,36 @@ func resourceArgoCDApplicationCreate(ctx context.Context, d *schema.ResourceData
104104
}
105105
}
106106

107+
featureIgnoreDiffJQPathExpressionsSupported, err := server.isFeatureSupported(featureIgnoreDiffJQPathExpressions)
108+
if err != nil {
109+
return []diag.Diagnostic{
110+
{
111+
Severity: diag.Error,
112+
Summary: "feature not supported",
113+
Detail: err.Error(),
114+
},
115+
}
116+
}
117+
hasJQPathExpressions := false
118+
if spec.IgnoreDifferences != nil {
119+
for _, id := range spec.IgnoreDifferences {
120+
if id.JQPathExpressions != nil {
121+
hasJQPathExpressions = true
122+
}
123+
}
124+
}
125+
if !featureIgnoreDiffJQPathExpressionsSupported && hasJQPathExpressions {
126+
return []diag.Diagnostic{
127+
{
128+
Severity: diag.Error,
129+
Summary: fmt.Sprintf(
130+
"jq path expressions are only supported from ArgoCD %s onwards",
131+
featureVersionConstraintsMap[featureIgnoreDiffJQPathExpressions].String()),
132+
Detail: err.Error(),
133+
},
134+
}
135+
}
136+
107137
app, err = c.Create(ctx, &applicationClient.ApplicationCreateRequest{
108138
Application: application.Application{
109139

@@ -256,6 +286,36 @@ func resourceArgoCDApplicationUpdate(ctx context.Context, d *schema.ResourceData
256286
}
257287
}
258288

289+
featureIgnoreDiffJQPathExpressionsSupported, err := server.isFeatureSupported(featureIgnoreDiffJQPathExpressions)
290+
if err != nil {
291+
return []diag.Diagnostic{
292+
{
293+
Severity: diag.Error,
294+
Summary: "feature not supported",
295+
Detail: err.Error(),
296+
},
297+
}
298+
}
299+
hasJQPathExpressions := false
300+
if spec.IgnoreDifferences != nil {
301+
for _, id := range spec.IgnoreDifferences {
302+
if id.JQPathExpressions != nil {
303+
hasJQPathExpressions = true
304+
}
305+
}
306+
}
307+
if !featureIgnoreDiffJQPathExpressionsSupported && hasJQPathExpressions {
308+
return []diag.Diagnostic{
309+
{
310+
Severity: diag.Error,
311+
Summary: fmt.Sprintf(
312+
"jq path expressions are only supported from ArgoCD %s onwards",
313+
featureVersionConstraintsMap[featureIgnoreDiffJQPathExpressions].String()),
314+
Detail: err.Error(),
315+
},
316+
}
317+
}
318+
259319
app, err := c.Get(ctx, &applicationClient.ApplicationQuery{
260320
Name: &appName,
261321
})

argocd/resource_argocd_application_test.go

Lines changed: 87 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package argocd
22

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

78
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
89
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
911
)
1012

1113
func TestAccArgoCDApplication(t *testing.T) {
@@ -190,6 +192,27 @@ ingress:
190192
),
191193
),
192194
},
195+
{
196+
SkipFunc: testAccSkipFeatureIgnoreDiffJQPathExpressions,
197+
Config: testAccArgoCDApplicationIgnoreDiffJQPathExpressions(
198+
acctest.RandomWithPrefix("test-acc")),
199+
Check: resource.ComposeTestCheckFunc(
200+
resource.TestCheckResourceAttrSet(
201+
"argocd_application.ignore_differences_jqpe",
202+
"metadata.0.uid",
203+
),
204+
resource.TestCheckResourceAttr(
205+
"argocd_application.ignore_differences_jqpe",
206+
"spec.0.ignore_difference.0.jq_path_expressions.0",
207+
".spec.replicas",
208+
),
209+
resource.TestCheckResourceAttr(
210+
"argocd_application.ignore_differences_jqpe",
211+
"spec.0.ignore_difference.1.jq_path_expressions.1",
212+
".spec.template.spec.metadata.labels.somelabel",
213+
),
214+
),
215+
},
193216
},
194217
})
195218
}
@@ -482,9 +505,9 @@ resource "argocd_application" "ignore_differences" {
482505
}
483506
484507
ignore_difference {
485-
group = "apps"
486-
kind = "Deployment"
487-
json_pointers = ["/spec/replicas"]
508+
group = "apps"
509+
kind = "Deployment"
510+
json_pointers = ["/spec/replicas"]
488511
}
489512
490513
ignore_difference {
@@ -500,3 +523,64 @@ resource "argocd_application" "ignore_differences" {
500523
}
501524
`, name)
502525
}
526+
527+
func testAccArgoCDApplicationIgnoreDiffJQPathExpressions(name string) string {
528+
return fmt.Sprintf(`
529+
resource "argocd_application" "ignore_differences_jqpe" {
530+
metadata {
531+
name = "%s"
532+
namespace = "argocd"
533+
labels = {
534+
acceptance = "true"
535+
}
536+
}
537+
538+
spec {
539+
source {
540+
repo_url = "https://charts.bitnami.com/bitnami"
541+
chart = "redis"
542+
target_revision = "15.3.0"
543+
}
544+
545+
destination {
546+
server = "https://kubernetes.default.svc"
547+
namespace = "default"
548+
}
549+
550+
ignore_difference {
551+
group = "apps"
552+
kind = "Deployment"
553+
jq_path_expressions = [".spec.replicas"]
554+
}
555+
556+
ignore_difference {
557+
group = "apps"
558+
kind = "StatefulSet"
559+
name = "someStatefulSet"
560+
jq_path_expressions = [
561+
".spec.replicas",
562+
".spec.template.spec.metadata.labels.somelabel",
563+
]
564+
}
565+
}
566+
}
567+
`, name)
568+
}
569+
570+
func testAccSkipFeatureIgnoreDiffJQPathExpressions() (bool, error) {
571+
p, _ := testAccProviders["argocd"]()
572+
_ = p.Configure(context.Background(), &terraform.ResourceConfig{})
573+
server := p.Meta().(*ServerInterface)
574+
err := server.initClients()
575+
if err != nil {
576+
return false, err
577+
}
578+
featureSupported, err := server.isFeatureSupported(featureIgnoreDiffJQPathExpressions)
579+
if err != nil {
580+
return false, err
581+
}
582+
if !featureSupported {
583+
return true, nil
584+
}
585+
return false, nil
586+
}

argocd/schema_application.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,14 @@ func applicationSpecSchema() *schema.Schema {
362362
Type: schema.TypeString,
363363
},
364364
},
365+
"jq_path_expressions": {
366+
Type: schema.TypeSet,
367+
Set: schema.HashString,
368+
Optional: true,
369+
Elem: &schema.Schema{
370+
Type: schema.TypeString,
371+
},
372+
},
365373
},
366374
},
367375
},

argocd/structure_application.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,12 @@ func expandApplicationIgnoreDifferences(ids []interface{}) (
354354
elem.JSONPointers = append(elem.JSONPointers, jp.(string))
355355
}
356356
}
357+
if v, ok := id["jq_path_expressions"]; ok {
358+
jqpes := v.(*schema.Set).List()
359+
for _, jqpe := range jqpes {
360+
elem.JQPathExpressions = append(elem.JQPathExpressions, jqpe.(string))
361+
}
362+
}
357363
result = append(result, elem)
358364
}
359365
return
@@ -473,11 +479,12 @@ func flattenApplicationIgnoreDifferences(ids []application.ResourceIgnoreDiffere
473479
result []map[string]interface{}) {
474480
for _, id := range ids {
475481
result = append(result, map[string]interface{}{
476-
"group": id.Group,
477-
"kind": id.Kind,
478-
"name": id.Name,
479-
"namespace": id.Namespace,
480-
"json_pointers": id.JSONPointers,
482+
"group": id.Group,
483+
"kind": id.Kind,
484+
"name": id.Name,
485+
"namespace": id.Namespace,
486+
"json_pointers": id.JSONPointers,
487+
"jq_path_expressions": id.JQPathExpressions,
481488
})
482489
}
483490
return

docs/resources/application.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ resource "argocd_application" "kustomize" {
6969
"/spec/replicas",
7070
"/spec/template/spec/metadata/labels/bar",
7171
]
72+
# Only available from ArgoCD 2.1.0 onwards
73+
jq_path_expressions = [
74+
".spec.replicas",
75+
".spec.template.spec.metadata.labels.bar",
76+
]
7277
}
7378
}
7479
}
@@ -174,6 +179,7 @@ Each `ignore_difference` block can have the following attributes:
174179
* `name` - (Optional) The targeted Kubernetes resource name.
175180
* `namespace` - (Optional) The targeted Kubernetes resource namespace.
176181
* `json_pointers` - (Optional) List of JSONPaths strings targeting the field(s) to ignore.
182+
* `jq_path_expressions` - (Optional) List of jq path expression strings targeting the field(s) to ignore (only available from ArgoCD 2.1.0 onwards).
177183

178184
The `source` block has the following attributes:
179185
* `repo_url` - (Required) string, repository URL of the application manifests.

manifests/install/git-private-repository.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,4 @@ spec:
109109
- port: 22
110110
targetPort: sshd
111111
name: sshd
112-
protocol: TCP
112+
protocol: TCP

manifests/install/kustomization.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ kind: Kustomization
44
namespace: argocd
55
resources:
66
- namespace.yml
7-
- https://raw.githubusercontent.com/argoproj/argo-cd/v1.8.3/manifests/install.yaml
7+
- argocd.yml
88
- git-private-repository.yml
99
- proxy-service.yml
1010
patchesStrategicMerge:

0 commit comments

Comments
 (0)