Skip to content

Commit

Permalink
v0.0.10
Browse files Browse the repository at this point in the history
Signed-off-by: PixelRobots <[email protected]>
  • Loading branch information
PixelRobots committed Sep 26, 2024
1 parent 68cd955 commit fcf0e55
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 23 deletions.
23 changes: 15 additions & 8 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@ 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.10] - 2024-09-27

### Added
- **Clickable Backup Path**: Backup path is now clickable if the terminal supports it, making it easier to navigate to backup files.
- **Manual YAML Handling for Single Entries**: Ensured proper YAML structure when only one cluster, context, or user remains in the kubeconfig file.

### Changed
- **Environment Detection**: Enhanced WSL detection and config path handling for both native Linux/macOS and WSL environments.
- **KubeConfig Path Handling**: Improved logic for determining the kubeconfig path in different environments (Windows, WSL, Linux/macOS).

### Fixed
- **Cluster Removal Formatting**: Fixed an issue where the kubeconfig format would break when cleaning up to a single cluster, context, or user.
- **Cluster Kept Count**: fixed so the number is correct. It was 1 out before.


## [0.0.9] - 2024-09-26

### Fixed
Expand Down Expand Up @@ -31,11 +46,3 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

### 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.

2 changes: 1 addition & 1 deletion KubeTidy.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
RootModule = 'KubeTidy.psm1'

# Version number of this module.
ModuleVersion = '0.0.9'
ModuleVersion = '0.0.10'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down
94 changes: 80 additions & 14 deletions KubeTidy.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ function Show-KubeTidyBanner {
Write-Host ""
}


# Function to create a backup of the kubeconfig file
function New-Backup {
[CmdletBinding()]
Expand All @@ -59,7 +58,9 @@ function New-Backup {
)
$backupPath = "$KubeConfigPath.bak_$(Get-Date -Format 'yyyyMMdd_HHmmss')"
Copy-Item -Path $KubeConfigPath -Destination $backupPath
Write-Host "Backup created at $backupPath" -ForegroundColor Green
# If the terminal supports clickable links, this will make the path clickable
$clickableLink = "`e]8;;file://$backupPath`e\$backupPath`e]8;;`e\"
Write-Host "Backup created at $clickableLink" -ForegroundColor Green
Write-Verbose "Backup of KubeConfig created at path: $backupPath"
}

Expand Down Expand Up @@ -117,9 +118,37 @@ function Invoke-KubeTidy {

# Ensure that the $KubeConfigPath is valid
if (-not $KubeConfigPath) {
$homePath = [System.Environment]::GetFolderPath("UserProfile")
Write-Verbose "No KubeConfig path provided. Using default: $homePath\.kube\config"
$KubeConfigPath = "$homePath\.kube\config"
# Function to detect if running inside WSL
function Test-WSL {
if (Test-Path "/proc/version") {
$versionInfo = Get-Content "/proc/version"
return $versionInfo -match "Microsoft"
}
return $false
}

# Determine the correct kubeconfig path based on the environment
if ($IsWindows) {
# Windows: Use the standard USERPROFILE path
$KubeConfigPath = "$env:USERPROFILE\.kube\config"
}
elseif (Test-WSL) {
# WSL: Use wslvar to get the Windows USERPROFILE and convert it to WSL path using wslpath
$windowsHomePath = wslpath "$(wslvar USERPROFILE)"
$KubeConfigPath = "$windowsHomePath/.kube/config"

if (-not $KubeConfigPath) {
Write-Error "Could not locate the Windows .kube config path in WSL."
return
}
}
else {
# Native Linux/macOS: Use the regular home directory path
$KubeConfigPath = "$HOME/.kube/config"
}

# Output the determined path (for debugging or informational purposes)
Write-Host "KubeConfig Path: $KubeConfigPath"
}

# Check if the powershell-yaml module is installed; if not, install it
Expand Down Expand Up @@ -208,24 +237,61 @@ function Invoke-KubeTidy {
Write-Verbose "No clusters were marked for removal."
}

# Save updated kubeconfig back to the file
Write-Verbose "Saving the updated KubeConfig to $KubeConfigPath"
$kubeConfig | ConvertTo-Yaml | Set-Content -Path $KubeConfigPath
Write-Host "Kubeconfig cleaned and saved." -ForegroundColor Green
# Manually build the YAML for clusters, contexts, and users
$clustersYaml = @"
clusters: `n
"@
foreach ($cluster in $kubeConfig.clusters) {
$clustersYaml += " - cluster:`n"
$clustersYaml += " certificate-authority-data: $($cluster.cluster.'certificate-authority-data')`n"
$clustersYaml += " server: $($cluster.cluster.server)`n"
$clustersYaml += " name: $($cluster.name)`n"
}

$contextsYaml = @"
contexts: `n
"@
foreach ($context in $kubeConfig.contexts) {
$contextsYaml += " - context:`n"
$contextsYaml += " cluster: $($context.context.cluster)`n"
$contextsYaml += " user: $($context.context.user)`n"
$contextsYaml += " name: $($context.name)`n"
}

$usersYaml = @"
users: `n
"@
foreach ($user in $kubeConfig.users) {
$usersYaml += " - name: $($user.name)`n"
$usersYaml += " user:`n"
$usersYaml += " client-certificate-data: $($user.user.'client-certificate-data')`n"
$usersYaml += " client-key-data: $($user.user.'client-key-data')`n"
}

# Manually define the top-level fields
$kubeConfigHeader = @"
apiVersion: v1
kind: Config
preferences: {} `n
"@

# Combine everything and save to file
$fullKubeConfigYaml = $kubeConfigHeader + $clustersYaml + $contextsYaml + $usersYaml
$fullKubeConfigYaml | Set-Content -Path $KubeConfigPath

Write-Host "Kubeconfig cleaned and saved." -ForegroundColor Green
# Display the summary with consistent padding
$retainedCount = $kubeConfig.clusters.Count
$removedCount = $removedClusters.Count
$checkedClustersText = "{0,5}" -f $checkedClusters
$removedCountText = "{0,5}" -f $removedCount
$retainedCountText = "{0,5}" -f $retainedCount
$retainedCountText = "{0,5}" -f ($checkedClusters - $removedCount)

Write-Host ""
Write-Host "╔════════════════════════════════════════════════╗" -ForegroundColor Magenta
Write-Host "║ KubeTidy Summary ║" -ForegroundColor Magenta
Write-Host "╠════════════════════════════════════════════════╣" -ForegroundColor Magenta
Write-Host "║ Clusters Checked: $checkedClustersText" -ForegroundColor Yellow
Write-Host "║ Clusters Removed: $removedCountText" -ForegroundColor Red
Write-Host "║ Clusters Kept: $retainedCountText" -ForegroundColor Green
Write-Host "║ Clusters Checked: $checkedClustersText" -ForegroundColor Yellow
Write-Host "║ Clusters Removed: $removedCountText" -ForegroundColor Red
Write-Host "║ Clusters Kept: $retainedCountText" -ForegroundColor Green
Write-Host "╚════════════════════════════════════════════════╝" -ForegroundColor Magenta
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ To exclude specific clusters from removal and clean up your kubeconfig:
```powershell
Invoke-KubeTidy -KubeConfigPath "$HOME\.kube\config" -ExclusionList "aks-prod-cluster,aks-staging-cluster"
```
![KubeTidy Cleanup Summary](./images/summary.png)

If no clusters are reachable and you still want to proceed:

Expand Down
Binary file added images/checking_clusters.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/summary.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit fcf0e55

Please sign in to comment.