使用 Google Cloud 控制台部署功能标志
准备工作
-
登录您的 Google 账号。
如果您还没有 Google 账号,请注册新账号。
-
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 role
(
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 App Lifecycle Manager, SaaS Config, App Design Center, Artifact Registry, Cloud Storage, and Infrastructure Manager APIs, if any are not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init -
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 role
(
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 App Lifecycle Manager, SaaS Config, App Design Center, Artifact Registry, Cloud Storage, and Infrastructure Manager APIs, if any are not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init -
确保您的用户账号在 Google Cloud 项目中具有以下 Identity and Access Management 角色:
- App Lifecycle Manager Admin (
roles/saasservicemgmt.admin) - App Design Center Admin (
roles/designcenter.admin) - SaaS Config Viewer (
roles/saasconfig.viewer) - Service Account Admin (
roles/iam.serviceAccountAdmin) 和 Project IAM Admin (roles/resourcemanager.projectIamAdmin)
- App Lifecycle Manager Admin (
-
确保已配置 App Design Center 空间和 App Lifecycle Manager 服务账号,包括已获授项目的 Service Account Token Creator (
roles/iam.serviceAccountTokenCreator)、Infra Manager Admin (roles/config.admin) 和 Storage Admin (roles/storage.admin) 角色的用户管理的执行服务账号。 如需详细了解 App Lifecycle Manager 服务账号,请参阅服务账号。 -
在本地机器上安装 Python 3.10 或更高版本以及
pip,然后对 Google Cloud CLI 进行身份验证,以获取应用默认凭据 (ADC):gcloud auth application-default login
在控制台中创建 SaaS 产品
创建一个 SaaS 产品/服务资源,作为服务模型和功能标志的顶级容器:
在 Google Cloud 控制台中,依次前往 App Lifecycle Manager > SaaS 产品。
点击创建。
在 SaaS 产品名称字段中,输入
flags-quickstart-offering。在区域列表中,选择
us-central1,然后点击确定。点击创建。创建完成后,系统会打开 SaaS 产品详情页面。
在 App Design Center 中对服务进行建模
在以应用为中心的工作流中,您可以通过在 App Design Center 中编写应用模板来定义单元类型。如果您想使用功能标志,但不想通过 App Lifecycle Manager 预配云基础设施,则可以为架构中的每种服务(例如后端应用、前端应用或网关服务)创建一个包含空标准应用模板的复合模板。空标准模板会声明服务类型,而不会部署云资源。当您准备好通过 App Lifecycle Manager 管理基础设施部署时,可以将 Terraform 组件添加到标准模板中。
如需在复合模板中创建空标准模板并生成单位种类,请完成以下步骤:
- 在
flags-quickstart-offering的 SaaS 产品/服务详情页面上,点击创建新模板。 - 在模板 ID 字段中,输入
flags-composite-template,然后点击创建模板。 - 在关联的应用模板部分中,点击管理模板菜单,然后点击在画布中配置以打开 App Design Center。
- 在设计画布上的组件区域中,点击嵌入模板,然后点击创建新模板。
- 在模板 ID 字段中,输入
backend-service-template,然后点击创建模板以打开标准模板画布。 - 将
backend-service-template画布留空,不添加任何组件,然后点击添加到目录,将标准模板发布到您空间的目录中。 - 返回
flags-composite-template设计画布,点击嵌入模板,从模板中选择backend-service-template,然后点击选择。 - 点击添加到目录以发布复合模板修订版本,然后依次点击继续 > 使用 App Lifecycle Manager 大规模部署。
- 在 SaaS 产品/服务详情页面的关联的应用模板部分中,点击更新模板修订版本,选择
flags-composite-template的最新修订版本,然后点击更新(如果关联直接在 App Design Center 中创建的模板,则点击关联现有模板)。 - 打开 SaaS 产品详情页面上的单元种类标签页,并验证 App Lifecycle Manager 是否为
backend-service-template创建了单元种类。
创建和配置单元
创建并预配一个单元,以表示接收功能标志配置的服务区域级实例。由于 App Design Center 中的标准模板为空,因此预配会运行无操作基础设施执行,而不会部署云资源,并将单元转换为就绪状态,以便 saasconfig.googleapis.com 可以提供其功能标志配置:
在 Google Cloud 控制台中,依次前往App Lifecycle Manager > 单元。
点击创建。
在单元名称字段中,输入
backend-service-unit。在 SaaS 产品列表中,选择
flags-quickstart-offering。在区域列表中,选择
us-central1。在单位种类列表中,选择您在上一部分中生成的
backend-service-template-单位种类。点击创建。创建完成后,系统会打开单元详情页面。
在单元详情页面上,点击预配以打开预配单元面板。
在发布版本列表中,选择为
backend-service-template-单元种类生成的发布版本。在服务账号列表中,选择您的执行启动操作的服务账号,然后选择授予 App Lifecycle Manager 权限,以便其代表执行启动操作的服务账号行事。
点击添加租户项目变量,然后选择您的项目以填充
tenant_project_id和tenant_project_number。点击预配,然后等待单元详情页面上的单元状态达到就绪。
在控制台中创建和部署功能标志
定义一个布尔值功能标志,并运行发布,将标志发布内容传送到您的单元:
- 在 Google Cloud 控制台中,依次打开App Lifecycle Manager > 功能标志,然后点击创建功能标志。
- 在单位种类字段中,选择
backend-service-template-单位种类。 - 在标志键字段中,输入
enable-new-feature(输入标志键后,标志 ID 会自动填充)。 - 将标志值设置为
enabled,然后点击创建。 - 前往应用生命周期管理器 > 标志版本,然后点击创建。
- 在标志发布版本名称字段中输入
enable-new-feature-rel-1,在单元种类列表中选择backend-service-template-单元种类,在发布版本类型列表中选择功能标志,在选择功能标志列表中选择enable-new-feature标志,然后点击创建。App Lifecycle Manager 会自动创建底层标志修订版本。 - 前往应用生命周期管理器 > 发布种类,然后点击创建。
- 在发布种类名称字段中,输入
flags-quickstart-rollout-kind。 - 在单元种类列表中,选择您的
backend-service-template-单元种类。 - 在发布策略列表中,选择同时在所有位置发布。
- 点击创建。
- 依次前往应用生命周期管理器 > 发布,然后点击创建。
- 选择功能标志发布。
- 在发布名称字段中,输入
flags-quickstart-rollout。 - 在单元种类列表中,选择您的
backend-service-template-单元种类。 - 在功能标志版本列表中,选择
enable-new-feature-rel-1,然后点击下一步。 - 选择选择现有的发布种类,然后在发布种类列表中选择
flags-quickstart-rollout-kind。 - 点击创建以开始发布并将配置传送到
backend-service-unit。 - 等待发布状态达到成功。
评估功能标志
使用 OpenFeature SDK 和连接到 saasconfig.googleapis.com:443 的 flagd 提供程序在本地评估 enable-new-feature 标志:
安装所需的 Python 软件包:
pip install --upgrade google-auth grpcio requests "openfeature-sdk>=0.8.2" "openfeature-provider-flagd>=0.6.1"创建一个名为
evaluate_flag.py的文件,其中包含以下代码:import os import time import google.auth import google.auth.transport.grpc import google.auth.transport.requests import grpc from openfeature import api from openfeature.contrib.provider.flagd import FlagdProvider from openfeature.contrib.provider.flagd.config import ResolverType provider_id = os.environ["FLAGD_SOURCE_PROVIDER_ID"] credentials, _ = google.auth.default( scopes=["https://www.googleapis.com/auth/cloud-platform"] ) channel_creds = grpc.composite_channel_credentials( grpc.ssl_channel_credentials(), grpc.metadata_call_credentials( google.auth.transport.grpc.AuthMetadataPlugin( credentials, google.auth.transport.requests.Request() ) ), grpc.metadata_call_credentials( lambda ctx, cb: cb( [("x-goog-request-params", f"name={provider_id}")], None ) ), ) api.set_provider( FlagdProvider( resolver_type=ResolverType.IN_PROCESS, host="saasconfig.googleapis.com", port=443, sync_metadata_disabled=True, provider_id=provider_id, channel_credentials=channel_creds, ) ) time.sleep(5) value = api.get_client().get_boolean_value("enable-new-feature", False) print(f"Flag 'enable-new-feature' evaluated to: {value}")将
FLAGD_SOURCE_PROVIDER_ID环境变量设置为单元的功能标志配置资源路径,然后运行脚本:export FLAGD_SOURCE_PROVIDER_ID="projects/PROJECT_ID/locations/us-central1/featureFlagsConfigs/backend-service-unit" python evaluate_flag.py将
PROJECT_ID替换为您的 Google Cloud项目 ID。该脚本会输出
Flag 'enable-new-feature' evaluated to: True。
清理
为避免因本页中使用的资源导致您的 Google Cloud 账号产生费用,请按照以下步骤操作。
为避免持续产生费用,请在管理资源中删除测试项目,或按依赖项的逆序移除您创建的资源。如需将功能标志标记为待清理(CLEANUP 状态),以便将其与单元种类解除关联,您需要使用 gcloud CLI 或 App Lifecycle Manager API:
将标志状态设置为
cleanup,创建清理标志发布,然后运行清理发布,以解除标志与单元种类的关联:gcloud beta app-lifecycle-manager flags update FLAG_ID \ --project=PROJECT_ID \ --location=global \ --state=cleanup gcloud beta app-lifecycle-manager flags releases create enable-new-feature-cleanup-rel \ --project=PROJECT_ID \ --unit-kind=UNIT_KIND_ID \ --flags=FLAG_ID \ --location=global gcloud beta app-lifecycle-manager rollouts create enable-new-feature-cleanup-rollout \ --project=PROJECT_ID \ --flag-release=enable-new-feature-cleanup-rel \ --rollout-kind=flags-quickstart-rollout-kind \ --location=global替换以下内容:
FLAG_ID:功能标志的 ID(自动填充为UNIT_KIND_ID-enable-new-feature)。PROJECT_ID:您的 Google Cloud 项目 ID。UNIT_KIND_ID:backend-service-template-单元种类的 ID。
清理发布达到成功后,在Google Cloud 控制台中删除发布、发布种类 (
flags-quickstart-rollout-kind) 和标志发布(enable-new-feature-rel-1和enable-new-feature-cleanup-rel)。查找并删除自动生成的标志修订版本,然后删除功能标志:
gcloud beta app-lifecycle-manager flags revisions list \ --project=PROJECT_ID \ --location=global \ --filter="flag:FLAG_ID" gcloud beta app-lifecycle-manager flags revisions delete REVISION_ID \ --project=PROJECT_ID \ --location=global将
REVISION_ID替换为list命令返回的每个修订版本 ID,然后在Google Cloud 控制台中删除功能标志。在其单元详情页面上取消预配
backend-service-unit(点击取消预配,在对话框中确认,然后等待状态变为未预配),然后按依赖项的反向顺序删除其余资源:单元 (backend-service-unit)、单元种类的默认版本关联和生成的蓝图版本(在删除蓝图版本之前,请先清除单元种类的默认版本)、单元种类和 SaaS 产品 (flags-quickstart-offering)。如需了解 App Lifecycle Manager 删除规则,请参阅删除资源。在应用设计中心内删除
flags-composite-template和backend-service-template。
后续步骤
- 阅读 App Lifecycle Manager 功能标志概览。
- 如需了解基础架构代管式部署,请参阅部署功能标志快速入门。
- 在使用逐步功能发布中配置基于百分比的流量分配。
- 在排查功能标志问题中诊断标志评估或发布问题。