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

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

必要條件

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

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

資源建模

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

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

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

1. 建立 SaaS 產品 (如果尚未建立)

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=betagroup=previewgroup=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"

後續步驟