使用事件网格事件自动化 Azure Kubernetes Fleet Manager 的审批门(预览版)

适用于: ✔️车队经理 ✔️中心群集的车队经理

机队管理器更新运行 审批入口 可以更好地控制何时处理更新组或阶段。

审批入口通过事件网格系统主题发布事件。 主题上的事件订阅可用作自动化的触发器,将待审批标记为已完成。

自动化可以包括与其他 Azure 服务的集成,或与外部系统集成,以执行诸如创建服务台工单或使用运行状况检查 API 等操作,以确保群集工作负载健康,然后才允许更新运行继续。

本文介绍如何配置事件网格和事件订阅以接收已发布事件。

重要

Azure Kubernetes 舰队管理器预览功能可以通过自助服务方式选择性启用。 预览版按“现状”和“视供应情况”提供,它们不包括在服务级别协议和有限保证范围内。 客户支持部门会尽力为 Azure Kubernetes 舰队管理器预览功能提供部分支持。 因此,这些功能并不适合用于生产。

了解审批网关和事件网格事件

有三种可用于事件订阅的车队管理器网关事件类型。

事件网格事件类型 Description
Microsoft.ResourceNotifications.AKSResources.FleetGateCreated 通过资源通知在 AKS 中创建的 Fleet Gate 当更新运行到达实例的 Gate 资源时触发。
Microsoft.ResourceNotifications.AKSResources.FleetGateUpdated 当门的状态发生更改时引发(挂起到已完成)。
Microsoft.ResourceNotifications.AKSResources.FleetGateDeleted 删除包含 Gate 实例的更新运行时引发。

使用以下事件属性定义高级事件网格事件订阅筛选器,以仅接收感兴趣的事件。

事件属性名称 Example Description
data.resourceInfo.id Azure 资源 ID 生成事件的审批门实例的完整 Azure 资源标识符。
data.resourceInfo.name 1eb18051-fff5-4c09-a15f-9eebd2e3b906 表示审批门实例的唯一值。
data.resourceInfo.properties.displayName “与销售团队联系” 在更新策略中定义审批门时所提供的一个可选名称。
data.resourceInfo.properties.网关类型 “审批” 门的类型。 目前只有一个有效的“批准”选项。
data.resourceInfo.properties.state “待处理”/“完成” 门的状态。 “挂起”= 等待审批;“已完成”= 已批准。
data.resourceInfo.properties.target.id Azure 资源 ID 生成事件的更新运行的完整 Azure 资源标识符。
data.resourceInfo.properties.target.updateRunProperties.name “update-k8s-1.33” 生成事件的更新运行的名称。
数据.资源信息.属性.目标.更新运行属性.阶段 “dev” 更新阶段的名称。 可以出现在阶段门和阶段内的组门。
数据.资源信息.属性.目标.更新运行属性.组 蓝色 应用入口的更新组的名称。 仅适用于组。
data.resourceInfo.properties.target.updateRunProperties.时间设定 之前/之后 表示逻辑门是应用在阶段或组之前还是之后。

下面提供了示例原始事件网格事件。 大多数事件处理程序只接收单个事件,使 data 有效负载可用于处理。

[
    {
        "id": "1b6e818e-5ec3-4c22-9e9c-03c1fd05ac21",
        "topic": "/subscriptions/xxxxxxxxxx",
        "subject": "/subscriptions/xxxxxxxxxx/resourceGroups/sw-ms-demo-01/providers/Microsoft.ContainerService/fleets/flt-mgr-approvals-01/gates/1eb18051-fff5-4c09-a15f-9eebd2e3b906",
        "data": {
            "resourceInfo": {
                "id": "/subscriptions/xxxxxxxxxx/resourceGroups/sw-ms-demo-01/providers/Microsoft.ContainerService/fleets/flt-mgr-approvals-01/gates/1eb18051-fff5-4c09-a15f-9eebd2e3b906",
                "name": "1eb18051-fff5-4c09-a15f-9eebd2e3b906",
                "type": "Microsoft.ContainerService/fleets/gates",
                "properties": {
                    "displayName": "Check with sales team",
                    "gateType": "Approval",
                    "provisioningState": "Succeeded",
                    "state": "Pending",
                    "target": {
                        "id": "/subscriptions/xxxxxxxxxx/resourceGroups/sw-ms-demo-01/providers/Microsoft.ContainerService/fleets/flt-mgr-approvals-01/updateRuns/test-01",
                        "updateRunProperties": {
                            "name": "test-01",
                            "stage": "chinanorth-development",
                            "timing": "Before"
                        }
                    }
                }
            },
            "operationalInfo": {
                "resourceEventTime": "2025-09-23T02:50:21.2604165+00:00"
            },
            "apiVersion": "2025-04-01-preview"
        },
        "eventType": "Microsoft.ResourceNotifications.AksResources.FleetGateCreated",
        "dataVersion": "1",
        "metadataVersion": "1",
        "eventTime": "2025-09-23T02:50:21.2604165Z"
    }
]

在您开始之前

  • 请参阅舰队更新的概念概述,其中提供了本指南中引用的更新运行、阶段、组和策略的说明。

  • 必须具有具有一个或多个成员群集的机群管理器。 如果没有,请按照快速入门创建舰队资源,并将 Azure Kubernetes 服务 (AKS) 群集加入其中。

  • 您必须拥有包含至少一个审批门的更新策略。

  • 确保任何自动化都使用具备 Azure Kubernetes 集群管理器参与者角色(Azure RBAC) 的身份,或具备 Microsoft.ContainerService/fleets/gates/write 操作权限的自定义 RBAC 角色。

  • 需要安装 Azure CLI 2.70.0 或更高版本。 若要安装或升级,请参阅安装 Azure CLI

  • 还需要 fleet Azure CLI 扩展 1.6.2 或更高版本,可以通过运行以下命令进行安装:

    az extension add --name fleet
    

    运行 az extension update 命令,更新到最新发布的扩展版本:

    az extension update --name fleet
    
  • 设置以下环境变量:

    export GROUP=<resource-group>
    export FLEET=<fleet-name>
    export SUBSCRIPTION_ID=<fleet-manager-subscriptionid>
    

