-
Notifications
You must be signed in to change notification settings - Fork 14
feat: 进程和配置管理多租户适配 #432
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
feat: 进程和配置管理多租户适配 #432
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR adapts CMDB/GSE synchronization and business host management to support multi-tenant operation, adding tenant scoping to data models, background tasks, and external component calls.
Changes:
- Add
tenant_idas part of the BizHost schema and wire it through DAO, sync, and cleanup flows, including a DB migration and generated DAL updates. - Propagate tenant ID through CMDB/GSE sync services, task builders/executors, and processors, ensuring process and instance data, as well as host/biz relations, are tenant-aware.
- Introduce multi-tenant handling for periodic crontab tasks (CMDB sync, CMDB resource watcher, biz/host sync & cleanup, host/biz-host cursors) and add a shared tenant selection utility via BK-User.
Reviewed changes
Copilot reviewed 19 out of 19 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| pkg/dal/table/biz_host.go | Adds TenantID as part of the BizHost composite primary key, enabling tenant-level separation of biz-host relations. |
| internal/task/step/gse/sync_gse.go | Extends GSE sync step to carry tenantID in function signature, logging, and payload. |
| internal/task/step/cmdb/sync_cmdb.go | Extends CMDB sync step to include tenantID in signature, logging, and SyncCMDB payload. |
| internal/task/executor/cmdb_gse/cmdb_sync_gse.go | Passes tenant ID into CMDB and GSE processors and updates logging to include tenant context. |
| internal/task/builder/cmdb_gse/sync_cmdb_gse.go | Updates task builder to store tenant ID and inject it into CMDB/GSE steps. |
| internal/processor/gse/sync_gse.go | Makes GSE sync service tenant-aware by storing tenantID and setting kit.TenantID before DAO usage. |
| internal/processor/cmdb/sync_cmdb.go | Makes CMDB process sync tenant-aware (processes and instances) via tenant field propagation and filtering, but currently does not set kit.TenantID from s.tenantID, which can break tenant isolation. |
| internal/dal/gen/biz_hosts.gen.go | Regenerates BizHost DAL metadata to include the tenant_id field and updates its field map. |
| internal/components/bkuser/bkuser.go | Adds TenantInfo and a ListEnabledTenants helper to fetch and filter enabled tenants via BK-User. |
| cmd/data-service/service/sync_cmdb.go | Updates CMDB/GSE sync service APIs and cron-triggered sync to pass tenant IDs into task builders. |
| cmd/data-service/service/crontab/watch_host_updates.go | Makes host-update watcher tenant-aware by iterating tenants, using tenant-prefixed cursor keys, and initializing per-tenant cursors. |
| cmd/data-service/service/crontab/watch_biz_host_relation.go | Makes biz-host relation watcher tenant-aware with per-tenant iteration and per-tenant cursor keys, and writes BizHost with TenantID. |
| cmd/data-service/service/crontab/sync_cmdb.go | Refactors periodic CMDB/GSE sync to iterate all enabled tenants (via BK-User) in multi-tenant mode and call SynchronizeCmdbData per tenant. |
| cmd/data-service/service/crontab/sync_biz_host.go | Refactors biz-host sync cron to iterate tenants (from App table), then sync biz-host relations per tenant while writing BizHost with TenantID. |
| cmd/data-service/service/crontab/cmdb_resource_watcher.go | Makes CMDB resource watcher tenant-aware: iterates tenants, prefixes cursor keys with tenant, and passes tenant ID into CMDB sync and GSE sync. |
| cmd/data-service/service/crontab/cleanup_biz_host.go | Refactors biz-host cleanup cron to operate per tenant using tenant IDs from the App table. |
| cmd/data-service/db-migration/migrations/20260122100000_add_tenant_id_to_biz_hosts.go | Adds a migration to introduce tenant_id to biz_hosts and adjust the primary key to (tenant_id, bk_biz_id, bk_host_id), with a down migration to revert. |
| cmd/data-service/app/app.go | Wraps biz-host-related cursor initialization in a per-tenant initializer that runs over tenants or falls back to single-tenant mode. |
| .cursor/rules/tenant.md | Documents multi-tenant development conventions, including parameter ordering, GORM hook behavior, cron patterns, schema requirements, and external API headers. |
Comments suppressed due to low confidence (1)
internal/processor/cmdb/sync_cmdb.go:171
syncCMDBService.SyncSingleBizconstructs akitfrom the incoming context but never setskit.TenantIDfroms.tenantID, whilesyncProcessDataand the DAO layer rely onkit.TenantID(via the GORM tenant hook) to scope process / process_instance operations by tenant. In multi-tenant mode this can cause process data operations (e.g. UpdateSyncStatusWithTx, DeleteStoppedUnmanagedWithTx, BatchCreateWithTx on process instances) to run without tenant filtering or against the wrong tenant if the context lacks the correct tenant metadata. To keep tenant isolation consistent with the newtenantIDparameter propagation, setkit.TenantID = s.tenantID(similar tosyncGSEService.SyncSingleBiz) before callingsyncProcessData.
// 构建并立即入库
bizs := Bizs{s.bizID: sets}
processBatch := buildProcess(bizs, s.tenantID)
// 开启事务并入库
tx := s.dao.GenQuery().Begin()
if err := s.syncProcessData(kit, tx, processBatch); err != nil {
if rbErr := tx.Rollback(); rbErr != nil {
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
No description provided.