Skip to content

Commit

Permalink
[AKS] support NodeLabels and Tags (#20240)
Browse files Browse the repository at this point in the history
* [AKS] support NodeLabels

* [AKS] support Tags

* rename input parameters to singular nouns

* remove Install-AzAksKubectl.md
  • Loading branch information
YanaXu authored Nov 28, 2022
1 parent 8bf099e commit 5661274
Show file tree
Hide file tree
Showing 19 changed files with 9,277 additions and 61 deletions.
25 changes: 25 additions & 0 deletions src/Aks/Aks.Test/ScenarioTests/Common.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,29 @@ function Assert-Error
Assert-True {$result}

$Error.Clear()
}

function Assert-HashTableEquals {
param([HashTable] $expected, [HashTable] $actual, [string] $message)

if (!$message) {
$expectedStr = $expected | Out-String
$actualStr = $actual | Out-String
$message = "Assertion failed because '$expectedStr' does not match actual '$actualStr'"
}

if ($expected.Count -ne $actual.Count) {
throw $message
}

foreach ($key in $expected.Keys) {
if (-not $expected.ContainsKey($key)) {
throw $message
}
if ($expected[$key] -ne $actual[$key]) {
throw $message
}
}

return $true
}
8 changes: 7 additions & 1 deletion src/Aks/Aks.Test/ScenarioTests/KubernetesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,18 @@ public void TestManagedIdentity()
TestRunner.RunTestScript("Test-ManagedIdentity");
}


[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestOsSku()
{
TestRunner.RunTestScript("Test-OSSku");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestNodeLabelsAndTags()
{
TestRunner.RunTestScript("Test-NodeLabels-Tags");
}
}
}
94 changes: 94 additions & 0 deletions src/Aks/Aks.Test/ScenarioTests/KubernetesTests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -393,4 +393,98 @@ function Test-OSSku
{
Remove-AzResourceGroup -Name $resourceGroupName -Force
}
}

