From 4d5618533f0e40aa2f5ac903960ebd389d008d10 Mon Sep 17 00:00:00 2001 From: Valentyna Bukhalova Date: Thu, 11 Apr 2024 12:39:40 +0200 Subject: [PATCH] add tests --- castai/resource_node_configuration.go | 24 +++++++++--- .../resource_node_configuration_eks_test.go | 2 + .../resource_node_configuration_gke_test.go | 2 + castai/resource_node_configuration_test.go | 10 +++-- castai/sdk/api.gen.go | 37 ++++++++++++------- docs/resources/node_configuration.md | 1 + 6 files changed, 54 insertions(+), 22 deletions(-) diff --git a/castai/resource_node_configuration.go b/castai/resource_node_configuration.go index 2cf247d1..54106acc 100644 --- a/castai/resource_node_configuration.go +++ b/castai/resource_node_configuration.go @@ -25,6 +25,7 @@ const ( FieldNodeConfigurationName = "name" FieldNodeConfigurationDiskCpuRatio = "disk_cpu_ratio" FieldNodeConfigurationMinDiskSize = "min_disk_size" + FieldNodeConfigurationDrainTimeoutSec = "drain_timeout_sec" FieldNodeConfigurationSubnets = "subnets" FieldNodeConfigurationSSHPublicKey = "ssh_public_key" FieldNodeConfigurationImage = "image" @@ -79,6 +80,13 @@ func resourceNodeConfiguration() *schema.Resource { ValidateDiagFunc: validation.ToDiagFunc(validation.IntAtLeast(0)), Description: "Disk to CPU ratio. Sets the number of GiBs to be added for every CPU on the node. Defaults to 0", }, + FieldNodeConfigurationDrainTimeoutSec: { + Type: schema.TypeInt, + Optional: true, + Default: 0, + ValidateDiagFunc: validation.ToDiagFunc(validation.IntAtLeast(-1)), + Description: "Timeout in seconds for draining the node. Defaults to 0", + }, FieldNodeConfigurationMinDiskSize: { Type: schema.TypeInt, Optional: true, @@ -317,9 +325,10 @@ func resourceNodeConfigurationCreate(ctx context.Context, d *schema.ResourceData clusterID := d.Get(FieldClusterID).(string) req := sdk.NodeConfigurationAPICreateConfigurationJSONRequestBody{ - Name: d.Get(FieldNodeConfigurationName).(string), - DiskCpuRatio: toPtr(int32(d.Get(FieldNodeConfigurationDiskCpuRatio).(int))), - MinDiskSize: toPtr(int32(d.Get(FieldNodeConfigurationMinDiskSize).(int))), + Name: d.Get(FieldNodeConfigurationName).(string), + DiskCpuRatio: toPtr(int32(d.Get(FieldNodeConfigurationDiskCpuRatio).(int))), + DrainTimeoutSec: toPtr(int32(d.Get(FieldNodeConfigurationDrainTimeoutSec).(int))), + MinDiskSize: toPtr(int32(d.Get(FieldNodeConfigurationMinDiskSize).(int))), } if v, ok := d.GetOk(FieldNodeConfigurationSubnets); ok { @@ -406,6 +415,9 @@ func resourceNodeConfigurationRead(ctx context.Context, d *schema.ResourceData, if err := d.Set(FieldNodeConfigurationDiskCpuRatio, nodeConfig.DiskCpuRatio); err != nil { return diag.FromErr(fmt.Errorf("setting disk cpu ratio: %w", err)) } + if err := d.Set(FieldNodeConfigurationDrainTimeoutSec, nodeConfig.DrainTimeoutSec); err != nil { + return diag.FromErr(fmt.Errorf("setting drain timeout: %w", err)) + } if err := d.Set(FieldNodeConfigurationMinDiskSize, nodeConfig.MinDiskSize); err != nil { return diag.FromErr(fmt.Errorf("setting min disk size: %w", err)) } @@ -466,6 +478,7 @@ func resourceNodeConfigurationRead(ctx context.Context, d *schema.ResourceData, func resourceNodeConfigurationUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { if !d.HasChanges( FieldNodeConfigurationDiskCpuRatio, + FieldNodeConfigurationDrainTimeoutSec, FieldNodeConfigurationMinDiskSize, FieldNodeConfigurationSubnets, FieldNodeConfigurationSSHPublicKey, @@ -487,8 +500,9 @@ func resourceNodeConfigurationUpdate(ctx context.Context, d *schema.ResourceData client := meta.(*ProviderConfig).api clusterID := d.Get(FieldClusterID).(string) req := sdk.NodeConfigurationAPIUpdateConfigurationJSONRequestBody{ - DiskCpuRatio: toPtr(int32(d.Get(FieldNodeConfigurationDiskCpuRatio).(int))), - MinDiskSize: toPtr(int32(d.Get(FieldNodeConfigurationMinDiskSize).(int))), + DiskCpuRatio: toPtr(int32(d.Get(FieldNodeConfigurationDiskCpuRatio).(int))), + MinDiskSize: toPtr(int32(d.Get(FieldNodeConfigurationMinDiskSize).(int))), + DrainTimeoutSec: toPtr(int32(d.Get(FieldNodeConfigurationDrainTimeoutSec).(int))), } if v, ok := d.GetOk(FieldNodeConfigurationSubnets); ok { diff --git a/castai/resource_node_configuration_eks_test.go b/castai/resource_node_configuration_eks_test.go index 3b5a2d63..e079afcc 100644 --- a/castai/resource_node_configuration_eks_test.go +++ b/castai/resource_node_configuration_eks_test.go @@ -67,6 +67,7 @@ func TestAccResourceNodeConfiguration_eks(t *testing.T) { Config: testAccEKSNodeConfigurationUpdated(rName, clusterName), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr(resourceName, "disk_cpu_ratio", "0"), + resource.TestCheckResourceAttr(resourceName, "drain_timeout_sec", "120"), resource.TestCheckResourceAttr(resourceName, "min_disk_size", "100"), resource.TestCheckResourceAttr(resourceName, "image", "amazon-eks-node-1.23-v20220824"), resource.TestCheckResourceAttr(resourceName, "init_script", ""), @@ -149,6 +150,7 @@ func testAccEKSNodeConfigurationUpdated(rName, clusterName string) string { resource "castai_node_configuration" "test" { name = %[1]q cluster_id = castai_eks_cluster.test.id + drain_timeout_sec = 120 subnets = data.aws_subnets.core.ids image = "amazon-eks-node-1.23-v20220824" container_runtime = "containerd" diff --git a/castai/resource_node_configuration_gke_test.go b/castai/resource_node_configuration_gke_test.go index 46533fee..e45c93d5 100644 --- a/castai/resource_node_configuration_gke_test.go +++ b/castai/resource_node_configuration_gke_test.go @@ -25,6 +25,7 @@ func TestAccResourceNodeConfiguration_gke(t *testing.T) { Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr(resourceName, "name", rName), resource.TestCheckResourceAttr(resourceName, "disk_cpu_ratio", "35"), + resource.TestCheckResourceAttr(resourceName, "drain_temout_sec", "0"), resource.TestCheckResourceAttr(resourceName, "min_disk_size", "122"), resource.TestCheckResourceAttr(resourceName, "aks.#", "0"), resource.TestCheckResourceAttr(resourceName, "eks.#", "0"), @@ -69,6 +70,7 @@ resource "castai_node_configuration" "test" { name = %[1]q cluster_id = castai_gke_cluster.test.id disk_cpu_ratio = 35 + drain_timeout_sec = 0 min_disk_size = 122 subnets = [local.subnet_id] tags = { diff --git a/castai/resource_node_configuration_test.go b/castai/resource_node_configuration_test.go index f72853fc..125aa0b6 100644 --- a/castai/resource_node_configuration_test.go +++ b/castai/resource_node_configuration_test.go @@ -50,8 +50,9 @@ func Test_resourceNodeConfigurationCreate(t *testing.T) { ImdsHopLimit: toPtr(int32(0)), ImdsV1: toPtr(false), }, - DiskCpuRatio: toPtr(int32(0)), - MinDiskSize: toPtr(int32(0)), + DiskCpuRatio: toPtr(int32(0)), + DrainTimeoutSec: toPtr(int32(0)), + MinDiskSize: toPtr(int32(0)), }). Return( &http.Response{ @@ -179,8 +180,9 @@ func Test_NodeConfiguration_UpdateContext(t *testing.T) { ImdsHopLimit: toPtr(int32(0)), ImdsV1: toPtr(false), }, - DiskCpuRatio: toPtr(int32(0)), - MinDiskSize: toPtr(int32(100)), + DiskCpuRatio: toPtr(int32(0)), + DrainTimeoutSec: toPtr(int32(0)), + MinDiskSize: toPtr(int32(100)), }). Return( &http.Response{ diff --git a/castai/sdk/api.gen.go b/castai/sdk/api.gen.go index 36f7d3a1..513b17db 100644 --- a/castai/sdk/api.gen.go +++ b/castai/sdk/api.gen.go @@ -87,8 +87,8 @@ const ( In K8sSelectorV1Operator = "in" Lt K8sSelectorV1Operator = "Lt" Lt1 K8sSelectorV1Operator = "lt" - NotIn K8sSelectorV1Operator = "notIn" - NotInt K8sSelectorV1Operator = "NotInt" + NotIn K8sSelectorV1Operator = "NotIn" + NotIn1 K8sSelectorV1Operator = "notIn" ) // Defines values for NodeconfigV1AKSConfigOsDiskType. @@ -1577,7 +1577,7 @@ type K8sSelectorV1KubernetesNodeAffinity struct { Key string `json:"key"` // - IN: In values - // - NotInt: Not int values + // - NotIn: Not in values // - Exists: Just exist // - DoesNotExist: Values does not exist // - Gt: Greater then @@ -1587,7 +1587,7 @@ type K8sSelectorV1KubernetesNodeAffinity struct { } // - IN: In values -// - NotInt: Not int values +// - NotIn: Not in values // - Exists: Just exist // - DoesNotExist: Values does not exist // - Gt: Greater then @@ -1685,8 +1685,11 @@ type NodeconfigV1NewNodeConfiguration struct { // Optional docker daemon configuration properties. Provide only properties that you want to override. Available values https://docs.docker.com/engine/reference/commandline/dockerd/#daemon-configuration-file DockerConfig *map[string]interface{} `json:"dockerConfig,omitempty"` - Eks *NodeconfigV1EKSConfig `json:"eks,omitempty"` - Gke *NodeconfigV1GKEConfig `json:"gke,omitempty"` + + // Drain timeout in seconds. Defaults to 0. + DrainTimeoutSec *int32 `json:"drainTimeoutSec"` + Eks *NodeconfigV1EKSConfig `json:"eks,omitempty"` + Gke *NodeconfigV1GKEConfig `json:"gke,omitempty"` // Image to be used while provisioning the node. If nothing is provided will be resolved to latest available image based on Kubernetes version if possible. Image *string `json:"image"` @@ -1737,8 +1740,11 @@ type NodeconfigV1NodeConfiguration struct { // Optional docker daemon configuration properties. Applicable for EKS only. DockerConfig *map[string]interface{} `json:"dockerConfig"` - Eks *NodeconfigV1EKSConfig `json:"eks,omitempty"` - Gke *NodeconfigV1GKEConfig `json:"gke,omitempty"` + + // Drain timeout in seconds. Defaults to 0. + DrainTimeoutSec *int32 `json:"drainTimeoutSec"` + Eks *NodeconfigV1EKSConfig `json:"eks,omitempty"` + Gke *NodeconfigV1GKEConfig `json:"gke,omitempty"` // The node configuration ID. Id *string `json:"id,omitempty"` @@ -1792,8 +1798,11 @@ type NodeconfigV1NodeConfigurationUpdate struct { // Optional docker daemon configuration properties. Provide only properties that you want to override. Available values https://docs.docker.com/engine/reference/commandline/dockerd/#daemon-configuration-file DockerConfig *map[string]interface{} `json:"dockerConfig,omitempty"` - Eks *NodeconfigV1EKSConfig `json:"eks,omitempty"` - Gke *NodeconfigV1GKEConfig `json:"gke,omitempty"` + + // Drain timeout in seconds. Defaults to 0. + DrainTimeoutSec *int32 `json:"drainTimeoutSec"` + Eks *NodeconfigV1EKSConfig `json:"eks,omitempty"` + Gke *NodeconfigV1GKEConfig `json:"gke,omitempty"` // Image to be used while provisioning the node. If nothing is provided will be resolved to latest available image based on Kubernetes version if possible. Image *string `json:"image"` @@ -2117,9 +2126,11 @@ type NodetemplatesV1TemplateConstraintsCustomPriority struct { // NodetemplatesV1TemplateConstraintsDedicatedNodeAffinity defines model for nodetemplates.v1.TemplateConstraints.DedicatedNodeAffinity. type NodetemplatesV1TemplateConstraintsDedicatedNodeAffinity struct { - AzName *string `json:"azName,omitempty"` - InstanceTypes *[]string `json:"instanceTypes,omitempty"` - Name *string `json:"name,omitempty"` + // The affinity rules required for choosing the node. + Affinity *[]K8sSelectorV1KubernetesNodeAffinity `json:"affinity,omitempty"` + AzName *string `json:"azName,omitempty"` + InstanceTypes *[]string `json:"instanceTypes,omitempty"` + Name *string `json:"name,omitempty"` } // NodetemplatesV1TemplateConstraintsGPUConstraints defines model for nodetemplates.v1.TemplateConstraints.GPUConstraints. diff --git a/docs/resources/node_configuration.md b/docs/resources/node_configuration.md index 30cf4f38..00795e05 100644 --- a/docs/resources/node_configuration.md +++ b/docs/resources/node_configuration.md @@ -66,6 +66,7 @@ resource "castai_node_configuration" "default" { - `container_runtime` (String) Optional container runtime to be used by kubelet. Applicable for EKS only. Supported values include: `dockerd`, `containerd` - `disk_cpu_ratio` (Number) Disk to CPU ratio. Sets the number of GiBs to be added for every CPU on the node. Defaults to 0 - `docker_config` (String) Optional docker daemon configuration properties in JSON format. Provide only properties that you want to override. Applicable for EKS only. [Available values](https://docs.docker.com/engine/reference/commandline/dockerd/#daemon-configuration-file) +- `drain_timeout_sec` (Number) Timeout in seconds for draining the node. Defaults to 0 - `eks` (Block List, Max: 1) (see [below for nested schema](#nestedblock--eks)) - `gke` (Block List, Max: 1) (see [below for nested schema](#nestedblock--gke)) - `image` (String) Image to be used while provisioning the node. If nothing is provided will be resolved to latest available image based on Kubernetes version if possible