-
Notifications
You must be signed in to change notification settings - Fork 89
/
resource_argocd_application.go
219 lines (208 loc) · 7.2 KB
/
resource_argocd_application.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package argocd
import (
"context"
"fmt"
applicationClient "github.com/argoproj/argo-cd/pkg/apiclient/application"
application "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
"github.com/argoproj/gitops-engine/pkg/health"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"strings"
"time"
)
func resourceArgoCDApplication() *schema.Resource {
return &schema.Resource{
Create: resourceArgoCDApplicationCreate,
Read: resourceArgoCDApplicationRead,
Update: resourceArgoCDApplicationUpdate,
Delete: resourceArgoCDApplicationDelete,
// TODO: add importer acceptance tests
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"metadata": metadataSchema("applications.argoproj.io"),
"spec": applicationSpecSchema(),
"wait": {
Type: schema.TypeBool,
Description: "Upon application creation or update, wait for application health/sync status to be healthy/Synced, upon application deletion, wait for application to be removed, when set to true.",
Optional: true,
Default: false,
},
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(5 * time.Minute),
Update: schema.DefaultTimeout(5 * time.Minute),
Delete: schema.DefaultTimeout(5 * time.Minute),
},
}
}
func resourceArgoCDApplicationCreate(d *schema.ResourceData, meta interface{}) error {
objectMeta, spec, err := expandApplication(d)
if err != nil {
return err
}
server := meta.(ServerInterface)
c := *server.ApplicationClient
app, err := c.Get(context.Background(), &applicationClient.ApplicationQuery{
Name: &objectMeta.Name,
})
if err != nil {
switch strings.Contains(err.Error(), "NotFound") {
case true:
default:
return err
}
}
if app != nil {
switch app.DeletionTimestamp {
case nil:
default:
// Pre-existing app is still in Kubernetes soft deletion queue
time.Sleep(time.Duration(*app.DeletionGracePeriodSeconds))
}
}
featureApplicationLevelSyncOptionsSupported, err := server.isFeatureSupported(featureApplicationLevelSyncOptions)
if err != nil {
return err
}
if !featureApplicationLevelSyncOptionsSupported &&
spec.SyncPolicy != nil &&
spec.SyncPolicy.SyncOptions != nil {
return fmt.Errorf(
"application-level sync_options is only supported from ArgoCD %s onwards",
featureVersionConstraintsMap[featureApplicationLevelSyncOptions].String())
}
app, err = c.Create(context.Background(), &applicationClient.ApplicationCreateRequest{
Application: application.Application{
ObjectMeta: objectMeta,
Spec: spec,
},
})
if err != nil {
return err
}
if app == nil {
return fmt.Errorf("something went wrong during application creation")
}
d.SetId(app.Name)
if wait, ok := d.GetOk("wait"); ok && wait.(bool) {
return resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
a, err := c.Get(context.Background(), &applicationClient.ApplicationQuery{
Name: &app.Name,
})
if err != nil {
return resource.NonRetryableError(fmt.Errorf("error while waiting for application %s to be synced and healthy: %s", app.Name, err))
}
if a.Status.Health.Status != health.HealthStatusHealthy {
return resource.RetryableError(fmt.Errorf("expected application health status to be healthy but was %s", a.Status.Health.Status))
}
if a.Status.Sync.Status != application.SyncStatusCodeSynced {
return resource.RetryableError(fmt.Errorf("expected application sync status to be synced but was %s", a.Status.Sync.Status))
}
return resource.NonRetryableError(resourceArgoCDApplicationRead(d, meta))
})
}
return resourceArgoCDApplicationRead(d, meta)
}
func resourceArgoCDApplicationRead(d *schema.ResourceData, meta interface{}) error {
server := meta.(ServerInterface)
c := *server.ApplicationClient
appName := d.Id()
app, err := c.Get(context.Background(), &applicationClient.ApplicationQuery{
Name: &appName,
})
if err != nil {
switch strings.Contains(err.Error(), "NotFound") {
case true:
d.SetId("")
return nil
default:
return err
}
}
err = flattenApplication(app, d)
return err
}
func resourceArgoCDApplicationUpdate(d *schema.ResourceData, meta interface{}) error {
appName := d.Id()
if ok := d.HasChanges("metadata", "spec"); ok {
objectMeta, spec, err := expandApplication(d)
if err != nil {
return err
}
server := meta.(ServerInterface)
c := *server.ApplicationClient
appRequest := &applicationClient.ApplicationUpdateRequest{
Application: &application.Application{
ObjectMeta: objectMeta,
Spec: spec,
}}
featureApplicationLevelSyncOptionsSupported, err := server.isFeatureSupported(featureApplicationLevelSyncOptions)
if err != nil {
return err
}
if !featureApplicationLevelSyncOptionsSupported &&
spec.SyncPolicy != nil &&
spec.SyncPolicy.SyncOptions != nil {
return fmt.Errorf(
"application-level sync_options is only supported from ArgoCD %s onwards",
featureVersionConstraintsMap[featureApplicationLevelSyncOptions].String())
}
app, err := c.Get(context.Background(), &applicationClient.ApplicationQuery{
Name: &appName,
})
if app != nil {
// Kubernetes API requires providing the up-to-date correct ResourceVersion for updates
appRequest.Application.ResourceVersion = app.ResourceVersion
}
_, err = c.Update(context.Background(), appRequest)
if err != nil {
return err
}
if wait, _ok := d.GetOk("wait"); _ok && wait.(bool) {
return resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
a, err := c.Get(context.Background(), &applicationClient.ApplicationQuery{
Name: &app.Name,
})
if err != nil {
return resource.NonRetryableError(fmt.Errorf("error while waiting for application %s to be synced and healthy: %s", app.Name, err))
}
if a.Status.Health.Status != health.HealthStatusHealthy {
return resource.RetryableError(fmt.Errorf("expected application health status to be healthy but was %s", a.Status.Health.Status))
}
if a.Status.Sync.Status != application.SyncStatusCodeSynced {
return resource.RetryableError(fmt.Errorf("expected application sync status to be synced but was %s", a.Status.Sync.Status))
}
return resource.NonRetryableError(resourceArgoCDApplicationRead(d, meta))
})
}
}
return resourceArgoCDApplicationRead(d, meta)
}
func resourceArgoCDApplicationDelete(d *schema.ResourceData, meta interface{}) error {
server := meta.(ServerInterface)
c := *server.ApplicationClient
appName := d.Id()
_, err := c.Delete(context.Background(), &applicationClient.ApplicationDeleteRequest{Name: &appName})
if err != nil && !strings.Contains(err.Error(), "NotFound") {
return err
}
if wait, ok := d.GetOk("wait"); ok && wait.(bool) {
return resource.Retry(d.Timeout(schema.TimeoutDelete), func() *resource.RetryError {
_, err = c.Get(context.Background(), &applicationClient.ApplicationQuery{
Name: &appName,
})
if err == nil {
return resource.RetryableError(fmt.Errorf("application %s is still present", appName))
}
if !strings.Contains(err.Error(), "NotFound") {
return resource.NonRetryableError(err)
}
d.SetId("")
return nil
})
}
d.SetId("")
return nil
}