A Kubernetes controller that monitors file system changes and updates Custom Resource Definitions (CRDs) with file information such as inode, file name, size, and modification time.
- Queries Kubernetes CRDs using the k8s.io/client-go library
- Monitors file system changes in specified paths
- Updates CRD status with file information (inode, name, size, modification time)
- Supports both in-cluster and out-of-cluster configurations
- Namespace-aware file monitoring
- Go 1.24 or later
- Kubernetes cluster (for deployment)
- kubectl configured to access your cluster
-
Install the CRD:
kubectl apply -f filemonitor-crd.yaml
-
Deploy the controller:
kubectl apply -f deployment.yaml
-
Create a FileMonitor resource:
kubectl apply -f example-filemonitor.yaml
-
Build the application:
go build -o file-monitor-controller
-
Run the controller:
./file-monitor-controller
-
Build and push Docker image:
docker build -t your-registry/file-monitor-controller:latest . docker push your-registry/file-monitor-controller:latest -
Update the deployment image:
# Edit deployment.yaml to use your image kubectl apply -f deployment.yaml
The FileMonitor CRD has the following structure:
apiVersion: sentinalfs.io/v1
kind: FileMonitor
metadata:
name: example-file-monitor
namespace: default
spec:
path: "/tmp" # Path to monitor
namespace: "default" # Target namespace
status:
files: # Updated by controller
- name: "example.txt"
inode: 12345
size: 1024
modTime: "2025-01-13T10:00:00Z"
path: "/tmp/example.txt"
isDir: falseThe controller performs the following operations in a continuous loop:
- Query CRDs: Lists all FileMonitor resources across namespaces
- Namespace-specific queries: Queries CRDs in specific namespaces
- File information updates: Updates CRD status with current file information
- Error handling: Gracefully handles missing CRDs and API errors
queryCRDs(): Queries all CRDs across all namespacesqueryCRDsInNamespace(): Queries CRDs in a specific namespaceupdateCRDWithFileInfo(): Updates CRD status with file informationinitKubernetesClients(): Initializes both regular and dynamic K8s clients
The controller automatically detects the runtime environment:
- In-cluster: Uses service account credentials
- Local development: Uses kubeconfig from
~/.kube/config
The controller requires the following permissions:
- Get, list, watch, create, update, patch, delete on
filemonitorsresources - Get, update, patch on
filemonitors/status - Create, patch on
eventsfor logging
The controller logs its operations including:
- CRD discovery and enumeration
- File system monitoring events
- Error conditions and retries
- Status updates
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request