-
Notifications
You must be signed in to change notification settings - Fork 132
/
types_cloud_project_kube_nodepool.go
400 lines (341 loc) · 13.9 KB
/
types_cloud_project_kube_nodepool.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
package ovh
import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
)
const (
NotATaint TaintEffectType = iota
NoExecute
NoSchedule
PreferNoSchedule
)
type CloudProjectKubeNodePoolCreateOpts struct {
AntiAffinity *bool `json:"antiAffinity,omitempty"`
Autoscale *bool `json:"autoscale,omitempty"`
DesiredNodes *int `json:"desiredNodes,omitempty"`
FlavorName string `json:"flavorName"`
MaxNodes *int `json:"maxNodes,omitempty"`
MinNodes *int `json:"minNodes,omitempty"`
MonthlyBilled *bool `json:"monthlyBilled,omitempty"`
Name *string `json:"name,omitempty"`
Autoscaling *CloudProjectKubeNodePoolAutoscaling `json:"autoscaling,omitempty"`
Template *CloudProjectKubeNodePoolTemplate `json:"template,omitempty"`
}
type TaintEffectType int
type Taint struct {
Effect TaintEffectType `json:"effect"`
Key string `json:"key"`
Value string `json:"value"`
}
type CloudProjectKubeNodePoolTemplateMetadata struct {
Annotations map[string]string `json:"annotations"`
Finalizers []string `json:"finalizers"`
Labels map[string]string `json:"labels"`
}
type CloudProjectKubeNodePoolTemplateSpec struct {
Taints []Taint `json:"taints"`
Unschedulable bool `json:"unschedulable"`
}
type CloudProjectKubeNodePoolTemplate struct {
Metadata CloudProjectKubeNodePoolTemplateMetadata `json:"metadata"`
Spec CloudProjectKubeNodePoolTemplateSpec `json:"spec"`
}
type CloudProjectKubeNodePoolAutoscaling struct {
ScaleDownUtilizationThreshold *float64 `json:"scaleDownUtilizationThreshold,omitempty"`
ScaleDownUnneededTimeSeconds *int `json:"scaleDownUnneededTimeSeconds,omitempty"`
ScaleDownUnreadyTimeSeconds *int `json:"scaleDownUnreadyTimeSeconds,omitempty"`
}
type CloudProjectKubeNodePoolUpdateOpts struct {
Autoscale *bool `json:"autoscale,omitempty"`
DesiredNodes *int `json:"desiredNodes,omitempty"`
MaxNodes *int `json:"maxNodes,omitempty"`
MinNodes *int `json:"minNodes,omitempty"`
Autoscaling *CloudProjectKubeNodePoolAutoscaling `json:"autoscaling,omitempty"`
Template *CloudProjectKubeNodePoolTemplate `json:"template,omitempty"`
}
var toString = map[TaintEffectType]string{
NotATaint: "",
NoExecute: "NoExecute",
NoSchedule: "NoSchedule",
PreferNoSchedule: "PreferNoSchedule",
}
var TaintEffecTypeToID = map[string]TaintEffectType{
"": NotATaint,
"NoExecute": NoExecute,
"NoSchedule": NoSchedule,
"PreferNoSchedule": PreferNoSchedule,
}
func GetAutoscalingOpts(d *schema.ResourceData) (*CloudProjectKubeNodePoolAutoscaling, error) {
var autoscaling CloudProjectKubeNodePoolAutoscaling
var e error
autoscaling.ScaleDownUtilizationThreshold, e = helpers.GetNilFloat64PointerFromData(d, "autoscaling_scale_down_utilization_threshold")
autoscaling.ScaleDownUnneededTimeSeconds = helpers.GetNilIntPointerFromData(d, "autoscaling_scale_down_unneeded_time_seconds")
autoscaling.ScaleDownUnreadyTimeSeconds = helpers.GetNilIntPointerFromData(d, "autoscaling_scale_down_unready_time_seconds")
return &autoscaling, e
}
func (opts *CloudProjectKubeNodePoolCreateOpts) FromResource(d *schema.ResourceData) (*CloudProjectKubeNodePoolCreateOpts, error) {
opts.Autoscale = helpers.GetNilBoolPointerFromData(d, "autoscale")
opts.AntiAffinity = helpers.GetNilBoolPointerFromData(d, "anti_affinity")
opts.DesiredNodes = helpers.GetNilIntPointerFromDataAndNilIfNotPresent(d, "desired_nodes")
opts.FlavorName = d.Get("flavor_name").(string)
opts.MaxNodes = helpers.GetNilIntPointerFromDataAndNilIfNotPresent(d, "max_nodes")
opts.MinNodes = helpers.GetNilIntPointerFromDataAndNilIfNotPresent(d, "min_nodes")
opts.MonthlyBilled = helpers.GetNilBoolPointerFromData(d, "monthly_billed")
opts.Name = helpers.GetNilStringPointerFromData(d, "name")
template, err := loadNodelPoolTemplateFromResource(d.Get("template"))
if err != nil {
return nil, err
}
autoscaling, err := GetAutoscalingOpts(d)
if err != nil {
return nil, err
}
opts.Autoscaling = autoscaling
opts.Template = template
return opts, nil
}
func loadNodelPoolTemplateFromResource(i interface{}) (*CloudProjectKubeNodePoolTemplate, error) {
if i == nil {
return nil, nil
}
// We need to initialize slices to empty slice instead of nil
template := CloudProjectKubeNodePoolTemplate{
Metadata: CloudProjectKubeNodePoolTemplateMetadata{
Annotations: make(map[string]string),
Finalizers: make([]string, 0),
Labels: make(map[string]string),
},
Spec: CloudProjectKubeNodePoolTemplateSpec{
Taints: make([]Taint, 0),
Unschedulable: false,
},
}
templateSet := i.(*schema.Set).List()
if templateSet == nil || len(templateSet) == 0 {
return &template, nil
}
// Due to this bug https://github.com/hashicorp/terraform-plugin-sdk/pull/1042
// when updating the 'template' object there is two objects, one is empty, take the not empty one
templateObject := templateSet[0].(map[string]interface{}) // by default take the first one
for _, to := range templateSet {
var (
annotations map[string]interface{}
labels map[string]interface{}
finalizers []interface{}
taints []interface{}
)
metadataSet := to.(map[string]interface{})["metadata"].(*schema.Set).List()
if len(metadataSet) > 0 {
metadata := metadataSet[0].(map[string]interface{})
annotations = metadata["annotations"].(map[string]interface{})
labels = metadata["labels"].(map[string]interface{})
finalizers = metadata["finalizers"].([]interface{})
}
specSet := to.(map[string]interface{})["spec"].(*schema.Set).List()
if len(specSet) > 0 {
spec := specSet[0].(map[string]interface{})
taints = spec["taints"].([]interface{})
}
if len(annotations) == 0 && len(labels) == 0 && len(finalizers) == 0 && len(taints) == 0 {
continue
}
// We found the not empty object
templateObject = to.(map[string]interface{})
}
// metadata
{
metadataSet := templateObject["metadata"].(*schema.Set).List()
if len(metadataSet) > 0 {
metadata := metadataSet[0].(map[string]interface{})
// metadata.annotations
annotations := metadata["annotations"].(map[string]interface{})
if len(annotations) > 0 {
for k, v := range annotations {
template.Metadata.Annotations[k] = v.(string)
}
}
// metadata.finalizers
finalizers := metadata["finalizers"].([]interface{})
if len(finalizers) > 0 {
for _, finalizer := range finalizers {
template.Metadata.Finalizers = append(template.Metadata.Finalizers, finalizer.(string))
}
}
// metadata.labels
labels := metadata["labels"].(map[string]interface{})
if len(labels) > 0 {
for k, v := range labels {
template.Metadata.Labels[k] = v.(string)
}
}
}
}
// spec
{
specSet := templateObject["spec"].(*schema.Set).List()
if len(specSet) > 0 {
spec := specSet[0].(map[string]interface{})
// spec.taints
taints := spec["taints"].([]interface{})
for _, taint := range taints {
effectString := taint.(map[string]interface{})["effect"].(string)
effect := TaintEffecTypeToID[effectString]
if effect == NotATaint {
return nil, fmt.Errorf("effect: %s is not a allowable taint %#v", effectString, TaintEffecTypeToID)
}
taintObject := Taint{
Effect: effect,
Key: taint.(map[string]interface{})["key"].(string),
}
if taint.(map[string]interface{})["value"] != nil {
taintObject.Value = taint.(map[string]interface{})["value"].(string)
}
template.Spec.Taints = append(template.Spec.Taints, taintObject)
}
// spec.unschedulable
template.Spec.Unschedulable = spec["unschedulable"].(bool)
}
}
return &template, nil
}
func (s *CloudProjectKubeNodePoolCreateOpts) String() string {
return fmt.Sprintf("%s(%s): %d/%d/%d", *s.Name, s.FlavorName, *s.DesiredNodes, *s.MinNodes, *s.MaxNodes)
}
func (e TaintEffectType) String() string {
return toString[e]
}
// MarshalJSON marshals the enum as a quoted json string
func (e TaintEffectType) MarshalJSON() ([]byte, error) {
buffer := bytes.NewBufferString(`"`)
buffer.WriteString(toString[e])
buffer.WriteString(`"`)
return buffer.Bytes(), nil
}
// UnmarshalJSON unmashals a quoted json string to the enum value
func (e *TaintEffectType) UnmarshalJSON(b []byte) error {
var j string
err := json.Unmarshal(b, &j)
if err != nil {
return err
}
// Note that if the string cannot be found then it will be set to the zero value, 'Created' in this case.
*e = TaintEffecTypeToID[j]
return nil
}
func (opts *CloudProjectKubeNodePoolUpdateOpts) FromResource(d *schema.ResourceData) (*CloudProjectKubeNodePoolUpdateOpts, error) {
opts.Autoscale = helpers.GetNilBoolPointerFromData(d, "autoscale")
opts.DesiredNodes = helpers.GetNilIntPointerFromDataAndNilIfNotPresent(d, "desired_nodes")
opts.MaxNodes = helpers.GetNilIntPointerFromDataAndNilIfNotPresent(d, "max_nodes")
opts.MinNodes = helpers.GetNilIntPointerFromDataAndNilIfNotPresent(d, "min_nodes")
var autoscaling CloudProjectKubeNodePoolAutoscaling
var e error
autoscaling.ScaleDownUtilizationThreshold, e = helpers.GetNilFloat64PointerFromData(d, "autoscaling_scale_down_utilization_threshold")
autoscaling.ScaleDownUnneededTimeSeconds = helpers.GetNilIntPointerFromData(d, "autoscaling_scale_down_unneeded_time_seconds")
autoscaling.ScaleDownUnreadyTimeSeconds = helpers.GetNilIntPointerFromData(d, "autoscaling_scale_down_unready_time_seconds")
opts.Autoscaling = &autoscaling
template, err := loadNodelPoolTemplateFromResource(d.Get("template"))
if err != nil {
return nil, err
} else if e != nil {
return nil, e
}
opts.Template = template
return opts, nil
}
func (s *CloudProjectKubeNodePoolUpdateOpts) String() string {
return fmt.Sprintf("%d/%d/%d", *s.DesiredNodes, *s.MinNodes, *s.MaxNodes)
}
type CloudProjectKubeNodePoolResponse struct {
Autoscale bool `json:"autoscale"`
AntiAffinity bool `json:"antiAffinity"`
AvailableNodes int `json:"availableNodes"`
CreatedAt string `json:"createdAt"`
CurrentNodes int `json:"currentNodes"`
DesiredNodes int `json:"desiredNodes"`
Flavor string `json:"flavor"`
Id string `json:"id"`
MaxNodes int `json:"maxNodes"`
MinNodes int `json:"minNodes"`
MonthlyBilled bool `json:"monthlyBilled"`
Name string `json:"name"`
ProjectId string `json:"projectId"`
SizeStatus string `json:"sizeStatus"`
Status string `json:"status"`
UpToDateNodes int `json:"upToDateNodes"`
UpdatedAt string `json:"updatedAt"`
Autoscaling CloudProjectKubeNodePoolAutoscaling `json:"autoscaling"`
Template *CloudProjectKubeNodePoolTemplate `json:"template,omitempty"`
}
func (v CloudProjectKubeNodePoolResponse) ToMap() map[string]interface{} {
obj := make(map[string]interface{})
obj["anti_affinity"] = v.AntiAffinity
obj["autoscale"] = v.Autoscale
obj["available_nodes"] = v.AvailableNodes
obj["created_at"] = v.CreatedAt
obj["current_nodes"] = v.CurrentNodes
obj["desired_nodes"] = v.DesiredNodes
obj["flavor"] = v.Flavor
obj["flavor_name"] = v.Flavor
obj["id"] = v.Id
obj["max_nodes"] = v.MaxNodes
obj["min_nodes"] = v.MinNodes
obj["monthly_billed"] = v.MonthlyBilled
obj["name"] = v.Name
obj["project_id"] = v.ProjectId
obj["size_status"] = v.SizeStatus
obj["status"] = v.Status
obj["up_to_date_nodes"] = v.UpToDateNodes
obj["updated_at"] = v.UpdatedAt
obj["autoscaling_scale_down_utilization_threshold"] = v.Autoscaling.ScaleDownUtilizationThreshold
obj["autoscaling_scale_down_unneeded_time_seconds"] = v.Autoscaling.ScaleDownUnneededTimeSeconds
obj["autoscaling_scale_down_unready_time_seconds"] = v.Autoscaling.ScaleDownUnreadyTimeSeconds
emptyTemplateResponse := &CloudProjectKubeNodePoolTemplate{
Metadata: CloudProjectKubeNodePoolTemplateMetadata{
Annotations: make(map[string]string),
Finalizers: make([]string, 0),
Labels: make(map[string]string),
},
Spec: CloudProjectKubeNodePoolTemplateSpec{
Taints: make([]Taint, 0),
Unschedulable: false,
},
}
// If the template is not nil and not empty, then we need to add it to the map
if v.Template != nil && !reflect.DeepEqual(v.Template, emptyTemplateResponse) {
obj["template"] = []map[string]interface{}{{}}
// template.metadata
{
data := map[string]interface{}{
"finalizers": v.Template.Metadata.Finalizers,
"labels": v.Template.Metadata.Labels,
"annotations": v.Template.Metadata.Annotations,
}
obj["template"].([]map[string]interface{})[0]["metadata"] = []map[string]interface{}{data}
}
// template.spec
{
data := map[string]interface{}{
"unschedulable": v.Template.Spec.Unschedulable,
}
var taints []map[string]interface{}
for _, taint := range v.Template.Spec.Taints {
t := map[string]interface{}{
"effect": taint.Effect.String(),
"key": taint.Key,
"value": taint.Value,
}
taints = append(taints, t)
}
data["taints"] = taints
obj["template"].([]map[string]interface{})[0]["spec"] = []map[string]interface{}{data}
}
}
return obj
}
func (n *CloudProjectKubeNodePoolResponse) String() string {
return fmt.Sprintf("%s(%s): %s", n.Name, n.Id, n.Status)
}