ArgoCD
通过 ArgoCD 集成,您可以根据映射和定义将 ArgoCD 实例中的 "集群"、"项目"、"应用程序 "和 "部署历史 "资源导入 Port。
常见被用于情况
- 在 ArgoCD 中映射受监控的 Kubernetes 资源。
- 实时关注对象变更(创建/更新),并自动将变更应用到 Port 中的实体。
安装
从以下安装方法中选择一种:
- Real Time & Always On
- Scheduled
使用该安装选项意味着集成将能使用 webhook 实时更新 Port。
本表总结了安装时可用的参数,请在下面的脚本中按自己的需要进行设置,然后复制并在终端运行:
Parameter | Description | Required |
---|---|---|
port.clientId | Your port client id | ✅ |
port.clientSecret | Your port client secret | ✅ |
port.baseUrl | Your port base url, relevant only if not using the default port app | ❌ |
integration.identifier | Change the identifier to describe your integration | ✅ |
integration.type | The integration type | ✅ |
integration.eventListener.type | The event listener type | ✅ |
integration.secrets.token | The ArgoCD API token token | ✅ |
integration.config.serverUrl | The ArgoCD server url | ✅ |
scheduledResyncInterval | The number of minutes between each resync | ❌ |
initializePortResources | Default true, When set to true the integration will create default blueprints and the port App config Mapping | ❌ |
helm repo add --force-update port-labs https://port-labs.github.io/helm-charts
helm upgrade --install my-argocd-integration port-labs/port-ocean \
--set port.clientId="CLIENT_ID" \
--set port.clientSecret="CLIENT_SECRET" \
--set initializePortResources=true \
--set scheduledResyncInterval=60 \
--set integration.identifier="my-argocd-integration" \
--set integration.type="argocd" \
--set integration.eventListener.type="POLLING" \
--set integration.secrets.token="<your-token>" \
--set integration.config.serverUrl="<your-server-url>"
- GitHub
- Jenkins
This workflow will run the ArgoCD integration once and then exit, this is useful for scheduled ingestion of data.
确保配置以下Github Secrets :
Parameter | Description | Required |
---|---|---|
OCEAN__INTEGRATION__CONFIG__TOKEN | The ArgoCD API token | ✅ |
OCEAN__INTEGRATION__CONFIG__SERVER_URL | The ArgoCD server URL | ✅ |
OCEAN__INITIALIZE_PORT_RESOURCES | Default true, When set to false the integration will not create default blueprints and the port App config Mapping | ❌ |
OCEAN__INTEGRATION__IDENTIFIER | Change the identifier to describe your integration, if not set will use the default one | ❌ |
OCEAN__PORT__CLIENT_ID | Your port client id | ✅ |
OCEAN__PORT__CLIENT_SECRET | Your port client secret | ✅ |
OCEAN__PORT__BASE_URL | Your port base url, relevant only if not using the default port app | ❌ |
下面是 argocd-integration.yml
工作流程文件的示例:
name: ArgoCD Exporter Workflow
# This workflow responsible for running ArgoCD exporter.
on:
workflow_dispatch:
jobs:
run-integration:
runs-on: ubuntu-latest
steps:
- name: Run ArgoCD Integration
run: |
# Set Docker image and run the container
integration_type="argocd"
version="latest"
image_name="ghcr.io/port-labs/port-ocean-$integration_type:$version"
docker run -i --rm --platform=linux/amd64 \
-e OCEAN__EVENT_LISTENER='{"type":"ONCE"}' \
-e OCEAN__INITIALIZE_PORT_RESOURCES=true \
-e OCEAN__INTEGRATION__CONFIG__TOKEN=${{ secrets.OCEAN__INTEGRATION__CONFIG__TOKEN }} \
-e OCEAN__INTEGRATION__CONFIG__SERVER_URL=${{ secrets.OCEAN__INTEGRATION__CONFIG__SERVER_URL }} \
-e OCEAN__PORT__CLIENT_ID=${{ secrets.OCEAN__PORT__CLIENT_ID }} \
-e OCEAN__PORT__CLIENT_SECRET=${{ secrets.OCEAN__PORT__CLIENT_SECRET }} \
$image_name
exit $?
This pipeline will run the ArgoCD integration once and then exit, this is useful for scheduled ingestion of data.
请确保配置以下Jenkins Credentials的 "Secret Text "类型:
Parameter | Description | Required |
---|---|---|
OCEAN__INTEGRATION__CONFIG__TOKEN | The ArgoCD API token | ✅ |
OCEAN__INTEGRATION__CONFIG__SERVER_URL | The ArgoCD server URL | ✅ |
OCEAN__INITIALIZE_PORT_RESOURCES | Default true, When set to false the integration will not create default blueprints and the port App config Mapping | ❌ |
OCEAN__INTEGRATION__IDENTIFIER | Change the identifier to describe your integration, if not set will use the default one | ❌ |
OCEAN__PORT__CLIENT_ID | Your port client id | ✅ |
OCEAN__PORT__CLIENT_SECRET | Your port client secret | ✅ |
OCEAN__PORT__BASE_URL | Your port base url, relevant only if not using the default port app | ❌ |
下面是 Jenkinsfile
groovy Pipelines 文件的示例:
pipeline {
agent any
stages {
stage('Run ArgoCD Integration') {
steps {
script {
withCredentials([
string(credentialsId: 'OCEAN__INTEGRATION__CONFIG__TOKEN', variable: 'OCEAN__INTEGRATION__CONFIG__TOKEN'),
string(credentialsId: 'OCEAN__INTEGRATION__CONFIG__SERVER_URL', variable: 'OCEAN__INTEGRATION__CONFIG__SERVER_URL'),
string(credentialsId: 'OCEAN__PORT__CLIENT_ID', variable: 'OCEAN__PORT__CLIENT_ID'),
string(credentialsId: 'OCEAN__PORT__CLIENT_SECRET', variable: 'OCEAN__PORT__CLIENT_SECRET'),
]) {
sh('''
#Set Docker image and run the container
integration_type="argocd"
version="latest"
image_name="ghcr.io/port-labs/port-ocean-${integration_type}:${version}"
docker run -i --rm --platform=linux/amd64 \
-e OCEAN__EVENT_LISTENER='{"type":"ONCE"}' \
-e OCEAN__INITIALIZE_PORT_RESOURCES=true \
-e OCEAN__INTEGRATION__CONFIG__TOKEN=$OCEAN__INTEGRATION__CONFIG__TOKEN \
-e OCEAN__INTEGRATION__CONFIG__SERVER_URL=$OCEAN__INTEGRATION__CONFIG__SERVER_URL \
-e OCEAN__PORT__CLIENT_ID=$OCEAN__PORT__CLIENT_ID \
-e OCEAN__PORT__CLIENT_SECRET=$OCEAN__PORT__CLIENT_SECRET \
$image_name
exit $?
''')
}
}
}
}
}
}
生成 ArgoCD 令牌
- 导航至
<serverURL>/settings/accounts/<user>
。例如,如果您在https://localhost:8080
访问 ArgoCD,则应导航到https://localhost:8080/settings/accounts/<user>
。 - 在 "令牌 "下,单击 "生成新"创建新令牌。
接收 ArgoCD 对象
ArgoCD 集成使用 YAML 配置来描述将数据加载到开发者门户的过程。
下面是配置中的一个示例片段,演示了从 ArgoCD 获取 "应用程序 "数据的过程:
createMissingRelatedEntities: true
deleteDependentEntities: true
resources:
- kind: application
selector:
query: "true"
port:
entity:
mappings:
identifier: .metadata.uid
title: .metadata.name
blueprint: '"argocdApplication"'
properties:
gitRepo: .spec.source.repoURL
gitPath: .spec.source.path
destinationServer: .spec.destination.server
namespace: .metadata.namespace
syncStatus: .status.sync.status
healthStatus: .status.health.status
createdAt: .metadata.creationTimestamp
relations:
project: .spec.project
该集成利用JQ JSON processor 从 ArgoCD 的 API 事件中对现有字段和 Values 进行选择、修改、连接、转换和其他操作。
配置结构
集成配置决定了从 ArgoCD 中查询哪些资源,以及在 Port 中创建哪些实体和属性。
-
集成配置的根密钥是 "资源 "密钥:
resources:
- kind: application
selector:
... -
kind "键是 ArgoCD 对象的说明符:
resources:
- kind: application
selector:
... -
通过 "选择器 "和 "查询 "键,您可以过滤哪些指定 "类型 "的对象将被录入软件目录:
resources:
- kind: application
selector:
query: "true" # JQ boolean expression. If evaluated to false - this object will be skipped.
port: -
Port"、"实体 "和 "映射 "键被用来将 ArgoCD 对象字段映射到Port实体。要创建多个同类映射,可在
resources
数组中添加另一项;resources:
- kind: application
selector:
query: "true"
port:
entity:
mappings: # Mappings between one ArgoCD object to a Port entity. Each value is a JQ query.
identifier: .metadata.uid
title: .metadata.name
blueprint: '"argocdApplication"'
properties:
gitRepo: .spec.source.repoURL
gitPath: .spec.source.path
destinationServer: .spec.destination.server
namespace: .metadata.namespace
syncStatus: .status.sync.status
healthStatus: .status.health.status
createdAt: .metadata.creationTimestamp
relations:
project: .spec.project
- kind: application # In this instance application is mapped again with a different filter
selector:
query: '.metadata.name == "MyApplicationName"'
port:
entity:
mappings: ...
blueprint
键的值 - 如果要使用硬编码字符串,需要用 2 组引号封装,例如使用一对单引号 ('
),然后再用一对双引号 ("
): :::配置实时更新
目前,ArgoCD REST API 不支持以编程方式创建 webhook。 要在 ArgoCD 中设置 webhook 配置,以便向 Ocean 集成发送通知,请按以下步骤操作:
前提条件
- 您可以访问部署 ArgoCD 的 Kubernetes 集群。
- 安装并配置了
kubectl
以访问集群。
步骤
- 配置 ArgoCD 通知清单;
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj-labs/argocd-notifications/release-1.0/manifests/install.yaml
2.安装 ArgoCD 触发器和模板配置清单;
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj-labs/argocd-notifications/release-1.0/catalog/install.yaml
3.使用 kubectl
连接到部署 ArgoCD 实例的 Kubernetes 集群;
kubectl config use-context <your-cluster-context>
4.将当前名称空间设置为 ArgoCD 名称空间,请使用以下命令;
kubectl config set-context --current --namespace=<your-namespace>
5.创建一个 YAML 文件(例如 argocd-webhook-config.yml
),配置 webhook 通知服务。下面的示例展示了如何设置 webhook,以便在 ArgoCD 应用程序更新时发送实时事件。YAML 文件包括以下组件:
1.通知服务定义;
2.webhook 消息正文模板;
3.触发器定义;
4.订阅通知。
下面是一个 YAML 示例。请确保将 <WEBHOOK_URL>
替换为部署海洋集成的 ingress 或服务的实际 URL。默认情况下,传入的 Webhook 事件会发送到 Ocean 中的 /integration/webhook
路径,因此请勿替换路径参数。
webhook manifest file
apiVersion: v1
kind: ConfigMap
metadata:
name: argocd-notifications-cm
data:
trigger.on-sync-operation-change: |
- description: Application syncing has updated
send:
- app-status-change
when: app.status.operationState.phase in ['Error', 'Failed', 'Succeeded', 'Running']
trigger.on-deployed: |
- description: Application is synced and healthy
send:
- app-status-change
when: app.status.operationState.phase in ['Succeeded'] and app.status.health.status == 'Healthy'
trigger.on-health-degraded: |
- description: Application has degraded
send:
- app-status-change
when: app.status.health.status == 'Degraded'
service.webhook.port-ocean: |
url: <WEBHOOK_URL>
headers:
- name: Content-Type
value: application/json
template.app-status-change: |
webhook:
port-ocean:
method: POST
path: /integration/webhook
body: |
{
"action": "upsert",
"application_name": "{{.app.metadata.name}}"
}
subscriptions: |
- recipients:
- port-ocean
triggers:
- on-deployed
- on-health-degraded
- on-sync-operation-change
6.使用 kubectl
将 YAML 文件引用到集群。运行以下命令,将 <your-namespace>
替换为 ArgoCD 名称空间,将 <path-to-yaml-file>
替换为 YAML 文件的实际路径:
kubectl apply -n <your-namespace> -f <path-to-yaml-file>
该命令将 webhook 通知配置部署到 ArgoCD 通知 configmaps("argocd-notifications-cm")中,使 Ocean 能够接收实时事件。
将数据输入Port
要使用integration configuration 引用 ArgoCD 对象,可以按照以下步骤操作:
- 转到 DevPortal Builder 页面。
- 选择要被 ArgoCD 引用的蓝图。
- 从菜单中选择采集数据选项。
- 在 Kubernetes Stack Provider 类别下选择 ArgoCD。
- 根据需要修改configuration 。
- 单击 "Resync"。
示例
蓝图和相关集成配置示例:
集群
Cluster blueprint
{
"identifier": "argocdCluster",
"description": "This blueprint represents an ArgoCD cluster",
"title": "ArgoCD Cluster",
"icon": "Argo",
"schema": {
"properties": {
"namespaces": {
"items": {
"type": "string"
},
"icon": "DefaultProperty",
"title": "Namespace",
"type": "array",
"description": "Holds list of namespaces which are accessible in that cluster."
},
"applicationsCount": {
"title": "Applications Count",
"type": "number",
"description": "The number of applications managed by Argo CD on the cluster",
"icon": "DefaultProperty"
},
"serverVersion": {
"title": "Server Version",
"type": "string",
"description": "Contains information about the Kubernetes version of the cluster",
"icon": "DefaultProperty"
},
"labels": {
"title": "Labels",
"type": "object",
"description": "Contains information about cluster metadata",
"icon": "DefaultProperty"
},
"updatedAt": {
"icon": "DefaultProperty",
"title": "Updated At",
"type": "string",
"format": "date-time"
},
"server": {
"title": "Server",
"description": "The API server URL of the Kubernetes cluster",
"type": "string",
"format": "url",
"icon": "DefaultProperty"
}
},
"required": []
},
"mirrorProperties": {},
"calculationProperties": {},
"relations": {}
}
Integration configuration
createMissingRelatedEntities: true
deleteDependentEntities: true
resources:
- kind: cluster
selector:
query: "true"
port:
entity:
mappings:
identifier: .name
title: .name
blueprint: '"argocdCluster"'
properties:
namespaces: .namespaces
applicationsCount: .info.applicationsCount
serverVersion: .serverVersion
labels: .labels
updatedAt: .connectionState.attemptedAt
server: .server
项目
Project blueprint
{
"identifier": "argocdProject",
"description": "This blueprint represents an ArgoCD Project",
"title": "ArgoCD Project",
"icon": "Argo",
"schema": {
"properties": {
"namespace": {
"title": "Namespace",
"type": "string",
"icon": "DefaultProperty"
},
"createdAt": {
"title": "Created At",
"type": "string",
"format": "date-time",
"icon": "DefaultProperty"
},
"description": {
"title": "Description",
"description": "Project description",
"type": "string",
"icon": "DefaultProperty"
}
},
"required": []
},
"mirrorProperties": {},
"calculationProperties": {},
"relations": {
"cluster": {
"title": "Cluster",
"target": "argocdCluster",
"required": false,
"many": true
}
}
}
Integration configuration
createMissingRelatedEntities: true
deleteDependentEntities: true
resources:
- kind: project
selector:
query: "true"
port:
entity:
mappings:
identifier: .metadata.name
title: .metadata.name
blueprint: '"argocdProject"'
properties:
namespace: .metadata.namespace
createdAt: .metadata.creationTimestamp
description: .spec.description
relations:
cluster: '[.spec.destinations[].name | select(test("^[a-zA-Z0-9@_.:/=-]+$"))]'
申请
Application blueprint
{
"identifier": "argocdApplication",
"description": "This blueprint represents an ArgoCD Application",
"title": "ArgoCD Application",
"icon": "Argo",
"schema": {
"properties": {
"gitRepo": {
"type": "string",
"format": "url",
"icon": "Git",
"title": "Repository URL",
"description": "The URL of the Git repository containing the application source code"
},
"gitPath": {
"type": "string",
"title": "Path",
"description": "The path within the Git repository where the application manifests are located"
},
"destinationServer": {
"type": "string",
"title": "Destination Server",
"format": "url"
},
"namespace": {
"type": "string",
"title": "Namespace"
},
"syncStatus": {
"type": "string",
"title": "Sync Status",
"enum": ["Synced", "OutOfSync", "Unknown"],
"enumColors": {
"Synced": "green",
"OutOfSync": "red",
"Unknown": "lightGray"
},
"description": "The sync status of the application"
},
"healthStatus": {
"type": "string",
"title": "Health Status",
"enum": [
"Healthy",
"Missing",
"Suspended",
"Degraded",
"Progressing",
"Unknown"
],
"enumColors": {
"Healthy": "green",
"Missing": "yellow",
"Suspended": "purple",
"Degraded": "red",
"Progressing": "blue",
"Unknown": "lightGray"
},
"description": "The health status of the application"
},
"createdAt": {
"title": "Created At",
"type": "string",
"format": "date-time"
}
},
"required": []
},
"mirrorProperties": {},
"calculationProperties": {},
"relations": {
"project": {
"title": "Project",
"target": "argocdProject",
"required": false,
"many": false
}
}
}
Integration configuration
createMissingRelatedEntities: true
deleteDependentEntities: true
resources:
- kind: application
selector:
query: "true"
port:
entity:
mappings:
identifier: .metadata.uid
title: .metadata.name
blueprint: '"argocdApplication"'
properties:
gitRepo: .spec.source.repoURL
gitPath: .spec.source.path
destinationServer: .spec.destination.server
namespace: .metadata.namespace
syncStatus: .status.sync.status
healthStatus: .status.health.status
createdAt: .metadata.creationTimestamp
relations:
project: .spec.project
部署历史
Deployment history blueprint
{
"identifier": "argocdDeploymentHistory",
"description": "This blueprint represents an ArgoCD deployment history",
"title": "ArgoCD Deployment History",
"icon": "Argo",
"schema": {
"properties": {
"deployedAt": {
"title": "Deployed At",
"type": "string",
"format": "date-time",
"icon": "DefaultProperty"
},
"deployStartedAt": {
"title": "Deploy Started At",
"type": "string",
"format": "date-time",
"icon": "DefaultProperty"
},
"revision": {
"title": "Revision",
"type": "string",
"icon": "DefaultProperty"
},
"initiatedBy": {
"title": "Initiated By",
"type": "string",
"icon": "DefaultProperty"
},
"repoURL": {
"icon": "DefaultProperty",
"title": "Repository URL",
"type": "string",
"format": "url"
},
"sourcePath": {
"title": "Source Path",
"type": "string",
"icon": "DefaultProperty"
}
},
"required": []
},
"mirrorProperties": {},
"calculationProperties": {},
"aggregationProperties": {},
"relations": {
"application": {
"title": "Application",
"target": "argocdApplication",
"required": false,
"many": false
}
}
}
Integration configuration
createMissingRelatedEntities: true
deleteDependentEntities: true
resources:
- kind: deployment-history
selector:
query: "true"
port:
entity:
mappings:
identifier: .__applicationId + "-" + (.id | tostring)
title: .id | tostring
blueprint: '"argocdDeploymentHistory"'
properties:
deployedAt: .deployedAt
deployStartedAt: .deployStartedAt
revision: .revision
initiatedBy: .initiatedBy.username
repoURL: .source.repoURL
sourcePath: .source.path
relations:
application: .__applicationId
通过 webhook 进行替代安装
虽然上述 Ocean 集成是推荐的安装方法,但您可能更喜欢使用 webhook 从 ArgoCD 引用数据。 如果是这样,请使用以下说明:
Webhook installation (click to expand)
在本例中,您将在ArgoCD 和 Port 之间创建一个 webhook 集成,该集成将摄取应用程序实体并将其映射到 ArgoCD 项目。
Port configuration
创建以下蓝图定义:
Project blueprint
{
"identifier": "argocdProject",
"description": "This blueprint represents an ArgoCD Project",
"title": "ArgoCD Project",
"icon": "Argo",
"schema": {
"properties": {
"description": {
"title": "Description",
"description": "Project description",
"type": "string",
"icon": "DefaultProperty"
}
},
"required": []
},
"mirrorProperties": {},
"calculationProperties": {},
"relations": {}
}
Application blueprint
{
"identifier": "argocdApplication",
"description": "This blueprint represents an ArgoCD Application",
"title": "ArgoCD Application",
"icon": "Argo",
"schema": {
"properties": {
"gitRepo": {
"type": "string",
"format": "url",
"icon": "Git",
"title": "Repository URL",
"description": "The URL of the Git repository containing the application source code"
},
"gitPath": {
"type": "string",
"title": "Path",
"description": "The path within the Git repository where the application manifests are located"
},
"destinationServer": {
"type": "string",
"title": "Destination Server",
"format": "url"
},
"namespace": {
"type": "string",
"title": "Namespace"
},
"syncStatus": {
"type": "string",
"title": "Sync Status",
"enum": ["Synced", "OutOfSync", "Unknown"],
"enumColors": {
"Synced": "green",
"OutOfSync": "red",
"Unknown": "lightGray"
},
"description": "The sync status of the application"
},
"healthStatus": {
"type": "string",
"title": "Health Status",
"enum": [
"Healthy",
"Missing",
"Suspended",
"Degraded",
"Progressing",
"Unknown"
],
"enumColors": {
"Healthy": "green",
"Missing": "yellow",
"Suspended": "purple",
"Degraded": "red",
"Progressing": "blue",
"Unknown": "lightGray"
},
"description": "The health status of the application"
},
"createdAt": {
"title": "Created At",
"type": "string",
"format": "date-time"
}
},
"required": []
},
"mirrorProperties": {},
"calculationProperties": {},
"relations": {
"project": {
"title": "Project",
"target": "argocdProject",
"required": false,
"many": false
}
}
}
创建以下 webhook 配置using Port UI
Application webhook configuration
- 基本信息 选项卡 - 填写以下详细信息:
1.title:
ArgoCD Application Mapper
; 2.标识符 :argocd_application_mapper
; 3.Description :将 ArgoCD 应用程序映射到 Port
的 webhook 配置; 4.图标 :Argo
; - 集成配置选项卡 - 填写以下 JQ 映射:
[
{
"blueprint": "argocdApplication",
"filter": "true",
"entity": {
"identifier": ".body.uid | tostring",
"title": ".body.name | tostring",
"properties": {
"gitRepo": ".body.git_repo",
"gitPath": ".body.git_path",
"destinationServer": ".body.destination_server",
"namespace": ".body.namespace",
"syncStatus": ".body.sync_status",
"healthStatus": ".body.health_status",
"createdAt": ".body.created_at"
},
"relations": {
"project": ".body.project"
}
}
}
]
3.点击页面底部的保存。
Create a webhook in ArgoCD
要在 ArgoCD 中设置向 Port 发送通知的 webhook 配置,请按以下步骤操作:
Prerequisite
- 您可以访问部署 ArgoCD 的 Kubernetes 集群。
- 安装并配置了
kubectl
以访问集群。
Steps
- 配置 ArgoCD 通知清单;
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj-labs/argocd-notifications/release-1.0/manifests/install.yaml
2.安装 ArgoCD 触发器和模板配置清单;
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj-labs/argocd-notifications/release-1.0/catalog/install.yaml
3.使用 kubectl
连接到部署 ArgoCD 实例的 Kubernetes 集群;
kubectl config use-context <your-cluster-context>
4.将当前名称空间设置为 ArgoCD 名称空间,请使用以下命令;
kubectl config set-context --current --namespace=<your-namespace>
5.创建一个 YAML 文件(例如 argocd-webhook-config.yml
),配置 webhook 通知服务。下面的示例展示了如何设置 webhook,以便在 ArgoCD 应用程序更新时发送实时事件。YAML 文件包括以下组件:
1.通知服务定义;
2.webhook 消息正文模板;
3.触发器定义;
4.订阅通知。
下面是一个 YAML 示例。请确保将 <YOUR_WEBHOOK_URL>
替换为创建 webhook 配置后收到的 url
键值。
webhook manifest file
apiVersion: v1
kind: ConfigMap
metadata:
name: argocd-notifications-cm
data:
trigger.on-sync-operation-change: |
- description: Application syncing has updated
send:
- app-status-change
when: app.status.operationState.phase in ['Error', 'Failed', 'Succeeded', 'Running']
trigger.on-deployed: |
- description: Application is synced and healthy
send:
- app-status-change
when: app.status.operationState.phase in ['Succeeded'] and app.status.health.status == 'Healthy'
trigger.on-health-degraded: |
- description: Application has degraded
send:
- app-status-change
when: app.status.health.status == 'Degraded'
service.webhook.port-webhook: |
url: <YOUR_WEBHOOK_URL>
headers:
- name: Content-Type
value: application/json
template.app-status-change: |
webhook:
port-webhook:
method: POST
body: |
{
"uid": "{{.app.metadata.uid}}",
"name": "{{.app.metadata.name}}",
"namespace": "{{.app.metadata.namespace}}",
"sync_status": "{{.app.status.sync.status}}",
"health_status": "{{.app.status.health.status}}",
"git_repo": "{{.app.spec.source.repoURL}}",
"git_path": "{{.app.spec.source.path}}",
"destination_server": "{{.app.spec.destination.server}}",
"created_at": "{{.app.metadata.creationTimestamp}}",
"project": "{{.app.spec.project}}"
}
subscriptions: |
- recipients:
- port-webhook
triggers:
- on-deployed
- on-health-degraded
- on-sync-operation-change
6.使用 kubectl
将 YAML 文件引用到集群。运行以下命令,将 <your-namespace>
替换为 ArgoCD 名称空间,将 <path-to-yaml-file>
替换为 YAML 文件的实际路径:
kubectl apply -n <your-namespace> -f <path-to-yaml-file>
该命令将 webhook 通知配置部署到 ArgoCD 通知 configmaps (argocd-notifications-cm
),使 Port 能够接收实时事件。
完成!您在 ArgoCD 中的应用程序发生的任何变化都将触发指向 Port 提供的 webhook URL 的 webhook 事件。 Port 将根据映射解析事件,并相应地更新目录实体。
Argocd Events
在本示例中,您将在ArgoCD 和 Port 之间创建一个 webhook 集成,该集成将摄取所有事件实体并将其映射到 ArgoCD 应用程序。
Port configuration
创建以下蓝图定义:
Events blueprint
{
"identifier": "argocdEvent",
"description": "This blueprint represents ArgoCD events",
"title": "ArgoCD Events",
"icon": "Argo",
"schema": {
"properties": {
"event": {
"type": "string",
"title": "Event Name"
},
"namespace": {
"type": "string",
"title": "Namespace"
},
"description": {
"type": "string",
"title": "Description"
}
},
"required": []
},
"mirrorProperties": {},
"calculationProperties": {},
"aggregationProperties": {},
"relations": {
"application": {
"title": "Application",
"target": "argocdApplication",
"required": false,
"many": false
}
}
}
创建以下 webhook 配置using Port UI
Application webhook configuration
- 基本信息 选项卡 - 填写以下详细信息:
1.title:
ArgoCD Event Mapper
; 2.标识符 :argocd_event_mapper
; 3.Description :将 ArgoCD 事件映射到 Port
的 webhook 配置; 4.图标 :Argo
; - 集成配置选项卡 - 填写以下 JQ 映射:
[
{
"blueprint": "argocdEvent",
"filter": "true",
"entity": {
"identifier": ".body.app_name + \"-\" + .body.event_name | tostring",
"title": ".body.event_name | tostring",
"properties": {
"event": ".body.event_name",
"namespace": ".body.namespace",
"description": ".body.description"
},
"relations": {
"application": ".body.uid"
}
}
}
]
3.点击页面底部的保存。
Create a webhook in ArgoCD
要在 ArgoCD 中设置向 Port 发送通知的 webhook 配置,请按以下步骤操作:
Prerequisite
- 您可以访问部署 ArgoCD 的 Kubernetes 集群。
- 安装并配置了
kubectl
以访问集群。
Steps
- 配置 ArgoCD 通知清单;
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj-labs/argocd-notifications/release-1.0/manifests/install.yaml
2.安装 ArgoCD 触发器和模板配置清单;
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj-labs/argocd-notifications/release-1.0/catalog/install.yaml
3.使用 kubectl
连接到部署 ArgoCD 实例的 Kubernetes 集群;
kubectl config use-context <your-cluster-context>
4.将当前名称空间设置为 ArgoCD 名称空间,请使用以下命令;
kubectl config set-context --current --namespace=<your-namespace>
5.创建一个 YAML 文件(例如 argocd-events-config.yaml
),配置 webhook 通知服务。下面的示例展示了如何设置 webhook 从 ArgoCD 发送实时事件。YAML 文件包括以下组件:
1.通知服务定义;
2.webhook 消息正文模板;
3.触发器定义;
4.订阅通知。
下面是一个 YAML 示例。请确保将 <YOUR_WEBHOOK_URL>
替换为创建 webhook 配置后收到的 url
键值。
event manifest file
apiVersion: v1
kind: ConfigMap
metadata:
name: argocd-notifications-cm
data:
trigger.on-created: |
- description: Application is created
send:
- app-created
when: true
oncePer: app.metadata.name
trigger.on-sync-running: |
- description: Application is being synced
send:
- app-sync-running
when: app.status.operationState.phase in ['Running']
trigger.on-sync-succeeded: |
- description: Application syncing has succeeded
send:
- app-sync-succeeded
when: app.status.operationState.phase in ['Succeeded']
trigger.on-sync-failed: |
- description: Application syncing has failed
send:
- app-sync-failed
when: app.status.operationState.phase in ['Error', 'Failed']
trigger.on-sync-status-unknown: |
- description: Application status is Unknown
send:
- app-sync-status-unknown
when: app.status.sync.status == 'Unknown'
trigger.on-deployed: |
- description: Application is synced and healthy
send:
- app-deployed
when: app.status.operationState.phase in ['Succeeded'] and app.status.health.status == 'Healthy'
trigger.on-health-degraded: |
- description: Application has degraded
send:
- app-health-degraded
when: app.status.health.status == 'Degraded'
trigger.on-deleted: |
- description: Application is deleted
send:
- app-deleted
when: app.metadata.deletionTimestamp != nil
oncePer: app.metadata.name
service.webhook.port-webhook: |
url: YOUR_WEBHOOK_URL
headers:
- name: Content-Type
value: application/json
template.app-created: |
webhook:
port-webhook:
method: POST
body: |
{
"uid": "{{.app.metadata.uid}}",
"event_name": "on-created",
"app_name": "{{.app.metadata.name}}",
"namespace": "{{.app.metadata.namespace}}",
"description": "Application is created"
}
template.app-sync-succeeded: |
webhook:
port-webhook:
method: POST
body: |
{
"uid": "{{.app.metadata.uid}}",
"event_name": "on-sync-succeeded",
"app_name": "{{.app.metadata.name}}",
"namespace": "{{.app.metadata.namespace}}",
"description": "Application syncing has succeeded"
}
template.app-sync-running: |
webhook:
port-webhook:
method: POST
body: |
{
"uid": "{{.app.metadata.uid}}",
"event_name": "on-sync-running",
"app_name": "{{.app.metadata.name}}",
"namespace": "{{.app.metadata.namespace}}",
"description": "Application is being synced"
}
template.app-sync-failed: |
webhook:
port-webhook:
method: POST
body: |
{
"uid": "{{.app.metadata.uid}}",
"event_name": "on-sync-failed",
"app_name": "{{.app.metadata.name}}",
"namespace": "{{.app.metadata.namespace}}",
"description": "Application syncing has failed"
}
template.app-health-degraded: |
webhook:
port-webhook:
method: POST
body: |
{
"uid": "{{.app.metadata.uid}}",
"event_name": "on-health-degraded",
"app_name": "{{.app.metadata.name}}",
"namespace": "{{.app.metadata.namespace}}",
"description": "Application has degraded"
}
template.app-deployed: |
webhook:
port-webhook:
method: POST
body: |
{
"uid": "{{.app.metadata.uid}}",
"event_name": "on-deployed",
"app_name": "{{.app.metadata.name}}",
"namespace": "{{.app.metadata.namespace}}",
"description": "Application is synced healthy"
}
template.app-sync-status-unknown: |
webhook:
port-webhook:
method: POST
body: |
{
"uid": "{{.app.metadata.uid}}",
"event_name": "on-sync-status-unknown",
"app_name": "{{.app.metadata.name}}",
"namespace": "{{.app.metadata.namespace}}",
"description": "Application status is Unknown"
}
template.app-deleted: |
webhook:
port-webhook:
method: POST
body: |
{
"uid": "{{.app.metadata.uid}}",
"event_name": "on-deleted",
"app_name": "{{.app.metadata.name}}",
"namespace": "{{.app.metadata.namespace}}",
"description": "Application deleted"
}
subscriptions: |
- recipients:
- port-webhook
triggers:
- on-created
- on-deployed
- on-health-degraded
- on-sync-failed
- on-sync-status-unknown
- on-sync-succeeded
- on-sync-running
- on-deleted
6.使用 kubectl
将 YAML 文件引用到集群。运行以下命令,将 <your-namespace>
替换为 ArgoCD 名称空间,将 <path-to-yaml-file>
替换为 YAML 文件的实际路径:
kubectl apply -n <your-namespace> -f <path-to-yaml-file>
该命令将 webhook 通知配置部署到 ArgoCD 通知 configmaps (argocd-notifications-cm
),使 Port 能够接收实时事件。
完成!您在 ArgoCD 中的应用程序发生的任何变化都将触发指向 Port 提供的 webhook URL 的 webhook 事件。 Port 将根据映射解析事件,并相应地更新目录实体。