使用功能旗標進行多租戶架構

本指南說明如何在多用戶群環境中,導入 App Lifecycle Manager 功能旗標,而不必修改現有的二進位檔部署程序。

必要條件

開始前,請先確認下列事項:

  1. 已完成部署功能旗標快速入門導覽課程。
  2. 已設定為管理 App Lifecycle Manager 資源的 gcloud 環境。
  3. 現有的應用程式服務。如要進一步瞭解如何建立應用程式產品,請參閱「建立應用程式產品」。

資源建模

在多租戶設定中,建議您為服務或應用程式定義單位 kind,並為每個服務或應用程式執行個體 (代表租戶或部署作業) 定義單位。

如要在多租戶環境中導入功能旗標,請定義可識別租戶的一般運算語言 (CEL) 屬性、設定應用程式在執行階段插入租戶 ID,並使用評估規則影響特定租戶的功能。

如果是較簡單的情況,您也可以手動為每位客戶建立個別推出作業。

1. 建立應用程式產品 (如果尚未建立)

gcloud beta app-lifecycle-manager saas create "SAAS_NAME" \
    --project="PROJECT_ID" \
    --location="global" \
    --locations=name="LOCATION_1"

2. 建立 UnitKind

gcloud beta app-lifecycle-manager unit-kinds create "tenant-service-kind" \
    --project="PROJECT_ID" \
    --location="global" \
    --saas="SAAS_NAME"

3. 為租戶建立單位

建立單元時,請使用「標籤」將房客服務分類到各個群組 (例如 group=beta、group=preview、group=all)。

# Create a unit for Tenant A (Beta group)
gcloud beta app-lifecycle-manager units create "tenant-a-service" \
    --unit-kind="tenant-service-kind" \
    --location="LOCATION_1" \
    --labels="group=beta"

# Create a unit for Tenant B (Standard group)
gcloud beta app-lifecycle-manager units create "tenant-b-service" \
    --unit-kind="tenant-service-kind" \
    --location="LOCATION_1" \
    --labels="group=all"

定義屬性和旗標

您必須正式定義屬性 (例如 customerID),確保評估規則中的類型安全。

1. 建立 customerID 屬性

gcloud beta app-lifecycle-manager flags attributes create "customer-id-attr" \
    --key="customerID" \
    --attribute-value-type="STRING" \
    --location=global

2. 建立功能旗標

gcloud beta app-lifecycle-manager flags create "enhanced-search" \
    --key="enhanced-search" \
    --flag-value-type=BOOL \
    --unit-kind="tenant-service-kind"

Application Integration

將 OpenFeature SDK 整合至服務。應用程式必須在執行階段將 customerID 插入情境。

Go 範例:

// Inject customerID into the evaluation context
evalCtx := map[string]any{
    "customerID": currentTenant.ID,
}

// Evaluate the flag
isEnabled, err := client.BooleanValue(
    context.Background(),
    "enhanced-search",
    false,
    evalCtx,
)

設定用戶群專屬指定目標

使用評估規則,為特定客戶啟用功能。

gcloud beta app-lifecycle-manager flags update "enhanced-search" \
    --location="global"
    --evaluation-spec='{
      "rules": [{
        "id": "allowlist-for-premium-tenants",
        "condition": "customerID in [\"tenant-xyz-123\", \"tenant-abc-789\"]",
        "target": "Enabled"
      }],
      "defaultTarget": "Disabled",
      "attributes": ["projects/PROJECT_ID/locations/global/flagAttributes/customer-id-attr"]
    }'

使用篩選器手動漸進式推出

如果是較簡單的用途,您可以手動為每個客戶標籤觸發個別推出作業,藉此模擬波次。

1. 建立修訂版本並發布

# Create a Revision (Snapshot)
gcloud beta app-lifecycle-manager flags revisions create "enhanced-search-rev-1" \
    --location=global \
    --flag="enhanced-search"

# Create a Release
gcloud beta app-lifecycle-manager flags releases create "release-of-enhanced-search" \
    --location=global \
    --unit-kind="tenant-service-kind" \
    --revisions="enhanced-search-rev-1"

2. 觸發手動推出作業

您可以使用 --unit-filter,為每個目標群組觸發個別作業,手動協調推出作業。

# Rollout to Beta group first
gcloud beta app-lifecycle-manager rollouts create "beta-rollout" \
    --flag-release="release-of-enhanced-search" \
    --rollout-kind="rollout-kind-of-enhanced-search" \
    --location=global \
    --rollout-orchestration-strategy="Google.Cloud.Simple.AllAtOnce" \
    --unit-filter="labels.group == beta"

# After verification, rollout to the rest of the fleet
gcloud beta app-lifecycle-manager rollouts create "full-rollout" \
    --flag-release="release-of-enhanced-search" \
    --rollout-kind="rollout-kind-of-enhanced-search" \
    --location=global \
    --rollout-orchestration-strategy="Google.Cloud.Production" \
    --unit-filter="labels.group == all"

後續步驟