This project is a Kubernetes controller built using the controller-runtime library. It manages the cleanup of PersistentVolumeClaim
(PVC) resources by removing their references from associated PersistentVolume
(PV) resources when the PVC is deleted.
- Automatically clears the
claimRef
field in aPersistentVolume
when its associatedPersistentVolumeClaim
is deleted. - Ensures proper cleanup by using a custom finalizer (
liberator.io/pv-claim-ref-cleanup
) on PVCs. - Provides health and readiness probes for monitoring the controller's status.
- Kubernetes cluster (v1.20+ recommended)
- Go (v1.19+)
- Controller-runtime library
-
Clone the repository:
git clone <repository-url> cd <repository-directory>
-
Build the controller:
go build -o pvc-controller main.go
-
Deploy the controller to your Kubernetes cluster. Ensure the necessary RBAC permissions are configured.
--health-probe-bind-address
: Address for health and readiness probes (default::8081
).--leader-elect
: Enable leader election for high availability (default:false
).
The controller does not automatically add the finalizer to PVCs. Users must manually add the finalizer liberator.io/pv-claim-ref-cleanup
to PVCs that require cleanup.
Example:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: example-pvc
namespace: default
finalizers:
- liberator.io/pv-claim-ref-cleanup
spec: ...
-
main.go
:- Initializes the controller manager.
- Configures health and readiness probes.
- Sets up the
PVCReconciler
.
-
PVCReconciler
:- Watches for PVC events.
- Handles PVC deletion by:
- Clearing the
claimRef
field in the associated PV. - Removing the custom finalizer from the PVC.
- Clearing the
-
Helper Functions:
hasFinalizer
: Checks if a PVC has the custom finalizer.removeFinalizer
: Removes the custom finalizer from a PVC.
- When a PVC with the custom finalizer is deleted:
- The controller fetches the associated PV.
- If the PV's
claimRef
matches the PVC, it clears theclaimRef
. - The finalizer is removed from the PVC, allowing it to be deleted.
- Health Probe:
/healthz
- Readiness Probe:
/readyz
These endpoints can be used to monitor the controller's status.
- Set up a Kubernetes cluster (e.g., using kind).
- Run the controller locally:
go run main.go
- Unit tests can be added for the reconciliation logic and helper functions.
- Use tools like envtest for integration testing.
Contributions are welcome! Please open an issue or submit a pull request.
This project is licensed under the Apache 2.0 License.