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

azurerm_kubernetes_cluster: Add support for KEDA Autoscaler #18967

Merged
merged 4 commits into from
Oct 27, 2022
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
106 changes: 89 additions & 17 deletions internal/services/containers/kubernetes_cluster_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,21 @@ func resourceKubernetesCluster() *pluginsdk.Resource {
},
},

"workload_autoscaler_profile": {
Type: pluginsdk.TypeList,
Optional: true,
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"keda_enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: false,
},
},
},
},

"workload_identity_enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
Expand Down Expand Up @@ -1128,6 +1143,9 @@ func resourceKubernetesClusterCreate(d *pluginsdk.ResourceData, meta interface{}
windowsProfileRaw := d.Get("windows_profile").([]interface{})
windowsProfile := expandKubernetesClusterWindowsProfile(windowsProfileRaw)

workloadAutoscalerProfileRaw := d.Get("workload_autoscaler_profile").([]interface{})
workloadAutoscalerProfile := expandKubernetesClusterWorkloadAutoscalerProfile(workloadAutoscalerProfileRaw)

apiServerAuthorizedIPRangesRaw := d.Get("api_server_authorized_ip_ranges").(*pluginsdk.Set).List()
apiServerAuthorizedIPRanges := utils.ExpandStringSlice(apiServerAuthorizedIPRangesRaw)

Expand Down Expand Up @@ -1200,23 +1218,24 @@ func resourceKubernetesClusterCreate(d *pluginsdk.ResourceData, meta interface{}
Tier: utils.ToPtr(managedclusters.ManagedClusterSKUTier(d.Get("sku_tier").(string))),
},
Properties: &managedclusters.ManagedClusterProperties{
ApiServerAccessProfile: &apiAccessProfile,
AadProfile: azureADProfile,
AddonProfiles: addonProfiles,
AgentPoolProfiles: agentProfiles,
AutoScalerProfile: autoScalerProfile,
DnsPrefix: utils.String(dnsPrefix),
EnableRBAC: utils.Bool(d.Get("role_based_access_control_enabled").(bool)),
KubernetesVersion: utils.String(kubernetesVersion),
LinuxProfile: linuxProfile,
WindowsProfile: windowsProfile,
NetworkProfile: networkProfile,
NodeResourceGroup: utils.String(nodeResourceGroup),
PublicNetworkAccess: &publicNetworkAccess,
DisableLocalAccounts: utils.Bool(d.Get("local_account_disabled").(bool)),
HTTPProxyConfig: httpProxyConfig,
OidcIssuerProfile: oidcIssuerProfile,
SecurityProfile: securityProfile,
ApiServerAccessProfile: &apiAccessProfile,
AadProfile: azureADProfile,
AddonProfiles: addonProfiles,
AgentPoolProfiles: agentProfiles,
AutoScalerProfile: autoScalerProfile,
DnsPrefix: utils.String(dnsPrefix),
EnableRBAC: utils.Bool(d.Get("role_based_access_control_enabled").(bool)),
KubernetesVersion: utils.String(kubernetesVersion),
LinuxProfile: linuxProfile,
WindowsProfile: windowsProfile,
NetworkProfile: networkProfile,
NodeResourceGroup: utils.String(nodeResourceGroup),
PublicNetworkAccess: &publicNetworkAccess,
DisableLocalAccounts: utils.Bool(d.Get("local_account_disabled").(bool)),
HTTPProxyConfig: httpProxyConfig,
OidcIssuerProfile: oidcIssuerProfile,
SecurityProfile: securityProfile,
WorkloadAutoScalerProfile: workloadAutoscalerProfile,
},
Tags: tags.Expand(t),
}
Expand Down Expand Up @@ -1678,6 +1697,21 @@ func resourceKubernetesClusterUpdate(d *pluginsdk.ResourceData, meta interface{}
existing.Model.Properties.SecurityProfile = microsoftDefender
}

if d.HasChange("workload_autoscaler_profile") {
updateCluster = true
workloadAutoscalerProfileRaw := d.Get("workload_autoscaler_profile").([]interface{})
workloadAutoscalerProfile := expandKubernetesClusterWorkloadAutoscalerProfile(workloadAutoscalerProfileRaw)
if workloadAutoscalerProfile == nil {
existing.Model.Properties.WorkloadAutoScalerProfile = &managedclusters.ManagedClusterWorkloadAutoScalerProfile{
Keda: &managedclusters.ManagedClusterWorkloadAutoScalerProfileKeda{
Enabled: false,
},
}
} else {
existing.Model.Properties.WorkloadAutoScalerProfile = workloadAutoscalerProfile
}
}

if d.HasChanges("workload_identity_enabled") {
updateCluster = true
workloadIdentity := d.Get("workload_identity_enabled").(bool)
Expand Down Expand Up @@ -1957,6 +1991,11 @@ func resourceKubernetesClusterRead(d *pluginsdk.ResourceData, meta interface{})
return fmt.Errorf("setting `windows_profile`: %+v", err)
}

workloadAutoscalerProfile := flattenKubernetesClusterWorkloadAutoscalerProfile(props.WorkloadAutoScalerProfile, d)
if err := d.Set("workload_autoscaler_profile", workloadAutoscalerProfile); err != nil {
return fmt.Errorf("setting `workload_autoscaler_profile`: %+v", err)
}

