Skip to content

Commit 50d254d

Browse files
author
mathieu prigent
committed
upgrade node pool missing parameters
possible autoscale update(versus destroy)
1 parent 525f786 commit 50d254d

6 files changed

+267
-19
lines changed

ovh/helpers/helpers.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,7 @@ func GetNilStringPointerFromData(data interface{}, id string) *string {
209209

210210
func GetNilIntPointerFromData(data interface{}, id string) *int {
211211
if resourceData, tok := data.(*schema.ResourceData); tok {
212-
if val, ok := resourceData.GetOk(id); ok {
213-
return GetNilIntPointer(val)
214-
}
212+
return GetNilIntPointer(resourceData.Get(id))
215213
} else if mapData, tok := data.(map[string]interface{}); tok {
216214
if val, ok := mapData[id]; ok {
217215
return GetNilIntPointer(val)

ovh/resource_cloud_project_kube.go

-2
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ func resourceCloudProjectKubeRead(d *schema.ResourceData, meta interface{}) erro
169169
kubeconfigRaw := CloudProjectKubeKubeConfigResponse{}
170170
endpoint := fmt.Sprintf("/cloud/project/%s/kube/%s/kubeconfig", serviceName, res.Id)
171171
err := config.OVHClient.Post(endpoint, nil, &kubeconfigRaw)
172-
173172
if err != nil {
174173
return err
175174
}
@@ -216,7 +215,6 @@ func resourceCloudProjectKubeUpdate(d *schema.ResourceData, meta interface{}) er
216215
err := config.OVHClient.Put(endpoint, CloudProjectKubePutOpts{
217216
Name: &value,
218217
}, nil)
219-
220218
if err != nil {
221219
return err
222220
}

ovh/resource_cloud_project_kube_nodepool.go

+62-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func resourceCloudProjectKubeNodePool() *schema.Resource {
4242
Type: schema.TypeBool,
4343
Description: "Enable auto-scaling for the pool",
4444
Optional: true,
45-
ForceNew: true,
45+
ForceNew: false,
4646
Default: "false",
4747
},
4848
"anti_affinity": {
@@ -137,6 +137,67 @@ func resourceCloudProjectKubeNodePool() *schema.Resource {
137137
Description: "Last update date",
138138
Computed: true,
139139
},
140+
"template": {
141+
Description: "Node pool template",
142+
Optional: true,
143+
Type: schema.TypeSet,
144+
MaxItems: 1,
145+
Elem: &schema.Resource{
146+
Schema: map[string]*schema.Schema{
147+
"metadata": {
148+
Description: "metadata",
149+
Optional: true,
150+
Type: schema.TypeSet,
151+
MaxItems: 1,
152+
Elem: &schema.Resource{
153+
Schema: map[string]*schema.Schema{
154+
"finalizers": {
155+
Description: "finalizers",
156+
Optional: true,
157+
Type: schema.TypeList,
158+
Elem: &schema.Schema{Type: schema.TypeString},
159+
},
160+
"labels": {
161+
Description: "labels",
162+
Optional: true,
163+
Type: schema.TypeMap,
164+
Elem: &schema.Schema{Type: schema.TypeString},
165+
},
166+
"annotations": {
167+
Description: "annotations",
168+
Optional: true,
169+
Type: schema.TypeMap,
170+
Elem: &schema.Schema{Type: schema.TypeString},
171+
},
172+
},
173+
},
174+
},
175+
"spec": {
176+
Description: "spec",
177+
Optional: true,
178+
Type: schema.TypeSet,
179+
MaxItems: 1,
180+
Elem: &schema.Resource{
181+
Schema: map[string]*schema.Schema{
182+
"unschedulable": {
183+
Description: "unschedulable",
184+
Optional: true,
185+
Type: schema.TypeBool,
186+
},
187+
"taints": {
188+
Description: "taints",
189+
Optional: true,
190+
Type: schema.TypeList,
191+
Elem: &schema.Schema{
192+
Type: schema.TypeMap,
193+
},
194+
},
195+
},
196+
},
197+
},
198+
},
199+
},
200+
},
140201
},
141202
}
142203
}

ovh/resource_cloud_project_kube_nodepool_test.go

+102
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,56 @@ resource "ovh_cloud_project_kube_nodepool" "pool" {
8888
desired_nodes = 1
8989
min_nodes = 0
9090
max_nodes = 1
91+
template {
92+
metadata {
93+
annotations = {
94+
a1 = "av1"
95+
}
96+
finalizers = ["F1"]
97+
labels = {
98+
l1 = "lv1"
99+
}
100+
}
101+
spec {
102+
unschedulable = false
103+
taints = [
104+
{
105+
effect = "PreferNoSchedule"
106+
key = "t1"
107+
value = "tv1"
108+
}
109+
]
110+
}
111+
}
112+
}
113+
`
114+
115+
var testAccCloudProjectKubeNodePoolConfigUpdated = `
116+
resource "ovh_cloud_project_kube" "cluster" {
117+
service_name = "%s"
118+
name = "%s"
119+
region = "%s"
120+
version = "%s"
121+
}
122+
123+
resource "ovh_cloud_project_kube_nodepool" "pool" {
124+
service_name = ovh_cloud_project_kube.cluster.service_name
125+
kube_id = ovh_cloud_project_kube.cluster.id
126+
name = ovh_cloud_project_kube.cluster.name
127+
flavor_name = "b2-7"
128+
desired_nodes = 1
129+
min_nodes = 0
130+
max_nodes = 1
131+
template {
132+
metadata {
133+
annotations = {
134+
a2 = "av2"
135+
}
136+
labels = {
137+
l2 = "lv2"
138+
}
139+
}
140+
}
91141
}
92142
`
93143

@@ -102,6 +152,13 @@ func TestAccCloudProjectKubeNodePool_basic(t *testing.T) {
102152
region,
103153
version,
104154
)
155+
configUpdated := fmt.Sprintf(
156+
testAccCloudProjectKubeNodePoolConfigUpdated,
157+
os.Getenv("OVH_CLOUD_PROJECT_SERVICE_TEST"),
158+
name,
159+
region,
160+
version,
161+
)
105162

106163
resource.Test(t, resource.TestCase{
107164
PreCheck: func() {
@@ -132,6 +189,51 @@ func TestAccCloudProjectKubeNodePool_basic(t *testing.T) {
132189
"ovh_cloud_project_kube_nodepool.pool", "min_nodes", "0"),
133190
resource.TestCheckResourceAttr(
134191
"ovh_cloud_project_kube_nodepool.pool", "max_nodes", "1"),
192+
resource.TestCheckResourceAttr(
193+
"ovh_cloud_project_kube_nodepool.pool", "template.0.metadata.0.annotations.a1", "av1"),
194+
resource.TestCheckResourceAttr(
195+
"ovh_cloud_project_kube_nodepool.pool", "template.0.metadata.0.finalizers.0", "F1"),
196+
resource.TestCheckResourceAttr(
197+
"ovh_cloud_project_kube_nodepool.pool", "template.0.metadata.0.labels.l1", "lv1"),
198+
resource.TestCheckResourceAttr(
199+
"ovh_cloud_project_kube_nodepool.pool", "template.0.spec.0.taints.0.effect", "PreferNoSchedule"),
200+
resource.TestCheckResourceAttr(
201+
"ovh_cloud_project_kube_nodepool.pool", "template.0.spec.0.taints.0.key", "t1"),
202+
resource.TestCheckResourceAttr(
203+
"ovh_cloud_project_kube_nodepool.pool", "template.0.spec.0.taints.0.value", "tv1"),
204+
resource.TestCheckResourceAttr(
205+
"ovh_cloud_project_kube_nodepool.pool", "template.0.spec.0.unschedulable", "false"),
206+
),
207+
},
208+
{
209+
Config: configUpdated,
210+
Check: resource.ComposeTestCheckFunc(
211+
resource.TestCheckResourceAttr(
212+
"ovh_cloud_project_kube.cluster", "region", region),
213+
resource.TestCheckResourceAttrSet(
214+
"ovh_cloud_project_kube.cluster", "kubeconfig"),
215+
resource.TestCheckResourceAttr(
216+
"ovh_cloud_project_kube.cluster", "name", name),
217+
resource.TestCheckResourceAttr(
218+
"ovh_cloud_project_kube.cluster", "version", version),
219+
resource.TestCheckResourceAttr(
220+
"ovh_cloud_project_kube_nodepool.pool", "name", name),
221+
resource.TestCheckResourceAttr(
222+
"ovh_cloud_project_kube_nodepool.pool", "flavor_name", "b2-7"),
223+
resource.TestCheckResourceAttr(
224+
"ovh_cloud_project_kube_nodepool.pool", "desired_nodes", "1"),
225+
resource.TestCheckResourceAttr(
226+
"ovh_cloud_project_kube_nodepool.pool", "min_nodes", "0"),
227+
resource.TestCheckResourceAttr(
228+
"ovh_cloud_project_kube_nodepool.pool", "max_nodes", "1"),
229+
resource.TestCheckResourceAttr(
230+
"ovh_cloud_project_kube_nodepool.pool", "template.0.metadata.0.annotations.a2", "av2"),
231+
resource.TestCheckResourceAttr(
232+
"ovh_cloud_project_kube_nodepool.pool", "template.0.metadata.0.finalizers.#", "0"),
233+
resource.TestCheckResourceAttr(
234+
"ovh_cloud_project_kube_nodepool.pool", "template.0.metadata.0.labels.l2", "lv2"),
235+
resource.TestCheckResourceAttr(
236+
"ovh_cloud_project_kube_nodepool.pool", "template.0.spec.0.taints.#", "0"),
135237
),
136238
},
137239
},

ovh/resource_cloud_project_kube_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ resource "ovh_cloud_project_kube" "cluster" {
7272
version = "%s"
7373
}
7474
`
75+
7576
var testAccCloudProjectKubeEmptyVersionConfig = `
7677
resource "ovh_cloud_project_kube" "cluster" {
7778
service_name = "%s"

ovh/types_cloud.go

+101-13
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func (u CloudProjectUser) ToMap() map[string]interface{} {
117117
obj := make(map[string]interface{})
118118
obj["creation_date"] = u.CreationDate
119119
obj["description"] = u.Description
120-
//Dont set password as it must be set only at creation time
120+
// Dont set password as it must be set only at creation time
121121
obj["status"] = u.Status
122122
obj["username"] = u.Username
123123

@@ -246,14 +246,15 @@ type CloudProjectKubeKubeConfigResponse struct {
246246
}
247247

248248
type CloudProjectKubeNodePoolCreateOpts struct {
249-
AntiAffinity *bool `json:"antiAffinity,omitempty"`
250-
Autoscale *bool `json:"autoscale,omitempty"`
251-
DesiredNodes *int `json:"desiredNodes,omitempty"`
252-
FlavorName string `json:"flavorName"`
253-
MaxNodes *int `json:"maxNodes,omitempty"`
254-
MinNodes *int `json:"minNodes,omitempty"`
255-
MonthlyBilled *bool `json:"monthlyBilled,omitempty"`
256-
Name *string `json:"name,omitempty"`
249+
AntiAffinity *bool `json:"antiAffinity,omitempty"`
250+
Autoscale *bool `json:"autoscale,omitempty"`
251+
DesiredNodes *int `json:"desiredNodes,omitempty"`
252+
FlavorName string `json:"flavorName"`
253+
MaxNodes *int `json:"maxNodes,omitempty"`
254+
MinNodes *int `json:"minNodes,omitempty"`
255+
MonthlyBilled *bool `json:"monthlyBilled,omitempty"`
256+
Name *string `json:"name,omitempty"`
257+
Template *CloudProjectKubeNodePoolTemplate `json:"template,omitempty"`
257258
}
258259

259260
func (opts *CloudProjectKubeNodePoolCreateOpts) FromResource(d *schema.ResourceData) *CloudProjectKubeNodePoolCreateOpts {
@@ -265,26 +266,113 @@ func (opts *CloudProjectKubeNodePoolCreateOpts) FromResource(d *schema.ResourceD
265266
opts.MinNodes = helpers.GetNilIntPointerFromData(d, "min_nodes")
266267
opts.MonthlyBilled = helpers.GetNilBoolPointerFromData(d, "monthly_billed")
267268
opts.Name = helpers.GetNilStringPointerFromData(d, "name")
269+
opts.Template = loadNodelPoolTemplateFromResource(d.Get("template"))
268270

269271
return opts
270272
}
271273

274+
func loadNodelPoolTemplateFromResource(i interface{}) *CloudProjectKubeNodePoolTemplate {
275+
template := CloudProjectKubeNodePoolTemplate{
276+
Metadata: &CloudProjectKubeNodePoolTemplateMetadata{
277+
Annotations: map[string]string{},
278+
Finalizers: []string{},
279+
Labels: map[string]string{},
280+
},
281+
Spec: &CloudProjectKubeNodePoolTemplateSpec{
282+
Taints: []Taint{},
283+
Unschedulable: false,
284+
},
285+
}
286+
287+
templateSet := i.(*schema.Set).List()
288+
for _, inter := range templateSet {
289+
290+
metadataSet := inter.(map[string]interface{})["metadata"].(*schema.Set).List()
291+
for _, meta := range metadataSet {
292+
293+
annotations := meta.(map[string]interface{})["annotations"].(map[string]interface{})
294+
template.Metadata.Annotations = make(map[string]string)
295+
for k, v := range annotations {
296+
template.Metadata.Annotations[k] = v.(string)
297+
}
298+
299+
labels := meta.(map[string]interface{})["labels"].(map[string]interface{})
300+
template.Metadata.Labels = make(map[string]string)
301+
for k, v := range labels {
302+
template.Metadata.Labels[k] = v.(string)
303+
}
304+
305+
finalizers := meta.(map[string]interface{})["finalizers"].([]interface{})
306+
for _, finalizer := range finalizers {
307+
template.Metadata.Finalizers = append(template.Metadata.Finalizers, finalizer.(string))
308+
}
309+
310+
}
311+
312+
specSet := inter.(map[string]interface{})["spec"].(*schema.Set).List()
313+
for _, spec := range specSet {
314+
315+
taints := spec.(map[string]interface{})["taints"].([]interface{})
316+
for _, taint := range taints {
317+
template.Spec.Taints = append(template.Spec.Taints, Taint{
318+
Effect: taint.(map[string]interface{})["effect"].(string),
319+
Key: taint.(map[string]interface{})["key"].(string),
320+
Value: taint.(map[string]interface{})["value"].(string),
321+
})
322+
}
323+
324+
unschedulable := spec.(map[string]interface{})["unschedulable"].(bool)
325+
template.Spec.Unschedulable = unschedulable
326+
327+
}
328+
}
329+
330+
return &template
331+
}
332+
272333
func (s *CloudProjectKubeNodePoolCreateOpts) String() string {
273334
return fmt.Sprintf("%s(%s): %d/%d/%d", *s.Name, s.FlavorName, *s.DesiredNodes, *s.MinNodes, *s.MaxNodes)
274335
}
275336

337+
type Taint struct {
338+
//"NoExecute"
339+
//"NoSchedule"
340+
//"PreferNoSchedule"
341+
Effect string `json:"effect,omitempty"`
342+
Key string `json:"key,omitempty"`
343+
Value string `json:"value,omitempty"`
344+
}
345+
type CloudProjectKubeNodePoolTemplateMetadata struct {
346+
Annotations map[string]string `json:"annotations"`
347+
Finalizers []string `json:"finalizers"`
348+
Labels map[string]string `json:"labels"`
349+
}
350+
351+
type CloudProjectKubeNodePoolTemplateSpec struct {
352+
Taints []Taint `json:"taints"`
353+
Unschedulable bool `json:"unschedulable"`
354+
}
355+
356+
type CloudProjectKubeNodePoolTemplate struct {
357+
Metadata *CloudProjectKubeNodePoolTemplateMetadata `json:"metadata,omitempty"`
358+
Spec *CloudProjectKubeNodePoolTemplateSpec `json:"spec,omitempty"`
359+
}
360+
276361
type CloudProjectKubeNodePoolUpdateOpts struct {
277-
Autoscale *bool `json:"autoscale,omitempty"`
278-
DesiredNodes *int `json:"desiredNodes,omitempty"`
279-
MaxNodes *int `json:"maxNodes,omitempty"`
280-
MinNodes *int `json:"minNodes,omitempty"`
362+
Autoscale *bool `json:"autoscale,omitempty"`
363+
DesiredNodes *int `json:"desiredNodes,omitempty"`
364+
MaxNodes *int `json:"maxNodes,omitempty"`
365+
MinNodes *int `json:"minNodes,omitempty"`
366+
Template *CloudProjectKubeNodePoolTemplate `json:"template,omitempty"`
281367
}
282368

283369
func (opts *CloudProjectKubeNodePoolUpdateOpts) FromResource(d *schema.ResourceData) *CloudProjectKubeNodePoolUpdateOpts {
284370
opts.Autoscale = helpers.GetNilBoolPointerFromData(d, "autoscale")
285371
opts.DesiredNodes = helpers.GetNilIntPointerFromData(d, "desired_nodes")
286372
opts.MaxNodes = helpers.GetNilIntPointerFromData(d, "max_nodes")
287373
opts.MinNodes = helpers.GetNilIntPointerFromData(d, "min_nodes")
374+
opts.Template = loadNodelPoolTemplateFromResource(d.Get("template"))
375+
288376
return opts
289377
}
290378

0 commit comments

Comments
 (0)