开发指南
text
# 相关代码:
- go.mod # 依赖管理
- Dockerfile # 镜像构建
- hack/tools.go # 代码生成工具
- api/v1alpha1/ # CRD 类型定义本地开发
环境要求
- Go 1.25+
- Kubernetes 集群(或 kind/minikube)
- kubectl 已配置
构建
bash
# 编译
go build -o running-control ./cmd
# 运行(需要 kubeconfig)
./running-control \
--gate-gvr=pods.v1. \
--gate-namespace=default \
--gate-name=test-pod \
--gate-expression='object.metadata.annotations["ready"] == "true"'本地测试流程
代码生成
CRD 类型
bash
# 安装 controller-tools
go install sigs.k8s.io/controller-tools/cmd/controller-gen@latest
# 生成 deepcopy 方法
controller-gen object paths=./api/v1alpha1/
# 生成 CRD YAML
controller-gen crd paths=./api/v1alpha1/ output:crd:dir=./manifests/crds生成的文件
| 文件 | 用途 |
|---|---|
zz_generated.deepcopy.go | DeepCopy 接口实现 |
zz_generated.register.go | Scheme 注册 |
manifests/crds/*.yaml | CRD 定义 |
项目结构
.
├── api/
│ └── v1alpha1/ # CRD 类型定义
│ ├── doc.go # 包文档和 marker
│ ├── podrunninggate_types.go # 核心类型
│ └── zz_generated.* # 生成文件
├── cel/
│ ├── condition.go # CEL 条件封装
│ └── evaluate.go # CEL 评估逻辑
├── cmd/
│ └── main.go # 程序入口
├── examples/ # 使用示例
├── hack/
│ └── tools.go # 构建工具依赖
├── manifests/
│ ├── crds/ # CRD YAML
│ └── rbac.yaml # RBAC 配置
├── vendor/ # 依赖副本
├── Dockerfile
├── go.mod
└── go.sum调试技巧
1. 查看 CEL 编译结果
在 main.go 中添加:
go
condition := compiler.CompileCELExpression(...)
fmt.Printf("Expression: %s\n", condition.ExpressionAccessor.GetExpression())
fmt.Printf("Error: %v\n", condition.Error)2. 查看 Informer 事件
go
gateChecker := func(obj *unstructured.Unstructured) {
fmt.Printf("Event: %s/%s\n", obj.GetNamespace(), obj.GetName())
fmt.Printf("Object: %+v\n", obj.Object)
// ... 原有逻辑
}3. 模拟不同场景
bash
# 创建测试 Pod
kubectl run test-pod --image=nginx --restart=Never
# 添加 annotation
kubectl annotate pod test-pod key=value
# 修改 annotation
kubectl annotate pod test-pod key=newvalue --overwrite
# 删除 annotation
kubectl annotate pod test-pod key-发布流程
构建镜像
bash
# 本地测试
docker build -t pod-running-control:dev .
# 多架构构建
docker buildx build \
--platform linux/amd64,linux/arm64 \
-t ghcr.io/iceber/pod-running-control:v0.1.0 \
--push .版本管理
项目使用 Git tag 管理版本:
bash
git tag -a v0.1.0 -m "Release v0.1.0"
git push origin v0.1.0贡献指南
- Fork 仓库
- 创建特性分支
git checkout -b feature/xxx - 提交更改
git commit -am 'Add xxx' - 推送分支
git push origin feature/xxx - 创建 Pull Request
代码规范
- 使用
gofmt格式化 - 通过
go vet检查 - 保持简洁,避免过度抽象