httpProxyConfig := flattenKubernetesClusterHttpProxyConfig(props)
if err := d.Set("http_proxy_config", httpProxyConfig); err != nil {
return fmt.Errorf("setting `http_proxy_config`: %+v", err)
Expand Down Expand Up @@ -2234,6 +2273,22 @@ func expandKubernetesClusterWindowsProfile(input []interface{}) *managedclusters
}
}

func expandKubernetesClusterWorkloadAutoscalerProfile(input []interface{}) *managedclusters.ManagedClusterWorkloadAutoScalerProfile {
if len(input) == 0 {
return nil
}

config := input[0].(map[string]interface{})
kedaEnabled := managedclusters.ManagedClusterWorkloadAutoScalerProfileKeda{}
if v := config["keda_enabled"].(bool); v {
kedaEnabled.Enabled = v
}

return &managedclusters.ManagedClusterWorkloadAutoScalerProfile{
Keda: &kedaEnabled,
}
}

func expandGmsaProfile(input []interface{}) *managedclusters.WindowsGmsaProfile {
if len(input) == 0 {
return nil
Expand Down Expand Up @@ -2278,6 +2333,23 @@ func flattenKubernetesClusterWindowsProfile(profile *managedclusters.ManagedClus
}
}

func flattenKubernetesClusterWorkloadAutoscalerProfile(profile *managedclusters.ManagedClusterWorkloadAutoScalerProfile, d *pluginsdk.ResourceData) []interface{} {
if profile == nil || len(d.Get("workload_autoscaler_profile").([]interface{})) == 0 {
return []interface{}{}
}

kedaEnabled := false
if v := profile.Keda; v != nil {
kedaEnabled = profile.Keda.Enabled
}

return []interface{}{
map[string]interface{}{
"keda_enabled": kedaEnabled,
},
}
}

func flattenGmsaProfile(profile *managedclusters.WindowsGmsaProfile) []interface{} {
if profile == nil {
return []interface{}{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,50 @@ func TestAccKubernetesCluster_runCommand(t *testing.T) {
})
}

func TestAccKubernetesCluster_workloadAutoscalerProfileKedaOnOff(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_kubernetes_cluster", "test")
r := KubernetesClusterResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.workloadAutoscalerProfileKeda(data, currentKubernetesVersion, true),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("workload_autoscaler_profile.0.keda_enabled").HasValue("true"),
),
},
{
Config: r.workloadAutoscalerProfileKeda(data, currentKubernetesVersion, false),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("workload_autoscaler_profile.0.keda_enabled").HasValue("false"),
),
},
})
}

func TestAccKubernetesCluster_workloadAutoscalerProfileKedaOnAbsent(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_kubernetes_cluster", "test")
r := KubernetesClusterResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.workloadAutoscalerProfileKeda(data, currentKubernetesVersion, true),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("workload_autoscaler_profile.0.keda_enabled").HasValue("true"),
),
},
{
Config: r.basicVMSSConfig(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func TestAccKubernetesCluster_edgeZone(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_kubernetes_cluster", "test")
r := KubernetesClusterResource{}
Expand Down Expand Up @@ -272,6 +316,41 @@ resource "azurerm_kubernetes_cluster" "test" {
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, controlPlaneVersion, runCommandEnabled)
}

func (KubernetesClusterResource) workloadAutoscalerProfileKeda(data acceptance.TestData, controlPlaneVersion string, kedaEnabled bool) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "test" {
name = "acctestRG-aks-%d"
location = "%s"
}

resource "azurerm_kubernetes_cluster" "test" {
name = "acctestaks%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
dns_prefix = "acctestaks%d"
kubernetes_version = %q

workload_autoscaler_profile {
keda_enabled = %t
}

default_node_pool {
name = "default"
node_count = 1
vm_size = "Standard_DS2_v2"
}

identity {
type = "SystemAssigned"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, controlPlaneVersion, kedaEnabled)
}

func (r KubernetesClusterResource) upgradeSettingsConfig(data acceptance.TestData, maxSurge string) string {
if maxSurge != "" {
maxSurge = fmt.Sprintf(`upgrade_settings {
Expand Down
9 changes: 9 additions & 0 deletions website/docs/r/kubernetes_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ In addition, one of either `identity` or `service_principal` blocks must be spec

-> **Note:** If you use BYO DNS Zone, AKS cluster should either use a User Assigned Identity or a service principal (which is deprecated) with the `Private DNS Zone Contributor` role and access to this Private DNS Zone. If `UserAssigned` identity is used - to prevent improper resource order destruction - cluster should depend on the role assignment, like in this example:

* `workload_autoscaler_profile` - (Optional) A `workload_autoscaler_profile` block defined below.

* `workload_identity_enabled` - (Optional) Specifies whether Azure AD Workload Identity should be enabled for the Cluster. Defaults to `false`.

-> **Note** To enable Azure AD Workload Identity `oidc_issuer_enabled` must be set to `true`.
Expand Down Expand Up @@ -708,6 +710,13 @@ A `gmsa` block supports the following:

-> **Note:** The properties `dns_server` and `root_domain` must both either be set or unset, i.e. empty.

---
A `workload_autoscaler_profile` block supports the following:

* `keda_enabled` - (Optional) Specifies whether KEDA Autoscaler can be used for workloads.
favoretti marked this conversation as resolved.
Show resolved Hide resolved

-> **Note:** This requires that the Preview Feature `Microsoft.ContainerService/AKS-KedaPreview` is enabled and the Resource Provider is re-registered, see [the documentation](Microsoft.ContainerService/AKS-KedaPreview) for more information.

---

A `http_proxy_config` block supports the following:
Expand Down