function Test-NodeLabels-Tags {
# Setup
$resourceGroupName = Get-RandomResourceGroupName
$kubeClusterName = Get-RandomClusterName
$location = 'eastus'
$nodeVmSize = "Standard_D2_v2"

try {
New-AzResourceGroup -Name $resourceGroupName -Location $location

# create aks cluster with default nodepool
$labels1 = @{"someId" = 123; "app" = "test" }
$tags1 = @{"dept"="IT"; "costcenter"=9999}
New-AzAksCluster -ResourceGroupName $resourceGroupName -Name $kubeClusterName -NodeVmSize $nodeVmSize -NodeCount 1 -NodePoolLabel $labels1 -NodePoolTag $tags1
$cluster = Get-AzAksCluster -ResourceGroupName $resourceGroupName -Name $kubeClusterName
Assert-AreEqual 1 $cluster.AgentPoolProfiles.Count
Assert-HashTableEquals $labels1 $cluster.AgentPoolProfiles[0].NodeLabels
Assert-HashTableEquals $tags1 $cluster.AgentPoolProfiles[0].Tags
$pools = Get-AzAksNodePool -ResourceGroupName $resourceGroupName -ClusterName $kubeClusterName
Assert-AreEqual 1 $pools.Count
Assert-HashTableEquals $labels1 $pools[0].NodeLabels
Assert-HashTableEquals $tags1 $pools[0].Tags

# update aks cluster default nodepool
$labels2 = @{"someId" = 124; "app" = "test"; "environment" = "dev" }
$tags2 = @{"dept"="Finance"; "costcenter"=8888}
Set-AzAksCluster -ResourceGroupName $resourceGroupName -Name $kubeClusterName -NodePoolLabel $labels2 -NodePoolTag $tags2
$cluster = Get-AzAksCluster -ResourceGroupName $resourceGroupName -Name $kubeClusterName
Assert-AreEqual 1 $cluster.AgentPoolProfiles.Count
Assert-HashTableEquals $labels2 $cluster.AgentPoolProfiles[0].NodeLabels
Assert-HashTableEquals $tags2 $cluster.AgentPoolProfiles[0].Tags
$pools = Get-AzAksNodePool -ResourceGroupName $resourceGroupName -ClusterName $kubeClusterName
Assert-AreEqual 1 $pools.Count
Assert-HashTableEquals $labels2 $pools[0].NodeLabels
Assert-HashTableEquals $tags2 $pools[0].Tags

# create a 2nd nodepool
$labels3 = @{"someId" = 125; "tier" = "frontend" }
$tags3 = @{"dept"="Finance"; "costcenter"=8888; "Admin"="Alice"}
New-AzAksNodePool -ResourceGroupName $resourceGroupName -ClusterName $kubeClusterName -Name "pool2" -Count 1 -NodeLabel $labels3 -Tag $tags3
$cluster = Get-AzAksCluster -ResourceGroupName $resourceGroupName -Name $kubeClusterName
Assert-AreEqual 2 $cluster.AgentPoolProfiles.Count
Assert-HashTableEquals $labels2 ($cluster.AgentPoolProfiles | where {$_.Name -eq "default"}).NodeLabels
Assert-HashTableEquals $tags2 ($cluster.AgentPoolProfiles | where {$_.Name -eq "default"}).Tags
Assert-HashTableEquals $labels3 ($cluster.AgentPoolProfiles | where {$_.Name -eq "pool2"}).NodeLabels
Assert-HashTableEquals $tags3 ($cluster.AgentPoolProfiles | where {$_.Name -eq "pool2"}).Tags
$pools = Get-AzAksNodePool -ResourceGroupName $resourceGroupName -ClusterName $kubeClusterName
Assert-AreEqual 2 $pools.Count
Assert-HashTableEquals $labels2 ($pools | where {$_.Name -eq "default"}).NodeLabels
Assert-HashTableEquals $tags2 ($pools | where {$_.Name -eq "default"}).Tags
Assert-HashTableEquals $labels3 ($pools | where {$_.Name -eq "pool2"}).NodeLabels
Assert-HashTableEquals $tags3 ($pools | where {$_.Name -eq "pool2"}).Tags

# update the 2nd nodepool
$labels4 = @{"someId" = 126; "app" = "test"; "environment" = "qa" }
$tags4 = @{"dept"="HR"; "costcenter"=6666; "Admin"="Bruce"}
Set-AzAksCluster -ResourceGroupName $resourceGroupName -Name $kubeClusterName -NodeName "pool2" -NodePoolLabel $labels4 -NodePoolTag $tags4
$cluster = Get-AzAksCluster -ResourceGroupName $resourceGroupName -Name $kubeClusterName
Assert-AreEqual 2 $cluster.AgentPoolProfiles.Count
Assert-HashTableEquals $labels2 ($cluster.AgentPoolProfiles | where {$_.Name -eq "default"}).NodeLabels
Assert-HashTableEquals $tags2 ($cluster.AgentPoolProfiles | where {$_.Name -eq "default"}).Tags
Assert-HashTableEquals $labels4 ($cluster.AgentPoolProfiles | where {$_.Name -eq "pool2"}).NodeLabels
Assert-HashTableEquals $tags4 ($cluster.AgentPoolProfiles | where {$_.Name -eq "pool2"}).Tags
$pools = Get-AzAksNodePool -ResourceGroupName $resourceGroupName -ClusterName $kubeClusterName
Assert-AreEqual 2 $pools.Count
Assert-HashTableEquals $labels2 ($pools | where {$_.Name -eq "default"}).NodeLabels
Assert-HashTableEquals $tags2 ($pools | where {$_.Name -eq "default"}).Tags
Assert-HashTableEquals $labels4 ($pools | where {$_.Name -eq "pool2"}).NodeLabels
Assert-HashTableEquals $tags4 ($pools | where {$_.Name -eq "pool2"}).Tags

# update the default nodepool
$labels5 = @{"someId" = 127; "tier" = "frontend"; "environment" = "qa" }
$tags5 = @{"dept"="MM"; "costcenter"=7777; "Admin"="Cindy"}
Update-AzAksNodePool -ResourceGroupName $resourceGroupName -ClusterName $kubeClusterName -Name "default" -NodeLabel $labels5 -Tag $tags5
$cluster = Get-AzAksCluster -ResourceGroupName $resourceGroupName -Name $kubeClusterName
Assert-AreEqual 2 $cluster.AgentPoolProfiles.Count
Assert-HashTableEquals $labels5 ($cluster.AgentPoolProfiles | where {$_.Name -eq "default"}).NodeLabels
Assert-HashTableEquals $tags5 ($cluster.AgentPoolProfiles | where {$_.Name -eq "default"}).Tags
Assert-HashTableEquals $labels4 ($cluster.AgentPoolProfiles | where {$_.Name -eq "pool2"}).NodeLabels
Assert-HashTableEquals $tags4 ($cluster.AgentPoolProfiles | where {$_.Name -eq "pool2"}).Tags
$pools = Get-AzAksNodePool -ResourceGroupName $resourceGroupName -ClusterName $kubeClusterName
Assert-AreEqual 2 $pools.Count
Assert-HashTableEquals $labels5 ($pools | where {$_.Name -eq "default"}).NodeLabels
Assert-HashTableEquals $tags5 ($pools | where {$_.Name -eq "default"}).Tags
Assert-HashTableEquals $labels4 ($pools | where {$_.Name -eq "pool2"}).NodeLabels
Assert-HashTableEquals $tags4 ($pools | where {$_.Name -eq "pool2"}).Tags

$cluster | Remove-AzAksCluster -Force
}
finally {
Remove-AzResourceGroup -Name $resourceGroupName -Force
}
}
Loading

0 comments on commit 5661274

Please sign in to comment.