Skip to content

Latest commit

 

History

History
120 lines (85 loc) · 5.08 KB

README.md

File metadata and controls

120 lines (85 loc) · 5.08 KB

KubeTidy


KubeTidy is a PowerShell tool designed to clean up your Kubernetes configuration file by removing unreachable clusters, along with associated users and contexts. It simplifies the management of your kubeconfig by ensuring that only reachable and valid clusters remain, making it easier to work with multiple Kubernetes environments.

Features

  • Cluster Reachability Check: Checks clusters to ensure they are reachable and removes those that are not.
  • Exclusion List: Allows you to specify clusters to skip from removal (useful for VPN-bound or temporarily unreachable clusters).
  • User and Context Cleanup: Automatically removes users and contexts associated with removed clusters.
  • 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

Installation

You can install KubeTidy directly from the PowerShell Gallery:

Install-Module -Name KubeTidy -Repository PSGallery -Scope CurrentUser

To update the module later:

Update-Module -Name KubeTidy

Ensure you have the required dependencies installed by running the tool. It will automatically install the powershell-yaml module if not already installed.

Usage

Once installed, run KubeTidy to clean your kubeconfig:

Invoke-KubeTidy -KubeConfigPath "$HOME\.kube\config" -ExclusionList "cluster1,cluster2,cluster3"

Parameters

  • -KubeConfigPath: Path to your kubeconfig file. Defaults to "$HOME\.kube\config" if not specified.
  • -ExclusionList: A comma-separated list of clusters to exclude from removal. (Useful for clusters requiring VPN or temporary networks.)
  • -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:

Invoke-KubeTidy -KubeConfigPath "$HOME\.kube\config" -ExclusionList "aks-prod-cluster,aks-staging-cluster"

KubeTidy Cleanup Summary

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

Invoke-KubeTidy -KubeConfigPath "$HOME\.kube\config" -ExclusionList "aks-prod-cluster,aks-staging-cluster" -Force

To list all clusters without performing any cleanup:

Invoke-KubeTidy -KubeConfigPath "$HOME\.kube\config" -ListClusters

For detailed logging during the execution:

Invoke-KubeTidy -KubeConfigPath "$HOME\.kube\config" -ExclusionList "aks-prod-cluster,aks-staging-cluster" -Verbose

Verbose Output Example

When using the -Verbose flag, you will receive detailed information like:

VERBOSE: No KubeConfig path provided. Using default: C:\Users\username\.kube\config
VERBOSE: powershell-yaml module loaded successfully.
VERBOSE: Creating a backup of the KubeConfig file.
VERBOSE: Checking reachability for cluster: aks-prod-cluster at https://example-cluster-url
VERBOSE: Cluster aks-prod-cluster is reachable via HTTPS.
VERBOSE: Removed the following clusters: aks-old-cluster

Output

After execution, you will receive a summary like the following:

╔════════════════════════════════════════════════╗
║               KubeTidy Summary                 ║
╠════════════════════════════════════════════════╣
║  Clusters Checked:    26                       ║
║  Clusters Removed:     2                       ║
║  Clusters Kept:       24                       ║
╚════════════════════════════════════════════════╝

Changelog

All notable changes to this project are documented in the CHANGELOG.

License

This project is licensed under the MIT License. See the LICENSE file for more details.