迁移 ConfigManagement 对象

本页面介绍如何将 Git 配置从 ConfigManagement 对象迁移到 RootSync 对象。迁移会启用 RootSyncRepoSync API,让您可以使用更多功能:

即使您只想使用根代码库并且不想使用任何命名空间代码库,您也可以启用这些 API。

迁移 ConfigManagement 设置

使用 nomos migrate

从 1.10.0 版开始,nomos 提供了 nomos migrate 命令来启用 RootSyncRepoSync API。您需要nomos 更新为 1.10.0 及更高版本。

如需详细了解如何运行该命令,请按照从 ConfigManagement 对象迁移到 RootSync 对象进行操作。 确保 ConfigManagement 对象未签入可靠来源并由 Config Sync 管理。如果是,您应按照手动迁移中的步骤修改可靠来源中的 ConfigManagement 对象。

手动迁移

如果您的 nomos 版本低于 1.10.0,则可以手动迁移设置。您需要在 ConfigManagement 对象中将 spec.enableMultiRepo 设置为 true,并创建一个 RootSync 对象以将根代码库同步到集群。根代码库可以是非结构化代码库,也可以是分层代码库。在迁移到使用 RootSync 对象后,您可以将代码库拆分为多个代码库,并且可以配置从多个代码库同步

如需通过迁移配置来配置根代码库,请完成以下任务:

  1. 打开您的 ConfigManagement 对象
  2. 复制 spec.git 字段中的值。您在创建 RootSync 对象时会用到这些值。
  3. 从 ConfigManagement 对象中移除所有 spec.git 字段(包括 git:)。
  4. 在 ConfigManagement 对象中,将 spec.enableMultiRepo 字段设置为 true

    # config-management.yaml
    apiVersion: configmanagement.gke.io/v1
    kind: ConfigManagement
    metadata:
      name: config-management
    spec:
      enableMultiRepo: true
    
  5. 应用更改:

    kubectl apply -f config-management.yaml
    
  6. 等待 RootSync CRD 创建完成。

    kubectl wait --for=condition=established crd rootsyncs.configsync.gke.io
    
  7. 使用从 ConfigManagement 对象复制的值创建 RootSync 对象。例如:

    # root-sync.yaml
    apiVersion: configsync.gke.io/v1beta1
    kind: RootSync
    metadata:
      name: root-sync
      namespace: config-management-system
    spec:
      sourceFormat: ROOT_FORMAT
      git:
        repo: ROOT_REPOSITORY
        revision: ROOT_REVISION
        branch: ROOT_BRANCH
        dir: "ROOT_DIRECTORY"
        auth: ROOT_AUTH_TYPE
        gcpServiceAccountEmail: ROOT_EMAIL
        # secretRef should be omitted if the auth type is none, gcenode, or gcpserviceaccount.
        secretRef:
          name: git-creds
    

    替换以下内容:

    • ROOT_FORMAT:添加 unstructured 以使用非结构化代码库,或添加 hierarchy 以使用分层代码库。 这些值区分大小写。 此字段是可选字段,默认值为 hierarchy。我们建议您添加 unstructured,因为您可以采用这种格式以最适合您的方式整理配置。
    • ROOT_REPOSITORY:添加要用作根代码库的 Git 代码库的网址。您可以输入使用 HTTPS 或 SSH 协议的网址。例如,https://github.com/GoogleCloudPlatform/anthos-config-management-samples 使用 HTTPS 协议。如果您不输入协议,则网址会被视为 HTTPS 网址。此字段是必填字段。
    • ROOT_REVISION:添加要签出的 Git 修订版本(标记或哈希)。此字段是可选字段,默认值为 HEAD
    • ROOT_BRANCH:添加要从中同步的代码库的分支。此字段是可选字段,默认值为 master
    • ROOT_DIRECTORY:添加 Git 代码库中指向您要同步的配置所在的根目录的路径。此字段是可选字段,默认值为代码库的根目录 (/)。
    • ROOT_AUTH_TYPE:添加以下身份验证类型之一:

      • none:不使用身份验证
      • ssh:使用 SSH 密钥对
      • cookiefile:使用 cookiefile
      • token:使用令牌
      • gcpserviceaccount:使用 Google 服务账号访问 Cloud Source Repositories 中的代码库。
      • gcenode:使用 Google 服务账号访问 Cloud Source Repositories 中的代码库。请仅在集群中未启用 Workload Identity 时选择此选项。

        如需详细了解这些身份验证类型,请参阅授予 Config Sync 对 Git 的只读权限

      此字段为必填字段。

    • ROOT_EMAIL:如果您已将 gcpserviceaccount 添加为 ROOT_AUTH_TYPE,请添加您的 Google 服务账号电子邮件地址。例如 acm@PROJECT_ID.iam.gserviceaccount.com

  8. 应用更改:

    kubectl apply -f root-sync.yaml
    

ConfigManagement 和 RootSync 对照表

下表简要介绍了 ConfigMangent 对象中的字段如何映射到 RootSync 对象中的字段。

ConfigManagement 字段 RootSync 字段
spec.git.gcpServiceAccountEmail spec.git.gcpServiceAccountEmail
spec.git.syncRepo spec.git.repo
spec.git.syncBranch spec.git.branch
spec.git.policyDir spec.git.dir
spec.git.syncWait spec.git.period
spec.git.syncRev spec.git.revision
spec.git.secretType spec.git.auth
git-creds(这是 ConfigManagement 对象中的固定值) spec.git.secretRef.name
spec.sourceFormat spec.sourceFormat
spec.git.proxy.httpProxyspec.git.proxy.httpsProxy spec.git.proxy

后续步骤