Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/484 #535

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions ovh/resource_cloud_project_kube_nodepool.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ovh

import (
"errors"
"fmt"
"log"
"strings"
Expand Down Expand Up @@ -203,6 +204,25 @@ func resourceCloudProjectKubeNodePool() *schema.Resource {
Elem: &schema.Schema{
Type: schema.TypeMap,
Set: schema.HashString,
ValidateFunc: func(taintInterface interface{}, path string) (warning []string, errorList []error) {
taint := taintInterface.(map[string]interface{})

if taint["key"] == nil {
return nil, []error{errors.New(fmt.Sprintf("key attribute is mandatory for taint: %s", path))}
}

if taint["effect"] == nil {
return nil, []error{errors.New(fmt.Sprintf("effect attribute is mandatory for taint: %s", path))}
}

effectString := taint["effect"].(string)
effect := TaintEffecTypeToID[effectString]
if effect == NotATaint {
return nil, []error{fmt.Errorf("effect: %s is not a allowable taint %#v", effectString, TaintEffecTypeToID)}
}

return nil, nil
},
},
},
},
Expand Down
153 changes: 153 additions & 0 deletions ovh/resource_cloud_project_kube_nodepool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"os"
"regexp"
"strings"
"testing"
"time"
Expand All @@ -13,6 +14,12 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

var (
effectTaintsErrorRegex = regexp.MustCompile("(.)*effect attribute is mandatory for taint(.)*")
keyTaintsErrorRegex = regexp.MustCompile("(.)*key attribute is mandatory for taint(.)*")
valueNoCrashTaintsErrorRegex = regexp.MustCompile("(.)*This service does not exist(.)*")
)

func init() {
resource.AddTestSweepers("ovh_cloud_project_kube_nodepool", &resource.Sweeper{
Name: "ovh_cloud_project_kube_nodepool",
Expand Down Expand Up @@ -73,6 +80,105 @@ func testSweepCloudProjectKubeNodePool(region string) error {
return nil
}

var testAccCloudProjectKubeNodePoolConfigEffectMissingInTaint = `
resource "ovh_cloud_project_kube_nodepool" "pool" {
service_name = "xxx"
kube_id = "xxx"
name = "xxx"
flavor_name = "b2-7"
desired_nodes = 1
min_nodes = 0
max_nodes = 1
template {
metadata {
annotations = {
a1 = "av1"
}
finalizers = ["finalizer.extensions/v1beta1"]
labels = {
l1 = "lv1"
}
}
spec {
unschedulable = false
taints = [
{
#effect = "PreferNoSchedule"
key = "t1"
value = "tv1"
}
]
}
}
}
`

var testAccCloudProjectKubeNodePoolConfigKeyMissingInTaint = `
resource "ovh_cloud_project_kube_nodepool" "pool" {
service_name = "xxx"
kube_id = "xxx"
name = "xxx"
flavor_name = "b2-7"
desired_nodes = 1
min_nodes = 0
max_nodes = 1
template {
metadata {
annotations = {
a1 = "av1"
}
finalizers = ["finalizer.extensions/v1beta1"]
labels = {
l1 = "lv1"
}
}
spec {
unschedulable = false
taints = [
{
effect = "PreferNoSchedule"
#key = "t1"
value = "tv1"
}
]
}
}
}
`

var testAccCloudProjectKubeNodePoolConfigValueMissingInTaint = `
resource "ovh_cloud_project_kube_nodepool" "pool" {
service_name = "xxx"
kube_id = "xxx"
name = "xxx"
flavor_name = "b2-7"
desired_nodes = 1
min_nodes = 0
max_nodes = 1
template {
metadata {
annotations = {
a1 = "av1"
}
finalizers = ["finalizer.extensions/v1beta1"]
labels = {
l1 = "lv1"
}
}
spec {
unschedulable = false
taints = [
{
effect = "PreferNoSchedule"
key = "t1"
#value = "tv1"
}
]
}
}
}
`

var testAccCloudProjectKubeNodePoolConfig = `
resource "ovh_cloud_project_kube" "cluster" {
service_name = "%s"
Expand Down Expand Up @@ -297,3 +403,50 @@ func TestAccCloudProjectKubeNodePool(t *testing.T) {
},
})
}

func TestAccCloudProjectKubeNodePoolTaints(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheckCloud(t)
testAccCheckCloudProjectExists(t)
testAccPreCheckKubernetes(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCloudProjectKubeNodePoolConfigEffectMissingInTaint,
ExpectError: effectTaintsErrorRegex,
},
},
})

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheckCloud(t)
testAccCheckCloudProjectExists(t)
testAccPreCheckKubernetes(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCloudProjectKubeNodePoolConfigKeyMissingInTaint,
ExpectError: keyTaintsErrorRegex,
},
},
})

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheckCloud(t)
testAccCheckCloudProjectExists(t)
testAccPreCheckKubernetes(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCloudProjectKubeNodePoolConfigValueMissingInTaint,
ExpectError: valueNoCrashTaintsErrorRegex,
},
},
})
}
10 changes: 7 additions & 3 deletions ovh/types_cloud_project_kube_nodepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,15 @@ func loadNodelPoolTemplateFromResource(i interface{}) (*CloudProjectKubeNodePool
return nil, fmt.Errorf("effect: %s is not a allowable taint %#v", effectString, TaintEffecTypeToID)
}

template.Spec.Taints = append(template.Spec.Taints, Taint{
taintObject := Taint{
Effect: effect,
Key: taint.(map[string]interface{})["key"].(string),
Value: taint.(map[string]interface{})["value"].(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
Expand Down
5 changes: 4 additions & 1 deletion website/docs/r/cloud_project_kube_nodepool.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ The following arguments are supported:
* `finalizers` - Finalizers to apply to each node. A finalizer name must be fully qualified, e.g. kubernetes.io/pv-protection , where you prefix it with hostname of your service which is related to the controller responsible for the finalizer.
* `labels` - Labels to apply to each node
* `spec` - Spec of each node in the pool
* `taints` - Taints to apply to each node
* `taints` - Taints to apply to each node [NodeSpec kubernetes documentation](https://kubernetes.io/docs/reference/kubernetes-api/cluster-resources/node-v1/#NodeSpec)
* `effect` - mandatory possible values: NoExecute, NoSchedule, PreferNoSchedule
* `key` - mandatory
* `value` - (Optional)
* `unschedulable` - If true, set nodes as un-schedulable

## Attributes Reference
Expand Down