-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathstructure_horizontal_pod_autoscaler.go
More file actions
111 lines (99 loc) · 3.15 KB
/
structure_horizontal_pod_autoscaler.go
File metadata and controls
111 lines (99 loc) · 3.15 KB
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
// Copyright IBM Corp. 2017, 2025
// SPDX-License-Identifier: MPL-2.0
package kubernetes
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
api "k8s.io/api/autoscaling/v1"
"k8s.io/utils/ptr"
)
func expandHorizontalPodAutoscalerSpec(in []interface{}) (*api.HorizontalPodAutoscalerSpec, error) {
spec := &api.HorizontalPodAutoscalerSpec{}
if len(in) == 0 || in[0] == nil {
return nil, fmt.Errorf("failed to expand HorizontalPodAutoscaler.Spec: null or empty input")
}
m := in[0].(map[string]interface{})
if v, ok := m["max_replicas"]; ok {
spec.MaxReplicas = int32(v.(int))
}
if v, ok := m["min_replicas"].(int); ok && v > 0 {
spec.MinReplicas = ptr.To(int32(v))
}
if v, ok := m["scale_target_ref"]; ok {
spec.ScaleTargetRef = expandCrossVersionObjectReference(v.([]interface{}))
}
if v, ok := m["target_cpu_utilization_percentage"].(int); ok && v > 0 {
spec.TargetCPUUtilizationPercentage = ptr.To(int32(v))
}
return spec, nil
}
func expandCrossVersionObjectReference(in []interface{}) api.CrossVersionObjectReference {
if len(in) == 0 || in[0] == nil {
return api.CrossVersionObjectReference{}
}
ref := api.CrossVersionObjectReference{}
m := in[0].(map[string]interface{})
if v, ok := m["api_version"]; ok {
ref.APIVersion = v.(string)
}
if v, ok := m["kind"]; ok {
ref.Kind = v.(string)
}
if v, ok := m["name"]; ok {
ref.Name = v.(string)
}
return ref
}
func flattenHorizontalPodAutoscalerSpec(spec api.HorizontalPodAutoscalerSpec) []interface{} {
m := make(map[string]interface{}, 0)
m["max_replicas"] = spec.MaxReplicas
if spec.MinReplicas != nil {
m["min_replicas"] = *spec.MinReplicas
}
m["scale_target_ref"] = flattenCrossVersionObjectReference(spec.ScaleTargetRef)
if spec.TargetCPUUtilizationPercentage != nil {
m["target_cpu_utilization_percentage"] = *spec.TargetCPUUtilizationPercentage
}
return []interface{}{m}
}
func flattenCrossVersionObjectReference(ref api.CrossVersionObjectReference) []interface{} {
m := make(map[string]interface{}, 0)
if ref.APIVersion != "" {
m["api_version"] = ref.APIVersion
}
if ref.Kind != "" {
m["kind"] = ref.Kind
}
if ref.Name != "" {
m["name"] = ref.Name
}
return []interface{}{m}
}
func patchHorizontalPodAutoscalerSpec(prefix string, pathPrefix string, d *schema.ResourceData) []PatchOperation {
ops := make([]PatchOperation, 0)
if d.HasChange(prefix + "max_replicas") {
ops = append(ops, &ReplaceOperation{
Path: pathPrefix + "/maxReplicas",
Value: d.Get(prefix + "max_replicas").(int),
})
}
if d.HasChange(prefix + "min_replicas") {
ops = append(ops, &ReplaceOperation{
Path: pathPrefix + "/minReplicas",
Value: d.Get(prefix + "min_replicas").(int),
})
}
if d.HasChange(prefix + "scale_target_ref") {
ops = append(ops, &ReplaceOperation{
Path: pathPrefix + "/scaleTargetRef",
Value: expandCrossVersionObjectReference(d.Get(prefix + "scale_target_ref").([]interface{})),
})
}
if d.HasChange(prefix + "target_cpu_utilization_percentage") {
ops = append(ops, &ReplaceOperation{
Path: pathPrefix + "/targetCPUUtilizationPercentage",
Value: d.Get(prefix + "target_cpu_utilization_percentage").(int),
})
}
return ops
}