Skip to content

Commit 3d397c7

Browse files
committed
feat[#1]: append data into crds
1 parent 8d72aca commit 3d397c7

File tree

8 files changed

+711
-1
lines changed

8 files changed

+711
-1
lines changed

Dockerfile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Build stage
2+
FROM golang:1.24-alpine AS builder
3+
4+
WORKDIR /app
5+
6+
COPY go.mod go.sum ./
7+
8+
RUN go mod download
9+
10+
COPY *.go ./
11+
12+
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o file-monitor-controller .
13+
14+
# Final stage
15+
FROM alpine:latest
16+
17+
RUN apk --no-cache add ca-certificates
18+
19+
WORKDIR /root/
20+
21+
COPY --from=builder /app/file-monitor-controller .
22+
23+
CMD ["./file-monitor-controller"]

README.md

Lines changed: 130 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,130 @@
1-
# file-monitor-kube-controller
1+
# file-monitor-kube-controller
2+
3+
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.
4+
5+
## Features
6+
7+
- Queries Kubernetes CRDs using the k8s.io/client-go library
8+
- Monitors file system changes in specified paths
9+
- Updates CRD status with file information (inode, name, size, modification time)
10+
- Supports both in-cluster and out-of-cluster configurations
11+
- Namespace-aware file monitoring
12+
13+
## Prerequisites
14+
15+
- Go 1.24 or later
16+
- Kubernetes cluster (for deployment)
17+
- kubectl configured to access your cluster
18+
19+
## Installation
20+
21+
1. **Install the CRD:**
22+
```bash
23+
kubectl apply -f filemonitor-crd.yaml
24+
```
25+
26+
2. **Deploy the controller:**
27+
```bash
28+
kubectl apply -f deployment.yaml
29+
```
30+
31+
3. **Create a FileMonitor resource:**
32+
```bash
33+
kubectl apply -f example-filemonitor.yaml
34+
```
35+
36+
## Usage
37+
38+
### Running locally (development)
39+
40+
1. **Build the application:**
41+
```bash
42+
go build -o file-monitor-controller
43+
```
44+
45+
2. **Run the controller:**
46+
```bash
47+
./file-monitor-controller
48+
```
49+
50+
### Running in Kubernetes
51+
52+
1. **Build and push Docker image:**
53+
```bash
54+
docker build -t your-registry/file-monitor-controller:latest .
55+
docker push your-registry/file-monitor-controller:latest
56+
```
57+
58+
2. **Update the deployment image:**
59+
```bash
60+
# Edit deployment.yaml to use your image
61+
kubectl apply -f deployment.yaml
62+
```
63+
64+
## CRD Structure
65+
66+
The FileMonitor CRD has the following structure:
67+
68+
```yaml
69+
apiVersion: sentinalfs.io/v1
70+
kind: FileMonitor
71+
metadata:
72+
name: example-file-monitor
73+
namespace: default
74+
spec:
75+
path: "/tmp" # Path to monitor
76+
namespace: "default" # Target namespace
77+
status:
78+
files: # Updated by controller
79+
- name: "example.txt"
80+
inode: 12345
81+
size: 1024
82+
modTime: "2025-01-13T10:00:00Z"
83+
path: "/tmp/example.txt"
84+
isDir: false
85+
```
86+
87+
## Controller Functionality
88+
89+
The controller performs the following operations in a continuous loop:
90+
91+
1. **Query CRDs:** Lists all FileMonitor resources across namespaces
92+
2. **Namespace-specific queries:** Queries CRDs in specific namespaces
93+
3. **File information updates:** Updates CRD status with current file information
94+
4. **Error handling:** Gracefully handles missing CRDs and API errors
95+
96+
## Key Functions
97+
98+
- `queryCRDs()`: Queries all CRDs across all namespaces
99+
- `queryCRDsInNamespace()`: Queries CRDs in a specific namespace
100+
- `updateCRDWithFileInfo()`: Updates CRD status with file information
101+
- `initKubernetesClients()`: Initializes both regular and dynamic K8s clients
102+
103+
## Configuration
104+
105+
The controller automatically detects the runtime environment:
106+
- **In-cluster:** Uses service account credentials
107+
- **Local development:** Uses kubeconfig from `~/.kube/config`
108+
109+
## RBAC Permissions
110+
111+
The controller requires the following permissions:
112+
- Get, list, watch, create, update, patch, delete on `filemonitors` resources
113+
- Get, update, patch on `filemonitors/status`
114+
- Create, patch on `events` for logging
115+
116+
## Monitoring
117+
118+
The controller logs its operations including:
119+
- CRD discovery and enumeration
120+
- File system monitoring events
121+
- Error conditions and retries
122+
- Status updates
123+
124+
## Contributing
125+
126+
1. Fork the repository
127+
2. Create a feature branch
128+
3. Make your changes
129+
4. Test thoroughly
130+
5. Submit a pull request

daemonset.yaml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
apiVersion: apps/v1
2+
kind: DaemonSet
3+
metadata:
4+
name: file-monitor-controller
5+
namespace: default
6+
labels:
7+
app: file-monitor-controller
8+
spec:
9+
selector:
10+
matchLabels:
11+
app: file-monitor-controller
12+
template:
13+
metadata:
14+
labels:
15+
app: file-monitor-controller
16+
spec:
17+
serviceAccountName: file-monitor-controller
18+
containers:
19+
- name: controller
20+
image: file-monitor-controller:latest
21+
imagePullPolicy: IfNotPresent
22+
env:
23+
- name: WATCH_NAMESPACE
24+
value: ""
25+
resources:
26+
limits:
27+
cpu: 200m
28+
memory: 128Mi
29+
requests:
30+
cpu: 100m
31+
memory: 64Mi
32+
---
33+
apiVersion: v1
34+
kind: ServiceAccount
35+
metadata:
36+
name: file-monitor-controller
37+
namespace: default
38+
---
39+
apiVersion: rbac.authorization.k8s.io/v1
40+
kind: ClusterRole
41+
metadata:
42+
name: file-monitor-controller
43+
rules:
44+
- apiGroups: ["sentinalfs.io"]
45+
resources: ["filemonitors"]
46+
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
47+
- apiGroups: ["sentinalfs.io"]
48+
resources: ["filemonitors/status"]
49+
verbs: ["get", "update", "patch"]
50+
- apiGroups: [""]
51+
resources: ["events"]
52+
verbs: ["create", "patch"]
53+
---
54+
apiVersion: rbac.authorization.k8s.io/v1
55+
kind: ClusterRoleBinding
56+
metadata:
57+
name: file-monitor-controller
58+
roleRef:
59+
apiGroup: rbac.authorization.k8s.io
60+
kind: ClusterRole
61+
name: file-monitor-controller
62+
subjects:
63+
- kind: ServiceAccount
64+
name: file-monitor-controller
65+
namespace: default

example-filemonitor.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
apiVersion: sentinalfs.io/v1
2+
kind: FileMonitor
3+
metadata:
4+
name: example-file-monitor
5+
namespace: default
6+
spec:
7+
path: "/tmp"
8+
namespace: "default"

filemonitor-crd.yaml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
apiVersion: apiextensions.k8s.io/v1
2+
kind: CustomResourceDefinition
3+
metadata:
4+
name: filemonitors.sentinalfs.io
5+
spec:
6+
group: sentinalfs.io
7+
versions:
8+
- name: v1
9+
served: true
10+
storage: true
11+
schema:
12+
openAPIV3Schema:
13+
type: object
14+
properties:
15+
spec:
16+
type: object
17+
properties:
18+
path:
19+
type: string
20+
description: "The file system path to monitor"
21+
namespace:
22+
type: string
23+
description: "The namespace this monitor belongs to"
24+
required:
25+
- path
26+
- namespace
27+
status:
28+
type: object
29+
properties:
30+
files:
31+
type: array
32+
items:
33+
type: object
34+
properties:
35+
name:
36+
type: string
37+
inode:
38+
type: integer
39+
format: int64
40+
size:
41+
type: integer
42+
format: int64
43+
modTime:
44+
type: string
45+
format: date-time
46+
path:
47+
type: string
48+
isDir:
49+
type: boolean
50+
required:
51+
- name
52+
- inode
53+
- size
54+
- modTime
55+
- path
56+
- isDir
57+
scope: Namespaced
58+
names:
59+
plural: filemonitors
60+
singular: filemonitor
61+
kind: FileMonitor

go.mod

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
module fileMonitorKubeController
2+
3+
go 1.24.5
4+
5+
require (
6+
k8s.io/apimachinery v0.33.2
7+
k8s.io/client-go v0.33.2
8+
)
9+
10+
require (
11+
github.com/davecgh/go-spew v1.1.1 // indirect
12+
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
13+
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
14+
github.com/go-logr/logr v1.4.2 // indirect
15+
github.com/go-openapi/jsonpointer v0.21.0 // indirect
16+
github.com/go-openapi/jsonreference v0.20.2 // indirect
17+
github.com/go-openapi/swag v0.23.0 // indirect
18+
github.com/gogo/protobuf v1.3.2 // indirect
19+
github.com/google/gnostic-models v0.6.9 // indirect
20+
github.com/google/go-cmp v0.7.0 // indirect
21+
github.com/google/uuid v1.6.0 // indirect
22+
github.com/josharian/intern v1.0.0 // indirect
23+
github.com/json-iterator/go v1.1.12 // indirect
24+
github.com/mailru/easyjson v0.7.7 // indirect
25+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
26+
github.com/modern-go/reflect2 v1.0.2 // indirect
27+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
28+
github.com/pkg/errors v0.9.1 // indirect
29+
github.com/spf13/pflag v1.0.5 // indirect
30+
github.com/x448/float16 v0.8.4 // indirect
31+
golang.org/x/net v0.38.0 // indirect
32+
golang.org/x/oauth2 v0.27.0 // indirect
33+
golang.org/x/sys v0.31.0 // indirect
34+
golang.org/x/term v0.30.0 // indirect
35+
golang.org/x/text v0.23.0 // indirect
36+
golang.org/x/time v0.9.0 // indirect
37+
google.golang.org/protobuf v1.36.5 // indirect
38+
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
39+
gopkg.in/inf.v0 v0.9.1 // indirect
40+
gopkg.in/yaml.v3 v3.0.1 // indirect
41+
k8s.io/api v0.33.2 // indirect
42+
k8s.io/klog/v2 v2.130.1 // indirect
43+
k8s.io/kube-openapi v0.0.0-20250318190949-c8a335a9a2ff // indirect
44+
k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738 // indirect
45+
sigs.k8s.io/json v0.0.0-20241010143419-9aa6b5e7a4b3 // indirect
46+
sigs.k8s.io/randfill v1.0.0 // indirect
47+
sigs.k8s.io/structured-merge-diff/v4 v4.6.0 // indirect
48+
sigs.k8s.io/yaml v1.4.0 // indirect
49+
)

0 commit comments

Comments
 (0)