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

feat: support for lb in aks #397

Merged
merged 5 commits into from
Oct 10, 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
115 changes: 115 additions & 0 deletions castai/resource_node_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const (
FieldNodeConfigurationEKSTargetGroup = "target_group"
FieldNodeConfigurationAKSImageFamily = "aks_image_family"
FieldNodeConfigurationEKSImageFamily = "eks_image_family"
FieldNodeConfigurationAKSLoadbalaners = "loadbalancers"
)

const (
Expand Down Expand Up @@ -321,6 +322,34 @@ func resourceNodeConfiguration() *schema.Resource {
return strings.EqualFold(oldValue, newValue)
},
},
FieldNodeConfigurationAKSLoadbalaners: {
Type: schema.TypeList,
Optional: true,
Description: "Loadboalancer configuration for CAST provisioned nodes",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "Name of loadbalancer",
},
"ip_based_backend_pools": {
Type: schema.TypeList,
Optional: true,
Description: "IP based backend pools configuration for CAST provisioned nodes",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "Name of the ip based backend pool",
},
},
},
},
},
},
},
},
},
},
Expand Down Expand Up @@ -880,9 +909,54 @@ func toAKSSConfig(obj map[string]interface{}) *sdk.NodeconfigV1AKSConfig {
out.ImageFamily = toAKSImageFamily(v)
}

if v, ok := obj[FieldNodeConfigurationAKSLoadbalaners].([]interface{}); ok && len(v) > 0 {
out.LoadBalancers = toAksLoadBalancers(v)
}

return out
}

func toAksLoadBalancers(obj []interface{}) *[]sdk.NodeconfigV1AKSConfigLoadBalancers {
if obj == nil {
return nil
}

out := make([]sdk.NodeconfigV1AKSConfigLoadBalancers, 0, len(obj))
for _, lbRaw := range obj {
if lb, ok := lbRaw.(map[string]interface{}); ok {
sdkLB := sdk.NodeconfigV1AKSConfigLoadBalancers{}
if name, ok := lb["name"].(string); ok && name != "" {
sdkLB.Name = lo.ToPtr(name)
}
if ipBasedBackendPools, ok := lb["ip_based_backend_pools"].([]interface{}); ok && len(ipBasedBackendPools) > 0 {
sdkLB.IpBasedBackendPools = toAksIpBasedBackendPools(ipBasedBackendPools)
}
out = append(out, sdkLB)
}
}

return &out
}

func toAksIpBasedBackendPools(obj []interface{}) *[]sdk.NodeconfigV1AKSConfigLoadBalancersIPBasedBackendPool {
if obj == nil {
return nil
}

out := make([]sdk.NodeconfigV1AKSConfigLoadBalancersIPBasedBackendPool, 0, len(obj))
for _, poolRaw := range obj {
if pool, ok := poolRaw.(map[string]interface{}); ok {
sdkPool := sdk.NodeconfigV1AKSConfigLoadBalancersIPBasedBackendPool{}
if name, ok := pool["name"].(string); ok && name != "" {
sdkPool.Name = lo.ToPtr(name)
}
out = append(out, sdkPool)
}
}

return &out
}

func toAKSOSDiskType(v string) *sdk.NodeconfigV1AKSConfigOsDiskType {
if v == "" {
return nil
Expand Down Expand Up @@ -932,9 +1006,50 @@ func flattenAKSConfig(config *sdk.NodeconfigV1AKSConfig) []map[string]interface{
m[FieldNodeConfigurationAKSImageFamily] = fromAKSImageFamily(*v)
}

if v := config.LoadBalancers; v != nil && len(*v) > 0 {
m[FieldNodeConfigurationAKSLoadbalaners] = fromAksLoadBalancers(*v)
}

return []map[string]interface{}{m}
}

func fromAksLoadBalancers(lbs []sdk.NodeconfigV1AKSConfigLoadBalancers) []map[string]interface{} {
if lbs == nil {
return nil
}

out := make([]map[string]interface{}, 0, len(lbs))
for _, lb := range lbs {
m := map[string]interface{}{}
if lb.Name != nil {
m["name"] = *lb.Name
}
if lb.IpBasedBackendPools != nil && len(*lb.IpBasedBackendPools) > 0 {
m["ip_based_backend_pools"] = fromAksIpBasedBackendPools(*lb.IpBasedBackendPools)
}
out = append(out, m)
}

return out
}

func fromAksIpBasedBackendPools(pools []sdk.NodeconfigV1AKSConfigLoadBalancersIPBasedBackendPool) []map[string]interface{} {
if pools == nil {
return nil
}

out := make([]map[string]interface{}, 0, len(pools))
for _, pool := range pools {
m := map[string]interface{}{}
if pool.Name != nil {
m["name"] = *pool.Name
}
out = append(out, m)
}

return out
}

func fromAKSDiskType(osDiskType *sdk.NodeconfigV1AKSConfigOsDiskType) string {
if osDiskType == nil {
return ""
Expand Down
8 changes: 8 additions & 0 deletions castai/resource_node_configuration_aks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ func TestAccResourceNodeConfiguration_aks(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "min_disk_size", "121"),
resource.TestCheckResourceAttr(resourceName, "aks.0.max_pods_per_node", "32"),
resource.TestCheckResourceAttr(resourceName, "aks.0.aks_image_family", "azure-linux"),
resource.TestCheckResourceAttr(resourceName, "aks.0.loadbalancers.0.name", "test-lb"),
resource.TestCheckResourceAttr(resourceName, "aks.0.loadbalancers.0.ip_based_backend_pools.0.name", "test"),
resource.TestCheckResourceAttr(resourceName, "eks.#", "0"),
resource.TestCheckResourceAttr(resourceName, "kops.#", "0"),
resource.TestCheckResourceAttr(resourceName, "gke.#", "0"),
Expand Down Expand Up @@ -100,6 +102,12 @@ resource "castai_node_configuration" "test" {
aks {
max_pods_per_node = 32
aks_image_family = "azure-linux"
loadbalancers {
name = "test-lb"
ip_based_backend_pools {
name = "test"
}
}
}
}
`, rgName, rName))
Expand Down
20 changes: 17 additions & 3 deletions castai/sdk/api.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions docs/resources/node_configuration.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading