# kube-sync **Repository Path**: hexug/kube-sync ## Basic Information - **Project Name**: kube-sync - **Description**: kube-sync 是一个用于 同步和过滤 Kubernetes 资源 的命令行工具。 - **Primary Language**: Go - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: https://gitee.com/hexug/kube-sync - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-09-08 - **Last Updated**: 2025-09-29 ## Categories & Tags **Categories**: Uncategorized **Tags**: Kubernetes, Go语言, sync ## README # kube-sync kube-sync 是一个用于 **同步和过滤 Kubernetes 资源** 的命令行工具。 通过自定义规则(include/exclude),用户可以灵活地指定需要保留或排除的字段,避免无关字段带来的干扰,专注于业务核心配置。 ## ✨ 功能特性 - 支持多命名空间资源同步 - 支持 `Deployment`、`Service`、`ConfigMap`、`ServiceAccount`、`Role`、`RoleBinding`、`ClusterRole`、`ClusterRoleBinding`、`PersistentVolumeClaim`、`PersistentVolume`、`secret`等常见 Kubernetes 资源 - 通过 **include/exclude 路径规则** 精准控制同步字段 - 支持从文件、环境变量或默认配置加载 - 提供 Debug 模式,便于问题排查 - 可输出到指定目录,便于与 GitOps 或配置管理结合 ## 📂 项目结构 ``` kube-sync/ ├── apps/ # 应用相关代码或示例 ├── cmd/ # 主程序入口(CLI 命令) ├── conf/ # 默认配置文件、样例配置 ├── worker/ # 核心同步逻辑(资源处理器/任务执行器) ├── old/ # 历史遗留代码(可作为参考) ├── main.go # 程序入口 ├── config.yaml # 配置文件示例 ├── go.mod ├── go.sum ├── Makefile # 构建脚本 └── README.md ``` ## 📦 安装与编译 ```bash # 克隆项目 git clone https://gitee.com/hexug/kube-sync.git cd kube-sync # 编译 go mod tidy go build -o kube-sync . ``` ## 🎯 使用方式 在项目根目录下执行: ```bash go run . -o abc -n ns1 -n ns2 -n ns3 -n ns4 -f config.yaml -d ``` 示例说明: - `-o abc` → 指定输出路径为 `abc` - `-n ns1 -n ns2 ...` → 指定需要处理的命名空间(支持多个) - `-f config.yaml` → 使用配置文件 `config.yaml` - `-d` → 开启 debug 模式,输出调试日志 ## ⚙️ 命令行参数 | 参数 | 简写 | 类型 | 说明 | | --------------- | ---- | -------- | ------------------------------------------------------------ | | `--version` | `-v` | bool | 打印简短版本信息 | | `--Version` | `-V` | bool | 打印完整版本信息 | | `--config-type` | `-t` | string | 指定配置加载方式,可选:`file`、`env`、`default` | | `--config-path` | `-f` | string | 指定配置文件路径 | | `--debug` | `-d` | bool | 开启 debug 模式 | | `--namespaces` | `-n` | string[] | 指定需要处理的命名空间(可多次传入) | | `--excludes` | `-e` | string[] | 指定排除的服务,格式 `::` | | `--out` | `-o` | string | 指定输出路径,默认为 `./` | ## 🛠️ 配置规则 `kube-sync` 使用 YAML 配置文件来定义同步规则。核心字段是 **rules**,每个资源类型(如 `deployment`、`service`、`configmap`、`ServiceAccount`、`Role`、`RoleBinding`、`ClusterRole`、`ClusterRoleBinding`、`PersistentVolumeClaim`、`PersistentVolume`、`secret`)可以通过 **include/exclude 路径规则** 来控制哪些字段参与同步,哪些字段被忽略。 ### 示例配置 ```yaml rules: config: deployment: include: - "metadata/labels/app" - "spec/template/metadata/labels/a" - "spec/template/metadata/labels/app" - "spec/template/metadata/labels/b" exclude: - "metadata/annotations" - "metadata/managedFields" - "metadata/creationTimestamp" - "metadata/generation" - "metadata/resourceVersion" - "metadata/uid" - "spec/progressDeadlineSeconds" - "spec/revisionHistoryLimit" - "spec/strategy" - "spec/template/metadata/annotations" - "spec/template/metadata/creationTimestamp" - "spec/template/spec/containers/terminationMessagePath" - "spec/template/spec/containers/terminationMessagePolicy" - "spec/template/spec/restartPolicy" - "spec/template/spec/terminationGracePeriodSeconds" - "spec/template/spec/dnsPolicy" - "spec/template/spec/securityContext" - "spec/template/spec/schedulerName" - "status" service: include: [] exclude: - "metadata/annotations" - "metadata/creationTimestamp" - "metadata/managedFields" - "metadata/resourceVersion" - "metadata/uid" - "status" - "spec/clusterIP" - "spec/clusterIPs" - "spec/internalTrafficPolicy" - "spec/ipFamilies" - "spec/ipFamilyPolicy" - "spec/sessionAffinity" - "spec/externalTrafficPolicy" configmap: include: - "apiVersion" - "kind" - "metadata/name" - "metadata/namespace" - "metadata/labels" - "data" exclude: [] serviceaccount: include: - "apiVersion" - "kind" - "metadata/name" - "metadata/namespace" exclude: role: include: - "apiVersion" - "kind" - "metadata/name" - "metadata/namespace" - "rules" exclude: rolebindings: include: - "apiVersion" - "kind" - "metadata/name" - "metadata/namespace" - "subjects" - "roleRef" exclude: clusterrole: include: - "apiVersion" - "kind" - "metadata/name" - "rules" exclude: clusterrolebindings: include: - "apiVersion" - "kind" - "metadata/name" - "subjects" - "roleRef" exclude: pv: exclude: - "metadata/annotations" - "metadata/finalizers" - "metadata/creationTimestamp" - "metadata/managedFields" - "metadata/resourceVersion" - "metadata/uid" - "spec/claimRef/kind" - "spec/claimRef/uid" - "spec/claimRef/apiVersion" - "spec/claimRef/resourceVersion" - "status" pvc: exclude: - "metadata/annotations" - "metadata/finalizers" - "metadata/creationTimestamp" - "metadata/managedFields" - "metadata/resourceVersion" - "metadata/uid" - "status" secret: include: - "apiVersion" - "kind" - "metadata/name" - "metadata/namespace" - "data" - "type" ``` ### 字段说明 - `rules.config.deployment.include` 指定 Deployment 同步时需要保留的字段路径。 - `rules.config.deployment.exclude` 指定 Deployment 同步时需要忽略的字段路径(通常是集群自动生成的、动态字段,例如 `status`、`uid` 等)。 - `rules.config.service` 定义 Service 资源的 include/exclude 规则。 - `rules.config.configmap` 定义 ConfigMap 资源的 include/exclude 规则。 - `rules.config.serviceaccount` 定义 ServiceAccount 资源的 include/exclude 规则。 - `rules.config.role` 定义 Role 资源的 include/exclude 规则。 - `rules.config.rolebindings` 定义 RoleBinding 资源的 include/exclude 规则。 - `rules.config.clusterrole` 定义 ClusterRole 资源的 include/exclude 规则。 - `rules.config.clusterrolebindings` 定义 ClusterRoleBinding 资源的 include/exclude 规则。 - `rules.config.pv` 定义 PersistentVolume 资源的 include/exclude 规则。 - `rules.config.pvc` 定义 PersistentVolumeClaim 资源的 include/exclude 规则。 - `rules.config.secret` 定义 Secret 资源的 include/exclude 规则。 ## 📄 License 本项目基于 [Apache 2.0 License](./LICENSE) 开源。