-
Notifications
You must be signed in to change notification settings - Fork 52
/
resource_kustomization.go
445 lines (362 loc) · 11.5 KB
/
resource_kustomization.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
package kustomize
import (
"context"
"errors"
"fmt"
"strings"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
k8scorev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
k8smeta "k8s.io/apimachinery/pkg/api/meta"
k8smetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sschema "k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"
)
func kustomizationResource() *schema.Resource {
return &schema.Resource{
Create: kustomizationResourceCreate,
Read: kustomizationResourceRead,
Exists: kustomizationResourceExists,
Update: kustomizationResourceUpdate,
Delete: kustomizationResourceDelete,
CustomizeDiff: kustomizationResourceDiff,
Importer: &schema.ResourceImporter{
State: kustomizationResourceImport,
},
Schema: map[string]*schema.Schema{
"manifest": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"wait": &schema.Schema{
Type: schema.TypeBool,
Default: false,
Optional: true,
},
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(5 * time.Minute),
Update: schema.DefaultTimeout(5 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},
}
}
func kustomizationResourceCreate(d *schema.ResourceData, m interface{}) error {
mapper := m.(*Config).Mapper
client := m.(*Config).Client
km := newKManifest(mapper, client)
err := km.load([]byte(d.Get("manifest").(string)))
if err != nil {
return logError(err)
}
// required for CRDs
err = km.waitKind(d.Timeout(schema.TimeoutCreate))
if err != nil {
return logError(err)
}
// required for namespaced resources
err = km.waitNamespace(d.Timeout(schema.TimeoutCreate))
if err != nil {
return logError(err)
}
// for secrets of type service account token
// wait for service account to exist
// https://github.com/kubernetes/kubernetes/issues/109401
if (km.gvk().Kind == "Secret") &&
(km.resource.UnstructuredContent()["type"] != nil) &&
(km.resource.UnstructuredContent()["type"].(string) == string(k8scorev1.SecretTypeServiceAccountToken)) {
annotations := km.resource.GetAnnotations()
for k, v := range annotations {
if k == k8scorev1.ServiceAccountNameKey {
saGvk := k8sschema.GroupVersionKind{
Group: "",
Version: "v1",
Kind: "ServiceAccount"}
mapping, err := mapper.RESTMapping(saGvk.GroupKind(), saGvk.GroupVersion().Version)
if err != nil {
return logError(km.fmtErr(
fmt.Errorf("api error: %q: %s", saGvk.String(), err),
))
}
_, err = waitForGVKCreated(d, client, mapping, km.namespace(), v)
if err != nil {
return logError(km.fmtErr(fmt.Errorf("timed out waiting for: %q: %s", km.id().string(), err)))
}
}
}
}
gzipLastAppliedConfig := m.(*Config).GzipLastAppliedConfig
setLastAppliedConfig(km, gzipLastAppliedConfig)
resp, err := km.apiCreate(k8smetav1.CreateOptions{})
if err != nil {
return logError(err)
}
if d.Get("wait").(bool) {
if err = km.waitCreatedOrUpdated(d.Timeout(schema.TimeoutCreate)); err != nil {
return logError(err)
}
}
id := string(resp.GetUID())
d.SetId(id)
d.Set("manifest", getLastAppliedConfig(resp, gzipLastAppliedConfig))
return kustomizationResourceRead(d, m)
}
func kustomizationResourceRead(d *schema.ResourceData, m interface{}) error {
km := newKManifest(m.(*Config).Mapper, m.(*Config).Client)
err := km.load([]byte(d.Get("manifest").(string)))
if err != nil {
return logError(err)
}
resp, err := km.apiGet(k8smetav1.GetOptions{})
if err != nil {
return logError(err)
}
id := string(resp.GetUID())
d.SetId(id)
d.Set("manifest", getLastAppliedConfig(resp, m.(*Config).GzipLastAppliedConfig))
return nil
}
func kustomizationResourceExists(d *schema.ResourceData, m interface{}) (bool, error) {
km := newKManifest(m.(*Config).Mapper, m.(*Config).Client)
err := km.load([]byte(d.Get("manifest").(string)))
if err != nil {
return false, logError(err)
}
err = km.waitKind(d.Timeout(schema.TimeoutCreate))
if err != nil {
if k8smeta.IsNoMatchError(err) {
// If the Kind does not exist in the K8s API,
// the resource can't exist either
return false, nil
}
return false, logError(err)
}
_, err = km.apiGet(k8smetav1.GetOptions{})
if err != nil {
if k8serrors.IsNotFound(err) {
return false, nil
}
return false, logError(err)
}
return true, nil
}
func kustomizationResourceDiff(ctx context.Context, d *schema.ResourceDiff, m interface{}) error {
if !d.HasChange("manifest") {
return nil
}
client := m.(*Config).Client
mapper := m.(*Config).Mapper
gzipLastAppliedConfig := m.(*Config).GzipLastAppliedConfig
do, dm := d.GetChange("manifest")
kmm := newKManifest(mapper, client)
err := kmm.load([]byte(dm.(string)))
if err != nil {
return logError(err)
}
setLastAppliedConfig(kmm, gzipLastAppliedConfig)
_, err = kmm.mappings()
if err != nil {
// if there are no mappings we can't dry-run
// this is for CRDs that do not exist yet
return nil
}
isNamespaced, err := kmm.isNamespaced()
if err != nil {
return logError(err)
}
if isNamespaced && kmm.namespace() == "" {
err = kmm.fmtErr(fmt.Errorf("is namespace scoped and must set metadata.namespace"))
return logError(err)
}
if !isNamespaced && kmm.namespace() != "" {
err = kmm.fmtErr(fmt.Errorf("is not namespace scoped but has metadata.namespace set"))
return logError(err)
}
if do.(string) == "" {
// diffing for create
_, err = kmm.apiCreate(k8smetav1.CreateOptions{DryRun: []string{k8smetav1.DryRunAll}})
if err != nil {
if k8serrors.IsAlreadyExists(err) {
// this is an edge case during tests
// get change above has empty original
// yet the create request fails with
// Error running pre-apply refresh
return nil
}
if k8serrors.IsNotFound(err) {
// we're dry-running a create
// the notfound seems mostly the namespace
return nil
}
return logError(kmm.fmtErr(err))
}
return nil
}
// diffing for update
kmo := newKManifest(mapper, client)
err = kmo.load([]byte(do.(string)))
if err != nil {
return logError(err)
}
setLastAppliedConfig(kmo, gzipLastAppliedConfig)
if kmo.name() != kmm.name() || kmo.namespace() != kmm.namespace() {
// if the resource name or namespace changes, we can't patch but have to destroy and re-create
d.ForceNew("manifest")
return nil
}
pt, p, err := kmm.apiPreparePatch(kmo, true)
if err != nil {
return logError(err)
}
dryRunPatch := k8smetav1.PatchOptions{DryRun: []string{k8smetav1.DryRunAll}}
_, err = kmm.apiPatch(pt, p, dryRunPatch)
if err != nil {
// Handle specific invalid errors
if k8serrors.IsInvalid(err) {
as := err.(k8serrors.APIStatus).Status()
// ForceNew only when exact single cause
if len(as.Details.Causes) == 1 {
msg := as.Details.Causes[0].Message
// if cause is immutable field force a delete and re-create plan
if k8serrors.HasStatusCause(err, k8smetav1.CauseTypeFieldValueInvalid) && strings.HasSuffix(msg, ": field is immutable") == true {
d.ForceNew("manifest")
return nil
}
// if cause is statefulset forbidden fields error force a delete and re-create plan
if k8serrors.HasStatusCause(err, k8smetav1.CauseType(field.ErrorTypeForbidden)) && strings.HasPrefix(msg, "Forbidden: updates to statefulset spec for fields") == true {
d.ForceNew("manifest")
return nil
}
// if cause is cannot change roleRef force a delete and re-create plan
if k8serrors.HasStatusCause(err, k8smetav1.CauseTypeFieldValueInvalid) && strings.HasSuffix(msg, ": cannot change roleRef") == true {
d.ForceNew("manifest")
return nil
}
// if cause is updates to storage class provisioner or parameters are forbidden force a delete and re-create plan
if k8serrors.HasStatusCause(err, k8smetav1.CauseType(field.ErrorTypeForbidden)) {
if strings.HasSuffix(msg, ": updates to provisioner are forbidden.") || strings.HasPrefix(msg, "Forbidden: updates to parameters are forbidden") {
d.ForceNew("manifest")
return nil
}
}
}
}
return logError(err)
}
return nil
}
func kustomizationResourceUpdate(d *schema.ResourceData, m interface{}) error {
client := m.(*Config).Client
mapper := m.(*Config).Mapper
gzipLastAppliedConfig := m.(*Config).GzipLastAppliedConfig
do, dm := d.GetChange("manifest")
kmo := newKManifest(mapper, client)
err := kmo.load([]byte(do.(string)))
if err != nil {
return logError(err)
}
kmm := newKManifest(mapper, client)
err = kmm.load([]byte(dm.(string)))
if err != nil {
return logError(err)
}
if !d.HasChange("manifest") && !d.HasChange("wait") {
return logError(kmm.fmtErr(
errors.New("update called without diff"),
))
}
setLastAppliedConfig(kmo, gzipLastAppliedConfig)
setLastAppliedConfig(kmm, gzipLastAppliedConfig)
pt, p, err := kmm.apiPreparePatch(kmo, false)
if err != nil {
return logError(err)
}
resp, err := kmm.apiPatch(pt, p, k8smetav1.PatchOptions{})
if err != nil {
return logError(err)
}
if d.Get("wait").(bool) {
if err = kmm.waitCreatedOrUpdated(d.Timeout(schema.TimeoutUpdate)); err != nil {
return logError(err)
}
}
id := string(resp.GetUID())
d.SetId(id)
d.Set("manifest", getLastAppliedConfig(resp, gzipLastAppliedConfig))
return kustomizationResourceRead(d, m)
}
func kustomizationResourceDelete(d *schema.ResourceData, m interface{}) error {
client := m.(*Config).Client
mapper := m.(*Config).Mapper
km := newKManifest(mapper, client)
err := parseResourceData(km, d.Get("manifest").(string))
if err != nil {
return logError(err)
}
// look for all versions of the GroupKind in case the resource uses a
// version that is no longer current
_, err = km.mappings()
if err != nil {
if k8smeta.IsNoMatchError(err) {
// If the Kind does not exist in the K8s API,
// the resource can't exist either
return nil
}
return logError(km.fmtErr(err))
}
err = km.apiDelete(k8smetav1.DeleteOptions{})
if err != nil {
// Consider not found during deletion a success
if k8serrors.IsNotFound(err) {
d.SetId("")
return nil
}
return logError(err)
}
err = km.waitDeleted(d.Timeout(schema.TimeoutDelete))
if err != nil {
return logError(err)
}
d.SetId("")
return nil
}
func kustomizationResourceImport(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
client := m.(*Config).Client
mapper := m.(*Config).Mapper
gzipLastAppliedConfig := m.(*Config).GzipLastAppliedConfig
k, err := parseProviderId(d.Id())
if err != nil {
return nil, logError(err)
}
gk := k8sschema.GroupKind{Group: k.group, Kind: k.kind}
// We don't need to use a specific API version here, as we're going to store the
// resource using the LastAppliedConfig information which we can get from any
// API version
mappings, err := mapper.RESTMappings(gk)
if err != nil {
return nil, logError(
fmt.Errorf("api error \"%s/%s/%s/%s\": %s", gk.Group, gk.Kind, k.namespace, k.name, err),
)
}
resp, err := client.
Resource(mappings[0].Resource).
Namespace(k.namespace).
Get(context.TODO(), k.name, k8smetav1.GetOptions{})
if err != nil {
return nil, logError(
fmt.Errorf("\"%s/%s/%s/%s\": %s", gk.Group, gk.Kind, k.namespace, k.name, err),
)
}
id := string(resp.GetUID())
d.SetId(id)
lac := getLastAppliedConfig(resp, gzipLastAppliedConfig)
if lac == "" {
return nil, logError(
fmt.Errorf("\"%s/%s/%s/%s\": can not import resources without %q or %q annotation", gk.Group, gk.Kind, k.namespace, k.name, lastAppliedConfigAnnotation, gzipLastAppliedConfigAnnotation),
)
}
d.Set("manifest", lac)
d.Set("wait", d.Get("wait"))
return []*schema.ResourceData{d}, nil
}