|
| 1 | +# Finalizer Cleaner |
| 2 | + |
| 3 | +A Kubernetes tool to safely remove finalizers from resources based on specified criteria. Features both CLI flags and an interactive TUI mode for easy finalizer management. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Interactive TUI Mode**: User-friendly interface for selecting namespaces, resource types, and finalizers |
| 8 | +- **Batch Operations**: Remove finalizers from multiple resources at once |
| 9 | +- **Dry Run Mode**: Preview changes before applying them |
| 10 | +- **Flexible Filtering**: Filter by namespaces, resource kinds, and specific finalizer names |
| 11 | +- **Comprehensive Logging**: Detailed logging with different log levels |
| 12 | +- **Safe Operations**: Only removes specified finalizers, preserving others |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +### From Source |
| 17 | + |
| 18 | +```bash |
| 19 | +go install github.com/risen228/finalizer-cleaner@latest |
| 20 | +``` |
| 21 | + |
| 22 | +### Build Locally |
| 23 | + |
| 24 | +```bash |
| 25 | +git clone https://github.com/risen228/finalizer-cleaner.git |
| 26 | +cd finalizer-cleaner |
| 27 | +go build . |
| 28 | +``` |
| 29 | + |
| 30 | +## Usage |
| 31 | + |
| 32 | +### Interactive Mode |
| 33 | + |
| 34 | +Run without arguments or with limited arguments to enter interactive TUI mode: |
| 35 | + |
| 36 | +```bash |
| 37 | +finalizer-cleaner |
| 38 | +``` |
| 39 | + |
| 40 | +The interactive mode will guide you through: |
| 41 | +1. Selecting namespaces (or all namespaces) |
| 42 | +2. Selecting resource types (or all types) |
| 43 | +3. Selecting which finalizers to remove |
| 44 | + |
| 45 | +### CLI Mode |
| 46 | + |
| 47 | +Specify all parameters via command-line flags: |
| 48 | + |
| 49 | +```bash |
| 50 | +./finalizer-cleaner \ |
| 51 | + --namespaces "default,kube-system" \ |
| 52 | + --kinds "apps/v1/Deployment,v1/Pod" \ |
| 53 | + --finalizers "kubernetes.io/pvc-protection,example.com/finalizer" |
| 54 | +``` |
| 55 | + |
| 56 | +### Flags |
| 57 | + |
| 58 | +- `--namespaces`: Comma-separated list of namespaces (empty = all namespaces) |
| 59 | +- `--kinds`: Comma-separated list of Group/Version/Kind triplets (empty = all kinds) |
| 60 | +- `--finalizers`: Comma-separated list of finalizers to remove (empty = all finalizers) |
| 61 | +- `--dry-run`: Print what would be changed without making actual modifications |
| 62 | +- `--interactive`: Force interactive mode even when other flags are provided |
| 63 | +- `--verbose`: Enable verbose logging for detailed operation information |
| 64 | +- `--timeout`: HTTP timeout for Kubernetes API calls (default: 30s) |
| 65 | + |
| 66 | +### Examples |
| 67 | + |
| 68 | +#### Remove specific finalizer from all deployments in default namespace |
| 69 | + |
| 70 | +```bash |
| 71 | +./finalizer-cleaner \ |
| 72 | + --namespaces default \ |
| 73 | + --kinds apps/v1/Deployment \ |
| 74 | + --finalizers "example.com/my-finalizer" |
| 75 | +``` |
| 76 | + |
| 77 | +#### Dry run to see what would be removed |
| 78 | + |
| 79 | +```bash |
| 80 | +./finalizer-cleaner \ |
| 81 | + --namespaces default \ |
| 82 | + --dry-run |
| 83 | +``` |
| 84 | + |
| 85 | +#### Verbose logging for detailed operation information |
| 86 | + |
| 87 | +```bash |
| 88 | +./finalizer-cleaner \ |
| 89 | + --namespaces default \ |
| 90 | + --kinds apps/v1/Deployment \ |
| 91 | + --verbose |
| 92 | +``` |
| 93 | + |
| 94 | +#### Remove all finalizers from failed pods (dangerous!) |
| 95 | + |
| 96 | +```bash |
| 97 | +./finalizer-cleaner \ |
| 98 | + --namespaces default \ |
| 99 | + --kinds v1/Pod \ |
| 100 | + --finalizers "" |
| 101 | +``` |
| 102 | + |
| 103 | +## Safety Considerations |
| 104 | + |
| 105 | +1. **Always use dry-run first**: Before performing actual removal, use `--dry-run` to preview changes |
| 106 | +2. **Be specific with finalizers**: Only remove finalizers you understand. Removing system finalizers can cause data loss |
| 107 | +3. **Understand the impact**: Finalizers exist to ensure proper cleanup. Removing them bypasses this cleanup |
| 108 | +4. **Backup important resources**: Consider backing up critical resources before removing finalizers |
| 109 | + |
| 110 | +## Common Use Cases |
| 111 | + |
| 112 | +### Stuck Namespace Deletion |
| 113 | + |
| 114 | +When a namespace is stuck in "Terminating" state: |
| 115 | + |
| 116 | +```bash |
| 117 | +./finalizer-cleaner \ |
| 118 | + --namespaces stuck-namespace \ |
| 119 | + --finalizers "kubernetes" |
| 120 | +``` |
| 121 | + |
| 122 | +### PVC Protection Removal |
| 123 | + |
| 124 | +To force-delete protected PVCs: |
| 125 | + |
| 126 | +```bash |
| 127 | +./finalizer-cleaner \ |
| 128 | + --kinds v1/PersistentVolumeClaim \ |
| 129 | + --finalizers "kubernetes.io/pvc-protection" |
| 130 | +``` |
| 131 | + |
| 132 | +### Custom Operator Cleanup |
| 133 | + |
| 134 | +Remove custom operator finalizers: |
| 135 | + |
| 136 | +```bash |
| 137 | +./finalizer-cleaner \ |
| 138 | + --kinds "example.com/v1/CustomResource" \ |
| 139 | + --finalizers "example.com/operator-finalizer" |
| 140 | +``` |
| 141 | + |
| 142 | +## Architecture |
| 143 | + |
| 144 | +The tool is structured into several modules: |
| 145 | + |
| 146 | +- `main.go`: Entry point and orchestration |
| 147 | +- `config.go`: Configuration parsing and validation |
| 148 | +- `tui.go`: Interactive terminal UI components |
| 149 | +- `kubernetes.go`: Kubernetes client and operations |
| 150 | +- `logger.go`: Structured logging utilities |
| 151 | +- `utils.go`: Helper functions |
| 152 | + |
| 153 | +## Requirements |
| 154 | + |
| 155 | +- Go 1.21 or later |
| 156 | +- Access to a Kubernetes cluster (via `~/.kube/config` or `KUBECONFIG` environment variable) |
| 157 | +- Appropriate RBAC permissions to list and patch target resources |
| 158 | + |
| 159 | +## Known Issues |
| 160 | + |
| 161 | +### Dependency Conflicts |
| 162 | +This project currently uses `replace` directives in `go.mod` to resolve protobuf registration conflicts between `gnostic` and `gnostic-models` libraries. See `DEPENDENCY_NOTES.md` for details and future migration plans. |
| 163 | + |
| 164 | +## License |
| 165 | + |
| 166 | +MIT License |
| 167 | + |
| 168 | +## Contributing |
| 169 | + |
| 170 | +Contributions are welcome! Please feel free to submit a Pull Request. |
0 commit comments