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

Improvement: azurerm_kubernetes_cluster - Add support for load_balancer_profile #5394

Merged
merged 8 commits into from
Jan 24, 2020
183 changes: 168 additions & 15 deletions azurerm/internal/services/containers/resource_arm_kubernetes_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,47 @@ func resourceArmKubernetesCluster() *schema.Resource {
}, true),
DiffSuppressFunc: suppress.CaseDifference,
},
"load_balancer_profile": {
Type: schema.TypeList,
MaxItems: 1,
ForceNew: true,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"managed_outbound_ips": {
evenh marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntBetween(1, 100),
ConflictsWith: []string{"network_profile.0.load_balancer_profile.0.outbound_ip_prefixes_ids", "network_profile.0.load_balancer_profile.0.outbound_ip_address_ids"},
},
"outbound_ip_prefixes_ids": {
evenh marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeSet,
Optional: true,
ConflictsWith: []string{"network_profile.0.load_balancer_profile.0.managed_outbound_ips", "network_profile.0.load_balancer_profile.0.outbound_ip_address_ids"},
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: azure.ValidateResourceID,
},
},
"outbound_ip_address_ids": {
Type: schema.TypeSet,
Optional: true,
ConflictsWith: []string{"network_profile.0.load_balancer_profile.0.managed_outbound_ips", "network_profile.0.load_balancer_profile.0.outbound_ip_prefixes_ids"},
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: azure.ValidateResourceID,
},
},
"effective_outbound_ips": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
},
},
},
Expand Down Expand Up @@ -656,7 +697,10 @@ func resourceArmKubernetesClusterCreate(d *schema.ResourceData, meta interface{}
addonProfiles := ExpandKubernetesAddOnProfiles(addOnProfilesRaw)

networkProfileRaw := d.Get("network_profile").([]interface{})
networkProfile := expandKubernetesClusterNetworkProfile(networkProfileRaw)
networkProfile, err := expandKubernetesClusterNetworkProfile(networkProfileRaw)
if err != nil {
return err
}

rbacRaw := d.Get("role_based_access_control").([]interface{})
rbacEnabled, azureADProfile := expandKubernetesClusterRoleBasedAccessControl(rbacRaw, tenantId)
Expand Down Expand Up @@ -822,7 +866,11 @@ func resourceArmKubernetesClusterUpdate(d *schema.ResourceData, meta interface{}
if d.HasChange("network_profile") {
updateCluster = true
networkProfileRaw := d.Get("network_profile").([]interface{})
networkProfile := expandKubernetesClusterNetworkProfile(networkProfileRaw)
networkProfile, err := expandKubernetesClusterNetworkProfile(networkProfileRaw)
if err != nil {
return err
}

existing.ManagedClusterProperties.NetworkProfile = networkProfile
}

Expand Down Expand Up @@ -1362,9 +1410,9 @@ func flattenKubernetesClusterWindowsProfile(profile *containerservice.ManagedClu
}
}

func expandKubernetesClusterNetworkProfile(input []interface{}) *containerservice.NetworkProfileType {
func expandKubernetesClusterNetworkProfile(input []interface{}) (*containerservice.NetworkProfileType, error) {
if len(input) == 0 {
return nil
return nil, nil
}

config := input[0].(map[string]interface{})
Expand All @@ -1373,10 +1421,16 @@ func expandKubernetesClusterNetworkProfile(input []interface{}) *containerservic
networkPolicy := config["network_policy"].(string)
loadBalancerSku := config["load_balancer_sku"].(string)

loadBalancerProfile, err := expandLoadBalancerProfile(config["load_balancer_profile"].([]interface{}), loadBalancerSku)
if err != nil {
return nil, err
}

networkProfile := containerservice.NetworkProfileType{
NetworkPlugin: containerservice.NetworkPlugin(networkPlugin),
NetworkPolicy: containerservice.NetworkPolicy(networkPolicy),
LoadBalancerSku: containerservice.LoadBalancerSku(loadBalancerSku),
NetworkPlugin: containerservice.NetworkPlugin(networkPlugin),
NetworkPolicy: containerservice.NetworkPolicy(networkPolicy),
LoadBalancerSku: containerservice.LoadBalancerSku(loadBalancerSku),
LoadBalancerProfile: loadBalancerProfile,
}

if v, ok := config["dns_service_ip"]; ok && v.(string) != "" {
Expand All @@ -1399,7 +1453,79 @@ func expandKubernetesClusterNetworkProfile(input []interface{}) *containerservic
networkProfile.ServiceCidr = utils.String(serviceCidr)
}

return &networkProfile
return &networkProfile, nil
}

func expandLoadBalancerProfile(d []interface{}, loadBalancerType string) (*containerservice.ManagedClusterLoadBalancerProfile, error) {
if len(d) == 0 {
return nil, nil
}

if strings.ToLower(loadBalancerType) != "standard" {
return nil, fmt.Errorf("Only load balancer SKU 'Standard' supports load balancer profiles. Provided load balancer type: %s", loadBalancerType)
}

config := d[0].(map[string]interface{})
evenh marked this conversation as resolved.
Show resolved Hide resolved

var managedOutboundIps *containerservice.ManagedClusterLoadBalancerProfileManagedOutboundIPs
var outboundIpPrefixes *containerservice.ManagedClusterLoadBalancerProfileOutboundIPPrefixes
var outboundIps *containerservice.ManagedClusterLoadBalancerProfileOutboundIPs

if ipCount := config["managed_outbound_ips"]; ipCount != nil {
if c := int32(ipCount.(int)); c > 0 {
managedOutboundIps = &containerservice.ManagedClusterLoadBalancerProfileManagedOutboundIPs{Count: &c}
}
}

if ipPrefixes := idsToResourceReferences(config["outbound_ip_prefixes_ids"]); ipPrefixes != nil {
outboundIpPrefixes = &containerservice.ManagedClusterLoadBalancerProfileOutboundIPPrefixes{PublicIPPrefixes: ipPrefixes}
}

if outIps := idsToResourceReferences(config["outbound_ip_address_ids"]); outIps != nil {
outboundIps = &containerservice.ManagedClusterLoadBalancerProfileOutboundIPs{PublicIPs: outIps}
}

return &containerservice.ManagedClusterLoadBalancerProfile{
ManagedOutboundIPs: managedOutboundIps,
OutboundIPPrefixes: outboundIpPrefixes,
OutboundIPs: outboundIps,
}, nil
}

func idsToResourceReferences(set interface{}) *[]containerservice.ResourceReference {
if set == nil {
return nil
}

s := set.(*schema.Set)
results := make([]containerservice.ResourceReference, 0)

for _, element := range s.List() {
id := element.(string)
results = append(results, containerservice.ResourceReference{ID: &id})
}

if len(results) > 0 {
return &results
}

return nil
}

func resourceReferencesToIds(refs *[]containerservice.ResourceReference) []string {
if refs != nil {
evenh marked this conversation as resolved.
Show resolved Hide resolved
ids := make([]string, 0)

for _, ref := range *refs {
if ref.ID != nil {
ids = append(ids, *ref.ID)
}
}

return ids
evenh marked this conversation as resolved.
Show resolved Hide resolved
}

return nil
}

func flattenKubernetesClusterNetworkProfile(profile *containerservice.NetworkProfileType) []interface{} {
Expand Down Expand Up @@ -1427,15 +1553,42 @@ func flattenKubernetesClusterNetworkProfile(profile *containerservice.NetworkPro
podCidr = *profile.PodCidr
}

lbProfiles := make([]interface{}, 0)
if profile.LoadBalancerProfile != nil {
evenh marked this conversation as resolved.
Show resolved Hide resolved
lb := make(map[string]interface{})

if profile.LoadBalancerProfile.ManagedOutboundIPs != nil {
if profile.LoadBalancerProfile.ManagedOutboundIPs.Count != nil {
lb["managed_outbound_ips"] = profile.LoadBalancerProfile.ManagedOutboundIPs.Count
}
}
evenh marked this conversation as resolved.
Show resolved Hide resolved

if profile.LoadBalancerProfile.OutboundIPs != nil {
if profile.LoadBalancerProfile.OutboundIPs.PublicIPs != nil {
lb["outbound_ip_address_ids"] = resourceReferencesToIds(profile.LoadBalancerProfile.OutboundIPs.PublicIPs)
}
}

if profile.LoadBalancerProfile.OutboundIPPrefixes != nil {
if profile.LoadBalancerProfile.OutboundIPPrefixes.PublicIPPrefixes != nil {
lb["outbound_ip_prefixes_ids"] = resourceReferencesToIds(profile.LoadBalancerProfile.OutboundIPPrefixes.PublicIPPrefixes)
}
}

lb["effective_outbound_ips"] = resourceReferencesToIds(profile.LoadBalancerProfile.EffectiveOutboundIPs)
lbProfiles = append(lbProfiles, lb)
}

return []interface{}{
map[string]interface{}{
"dns_service_ip": dnsServiceIP,
"docker_bridge_cidr": dockerBridgeCidr,
"load_balancer_sku": string(profile.LoadBalancerSku),
"network_plugin": string(profile.NetworkPlugin),
"network_policy": string(profile.NetworkPolicy),
"pod_cidr": podCidr,
"service_cidr": serviceCidr,
"dns_service_ip": dnsServiceIP,
"docker_bridge_cidr": dockerBridgeCidr,
"load_balancer_sku": string(profile.LoadBalancerSku),
"load_balancer_profile": lbProfiles,
"network_plugin": string(profile.NetworkPlugin),
"network_policy": string(profile.NetworkPolicy),
"pod_cidr": podCidr,
"service_cidr": serviceCidr,
},
}
}
Expand Down
Loading