Skip to content

Commit e92fe03

Browse files
author
Bavneet Singh
committed
add null check for empty kube-config in connectedk8s proxy scenario
1 parent 207cad1 commit e92fe03

23 files changed

+1377
-6
lines changed

src/connectedk8s/HISTORY.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
Release History
44
===============
55

6+
1.10.4
7+
++++++
8+
* Fixed the issue where the 'connectedk8s proxy' command would fail if the kubeconfig file was empty.
9+
610
1.10.3
711
++++++
812
* Fixed linting and styling issues, and added type annotations.

src/connectedk8s/azext_connectedk8s/custom.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3372,11 +3372,14 @@ def merge_kubernetes_configurations(
33723372
break
33733373
except (KeyError, TypeError):
33743374
continue
3375-
3376-
handle_merge(existing, addition, "clusters", replace)
3377-
handle_merge(existing, addition, "users", replace)
3378-
handle_merge(existing, addition, "contexts", replace)
3379-
existing["current-context"] = addition["current-context"]
3375+
3376+
if existing is None:
3377+
existing = addition
3378+
else:
3379+
handle_merge(existing, addition, "clusters", replace)
3380+
handle_merge(existing, addition, "users", replace)
3381+
handle_merge(existing, addition, "contexts", replace)
3382+
existing["current-context"] = addition["current-context"]
33803383

33813384
# check that ~/.kube/config is only read- and writable by its owner
33823385
if platform.system() != "Windows":

src/connectedk8s/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# TODO: Confirm this is the right version number you want and it matches your
1414
# HISTORY.rst entry.
1515

16-
VERSION = "1.10.3"
16+
VERSION = "1.10.4"
1717

1818
# The full list of classifiers is available at
1919
# https://pypi.python.org/pypi?%3Aaction=list_classifiers

testing/.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
settings.json
2+
tmp/
3+
bin/*
4+
!bin/connectedk8s-1.0.0-py3-none-any.whl
5+
!bin/k8s_extension-0.3.0-py3-none-any.whl
6+
!bin/k8s_extension_private-0.1.0-py3-none-any.whl
7+
!bin/k8s_configuration-1.0.0-py3-none-any.whl
8+
!bin/connectedk8s-values.yaml
9+
*.xml

testing/Bootstrap.ps1

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
param (
2+
[switch] $SkipInstall,
3+
[switch] $CI
4+
)
5+
6+
# Disable confirm prompt for script
7+
az config set core.disable_confirm_prompt=true
8+
9+
# Configuring the environment
10+
$ENVCONFIG = Get-Content -Path $PSScriptRoot/settings.json | ConvertFrom-Json
11+
12+
az account set --subscription $ENVCONFIG.subscriptionId
13+
14+
if (-not (Test-Path -Path $PSScriptRoot/tmp)) {
15+
New-Item -ItemType Directory -Path $PSScriptRoot/tmp
16+
}
17+
18+
az group show --name $envConfig.resourceGroup
19+
if (!$?) {
20+
Write-Host "Resource group does not exist, creating it now in region 'eastus2euap'"
21+
az group create --name $envConfig.resourceGroup --location eastus2euap
22+
23+
if (!$?) {
24+
Write-Host "Failed to create Resource Group - exiting!"
25+
Exit 1
26+
}
27+
}
28+
29+
30+
Copy-Item $HOME/.kube/config -Destination $PSScriptRoot/tmp/KUBECONFIG

testing/README.md

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# K8s Partner Extension Test Suite
2+
3+
This repository serves as the integration testing suite for the `k8s-extension` Azure CLI module.
4+
5+
## Testing Requirements
6+
7+
All partners who wish to merge their __Custom Private Preview Release__ (owner: _Partner_) into the __Official Private Preview Release__ are required to author additional integration tests for their extension to ensure that their extension will continue to function correctly as more extensions are added into the __Official Private Preview Release__.
8+
9+
For more information on creating these tests, see [Authoring Tests](docs/test_authoring.md)
10+
11+
## Pre-Requisites
12+
13+
In order to properly test all regression tests within the test suite, you must onboard an AKS cluster which you will use to generate your Azure Arc resource to test the extensions. Ensure that you have a resource group where you can onboard this cluster.
14+
15+
### Required Installations
16+
17+
The following installations are required in your environment for the integration tests to run correctly:
18+
19+
1. [Helm 3](https://helm.sh/docs/intro/install/)
20+
2. [Kubectl](https://kubernetes.io/docs/tasks/tools/install-kubectl/)
21+
3. [Azure CLI](https://docs.microsoft.com/en-us/cli/azure/install-azure-cli)
22+
23+
## Setup
24+
25+
### Step 1: Install Pester
26+
27+
This project contains [Pester](https://pester.dev/) test framework commands that are required for the integration tests to run. In an admin powershell terminal, run
28+
29+
```powershell
30+
Install-Module Pester -Force -SkipPublisherCheck
31+
Import-Module Pester -PassThru
32+
```
33+
34+
If you run into issues installing the framework, refer to the [Installation Guide](https://pester.dev/docs/introduction/installation) provided by the Pester docs.
35+
36+
### Step 2: Get Test suite files
37+
38+
You can either clone this repo (preferred option, since you will be adding your tests to this suite) or copy the files in this repo locally. Rest of the instructions here assume your working directory is k8spartner-extension-testing.
39+
40+
### Step 3: Update the `k8s-extension`/`k8s-extension-private` .whl package
41+
42+
This integration test suite references the .whl packages found in the `\bin` directory. After generating your `k8s-extension`/`k8s-extension-private` .whl package, copy your updated package into the `\bin` directory.
43+
44+
### Step 4: Create a `settings.json`
45+
46+
To onboard the AKS and Arc clusters correctly, you will need to create a `settings.json` configuration. Create a new `settings.json` file by copying the contents of the `settings.template.json` into this file. Update the subscription id, resource group, and AKS and Arc cluster name fields with your specific values.
47+
48+
### Step 5: Update the extension version value in `settings.json`
49+
50+
To ensure that the tests point to your `k8s-extension-private` `.whl` package, change the value of the `k8s-extension-private` to match your package versioning in the format (Major.Minor.Patch.Extension). For example, the `k8s_extension_private-0.1.0.openservicemesh_5-py3-none-any.whl` whl package would have extension versions set to
51+
```json
52+
{
53+
"k8s-extension": "0.1.0",
54+
"k8s-extension-private": "0.1.0.openservicemesh_5",
55+
"connectedk8s": "0.3.5"
56+
}
57+
58+
```
59+
60+
_Note: Updates to the `connectedk8s` version and `k8s-extension` version can also be made by adding a different version of the `connectedk8s` and `k8s-extension` whl packages and changing the `connectedk8s` and `k8s-extension` values to match the (Major.Minor.Patch) version format shown above_
61+
62+
### Step 6: Run the Bootstrap Command
63+
To bootstrap the environment with AKS and Arc clusters, run
64+
```powershell
65+
.\Bootstrap.ps1
66+
```
67+
This script will provision the AKS and Arc clusters needed to run the integration test suite
68+
69+
## Testing
70+
71+
### Testing All Extension Suites
72+
To test all extension test suites, you must call `.\Test.ps1` with the `-ExtensionType` parameter set to either `Public` or `Private`. Based on this flag, the test suite will install the extension type specified below
73+
74+
| `-ExtensionType` | Installs `az extension` |
75+
| ---------------- | --------------------- |
76+
| `Public` | `k8s-extension` |
77+
| `Private` | `k8s-extension-private` |
78+
79+
For example, when calling
80+
```bash
81+
.\Test.ps1 -ExtensionType Public
82+
```
83+
the script will install your `k8s-extension` whl package and run the full test suite of `*.Tests.ps1` files included in the `\test\extensions` directory
84+
85+
### Testing Public Extensions Only
86+
If you only want to run the test cases against public-preview or GA extension test cases, you can use the `-OnlyPublicTests` flag to specify this
87+
```bash
88+
.\Test.ps1 -ExtensionType Public -OnlyPublicTests
89+
```
90+
91+
### Testing Specific Extension Suite
92+
93+
If you only want to run the test script on your specific test file, you can do so by specifying path to your extension test suite in the execution call
94+
95+
```powershell
96+
.\Test.ps1 -Path <path\to\extensionsuite>
97+
```
98+
For example to call the `AzureMonitor.Tests.ps1` test suite, we run
99+
```powershell
100+
.\Test.ps1 -ExtensionType Public -Path .\test\extensions\public\AzureMonitor.Tests.ps1
101+
```
102+
103+
### Skipping Extension Re-Install
104+
105+
By default the `Test.ps1` script will uninstall any old versions of `k8s-extension`/'`k8s-extension-private` and re-install the version specified in `settings.json`. If you do not want this re-installation to occur, you can specify the `-SkipInstall` flag to skip this process.
106+
107+
```powershell
108+
.\Test.ps1 -ExtensionType Public -SkipInstall
109+
```
110+
111+
## Cleanup
112+
To cleanup the AKS and Arc clusters you have provisioned in testing, run
113+
```powershell
114+
.\Cleanup.ps1
115+
```
116+
This will remove the AKS and Arc clusters as well as the `\tmp` directory that were created by the bootstrapping script.

testing/Test.ps1

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
param (
2+
[string] $Path,
3+
[switch] $SkipInstall,
4+
[switch] $CI,
5+
[switch] $ParallelCI,
6+
[switch] $OnlyPublicTests,
7+
8+
[Parameter(Mandatory=$True)]
9+
[ValidateSet('connectedk8s')]
10+
[string]$Type
11+
)
12+
13+
# Disable confirm prompt for script
14+
# Only show errors, don't show warnings
15+
az config set core.disable_confirm_prompt=true
16+
az config set core.only_show_errors=true
17+
18+
$ENVCONFIG = Get-Content -Path $PSScriptRoot/settings.json | ConvertFrom-Json
19+
20+
az account set --subscription $ENVCONFIG.subscriptionId
21+
22+
$Env:KUBECONFIG="$PSScriptRoot/tmp/KUBECONFIG"
23+
$TestFileDirectory="$PSScriptRoot/results"
24+
25+
if (-not (Test-Path -Path $TestFileDirectory)) {
26+
New-Item -ItemType Directory -Path $TestFileDirectory
27+
}
28+
29+
if ($Type -eq 'connectedk8s') {
30+
$connectedk8sVersion = $ENVCONFIG.extensionVersion.'connectedk8s'
31+
if (!$SkipInstall) {
32+
Write-Host "Removing the old connectedk8s extension..."
33+
az extension remove -n connectedk8s
34+
Write-Host "Installing connectedk8s version $connectedk8sVersion..."
35+
az extension add --source ./bin/connectedk8s-$connectedk8sVersion-py2.py3-none-any.whl
36+
}
37+
$testFilePaths = "$PSScriptRoot/test/configurations"
38+
}
39+
40+
if ($ParallelCI) {
41+
# This runs the tests in parallel during the CI pipline to speed up testing
42+
43+
Write-Host "Invoking Pester to run tests from '$testFilePath'..."
44+
$testFiles = @()
45+
foreach ($paths in $testFilePaths)
46+
{
47+
$temp = Get-ChildItem $paths
48+
$testFiles += $temp
49+
}
50+
$resultFileNumber = 0
51+
foreach ($testFile in $testFiles)
52+
{
53+
$resultFileNumber++
54+
$testName = Split-Path $testFile –leaf
55+
Start-Job -ArgumentList $testName, $testFile, $resultFileNumber, $TestFileDirectory -Name $testName -ScriptBlock {
56+
param($name, $testFile, $resultFileNumber, $testFileDirectory)
57+
58+
Write-Host "$testFile to result file #$resultFileNumber"
59+
$testResult = Invoke-Pester $testFile -Passthru -Output Detailed
60+
$testResult | Export-JUnitReport -Path "$testFileDirectory/$name.xml"
61+
}
62+
}
63+
64+
do {
65+
Write-Host ">> Still running tests @ $(Get-Date –Format "HH:mm:ss")" –ForegroundColor Blue
66+
Get-Job | Where-Object { $_.State -eq "Running" } | Format-Table –AutoSize
67+
Start-Sleep –Seconds 30
68+
} while((Get-Job | Where-Object { $_.State -eq "Running" } | Measure-Object).Count -ge 1)
69+
70+
Get-Job | Wait-Job
71+
$failedJobs = Get-Job | Where-Object { -not ($_.State -eq "Completed")}
72+
Get-Job | Receive-Job –AutoRemoveJob –Wait –ErrorAction 'Continue'
73+
74+
if ($failedJobs.Count -gt 0) {
75+
Write-Host "Failed Jobs" –ForegroundColor Red
76+
$failedJobs
77+
throw "One or more tests failed"
78+
}
79+
} elseif ($CI) {
80+
if ($Path) {
81+
$testFilePath = "$PSScriptRoot/$Path"
82+
}
83+
Write-Host "Invoking Pester to run tests from '$testFilePath'..."
84+
$testResult = Invoke-Pester $testFilePath -Passthru -Output Detailed
85+
$testName = Split-Path $testFilePath –leaf
86+
$testResult | Export-JUnitReport -Path "$testFileDirectory/$testName.xml"
87+
} else {
88+
if ($Path) {
89+
Write-Host "Invoking Pester to run tests from '$PSScriptRoot/$Path'"
90+
Invoke-Pester -Output Detailed $PSScriptRoot/$Path
91+
} else {
92+
Write-Host "Invoking Pester to run tests from '$testFilePath'..."
93+
Invoke-Pester -Output Detailed $testFilePath
94+
}
95+
}
61.3 KB
Binary file not shown.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
systemDefaultValues:
2+
extensionoperator:
3+
enabled: true
41.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)