创建事件网格系统主题

审批入口通过可在 Azure 订阅级别创建的事件网格系统主题发布事件。 只能创建一个类型为Microsoft.ResourceNotifications.AKSResources的事件网格系统主题。

使用 az eventgrid system-topic create 命令创建新的系统主题,如下所示。

az eventgrid system-topic create \
  --name stpc-aks-resource-notifications \
  --resource-group $GROUP \
  --source /subscriptions/$SUBSCRIPTION_ID \
  --topic-type Microsoft.ResourceNotifications.AKSResources \
  --location Global

创建事件订阅

在此示例中,我们将为来自特定车队管理器flt-mgr-approvals-01的待审批关卡事件创建一个订阅。 必须将Approval命名为Check with sales teams,并在任何更新运行中应用于Before阶段Dev Gate。 最后,事件将路由到处理事件的现有 Azure 函数。

使用 az eventgrid system-topic event-subscription create 命令创建新订阅,如下所示。

az eventgrid system-topic event-subscription create \
  --name stes-fleet-gates-sales-before-dev \
  --resource-group $GROUP \
  --system-topic-name stpc-aks-resource-notifications \
  --included-event-types Microsoft.ResourceNotifications.AKSResources.FleetGateCreated \
  --advanced-filter data.resourceInfo.properties.target.id StringContains "fleets/flt-mgr-approvals-01" \
  --advanced-filterdata.resourceInfo.properties.gateType StringIn Approval \
  --advanced-filter data.resourceInfo.properties.state StringIn Pending \
  --advanced-filter data.resourceInfo.properties.displayName StringContains "Check with sales teams" \
  --advanced-filter data.resourceInfo.properties.target.updateRunProperties.timing StringIn Before \
  --advanced-filter data.resourceInfo.properties.target.updateRunProperties.stage StringIn Dev \
  --endpoint-type azurefunction \
  --endpoint /subscriptions/$SUBSCRIPTION_ID/resourceGroups/$GROUP/providers/Microsoft.Web/sites/fap-process-fleet-events/functions/fa-handle-pre-dev-events \
  --max-delivery-attempts 10 \
  --event-ttl 120

处理事件网格订阅事件

Azure 事件网格事件订阅通过 Webhook 支持许多不同的 事件处理程序终结点类型 ,包括 Azure Functions、服务总线队列、Azure 逻辑应用或其他第三方系统。

在此示例中,我们使用 Python Azure 函数来处理门引发的事件。 Azure 函数还使用 Fleet Manager Python 库 将审批标记为已完成。

执行 Azure 函数的标识必须具有 Azure Kubernetes Fleet Manager Contributor Azure RBAC 角色。

import logging
import azure.functions as func

from azure.identity import DefaultAzureCredential
from azure.mgmt.containerservicefleet import ContainerServiceFleetMgmtClient
from azure.core.exceptions import AzureError

app = func.FunctionApp()

@app.event_grid_trigger(arg_name="azeventgrid")
def HandleFleetGateEvent(azeventgrid: func.EventGridEvent):
    logging.info('Processing gate created event.')
    
    try:
        event_data = azeventgrid.get_json()

        target_resource_id = event_data["resourceInfo"]["properties"]["target"]["id"]
        parts = target_resource_id.split('/')

        # Extract the values required to update gate resource later

        subscription_id = parts[2]  # subscriptions/{subscription_id}
        resource_group = parts[4]   # resourceGroups/{resource_group}
        fleet_name = parts[8]       # fleets/{fleet_name}

        # The UUID of the instance of the gate. This is required to
        # integrate with just this instance.
        gate_name_uuid = event_data["resourceInfo"]["name"]

        #
        # Perform activities here to check if OK to proceed
        #

        # Update the Gate instance to mark it as 'Completed'
      
        # Create new Fleet Service Management client 
        # Requires v4.0.0b1 of azure-mgmt-containerservicefleet
        client = ContainerServiceFleetMgmtClient(
            credential=DefaultAzureCredential(),
            subscription_id=subscription_id
        )

        # End request to update gate status
        response = client.gates.begin_update(
            resource_group_name=resource_group,
            fleet_name=fleet_name,
            gate_name=gate_name_uuid,
            properties={"properties": {"state": "Completed"}}
        ).result()
        
        logging.info(f"Successfully updated gate {gate_name_uuid} to Completed state")
        
    except KeyError as e:
        logging.error(f"Missing required field in event data: {e}")
        
    except IndexError as e:
        logging.error(f"Error parsing target_id - unexpected format: {e}")
        
    except AzureError as e:
        logging.error(f"Azure API error: {e}")
        
    except Exception as e:
        logging.error(f"Unexpected error: {e}")

必须包含requirements.txt显示的模块。

azure-common
azure-core
azure-functions
azure-identity
azure-mgmt-containerservicefleet==4.0.0b1
azure-mgmt-core

在本文中,我们探讨了如何将事件网格配置为将审批门事件传送到 Azure 函数进行处理,包括如何使用 Fleet Manager Python 库将审批门标记为已完成,从而允许更新运行继续。

后续步骤