本教程介绍如何在使用 Google Kubernetes Engine 集群时通过 Cloud Build 验证配置。经过少量更改后,同样的设置也适用于任何其他基于容器的 CI/CD 系统,例如 CircleCI。
除了通过运行 nomos vet 命令检查配置的有效性之外,我们还建议您验证 CI/CD 流水线中的所有配置更改。
目标
- 创建一个 Cloud Build 配置文件,用于指示 Config Sync 对代码库中的配置使用 nomos vet。
- 创建一个 Cloud Build 触发器,以便在开发分支发生变化时检查您的配置。
费用
在本文档中,您将使用 Google Cloud的以下收费组件:
 
 
 
  如需根据您的预计使用量来估算费用,请使用价格计算器。
  
完成本文档中描述的任务后,您可以通过删除所创建的资源来避免继续计费。如需了解详情,请参阅清理。
准备工作
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
- 
    
    
      In the Google Cloud console, on the project selector page, select or create a Google Cloud project. Roles required to select or create a project - Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
- 
      Create a project: To create a project, you need the Project Creator
      (roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
 
- 
  
    Verify that billing is enabled for your Google Cloud project. 
- 
  
  
    
      Enable the Cloud Build API. Roles required to enable APIs To enable APIs, you need the Service Usage Admin IAM role ( roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.
- 
    
    
      In the Google Cloud console, on the project selector page, select or create a Google Cloud project. Roles required to select or create a project - Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
- 
      Create a project: To create a project, you need the Project Creator
      (roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
 
- 
  
    Verify that billing is enabled for your Google Cloud project. 
- 
  
  
    
      Enable the Cloud Build API. Roles required to enable APIs To enable APIs, you need the Service Usage Admin IAM role ( roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.
- 创建或有权访问满足 Config Sync 要求的 GKE 集群。如需详细了解如何创建此类集群,请参阅 Config Sync 使用入门。
- 在 Google Cloud 控制台中打开 IAM 页面。 
- 在成员列中找到 Cloud Build 服务账号所在的行: - PROJECT_NUMBER@cloudbuild.gserviceaccount.com
- 在该行中,点击 修改主账号。 
- 点击添加其他角色。 
- 在选择角色列表中,选择 - Kubernetes Engine Developer,然后点击保存。
- ZONE:您的集群运行的可用区
- CLUSTER_NAME:您的集群的名称
- POLICY_DIR:Git 代码库中的路径,表示要同步的代码库的顶层目录
- 运行 kubectl config current-context以生成向my-clusterGKE 集群进行身份验证所需的 kubeconfig 文件。根用户生成的此文件具有受限的权限。
- 运行 chmod 444 /kube/config使此文件在一步中可被读取。
- 对在 /workspace中自动克隆的 Git 代码库运行nomos vet。如果您使用非结构化代码库,请改为运行nomos vet --source-format=unstructured。
- 在 Google Cloud 控制台中打开“触发器”页面。 
- 点击连接代码库。 
- 选择 GitHub(已创建镜像),然后点击继续。 
- 选择您的代码库,然后点击关联代码库。 
- 点击添加触发器。 
- 在下表中的每个字段中输入或选择相应的条目: - 字段 - 条目 - 事件 - 推送到分支 - 分支 - ^master$ - 配置 - Cloud Build 配置文件(yaml 或 json) - Cloud Build 配置文件位置 - / cloudbuild.yaml 
- 点击创建以保存您的构建触发器。 
- 在 Google Cloud 控制台中打开“触发器”页面。 
- 找到您创建的触发器,然后点击运行触发器。 - 系统会显示消息“已启动‘master’分支的构建”。 
- 点击显示。 - 如果设置正确,Cloud Build 步骤会显示为绿色。 
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
- 删除 Cloud Build 配置文件。
- 删除您创建的 Cloud Build 触发器。
- 删除您用于本教程的集群。
授予 Cloud Build 服务账号权限
授予 Cloud Build 服务账号访问 GKE 集群的权限。
gcloud
如需将 Kubernetes Engine Developer 角色添加到 Cloud Build 服务账号,请运行以下命令:
PROJECT_ID=$(gcloud config get-value project)
PROJECT_NUM=$(gcloud projects list --filter="$PROJECT_ID" --format="value(PROJECT_NUMBER)")
gcloud projects add-iam-policy-binding $PROJECT_ID \
    --member=serviceAccount:$PROJECT_NUM@cloudbuild.gserviceaccount.com \
    --role=roles/container.developer
控制台
创建 Cloud Build 配置
创建 Cloud Build 配置文件并将其存储在包含配置文件的代码库的根目录下(例如 my-repo/cloudbuild.yaml)。
steps:
- name: 'gcr.io/cloud-builders/kubectl'
  args: ['config', 'current-context']
  volumes:
  - name: 'kube'
    path: '/kube'
  env:
  - 'KUBECONFIG=/kube/config'
  - 'CLOUDSDK_COMPUTE_ZONE=ZONE'
  - 'CLOUDSDK_CONTAINER_CLUSTER=CLUSTER_NAME'
  - 'CLOUDSDK_CONTAINER_USE_APPLICATION_DEFAULT_CREDENTIALS=true'
- name: 'bash'
  args: ['chmod', '444', '/kube/config']
  volumes:
  - name: 'kube'
    path: '/kube'
- name: 'gcr.io/config-management-release/nomos:stable'
  args: ['nomos', 'vet', '--path', '/workspace/POLICY_DIR']
  volumes:
  - name: 'kube'
    path: '/kube'
  env:
  - 'KUBECONFIG=/kube/config'
  timeout: 30s
请替换以下内容:
此配置包含三个步骤:
创建构建触发器
以下示例将创建一个触发器。当每次提交到 Cloud Source Repositories 代码库的主分支时,该触发器会运行。
测试构建触发器
通过运行触发器手动测试设置:
Cloud Build 配置无效
如果 Cloud Build 配置文件无效,则触发器无法运行。
如需对此进行测试,请使用以下文件更新代码库中的 Cloud Build 配置。请注意第 6 行中的无效缩进:
steps:
- name: 'gcr.io/cloud-builders/kubectl'
  args: ['config', 'current-context']
  volumes:
  - name: 'kube'
  path: '/kube'
  env:
  - 'KUBECONFIG=/kube/config'
  - 'CLOUDSDK_COMPUTE_ZONE=ZONE'
  - 'CLOUDSDK_CONTAINER_CLUSTER=CLUSTER_NAME'
  - 'CLOUDSDK_CONTAINER_USE_APPLICATION_DEFAULT_CREDENTIALS=true'
- name: 'bash'
  args: ['chmod', '444', '/kube/config']
  volumes:
  - name: 'kube'
    path: '/kube'
- name: 'gcr.io/nomos-release/nomos:stable'
  args: ['nomos', 'vet', '--path', '/workspace/POLICY_DIR']
  volumes:
  - name: 'kube'
    path: '/kube'
  env:
  - 'KUBECONFIG=/kube/config'
  timeout: 30s
如果您再次手动运行触发器,则会收到以下错误消息,因为第 6 行上的 path: 未正确缩进:
Failed to trigger build: failed unmarshalling build config cloudbuild.yaml:
unknown field "path" in cloudbuild_go_proto.BuildStep.
如需更正此配置,请缩进第 6 行的 path:,使其与第 5 行的 name: 齐平。如需详细了解 Cloud Build 配置文件的结构,请参阅创建基本 Cloud Build 配置。
清理
删除项目
删除各个资源
要逐个删除资源,请完成以下步骤: