From 9af70e6422d83fd95fcba755dcb5a1ef5412f7d0 Mon Sep 17 00:00:00 2001 From: PixelRobots <22979170+PixelRobots@users.noreply.github.com> Date: Thu, 26 Sep 2024 09:27:58 +0100 Subject: [PATCH] v0.0.8 added listclusters Signed-off-by: PixelRobots <22979170+PixelRobots@users.noreply.github.com> --- CHANGELOG.md | 36 ++++++++++++++++++++++++++++++ KubeTidy.psd1 | 4 ++-- KubeTidy.psm1 | 61 +++++++++++++++++++++++++++++++++++++++++---------- README.md | 20 +++++++++++++---- 4 files changed, 103 insertions(+), 18 deletions(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..972a104 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,36 @@ +# Changelog + +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [0.0.8] - 2024-09-26 + +### Added +- Introduced the `-ListClusters` parameter, allowing users to list all clusters in the kubeconfig file without performing any cleanup. +- Added function `Show-KubeTidyBanner` to display ASCII art and start message as a reusable function. +- Updated main command to `Invoke-KubeTidy` for better clarity and consistency. + +### Changed +- Improved documentation to reflect new functionality and renamed the primary function to `Invoke-KubeTidy`. +- Updated README to include examples and instructions for the `-ListClusters` option. + +## [0.0.7] - 2024-09-25 + +### Added +- Verbose output option with the `-Verbose` parameter for detailed logging of operations and cluster checks. +- Backup creation before cleanup to ensure that no data is lost in case of an error during the operation. +- Exclusion list functionality to skip specific clusters from removal using the `-ExclusionList` parameter. + +## [0.0.6] - 2024-09-25 + +### Added +- Initial release of **KubeTidy**, featuring Kubernetes cluster reachability checks and cleanup of unreachable clusters, contexts, and users. + +--- + +### How to Use the Changelog + +1. Make sure to increment the version number in the changelog with every new release. +2. Keep the changes concise and organized under headings like "Added", "Changed", "Fixed", and "Removed" to ensure easy tracking of what was done in each version. + diff --git a/KubeTidy.psd1 b/KubeTidy.psd1 index 21f0121..9057f9b 100644 --- a/KubeTidy.psd1 +++ b/KubeTidy.psd1 @@ -12,7 +12,7 @@ RootModule = 'KubeTidy.psm1' # Version number of this module. -ModuleVersion = '0.0.7' +ModuleVersion = '0.0.8' # Supported PSEditions # CompatiblePSEditions = @() @@ -69,7 +69,7 @@ Description = 'A tool to clean up Kubernetes config files.' # NestedModules = @() # Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export. -FunctionsToExport = 'Invoke-KubeTidyCleanup' +FunctionsToExport = 'Invoke-KubeTidy' # Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. CmdletsToExport = '*' diff --git a/KubeTidy.psm1 b/KubeTidy.psm1 index 9f875b9..0803247 100644 --- a/KubeTidy.psm1 +++ b/KubeTidy.psm1 @@ -18,6 +18,9 @@ .PARAMETER Force Forces cleanup even if no clusters are reachable. Defaults to false. +.PARAMETER ListClusters + Displays a list of all clusters in the kubeconfig file without performing cleanup. + .PARAMETER Verbose Enables verbose logging for detailed output. #> @@ -27,12 +30,27 @@ param ( [string]$KubeConfigPath = "", [string]$ExclusionList = "", [bool]$Backup = $true, - [switch]$Force + [switch]$Force, + [switch]$ListClusters ) # Split the ExclusionList by commas to create an array of clusters $ExclusionArray = $ExclusionList -split ',' | ForEach-Object { $_.Trim() } +# Function to show KubeTidy Banner +function Show-KubeTidyBanner { + # Display ASCII art and start message + Write-Host "" + Write-Host " ██╗ ██╗██╗ ██╗██████╗ ███████╗████████╗██╗██████╗ ██╗ ██╗" -ForegroundColor Cyan + Write-Host " ██║ ██╔╝██║ ██║██╔══██╗██╔════╝╚══██╔══╝██║██╔══██╗╚██╗ ██╔╝" -ForegroundColor Cyan + Write-Host " █████╔╝ ██║ ██║██████╔╝█████╗ ██║ ██║██║ ██║ ╚████╔╝ " -ForegroundColor Cyan + Write-Host " ██╔═██╗ ██║ ██║██╔══██╗██╔══╝ ██║ ██║██║ ██║ ╚██╔╝ " -ForegroundColor Cyan + Write-Host " ██║ ██╗╚██████╔╝██████╔╝███████╗ ██║ ██║██████╔╝ ██║ " -ForegroundColor Cyan + Write-Host " ╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚══════╝ ╚═╝ ╚═╝╚═════╝ ╚═╝ " -ForegroundColor Cyan + Write-Host "" +} + + # Function to create a backup of the kubeconfig file function New-Backup { [CmdletBinding()] @@ -69,13 +87,32 @@ function Test-ClusterReachability { } } +# Function to list all clusters in the kubeconfig file +function Get-AllClusters { + [CmdletBinding()] + param ( + [string]$KubeConfigPath + ) + + Write-Host "Listing all clusters in KubeConfig file:" -ForegroundColor Yellow + Write-Host "" + $kubeConfigContent = Get-Content -Raw -Path $KubeConfigPath + $kubeConfig = $kubeConfigContent | ConvertFrom-Yaml + + foreach ($cluster in $kubeConfig.clusters) { + $clusterName = $cluster.name + Write-Host "Cluster: $clusterName" -ForegroundColor Cyan + } +} + # Main Cleanup Function -function Invoke-KubeTidyCleanup { +function Invoke-KubeTidy { [CmdletBinding()] param ( [string]$KubeConfigPath, [array]$ExclusionArray, - [switch]$Force + [switch]$Force, + [switch]$ListClusters ) # Ensure that the $KubeConfigPath is valid @@ -95,15 +132,15 @@ function Invoke-KubeTidyCleanup { Import-Module powershell-yaml -ErrorAction Stop Write-Verbose "powershell-yaml module loaded successfully." - # Display ASCII art and start message - Write-Host "" - Write-Host " ██╗ ██╗██╗ ██╗██████╗ ███████╗████████╗██╗██████╗ ██╗ ██╗" -ForegroundColor Cyan - Write-Host " ██║ ██╔╝██║ ██║██╔══██╗██╔════╝╚══██╔══╝██║██╔══██╗╚██╗ ██╔╝" -ForegroundColor Cyan - Write-Host " █████╔╝ ██║ ██║██████╔╝█████╗ ██║ ██║██║ ██║ ╚████╔╝ " -ForegroundColor Cyan - Write-Host " ██╔═██╗ ██║ ██║██╔══██╗██╔══╝ ██║ ██║██║ ██║ ╚██╔╝ " -ForegroundColor Cyan - Write-Host " ██║ ██╗╚██████╔╝██████╔╝███████╗ ██║ ██║██████╔╝ ██║ " -ForegroundColor Cyan - Write-Host " ╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚══════╝ ╚═╝ ╚═╝╚═════╝ ╚═╝ " -ForegroundColor Cyan - Write-Host "" + # If ListClusters flag is provided, list clusters and exit + if ($ListClusters) { + Show-KubeTidyBanner + Get-AllClusters -KubeConfigPath $KubeConfigPath + return + } + + # Call the function wherever you need to show the banner + Show-KubeTidyBanner Write-Host "Starting KubeTidy cleanup..." -ForegroundColor Yellow Write-Host "" diff --git a/README.md b/README.md index 68e7454..2814d6c 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ - **Backup Creation:** Automatically creates a backup of the original kubeconfig before performing any cleanup. - **Summary Report:** Provides a neat summary of how many clusters were checked, removed, and retained. - **Force Cleanup Option:** If all clusters are unreachable, KubeTidy can force a cleanup using the `-Force` parameter. +- **List Clusters Option**: Use the `-ListClusters` parameter to simply list all clusters in your kubeconfig without performing any cleanup. - **Verbose Output**: Provides detailed logging about cluster reachability and other operations using the `-Verbose` flag. ## Requirements @@ -44,7 +45,7 @@ Ensure you have the required dependencies installed by running the tool. It will Once installed, run **KubeTidy** to clean your kubeconfig: ```powershell -Invoke-KubeTidyCleanup -KubeConfigPath "$HOME\.kube\config" -ExclusionList "cluster1,cluster2,cluster3" +Invoke-KubeTidy -KubeConfigPath "$HOME\.kube\config" -ExclusionList "cluster1,cluster2,cluster3" ``` ### Parameters @@ -54,25 +55,32 @@ Invoke-KubeTidyCleanup -KubeConfigPath "$HOME\.kube\config" -ExclusionList "clus - **`-Backup`**: Set to `false` if you don't want a backup to be created. Defaults to `true`. - **`-Force`**: Forces cleanup even if no clusters are reachable. Use this when you want to proceed with cleanup despite network issues. - **`-Verbose`**: Enables detailed logging during the cleanup process, including information about cluster reachability, backup creation, and module imports. +- **`-ListClusters`**: Lists all clusters in the kubeconfig file without performing any cleanup. ### Example To exclude specific clusters from removal and clean up your kubeconfig: ```powershell -Invoke-KubeTidyCleanup -KubeConfigPath "$HOME\.kube\config" -ExclusionList "aks-prod-cluster,aks-staging-cluster" +Invoke-KubeTidy -KubeConfigPath "$HOME\.kube\config" -ExclusionList "aks-prod-cluster,aks-staging-cluster" ``` If no clusters are reachable and you still want to proceed: ```powershell -Invoke-KubeTidyCleanup -KubeConfigPath "$HOME\.kube\config" -ExclusionList "aks-prod-cluster,aks-staging-cluster" -Force +Invoke-KubeTidy -KubeConfigPath "$HOME\.kube\config" -ExclusionList "aks-prod-cluster,aks-staging-cluster" -Force +``` + +To list all clusters without performing any cleanup: + +```powershell +Invoke-KubeTidy -KubeConfigPath "$HOME\.kube\config" -ListClusters ``` For detailed logging during the execution: ```powershell -Invoke-KubeTidyCleanup -KubeConfigPath "$HOME\.kube\config" -ExclusionList "aks-prod-cluster,aks-staging-cluster" -Verbose +Invoke-KubeTidy -KubeConfigPath "$HOME\.kube\config" -ExclusionList "aks-prod-cluster,aks-staging-cluster" -Verbose ``` ### Verbose Output Example @@ -102,6 +110,10 @@ After execution, you will receive a summary like the following: ╚════════════════════════════════════════════════╝ ``` +## Changelog + +All notable changes to this project are documented in the [CHANGELOG](./CHANGELOG.md). + ## License This project is licensed under the MIT License. See the [LICENSE](./LICENSE) file for more details. \ No newline at end of file