Azure / benchpress

Azure Testing Framework/Library project
MIT License
56 stars 13 forks source link

Spike dotnet k8s sdk integration with powershell #354

Open polatengin opened 9 months ago

polatengin commented 9 months ago

Spike using official Kubernetes Client library (https://github.com/kubernetes-client/csharp) in PowerShell to access the target cluster and get resource information

Parent issue; #353

polatengin commented 9 months ago

Spike

In order to use official Kubernetes Client library (https://github.com/kubernetes-client/csharp) with PowerShell, we need to follow these steps;

When all the dependencies are in place, load types from KubernetesClient.dll, YamlDotNet.dll, Fractions.dll, IdentityModel.OidcClient.dll, IdentityModel.dll to current scope

To be able to access types, we have to add using statement for the root namespace of the Kubernetes Client library

To be able to connect to the target Kubernetes Cluster, we have to build the configuration object and use it to instantiate Kubernetes class

So, we can call public methods of KubernetesClient.dll to access list of namespaces, list of pods, etc.

Sample PowerShell code can be found below;

using namespace k8s

# download official Kubernetes Client library
Invoke-WebRequest -Uri "https://www.nuget.org/api/v2/package/KubernetesClient/12.1.1" -OutFile "/tmp/KubernetesClient.zip"

# expand nuget package to ./bin folder
Expand-Archive -Path "/tmp/KubernetesClient.zip" -DestinationPath "./bin" -Force

# download YamlDotNet dependency
Invoke-WebRequest -Uri "https://www.nuget.org/api/v2/package/YamlDotNet/13.4.0" -OutFile "/tmp/YamlDotNet.zip"

# expand nuget package to ./bin folder
Expand-Archive -Path "/tmp/YamlDotNet.zip" -DestinationPath "./bin" -Force

# download Fractions dependency
Invoke-WebRequest -Uri "https://www.nuget.org/api/v2/package/Fractions/7.3.0" -OutFile "/tmp/Fractions.zip"

# expand nuget package to ./bin folder
Expand-Archive -Path "/tmp/Fractions.zip" -DestinationPath "./bin" -Force

# download IdentityModel.OidcClient dependency
Invoke-WebRequest -Uri "https://www.nuget.org/api/v2/package/IdentityModel.OidcClient/5.2.1" -OutFile "/tmp/IdentityModel.OidcClient.zip"

# expand nuget package to ./bin folder
Expand-Archive -Path "/tmp/IdentityModel.OidcClient.zip" -DestinationPath "./bin" -Force

# download IdentityModel dependency
Invoke-WebRequest -Uri "https://www.nuget.org/api/v2/package/IdentityModel/6.2.0" -OutFile "/tmp/IdentityModel.zip"

# expand nuget package to ./bin folder
Expand-Archive -Path "/tmp/IdentityModel.zip" -DestinationPath "./bin" -Force

# import all types from downloaded binaries to current scope
Add-Type -Path "./bin/lib/net7.0/KubernetesClient.dll"
Add-Type -Path "./bin/lib/net7.0/YamlDotNet.dll"
Add-Type -Path "./bin/lib/netstandard2.1/Fractions.dll"
Add-Type -Path "./bin/lib/netstandard2.0/IdentityModel.OidcClient.dll"
Add-Type -Path "./bin/lib/netstandard2.0/IdentityModel.dll"

# build config to connect to default cluster in the system
$config = [KubernetesClientConfiguration]::BuildDefaultConfig()

# create the Kubernetes object to fetch data from the target Kubernetes cluster
$client = [Kubernetes]::new($config)

# default cancellation token to call async methods
$cancellationToken = [System.Threading.CancellationToken]::None

# get list of namespaces exists in the target Kubernetes cluster
$namespaces = $client.CoreV1.ListNamespaceWithHttpMessagesAsync($true, $null, $null, $null, $null, $null, $null, $null, $null, $null, $null, $null, $cancellationToken).Result

# get list of pods in the default namespace in the target Kubernetes cluster
$pods = $client.CoreV1.ListNamespacedPodWithHttpMessagesAsync("default", $true, $null, $null, $null, $null, $null, $null, $null, $null, $null, $null, $null, $cancellationToken).Result

# log namespaces to terminal in json format
Write-Host ($namespaces | ConvertTo-Json)

# log pods to terminal in json format
Write-Host ($pods | ConvertTo-Json)

When we run the sample PowerShell script above, we get the following result in the terminal;

terminal output