-
Notifications
You must be signed in to change notification settings - Fork 107
/
resource_kubectl_manifest.go
960 lines (819 loc) · 32.7 KB
/
resource_kubectl_manifest.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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
package kubernetes
import (
"context"
"crypto/sha256"
"fmt"
"github.com/gavinbunney/terraform-provider-kubectl/flatten"
"github.com/gavinbunney/terraform-provider-kubectl/yaml"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"io/ioutil"
"k8s.io/cli-runtime/pkg/printers"
"k8s.io/kubectl/pkg/validation"
"os"
"sort"
"time"
"log"
"strings"
"k8s.io/cli-runtime/pkg/genericclioptions"
k8sresource "k8s.io/cli-runtime/pkg/resource"
apiregistration "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1"
"k8s.io/kubectl/pkg/cmd/apply"
k8sdelete "k8s.io/kubectl/pkg/cmd/delete"
backoff "github.com/cenkalti/backoff/v4"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
apps_v1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/api/errors"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
meta_v1_unstruct "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
k8sschema "k8s.io/apimachinery/pkg/runtime/schema"
yamlWriter "sigs.k8s.io/yaml"
"k8s.io/client-go/discovery"
"k8s.io/client-go/dynamic"
)
const (
// https://github.com/kubernetes/kubernetes/blob/master/pkg/controller/deployment/util/deployment_util.go#L93
TimedOutReason = "ProgressDeadlineExceeded"
)
func resourceKubectlManifest() *schema.Resource {
return &schema.Resource{
CreateContext: func(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
exponentialBackoffConfig := backoff.NewExponentialBackOff()
exponentialBackoffConfig.InitialInterval = 3 * time.Second
exponentialBackoffConfig.MaxInterval = 30 * time.Second
if kubectlApplyRetryCount > 0 {
retryConfig := backoff.WithMaxRetries(exponentialBackoffConfig, kubectlApplyRetryCount)
retryErr := backoff.Retry(func() error {
err := resourceKubectlManifestApply(ctx, d, meta)
if err != nil {
log.Printf("[ERROR] creating manifest failed: %+v", err)
}
return err
}, retryConfig)
if retryErr != nil {
return diag.FromErr(retryErr)
}
return nil
} else {
if applyErr := resourceKubectlManifestApply(ctx, d, meta); applyErr != nil {
return diag.FromErr(applyErr)
}
return nil
}
},
ReadContext: func(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
if err := resourceKubectlManifestRead(ctx, d, meta); err != nil {
return diag.FromErr(err)
}
return nil
},
DeleteContext: func(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
if err := resourceKubectlManifestDelete(ctx, d, meta); err != nil {
return diag.FromErr(err)
}
return nil
},
UpdateContext: func(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
exponentialBackoffConfig := backoff.NewExponentialBackOff()
exponentialBackoffConfig.InitialInterval = 3 * time.Second
exponentialBackoffConfig.MaxInterval = 30 * time.Second
if kubectlApplyRetryCount > 0 {
retryConfig := backoff.WithMaxRetries(exponentialBackoffConfig, kubectlApplyRetryCount)
retryErr := backoff.Retry(func() error {
err := resourceKubectlManifestApply(ctx, d, meta)
if err != nil {
log.Printf("[ERROR] updating manifest failed: %+v", err)
}
return err
}, retryConfig)
if retryErr != nil {
return diag.FromErr(retryErr)
}
return nil
} else {
if applyErr := resourceKubectlManifestApply(ctx, d, meta); applyErr != nil {
return diag.FromErr(applyErr)
}
return nil
}
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
},
Importer: &schema.ResourceImporter{
StateContext: func(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
idParts := strings.Split(d.Id(), "//")
if len(idParts) != 3 && len(idParts) != 4 {
return []*schema.ResourceData{}, fmt.Errorf("expected ID in format apiVersion//kind//name//namespace, received: %s", d.Id())
}
apiVersion := idParts[0]
kind := idParts[1]
name := idParts[2]
var yamlString = ""
if len(idParts) == 4 {
yamlString = fmt.Sprintf(`
apiVersion: %s
kind: %s
metadata:
namespace: %s
name: %s
`, apiVersion, kind, idParts[3], name)
} else {
yamlString = fmt.Sprintf(`
apiVersion: %s
kind: %s
metadata:
name: %s
`, apiVersion, kind, name)
}
manifest, err := yaml.ParseYAML(yamlString)
if err != nil {
return nil, err
}
restClient := getRestClientFromUnstructured(manifest, meta.(*KubeProvider))
if restClient.Error != nil {
return []*schema.ResourceData{}, fmt.Errorf("failed to create kubernetes rest client for import of resource: %s %s %s %+v %s %s", apiVersion, kind, name, restClient.Error, yamlString, manifest.Raw)
}
// Get the resource from Kubernetes
metaObjLiveRaw, err := restClient.ResourceInterface.Get(ctx, manifest.GetName(), meta_v1.GetOptions{})
if err != nil {
return []*schema.ResourceData{}, fmt.Errorf("failed to get resource %s %s %s from kubernetes: %+v", apiVersion, kind, name, err)
}
if metaObjLiveRaw.GetUID() == "" {
return []*schema.ResourceData{}, fmt.Errorf("failed to parse item and get UUID: %+v", metaObjLiveRaw)
}
metaObjLive := yaml.NewFromUnstructured(metaObjLiveRaw)
// Capture the UID from the cluster at the current time
_ = d.Set("uid", metaObjLive.GetUID())
_ = d.Set("live_uid", metaObjLive.GetUID())
liveManifestFingerprint := getLiveManifestFingerprint(d, metaObjLive, metaObjLive)
_ = d.Set("yaml_incluster", liveManifestFingerprint)
_ = d.Set("live_manifest_incluster", liveManifestFingerprint)
// set fields captured normally during creation/updates
d.SetId(metaObjLive.GetSelfLink())
_ = d.Set("api_version", metaObjLive.GetAPIVersion())
_ = d.Set("kind", metaObjLive.GetKind())
_ = d.Set("namespace", metaObjLive.GetNamespace())
_ = d.Set("name", metaObjLive.GetName())
_ = d.Set("force_new", false)
_ = d.Set("server_side_apply", false)
_ = d.Set("apply_only", false)
// clear out fields user can't set to try and get parity with yaml_body
meta_v1_unstruct.RemoveNestedField(metaObjLive.Raw.Object, "metadata", "creationTimestamp")
meta_v1_unstruct.RemoveNestedField(metaObjLive.Raw.Object, "metadata", "resourceVersion")
meta_v1_unstruct.RemoveNestedField(metaObjLive.Raw.Object, "metadata", "selfLink")
meta_v1_unstruct.RemoveNestedField(metaObjLive.Raw.Object, "metadata", "uid")
meta_v1_unstruct.RemoveNestedField(metaObjLive.Raw.Object, "metadata", "annotations", "kubectl.kubernetes.io/last-applied-configuration")
if len(metaObjLive.Raw.GetAnnotations()) == 0 {
meta_v1_unstruct.RemoveNestedField(metaObjLive.Raw.Object, "metadata", "annotations")
}
yamlParsed, err := metaObjLive.AsYAML()
if err != nil {
return []*schema.ResourceData{}, fmt.Errorf("failed to convert manifest to yaml: %+v", err)
}
_ = d.Set("yaml_body", yamlParsed)
_ = d.Set("yaml_body_parsed", yamlParsed)
return []*schema.ResourceData{d}, nil
},
},
CustomizeDiff: func(context context.Context, d *schema.ResourceDiff, meta interface{}) error {
// trigger a recreation if the yaml-body has any pending changes
if d.Get("force_new").(bool) {
_ = d.ForceNew("yaml_body")
}
if !d.NewValueKnown("yaml_body") {
log.Printf("[TRACE] yaml_body value interpolated, skipping customized diff")
return nil
}
parsedYaml, err := yaml.ParseYAML(d.Get("yaml_body").(string))
if err != nil {
return err
}
if overrideNamespace, ok := d.GetOk("override_namespace"); ok {
parsedYaml.SetNamespace(overrideNamespace.(string))
}
// set calculated fields based on parsed yaml values
_ = d.SetNew("api_version", parsedYaml.GetAPIVersion())
_ = d.SetNew("kind", parsedYaml.GetKind())
_ = d.SetNew("namespace", parsedYaml.GetNamespace())
_ = d.SetNew("name", parsedYaml.GetName())
// set the yaml_body_parsed field to provided value and obfuscate the yaml_body values manually
// this allows us to show a nice diff to the users with specific fields obfuscated, whilst storing the
// real value to apply in yaml_body
obfuscatedYaml, _ := yaml.ParseYAML(d.Get("yaml_body").(string))
if obfuscatedYaml.Raw.Object == nil {
obfuscatedYaml.Raw.Object = make(map[string]interface{})
}
if overrideNamespace, ok := d.GetOk("override_namespace"); ok {
obfuscatedYaml.SetNamespace(overrideNamespace.(string))
}
var sensitiveFields []string
sensitiveFieldsRaw, hasSensitiveFields := d.GetOk("sensitive_fields")
if hasSensitiveFields {
sensitiveFields = expandStringList(sensitiveFieldsRaw.([]interface{}))
} else if parsedYaml.GetKind() == "Secret" && parsedYaml.GetAPIVersion() == "v1" {
sensitiveFields = []string{"data"}
}
for _, s := range sensitiveFields {
fields := strings.Split(s, ".")
_, fieldExists, err := meta_v1_unstruct.NestedFieldNoCopy(obfuscatedYaml.Raw.Object, fields...)
if fieldExists {
err = meta_v1_unstruct.SetNestedField(obfuscatedYaml.Raw.Object, "(sensitive value)", fields...)
if err != nil {
return fmt.Errorf("failed to obfuscate sensitive field '%s': %+v\nNote: only map values are supported!", s, err)
}
} else {
log.Printf("[TRACE] sensitive field %s skipped does not exist", s)
}
}
obfuscatedYamlBytes, obfuscatedYamlBytesErr := yamlWriter.Marshal(obfuscatedYaml.Raw.Object)
if obfuscatedYamlBytesErr != nil {
return fmt.Errorf("failed to serialized obfuscated yaml: %+v", obfuscatedYamlBytesErr)
}
_ = d.SetNew("yaml_body_parsed", string(obfuscatedYamlBytes))
// Get the UID of the K8s resource as it was when the `resourceKubectlManifestCreate` func completed.
createdAtUID := d.Get("uid").(string)
// Get the UID of the K8s resource as it currently is in the cluster.
UID, exists := d.Get("live_uid").(string)
if !exists {
return nil
}
if UID != createdAtUID {
log.Printf("[TRACE] DETECTED %s vs %s", UID, createdAtUID)
_ = d.SetNewComputed("uid")
return nil
}
// Check that the fields specified in our YAML for diff against cluster representation
stateYaml := d.Get("yaml_incluster").(string)
liveStateYaml := d.Get("live_manifest_incluster").(string)
if stateYaml != liveStateYaml {
log.Printf("[TRACE] DETECTED YAML STATE %s vs %s", stateYaml, liveStateYaml)
_ = d.SetNewComputed("yaml_incluster")
}
return nil
},
Schema: kubectlManifestSchema,
SchemaVersion: 1,
StateUpgraders: []schema.StateUpgrader{
{
Version: 0,
Type: resourceKubectlManifestV0().CoreConfigSchema().ImpliedType(),
Upgrade: func(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
rawState["yaml_incluster"] = getFingerprint(rawState["yaml_incluster"].(string))
rawState["live_manifest_incluster"] = getFingerprint(rawState["live_manifest_incluster"].(string))
return rawState, nil
},
},
},
}
}
func resourceKubectlManifestV0() *schema.Resource {
return &schema.Resource{
Schema: kubectlManifestSchema,
}
}
var (
kubectlManifestSchema = map[string]*schema.Schema{
"uid": {
Type: schema.TypeString,
Computed: true,
},
"live_uid": {
Type: schema.TypeString,
Computed: true,
},
"yaml_incluster": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
"live_manifest_incluster": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
"api_version": {
Type: schema.TypeString,
Computed: true,
ForceNew: true,
},
"kind": {
Type: schema.TypeString,
Computed: true,
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
ForceNew: true,
},
"namespace": {
Type: schema.TypeString,
Computed: true,
ForceNew: true,
},
"override_namespace": {
Type: schema.TypeString,
Description: "Override the namespace to apply the kubernetes resource to",
Optional: true,
},
"yaml_body": {
Type: schema.TypeString,
Required: true,
Sensitive: true,
},
"yaml_body_parsed": {
Type: schema.TypeString,
Description: "Yaml body that is being applied, with sensitive values obfuscated",
Computed: true,
},
"sensitive_fields": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "List of yaml keys with sensitive values. Set these for fields which you want obfuscated in the yaml_body output",
Optional: true,
},
"force_new": {
Type: schema.TypeBool,
Description: "Default to update in-place. Setting to true will delete and create the kubernetes instead.",
Optional: true,
Default: false,
},
"server_side_apply": {
Type: schema.TypeBool,
Description: "Default to client-side-apply. Setting to true will use server-side apply.",
Optional: true,
Default: false,
},
"force_conflicts": {
Type: schema.TypeBool,
Description: "Default false.",
Optional: true,
Default: false,
},
"apply_only": {
Type: schema.TypeBool,
Description: "Apply only. In other words, it does not delete resource in any case.",
Optional: true,
Default: false,
},
"ignore_fields": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "List of yaml keys to ignore changes to. Set these for fields set by Operators or other processes in kubernetes and as such you don't want to update.",
Optional: true,
},
"wait": {
Type: schema.TypeBool,
Description: "Default to false (not waiting). Set this flag to wait or not for any deleted resources to be gone. This waits for finalizers.",
Optional: true,
},
"wait_for_rollout": {
Type: schema.TypeBool,
Description: "Default to true (waiting). Set this flag to wait or not for Deployments and APIService to complete rollout",
Optional: true,
Default: true,
},
"validate_schema": {
Type: schema.TypeBool,
Description: "Default to true (validate). Set this flag to not validate the yaml schema before appying.",
Optional: true,
Default: true,
},
}
)
func resourceKubectlManifestApply(ctx context.Context, d *schema.ResourceData, meta interface{}) error {
yamlBody := d.Get("yaml_body").(string)
manifest, err := yaml.ParseYAML(yamlBody)
if err != nil {
return fmt.Errorf("failed to parse kubernetes resource: %+v", err)
}
if overrideNamespace, ok := d.GetOk("override_namespace"); ok {
manifest.SetNamespace(overrideNamespace.(string))
}
log.Printf("[DEBUG] %v apply kubernetes resource:\n%s", manifest, yamlBody)
// Create a client to talk to the resource API based on the APIVersion and Kind
// defined in the YAML
restClient := getRestClientFromUnstructured(manifest, meta.(*KubeProvider))
if restClient.Error != nil {
return fmt.Errorf("%v failed to create kubernetes rest client for update of resource: %+v", manifest, restClient.Error)
}
// Update the resource in Kubernetes, using a temp file
yamlBody, err = manifest.AsYAML()
if err != nil {
return fmt.Errorf("%v failed to convert to yaml: %+v", manifest, err)
}
tmpfile, _ := ioutil.TempFile("", "*kubectl_manifest.yaml")
_, _ = tmpfile.Write([]byte(yamlBody))
_ = tmpfile.Close()
applyOptions := apply.NewApplyOptions(genericclioptions.IOStreams{
In: strings.NewReader(yamlBody),
Out: log.Writer(),
ErrOut: log.Writer(),
})
applyOptions.Builder = k8sresource.NewBuilder(k8sresource.RESTClientGetter(meta.(*KubeProvider)))
applyOptions.DeleteOptions = &k8sdelete.DeleteOptions{
FilenameOptions: k8sresource.FilenameOptions{
Filenames: []string{tmpfile.Name()},
},
}
applyOptions.ToPrinter = func(string) (printers.ResourcePrinter, error) {
return printers.NewDiscardingPrinter(), nil
}
if !d.Get("validate_schema").(bool) {
applyOptions.Validator = validation.NullSchema{}
}
if d.Get("server_side_apply").(bool) {
applyOptions.ServerSideApply = true
applyOptions.FieldManager = "kubectl"
}
if d.Get("force_conflicts").(bool) {
applyOptions.ForceConflicts = true
}
if manifest.HasNamespace() {
applyOptions.Namespace = manifest.GetNamespace()
}
log.Printf("[INFO] %s perform apply of manifest", manifest)
err = applyOptions.Run()
_ = os.Remove(tmpfile.Name())
if err != nil {
return fmt.Errorf("%v failed to run apply: %+v", manifest, err)
}
log.Printf("[INFO] %v manifest applied, fetch resource from kubernetes", manifest)
// get the resource from Kubernetes
rawResponse, err := restClient.ResourceInterface.Get(ctx, manifest.GetName(), meta_v1.GetOptions{})
if err != nil {
return fmt.Errorf("%v failed to fetch resource from kubernetes: %+v", manifest, err)
}
response := yaml.NewFromUnstructured(rawResponse)
d.SetId(response.GetSelfLink())
log.Printf("[DEBUG] %v fetched successfully, set id to: %v", manifest, d.Id())
// Capture the UID at time of update
// this allows us to diff these against the actual values
// read in by the 'resourceKubectlManifestRead'
_ = d.Set("uid", response.GetUID())
_ = d.Set("live_uid", response.GetUID())
liveManifestFingerprint := getLiveManifestFingerprint(d, manifest, response)
_ = d.Set("yaml_incluster", liveManifestFingerprint)
_ = d.Set("live_manifest_incluster", liveManifestFingerprint)
if d.Get("wait_for_rollout").(bool) {
timeout := d.Timeout(schema.TimeoutCreate)
if manifest.GetKind() == "Deployment" {
log.Printf("[INFO] %v waiting for deployment rollout for %vmin", manifest, timeout.Minutes())
err = resource.RetryContext(ctx, timeout,
waitForDeploymentReplicasFunc(ctx, meta.(*KubeProvider), manifest.GetNamespace(), manifest.GetName()))
if err != nil {
return err
}
} else if manifest.GetKind() == "APIService" && manifest.GetAPIVersion() == "apiregistration.k8s.io/v1" {
log.Printf("[INFO] %v waiting for APIService rollout for %vmin", manifest, timeout.Minutes())
err = resource.RetryContext(ctx, timeout,
waitForAPIServiceAvailableFunc(ctx, meta.(*KubeProvider), manifest.GetName()))
if err != nil {
return err
}
}
}
return resourceKubectlManifestReadUsingClient(ctx, d, meta, restClient.ResourceInterface, manifest)
}
func resourceKubectlManifestRead(ctx context.Context, d *schema.ResourceData, meta interface{}) error {
yamlBody := d.Get("yaml_body").(string)
manifest, err := yaml.ParseYAML(yamlBody)
if err != nil {
return fmt.Errorf("failed to parse kubernetes resource: %+v", err)
}
if overrideNamespace, ok := d.GetOk("override_namespace"); ok {
manifest.SetNamespace(overrideNamespace.(string))
}
// Create a client to talk to the resource API based on the APIVersion and Kind
// defined in the YAML
restClient := getRestClientFromUnstructured(manifest, meta.(*KubeProvider))
if restClient.Status == RestClientInvalidTypeError {
log.Printf("[WARN] kubernetes resource (%s) has an invalid type, removing from state", d.Id())
d.SetId("")
return nil
}
if restClient.Error != nil {
return fmt.Errorf("failed to create kubernetes rest client for read of resource: %+v", restClient.Error)
}
return resourceKubectlManifestReadUsingClient(ctx, d, meta, restClient.ResourceInterface, manifest)
}
func resourceKubectlManifestReadUsingClient(ctx context.Context, d *schema.ResourceData, meta interface{}, client dynamic.ResourceInterface, manifest *yaml.Manifest) error {
log.Printf("[DEBUG] %v fetch from kubernetes", manifest)
// Get the resource from Kubernetes
metaObjLiveRaw, err := client.Get(ctx, manifest.GetName(), meta_v1.GetOptions{})
resourceGone := errors.IsGone(err) || errors.IsNotFound(err)
if resourceGone {
log.Printf("[WARN] kubernetes resource (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("%v failed to get resource from kubernetes: %+v", manifest, err)
}
if metaObjLiveRaw.GetUID() == "" {
return fmt.Errorf("%v failed to parse item and get UUID: %+v", manifest, metaObjLiveRaw)
}
metaObjLive := yaml.NewFromUnstructured(metaObjLiveRaw)
// Capture the UID from the cluster at the current time
_ = d.Set("live_uid", metaObjLive.GetUID())
liveManifestFingerprint := getLiveManifestFingerprint(d, manifest, metaObjLive)
_ = d.Set("live_manifest_incluster", liveManifestFingerprint)
return nil
}
func resourceKubectlManifestDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) error {
if d.Get("apply_only").(bool) {
return nil
}
yamlBody := d.Get("yaml_body").(string)
manifest, err := yaml.ParseYAML(yamlBody)
if err != nil {
return fmt.Errorf("failed to parse kubernetes resource: %+v", err)
}
if overrideNamespace, ok := d.GetOk("override_namespace"); ok {
manifest.SetNamespace(overrideNamespace.(string))
}
log.Printf("[DEBUG] %v delete kubernetes resource:\n%s", manifest, yamlBody)
restClient := getRestClientFromUnstructured(manifest, meta.(*KubeProvider))
if restClient.Error != nil {
return fmt.Errorf("%v failed to create kubernetes rest client for delete of resource: %+v", manifest, restClient.Error)
}
log.Printf("[INFO] %s perform delete of manifest", manifest)
propagationPolicy := meta_v1.DeletePropagationBackground
waitForDelete := d.Get("wait").(bool)
if waitForDelete {
propagationPolicy = meta_v1.DeletePropagationForeground
}
err = restClient.ResourceInterface.Delete(ctx, manifest.GetName(), meta_v1.DeleteOptions{PropagationPolicy: &propagationPolicy})
resourceGone := errors.IsGone(err) || errors.IsNotFound(err)
if err != nil && !resourceGone {
return fmt.Errorf("%v failed to delete kubernetes resource: %+v", manifest, err)
}
// at the moment the foreground propagation policy does not behave as expected (it won't block waiting for deletion
// and it's up to us to check that the object has been successfully deleted.
for waitForDelete {
_, err := restClient.ResourceInterface.Get(ctx, manifest.GetName(), meta_v1.GetOptions{})
resourceGone = errors.IsGone(err) || errors.IsNotFound(err)
if err != nil {
if resourceGone {
break
}
return fmt.Errorf("%v failed to delete kubernetes resource: %+v", manifest, err)
}
log.Printf("[DEBUG] %v waiting for deletion of the resource:\n%s", manifest, yamlBody)
time.Sleep(time.Second * 10)
}
// Success remove it from state
d.SetId("")
return nil
}
type RestClientStatus int
const (
RestClientOk = iota
RestClientGenericError
RestClientInvalidTypeError
)
type RestClientResult struct {
ResourceInterface dynamic.ResourceInterface
Error error
Status RestClientStatus
}
func RestClientResultSuccess(resourceInterface dynamic.ResourceInterface) *RestClientResult {
return &RestClientResult{
ResourceInterface: resourceInterface,
Error: nil,
Status: RestClientOk,
}
}
func RestClientResultFromErr(err error) *RestClientResult {
return &RestClientResult{
ResourceInterface: nil,
Error: err,
Status: RestClientGenericError,
}
}
func RestClientResultFromInvalidTypeErr(err error) *RestClientResult {
return &RestClientResult{
ResourceInterface: nil,
Error: err,
Status: RestClientInvalidTypeError,
}
}
func getRestClientFromUnstructured(manifest *yaml.Manifest, provider *KubeProvider) *RestClientResult {
doGetRestClientFromUnstructured := func(manifest *yaml.Manifest, provider *KubeProvider) *RestClientResult {
// Use the k8s Discovery service to find all valid APIs for this cluster
discoveryClient, _ := provider.ToDiscoveryClient()
var resources []*meta_v1.APIResourceList
var err error
_, resources, err = discoveryClient.ServerGroupsAndResources()
// There is a partial failure mode here where not all groups are returned `GroupDiscoveryFailedError`
// we'll try and continue in this condition as it's likely something we don't need
// and if it is the `checkAPIResourceIsPresent` check will fail and stop the process
if err != nil && !discovery.IsGroupDiscoveryFailedError(err) {
return RestClientResultFromErr(err)
}
// Validate that the APIVersion provided in the YAML is valid for this cluster
apiResource, exists := checkAPIResourceIsPresent(resources, *manifest.Raw)
if !exists {
// api not found, invalidate the cache and try again
// this handles the case when a CRD is being created by another kubectl_manifest resource run
discoveryClient.Invalidate()
_, resources, err = discoveryClient.ServerGroupsAndResources()
if err != nil && !discovery.IsGroupDiscoveryFailedError(err) {
return RestClientResultFromErr(err)
}
// check for resource again
apiResource, exists = checkAPIResourceIsPresent(resources, *manifest.Raw)
if !exists {
return RestClientResultFromInvalidTypeErr(fmt.Errorf("resource [%s/%s] isn't valid for cluster, check the APIVersion and Kind fields are valid", manifest.Raw.GroupVersionKind().GroupVersion().String(), manifest.GetKind()))
}
}
resourceStruct := k8sschema.GroupVersionResource{Group: apiResource.Group, Version: apiResource.Version, Resource: apiResource.Name}
// For core services (ServiceAccount, Service etc) the group is incorrectly parsed.
// "v1" should be empty group and "v1" for version
if resourceStruct.Group == "v1" && resourceStruct.Version == "" {
resourceStruct.Group = ""
resourceStruct.Version = "v1"
}
client := dynamic.NewForConfigOrDie(&provider.RestConfig).Resource(resourceStruct)
if apiResource.Namespaced {
if !manifest.HasNamespace() {
manifest.SetNamespace("default")
}
return RestClientResultSuccess(client.Namespace(manifest.GetNamespace()))
}
return RestClientResultSuccess(client)
}
discoveryWithTimeout := func(manifest *yaml.Manifest, provider *KubeProvider) <-chan *RestClientResult {
ch := make(chan *RestClientResult)
go func() {
ch <- doGetRestClientFromUnstructured(manifest, provider)
}()
return ch
}
timeout := time.NewTimer(60 * time.Second)
defer timeout.Stop()
select {
case res := <-discoveryWithTimeout(manifest, provider):
return res
case <-timeout.C:
log.Printf("[ERROR] %v timed out fetching resources from discovery client", manifest)
return RestClientResultFromErr(fmt.Errorf("%v timed out fetching resources from discovery client", manifest))
}
}
// checkAPIResourceIsPresent Loops through a list of available APIResources and
// checks there is a resource for the APIVersion and Kind defined in the 'resource'
// if found it returns true and the APIResource which matched
func checkAPIResourceIsPresent(available []*meta_v1.APIResourceList, resource meta_v1_unstruct.Unstructured) (*meta_v1.APIResource, bool) {
for _, rList := range available {
if rList == nil {
continue
}
group := rList.GroupVersion
for _, r := range rList.APIResources {
if group == resource.GroupVersionKind().GroupVersion().String() && r.Kind == resource.GetKind() {
r.Group = rList.GroupVersion
r.Kind = rList.Kind
return &r, true
}
}
}
return nil, false
}
// GetDeploymentConditionInternal returns the condition with the provided type.
// Borrowed from: https://github.com/kubernetes/kubernetes/blob/master/pkg/controller/deployment/util/deployment_util.go#L135
func GetDeploymentCondition(status apps_v1.DeploymentStatus, condType apps_v1.DeploymentConditionType) *apps_v1.DeploymentCondition {
for i := range status.Conditions {
c := status.Conditions[i]
if c.Type == condType {
return &c
}
}
return nil
}
func waitForDeploymentReplicasFunc(ctx context.Context, provider *KubeProvider, ns, name string) resource.RetryFunc {
return func() *resource.RetryError {
// Query the deployment to get a status update.
dply, err := provider.MainClientset.AppsV1().Deployments(ns).Get(ctx, name, meta_v1.GetOptions{})
if err != nil {
return resource.NonRetryableError(err)
}
if dply.Generation <= dply.Status.ObservedGeneration {
cond := GetDeploymentCondition(dply.Status, apps_v1.DeploymentProgressing)
if cond != nil && cond.Reason == TimedOutReason {
err := fmt.Errorf("Deployment exceeded its progress deadline: %v", cond.String())
return resource.NonRetryableError(err)
}
if dply.Status.UpdatedReplicas < *dply.Spec.Replicas {
return resource.RetryableError(fmt.Errorf("Waiting for rollout to finish: %d out of %d new replicas have been updated...", dply.Status.UpdatedReplicas, dply.Spec.Replicas))
}
if dply.Status.Replicas > dply.Status.UpdatedReplicas {
return resource.RetryableError(fmt.Errorf("Waiting for rollout to finish: %d old replicas are pending termination...", dply.Status.Replicas-dply.Status.UpdatedReplicas))
}
if dply.Status.AvailableReplicas < dply.Status.UpdatedReplicas {
return resource.RetryableError(fmt.Errorf("Waiting for rollout to finish: %d of %d updated replicas are available...", dply.Status.AvailableReplicas, dply.Status.UpdatedReplicas))
}
} else if dply.Status.ObservedGeneration == 0 {
return resource.RetryableError(fmt.Errorf("Waiting for rollout to start"))
}
return nil
}
}
func waitForAPIServiceAvailableFunc(ctx context.Context, provider *KubeProvider, name string) resource.RetryFunc {
return func() *resource.RetryError {
apiService, err := provider.AggregatorClientset.ApiregistrationV1().APIServices().Get(ctx, name, meta_v1.GetOptions{})
if err != nil {
return resource.NonRetryableError(err)
}
for i := range apiService.Status.Conditions {
if apiService.Status.Conditions[i].Type == apiregistration.Available {
return nil
}
}
return resource.RetryableError(fmt.Errorf("Waiting for APIService %v to be Available", name))
}
}
// Takes the result of flatmap.Expand for an array of strings
// and returns a []*string
func expandStringList(configured []interface{}) []string {
vs := make([]string, 0, len(configured))
for _, v := range configured {
val, ok := v.(string)
if ok && val != "" {
vs = append(vs, val)
}
}
return vs
}
func getLiveManifestFingerprint(d *schema.ResourceData, userProvided *yaml.Manifest, liveManifest *yaml.Manifest) string {
fields := getLiveManifestFields(d, userProvided, liveManifest)
return getFingerprint(fields)
}
func getLiveManifestFields(d *schema.ResourceData, userProvided *yaml.Manifest, liveManifest *yaml.Manifest) string {
var ignoreFields []string = nil
ignoreFieldsRaw, hasIgnoreFields := d.GetOk("ignore_fields")
if hasIgnoreFields {
ignoreFields = expandStringList(ignoreFieldsRaw.([]interface{}))
}
return getLiveManifestFields_WithIgnoredFields(ignoreFields, userProvided, liveManifest)
}
func getFingerprint(s string) string {
fingerprint := sha256.New()
fingerprint.Write([]byte(s))
return fmt.Sprintf("%x", fingerprint.Sum(nil))
}
func getLiveManifestFields_WithIgnoredFields(ignoredFields []string, userProvided *yaml.Manifest, liveManifest *yaml.Manifest) string {
flattenedUser := flatten.Flatten(userProvided.Raw.Object)
flattenedLive := flatten.Flatten(liveManifest.Raw.Object)
// remove any fields from the user provided set or control fields that we want to ignore
fieldsToTrim := append([]string(nil), kubernetesControlFields...)
if len(ignoredFields) > 0 {
fieldsToTrim = append(fieldsToTrim, ignoredFields...)
}
for _, field := range fieldsToTrim {
delete(flattenedUser, field)
// check for any nested fields to ignore
for k, _ := range flattenedUser {
if strings.HasPrefix(k, field+".") {
delete(flattenedUser, k)
}
}
}
// update the user provided flattened string with the live versions of the keys
// this implicitly excludes anything that the user didn't provide as it was added by kubernetes runtime (annotations/mutations etc)
userKeys := []string{}
for userKey, userValue := range flattenedUser {
normalizedUserValue := strings.TrimSpace(userValue)
// only include the value if it exists in the live version
// that is, don't add to the userKeys array unless the key still exists in the live manifest
if _, exists := flattenedLive[userKey]; exists {
userKeys = append(userKeys, userKey)
normalizedLiveValue := strings.TrimSpace(flattenedLive[userKey])
flattenedUser[userKey] = normalizedLiveValue
if normalizedUserValue != normalizedLiveValue {
log.Printf("[TRACE] yaml drift detected in %s for %s, was: %s now: %s", userProvided.GetSelfLink(), userKey, normalizedUserValue, normalizedLiveValue)
}
} else {
if normalizedUserValue != "" {
log.Printf("[TRACE] yaml drift detected in %s for %s, was %s now blank", userProvided.GetSelfLink(), userKey, normalizedUserValue)
}
}
}
sort.Strings(userKeys)
returnedValues := []string{}
for _, k := range userKeys {
returnedValues = append(returnedValues, fmt.Sprintf("%s=%s", k, flattenedUser[k]))
}
return strings.Join(returnedValues, ",")
}
var kubernetesControlFields = []string{
"status",
"metadata.finalizers",
"metadata.initializers",
"metadata.ownerReferences",
"metadata.creationTimestamp",
"metadata.generation",
"metadata.resourceVersion",
"metadata.uid",
"metadata.annotations.kubectl.kubernetes.io/last-applied-configuration",
}