Skip to content

Commit 749e1cf

Browse files
authored
Merge pull request #773 from terraform-providers/hpa-v2
Add support for metrics field in horizontal_pod_autoscaler
2 parents 69a6932 + 2337b28 commit 749e1cf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+2380
-190
lines changed

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
module github.com/terraform-providers/terraform-provider-kubernetes
22

33
require (
4+
github.com/Azure/go-autorest/autorest v0.9.2 // indirect
5+
github.com/Azure/go-autorest/autorest/adal v0.8.1-0.20191028180845-3492b2aff503 // indirect
46
github.com/frankban/quicktest v1.4.2 // indirect
57
github.com/gogo/protobuf v1.3.0 // indirect
68
github.com/google/go-cmp v0.3.1
79
github.com/googleapis/gnostic v0.2.0 // indirect
810
github.com/gophercloud/gophercloud v0.3.1-0.20190807175045-25a84d593c97 // indirect
11+
github.com/hashicorp/go-getter v1.4.2-0.20200106182914-9813cbd4eb02 // indirect
912
github.com/hashicorp/go-version v1.2.0
13+
github.com/hashicorp/hcl/v2 v2.3.0 // indirect
14+
github.com/hashicorp/terraform-config-inspect v0.0.0-20191212124732-c6ae6269b9d7 // indirect
1015
github.com/hashicorp/terraform-plugin-sdk v1.7.0
1116
github.com/hashicorp/vault v1.1.2 // indirect
1217
github.com/imdario/mergo v0.3.7 // indirect

go.sum

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,21 @@ github.com/Azure/go-autorest v12.1.0+incompatible h1:x0sVyfVo0Qw9jcgVHuKIAiTHGRv
2323
github.com/Azure/go-autorest v12.1.0+incompatible/go.mod h1:r+4oMnoxhatjLLJ6zxSWATqVooLgysK6ZNox3g/xq24=
2424
github.com/Azure/go-autorest/autorest v0.9.0 h1:MRvx8gncNaXJqOoLmhNjUAKh33JJF8LyxPhomEtOsjs=
2525
github.com/Azure/go-autorest/autorest v0.9.0/go.mod h1:xyHB1BMZT0cuDHU7I0+g046+BFDTQ8rEZB0s4Yfa6bI=
26+
github.com/Azure/go-autorest/autorest v0.9.2 h1:6AWuh3uWrsZJcNoCHrCF/+g4aKPCU39kaMO6/qrnK/4=
27+
github.com/Azure/go-autorest/autorest v0.9.2/go.mod h1:xyHB1BMZT0cuDHU7I0+g046+BFDTQ8rEZB0s4Yfa6bI=
2628
github.com/Azure/go-autorest/autorest/adal v0.5.0 h1:q2gDruN08/guU9vAjuPWff0+QIrpH6ediguzdAzXAUU=
2729
github.com/Azure/go-autorest/autorest/adal v0.5.0/go.mod h1:8Z9fGy2MpX0PvDjB1pEgQTmVqjGhiHBW7RJJEciWzS0=
30+
github.com/Azure/go-autorest/autorest/adal v0.8.1-0.20191028180845-3492b2aff503 h1:Hxqlh1uAA8aGpa1dFhDNhll7U/rkWtG8ZItFvRMr7l0=
31+
github.com/Azure/go-autorest/autorest/adal v0.8.1-0.20191028180845-3492b2aff503/go.mod h1:Z6vX6WXXuyieHAXwMj0S6HY6e6wcHn37qQMBQlvY3lc=
2832
github.com/Azure/go-autorest/autorest/date v0.1.0 h1:YGrhWfrgtFs84+h0o46rJrlmsZtyZRg470CqAXTZaGM=
2933
github.com/Azure/go-autorest/autorest/date v0.1.0/go.mod h1:plvfp3oPSKwf2DNjlBjWF/7vwR+cUD/ELuzDCXwHUVA=
34+
github.com/Azure/go-autorest/autorest/date v0.2.0 h1:yW+Zlqf26583pE43KhfnhFcdmSWlm5Ew6bxipnr/tbM=
35+
github.com/Azure/go-autorest/autorest/date v0.2.0/go.mod h1:vcORJHLJEh643/Ioh9+vPmf1Ij9AEBM5FuBIXLmIy0g=
3036
github.com/Azure/go-autorest/autorest/mocks v0.1.0/go.mod h1:OTyCOPRA2IgIlWxVYxBee2F5Gr4kF2zd2J5cFRaIDN0=
3137
github.com/Azure/go-autorest/autorest/mocks v0.2.0 h1:Ww5g4zThfD/6cLb4z6xxgeyDa7QDkizMkJKe0ysZXp0=
3238
github.com/Azure/go-autorest/autorest/mocks v0.2.0/go.mod h1:OTyCOPRA2IgIlWxVYxBee2F5Gr4kF2zd2J5cFRaIDN0=
39+
github.com/Azure/go-autorest/autorest/mocks v0.3.0 h1:qJumjCaCudz+OcqE9/XtEPfvtOjOmKaui4EOpFI6zZc=
40+
github.com/Azure/go-autorest/autorest/mocks v0.3.0/go.mod h1:a8FDP3DYzQ4RYfVAxAN3SVSiiO77gL2j2ronKKP0syM=
3341
github.com/Azure/go-autorest/logger v0.1.0 h1:ruG4BSDXONFRrZZJ2GUXDiUyVpayPmb1GnWeHDdaNKY=
3442
github.com/Azure/go-autorest/logger v0.1.0/go.mod h1:oExouG+K6PryycPJfVSxi/koC6LSNgds39diKLz7Vrc=
3543
github.com/Azure/go-autorest/tracing v0.5.0 h1:TRn4WjSnkcSy5AEG3pnbtFSwNtwzjr4VYyQflFE619k=
@@ -272,6 +280,8 @@ github.com/hashicorp/go-cleanhttp v0.5.1 h1:dH3aiDG9Jvb5r5+bYHsikaOUIpcM0xvgMXVo
272280
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
273281
github.com/hashicorp/go-getter v1.4.0 h1:ENHNi8494porjD0ZhIrjlAHnveSFhY7hvOJrV/fsKkw=
274282
github.com/hashicorp/go-getter v1.4.0/go.mod h1:7qxyCd8rBfcShwsvxgIguu4KbS3l8bUCwg2Umn7RjeY=
283+
github.com/hashicorp/go-getter v1.4.2-0.20200106182914-9813cbd4eb02 h1:l1KB3bHVdvegcIf5upQ5mjcHjs2qsWnKh4Yr9xgIuu8=
284+
github.com/hashicorp/go-getter v1.4.2-0.20200106182914-9813cbd4eb02/go.mod h1:7qxyCd8rBfcShwsvxgIguu4KbS3l8bUCwg2Umn7RjeY=
275285
github.com/hashicorp/go-hclog v0.0.0-20180709165350-ff2cf002a8dd/go.mod h1:9bjs9uLqI8l75knNv3lV1kA55veR+WUPSiKIWcQHudI=
276286
github.com/hashicorp/go-hclog v0.9.2 h1:CG6TE5H9/JXsFWJCfoIVpKFIkFe6ysEuHirp4DxCsHI=
277287
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
@@ -298,21 +308,24 @@ github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
298308
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
299309
github.com/hashicorp/hcl/v2 v2.0.0 h1:efQznTz+ydmQXq3BOnRa3AXzvCeTq1P4dKj/z5GLlY8=
300310
github.com/hashicorp/hcl/v2 v2.0.0/go.mod h1:oVVDG71tEinNGYCxinCYadcmKU9bglqW9pV3txagJ90=
311+
github.com/hashicorp/hcl/v2 v2.3.0 h1:iRly8YaMwTBAKhn1Ybk7VSdzbnopghktCD031P8ggUE=
312+
github.com/hashicorp/hcl/v2 v2.3.0/go.mod h1:d+FwDBbOLvpAM3Z6J7gPj/VoAGkNe/gm352ZhjJ/Zv8=
301313
github.com/hashicorp/hcl2 v0.0.0-20190821123243-0c888d1241f6 h1:JImQpEeUQ+0DPFMaWzLA0GdUNPaUlCXLpfiqkSZBUfc=
302314
github.com/hashicorp/hcl2 v0.0.0-20190821123243-0c888d1241f6/go.mod h1:Cxv+IJLuBiEhQ7pBYGEuORa0nr4U994pE8mYLuFd7v0=
315+
github.com/hashicorp/hil v0.0.0-20190212112733-ab17b08d6590 h1:2yzhWGdgQUWZUCNK+AoO35V+HTsgEmcM4J9IkArh7PI=
303316
github.com/hashicorp/hil v0.0.0-20190212112733-ab17b08d6590/go.mod h1:n2TSygSNwsLJ76m8qFXTSc7beTb+auJxYdqrnoqwZWE=
304317
github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI65Y=
305318
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
306319
github.com/hashicorp/terraform-config-inspect v0.0.0-20190821133035-82a99dc22ef4 h1:fTkL0YwjohGyN7AqsDhz6bwcGBpT+xBqi3Qhpw58Juw=
307320
github.com/hashicorp/terraform-config-inspect v0.0.0-20190821133035-82a99dc22ef4/go.mod h1:JDmizlhaP5P0rYTTZB0reDMefAiJyfWPEtugV4in1oI=
308321
github.com/hashicorp/terraform-config-inspect v0.0.0-20191115094559-17f92b0546e8 h1:+RyjwU+Gnd/aTJBPZVDNm903eXVjjqhbaR4Ypx3xYyY=
309322
github.com/hashicorp/terraform-config-inspect v0.0.0-20191115094559-17f92b0546e8/go.mod h1:p+ivJws3dpqbp1iP84+npOyAmTTOLMgCzrXd3GSdn/A=
323+
github.com/hashicorp/terraform-config-inspect v0.0.0-20191212124732-c6ae6269b9d7 h1:Pc5TCv9mbxFN6UVX0LH6CpQrdTM5YjbVI2w15237Pjk=
324+
github.com/hashicorp/terraform-config-inspect v0.0.0-20191212124732-c6ae6269b9d7/go.mod h1:p+ivJws3dpqbp1iP84+npOyAmTTOLMgCzrXd3GSdn/A=
310325
github.com/hashicorp/terraform-json v0.4.0 h1:KNh29iNxozP5adfUFBJ4/fWd0Cu3taGgjHB38JYqOF4=
311326
github.com/hashicorp/terraform-json v0.4.0/go.mod h1:eAbqb4w0pSlRmdvl8fOyHAi/+8jnkVYN28gJkSJrLhU=
312327
github.com/hashicorp/terraform-plugin-sdk v1.0.0 h1:3AjuuV1LJKs1NlG+heUgqWN6/QCSx2kDhyS6K7F0fTw=
313328
github.com/hashicorp/terraform-plugin-sdk v1.0.0/go.mod h1:NuwtLpEpPsFaKJPJNGtMcn9vlhe6Ofe+Y6NqXhJgV2M=
314-
github.com/hashicorp/terraform-plugin-sdk v1.4.1 h1:REgN6WbySD6aIYdF6Uob3ic4eQkfh4NXSWU/casmgb4=
315-
github.com/hashicorp/terraform-plugin-sdk v1.4.1/go.mod h1:H5QLx/uhwfxBZ59Bc5SqT19M4i+fYt7LZjHTpbLZiAg=
316329
github.com/hashicorp/terraform-plugin-sdk v1.7.0 h1:B//oq0ZORG+EkVrIJy0uPGSonvmXqxSzXe8+GhknoW0=
317330
github.com/hashicorp/terraform-plugin-sdk v1.7.0/go.mod h1:OjgQmey5VxnPej/buEhe+YqKm0KNvV3QqU4hkqHqPCY=
318331
github.com/hashicorp/terraform-plugin-test v1.2.0 h1:AWFdqyfnOj04sxTdaAF57QqvW7XXrT8PseUHkbKsE8I=
@@ -468,6 +481,7 @@ github.com/pierrec/lz4 v2.3.0+incompatible h1:CZzRn4Ut9GbUkHlQ7jqBXeZQV41ZSKWFc3
468481
github.com/pierrec/lz4 v2.3.0+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
469482
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
470483
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
484+
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
471485
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
472486
github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
473487
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@@ -584,6 +598,7 @@ github.com/zclconf/go-cty v1.0.0 h1:EWtv3gKe2wPLIB9hQRQJa7k/059oIfAqcEkCNnaVckk=
584598
github.com/zclconf/go-cty v1.0.0/go.mod h1:xnAOWiHeOqg2nWS62VtQ7pbOu17FtxJNW8RLEih+O3s=
585599
github.com/zclconf/go-cty v1.1.0 h1:uJwc9HiBOCpoKIObTQaLR+tsEXx1HBHnOsOOpcdhZgw=
586600
github.com/zclconf/go-cty v1.1.0/go.mod h1:xnAOWiHeOqg2nWS62VtQ7pbOu17FtxJNW8RLEih+O3s=
601+
github.com/zclconf/go-cty v1.2.0/go.mod h1:hOPWgoHbaTUnI5k4D2ld+GRpFJSCe6bCM7m1q/N4PQ8=
587602
github.com/zclconf/go-cty v1.2.1 h1:vGMsygfmeCl4Xb6OA5U5XVAaQZ69FvoG7X2jUtQujb8=
588603
github.com/zclconf/go-cty v1.2.1/go.mod h1:hOPWgoHbaTUnI5k4D2ld+GRpFJSCe6bCM7m1q/N4PQ8=
589604
github.com/zclconf/go-cty-yaml v1.0.1 h1:up11wlgAaDvlAGENcFDnZgkn0qUJurso7k6EpURKNF8=

kubernetes/resource_kubernetes_horizontal_pod_autoscaler.go

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ func resourceKubernetesHorizontalPodAutoscaler() *schema.Resource {
2121
Importer: &schema.ResourceImporter{
2222
State: schema.ImportStatePassthrough,
2323
},
24-
2524
Schema: map[string]*schema.Schema{
2625
"metadata": namespacedMetadataSchema("horizontal pod autoscaler", true),
2726
"spec": {
@@ -36,6 +35,12 @@ func resourceKubernetesHorizontalPodAutoscaler() *schema.Resource {
3635
Description: "Upper limit for the number of pods that can be set by the autoscaler.",
3736
Required: true,
3837
},
38+
"metric": {
39+
Type: schema.TypeList,
40+
Optional: true,
41+
Description: "The specifications for which to use to calculate the desired replica count (the maximum replica count across all metrics will be used). The desired replica count is calculated multiplying the ratio between the target value and the current value by the current number of pods. Ergo, metrics used must decrease as the pod count is increased, and vice-versa. See the individual metric source types for more information about how each type of metric must respond. If not set, the default metric will be set to 80% average CPU utilization.",
42+
Elem: metricSpecFields(),
43+
},
3944
"min_replicas": {
4045
Type: schema.TypeInt,
4146
Description: "Lower limit for the number of pods that can be set by the autoscaler, defaults to `1`.",
@@ -81,6 +86,10 @@ func resourceKubernetesHorizontalPodAutoscaler() *schema.Resource {
8186
}
8287

8388
func resourceKubernetesHorizontalPodAutoscalerCreate(d *schema.ResourceData, meta interface{}) error {
89+
if useV2(d) {
90+
return resourceKubernetesHorizontalPodAutoscalerV2Create(d, meta)
91+
}
92+
8493
conn, err := meta.(KubeClientsets).MainClientset()
8594
if err != nil {
8695
return err
@@ -109,6 +118,10 @@ func resourceKubernetesHorizontalPodAutoscalerCreate(d *schema.ResourceData, met
109118
}
110119

111120
func resourceKubernetesHorizontalPodAutoscalerRead(d *schema.ResourceData, meta interface{}) error {
121+
if useV2(d) {
122+
return resourceKubernetesHorizontalPodAutoscalerV2Read(d, meta)
123+
}
124+
112125
conn, err := meta.(KubeClientsets).MainClientset()
113126
if err != nil {
114127
return err
@@ -119,18 +132,24 @@ func resourceKubernetesHorizontalPodAutoscalerRead(d *schema.ResourceData, meta
119132
return err
120133
}
121134
log.Printf("[INFO] Reading horizontal pod autoscaler %s", name)
122-
svc, err := conn.AutoscalingV1().HorizontalPodAutoscalers(namespace).Get(name, meta_v1.GetOptions{})
135+
hpa, err := conn.AutoscalingV1().HorizontalPodAutoscalers(namespace).Get(name, meta_v1.GetOptions{})
123136
if err != nil {
124137
log.Printf("[DEBUG] Received error: %#v", err)
125138
return err
126139
}
127-
log.Printf("[INFO] Received horizontal pod autoscaler: %#v", svc)
128-
err = d.Set("metadata", flattenMetadata(svc.ObjectMeta, d))
140+
141+
// NOTE: this is needed for import
142+
if _, exists := hpa.ObjectMeta.GetAnnotations()["autoscaling.alpha.kubernetes.io/metrics"]; exists {
143+
return resourceKubernetesHorizontalPodAutoscalerV2Read(d, meta)
144+
}
145+
146+
log.Printf("[INFO] Received horizontal pod autoscaler: %#v", hpa)
147+
err = d.Set("metadata", flattenMetadata(hpa.ObjectMeta, d))
129148
if err != nil {
130149
return err
131150
}
132151

133-
flattened := flattenHorizontalPodAutoscalerSpec(svc.Spec)
152+
flattened := flattenHorizontalPodAutoscalerSpec(hpa.Spec)
134153
log.Printf("[DEBUG] Flattened horizontal pod autoscaler spec: %#v", flattened)
135154
err = d.Set("spec", flattened)
136155
if err != nil {
@@ -141,6 +160,10 @@ func resourceKubernetesHorizontalPodAutoscalerRead(d *schema.ResourceData, meta
141160
}
142161

143162
func resourceKubernetesHorizontalPodAutoscalerUpdate(d *schema.ResourceData, meta interface{}) error {
163+
if useV2(d) {
164+
return resourceKubernetesHorizontalPodAutoscalerV2Update(d, meta)
165+
}
166+
144167
conn, err := meta.(KubeClientsets).MainClientset()
145168
if err != nil {
146169
return err
@@ -172,6 +195,10 @@ func resourceKubernetesHorizontalPodAutoscalerUpdate(d *schema.ResourceData, met
172195
}
173196

174197
func resourceKubernetesHorizontalPodAutoscalerDelete(d *schema.ResourceData, meta interface{}) error {
198+
if useV2(d) {
199+
return resourceKubernetesHorizontalPodAutoscalerV2Delete(d, meta)
200+
}
201+
175202
conn, err := meta.(KubeClientsets).MainClientset()
176203
if err != nil {
177204
return err
@@ -194,6 +221,10 @@ func resourceKubernetesHorizontalPodAutoscalerDelete(d *schema.ResourceData, met
194221
}
195222

196223
func resourceKubernetesHorizontalPodAutoscalerExists(d *schema.ResourceData, meta interface{}) (bool, error) {
224+
if useV2(d) {
225+
return resourceKubernetesHorizontalPodAutoscalerV2Exists(d, meta)
226+
}
227+
197228
conn, err := meta.(KubeClientsets).MainClientset()
198229
if err != nil {
199230
return false, err
@@ -214,3 +245,11 @@ func resourceKubernetesHorizontalPodAutoscalerExists(d *schema.ResourceData, met
214245
}
215246
return true, err
216247
}
248+
249+
func useV2(d *schema.ResourceData) bool {
250+
if len(d.Get("spec.0.metric").([]interface{})) > 0 {
251+
log.Printf("[INFO] Using autoscaling/v2beta2 because this resource has a metric field")
252+
return true
253+
}
254+
return false
255+
}
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
package kubernetes
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
8+
autoscalingv2beta2 "k8s.io/api/autoscaling/v2beta2"
9+
"k8s.io/apimachinery/pkg/api/errors"
10+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11+
types "k8s.io/apimachinery/pkg/types"
12+
)
13+
14+
func resourceKubernetesHorizontalPodAutoscalerV2Create(d *schema.ResourceData, meta interface{}) error {
15+
conn, err := meta.(KubeClientsets).MainClientset()
16+
if err != nil {
17+
return err
18+
}
19+
20+
metadata := expandMetadata(d.Get("metadata").([]interface{}))
21+
spec, err := expandHorizontalPodAutoscalerV2Spec(d.Get("spec").([]interface{}))
22+
if err != nil {
23+
return err
24+
}
25+
26+
hpa := autoscalingv2beta2.HorizontalPodAutoscaler{
27+
ObjectMeta: metadata,
28+
Spec: *spec,
29+
}
30+
log.Printf("[INFO] Creating new horizontal pod autoscaler: %#v", hpa)
31+
out, err := conn.AutoscalingV2beta2().HorizontalPodAutoscalers(metadata.Namespace).Create(&hpa)
32+
if err != nil {
33+
return err
34+
}
35+
36+
log.Printf("[INFO] Submitted new horizontal pod autoscaler: %#v", out)
37+
d.SetId(buildId(out.ObjectMeta))
38+
39+
return resourceKubernetesHorizontalPodAutoscalerV2Read(d, meta)
40+
}
41+
42+
func resourceKubernetesHorizontalPodAutoscalerV2Read(d *schema.ResourceData, meta interface{}) error {
43+
conn, err := meta.(KubeClientsets).MainClientset()
44+
if err != nil {
45+
return err
46+
}
47+
48+
namespace, name, err := idParts(d.Id())
49+
if err != nil {
50+
return err
51+
}
52+
log.Printf("[INFO] Reading horizontal pod autoscaler %s", name)
53+
hpa, err := conn.AutoscalingV2beta2().HorizontalPodAutoscalers(namespace).Get(name, metav1.GetOptions{})
54+
if err != nil {
55+
log.Printf("[DEBUG] Received error: %#v", err)
56+
return err
57+
}
58+
log.Printf("[INFO] Received horizontal pod autoscaler: %#v", hpa)
59+
err = d.Set("metadata", flattenMetadata(hpa.ObjectMeta, d))
60+
if err != nil {
61+
return err
62+
}
63+
64+
flattened := flattenHorizontalPodAutoscalerV2Spec(hpa.Spec)
65+
log.Printf("[DEBUG] Flattened horizontal pod autoscaler spec: %#v", flattened)
66+
err = d.Set("spec", flattened)
67+
if err != nil {
68+
return err
69+
}
70+
71+
return nil
72+
}
73+
74+
func resourceKubernetesHorizontalPodAutoscalerV2Update(d *schema.ResourceData, meta interface{}) error {
75+
conn, err := meta.(KubeClientsets).MainClientset()
76+
if err != nil {
77+
return err
78+
}
79+
80+
namespace, name, err := idParts(d.Id())
81+
if err != nil {
82+
return err
83+
}
84+
85+
ops := patchMetadata("metadata.0.", "/metadata/", d)
86+
if d.HasChange("spec") {
87+
diffOps := patchHorizontalPodAutoscalerV2Spec("spec.0.", "/spec", d)
88+
ops = append(ops, diffOps...)
89+
}
90+
data, err := ops.MarshalJSON()
91+
if err != nil {
92+
return fmt.Errorf("Failed to marshal update operations: %s", err)
93+
}
94+
log.Printf("[INFO] Updating horizontal pod autoscaler %q: %v", name, string(data))
95+
out, err := conn.AutoscalingV2beta2().HorizontalPodAutoscalers(namespace).Patch(name, types.JSONPatchType, data)
96+
if err != nil {
97+
return fmt.Errorf("Failed to update horizontal pod autoscaler: %s", err)
98+
}
99+
log.Printf("[INFO] Submitted updated horizontal pod autoscaler: %#v", out)
100+
d.SetId(buildId(out.ObjectMeta))
101+
102+
return resourceKubernetesHorizontalPodAutoscalerV2Read(d, meta)
103+
}
104+
105+
func resourceKubernetesHorizontalPodAutoscalerV2Delete(d *schema.ResourceData, meta interface{}) error {
106+
conn, err := meta.(KubeClientsets).MainClientset()
107+
if err != nil {
108+
return err
109+
}
110+
111+
namespace, name, err := idParts(d.Id())
112+
if err != nil {
113+
return err
114+
}
115+
log.Printf("[INFO] Deleting horizontal pod autoscaler: %#v", name)
116+
err = conn.AutoscalingV2beta2().HorizontalPodAutoscalers(namespace).Delete(name, &metav1.DeleteOptions{})
117+
if err != nil {
118+
return err
119+
}
120+
121+
log.Printf("[INFO] Horizontal Pod Autoscaler %s deleted", name)
122+
123+
d.SetId("")
124+
return nil
125+
}
126+
127+
func resourceKubernetesHorizontalPodAutoscalerV2Exists(d *schema.ResourceData, meta interface{}) (bool, error) {
128+
conn, err := meta.(KubeClientsets).MainClientset()
129+
if err != nil {
130+
return false, err
131+
}
132+
133+
namespace, name, err := idParts(d.Id())
134+
if err != nil {
135+
return false, err
136+
}
137+
138+
log.Printf("[INFO] Checking horizontal pod autoscaler %s", name)
139+
_, err = conn.AutoscalingV2beta2().HorizontalPodAutoscalers(namespace).Get(name, metav1.GetOptions{})
140+
if err != nil {
141+
if statusErr, ok := err.(*errors.StatusError); ok && statusErr.ErrStatus.Code == 404 {
142+
return false, nil
143+
}
144+
log.Printf("[DEBUG] Received error: %#v", err)
145+
}
146+
return true, err
147+
}

0 commit comments

Comments
 (0)