조건에 따라 헤더 추가
조건에 따라 헤더 추가 (Modify Headers: Add header based on a condition)
이 문서는 레코드가 특정 조건을 만족할 때 헤더를 추가하는 modify_headers 정책 설정 방법을 설명해요. 토픽 이름이 my_suffix로 끝나는 레코드에 example-header라는 헤더를 example 값으로 추가하는 예시를 통해 동작 방식을 보여줘요. Konnect API, Terraform, kongctl 세 가지 방식으로 정책을 구성하는 절차를 단계별로 안내해요
출처: 문서
본문
조건에 따라 헤더 추가하기
특정 조건을 충족하는 경우 레코드에 헤더를 추가해요.
다음 예제에서는 레코드의 토픽 이름이 my_suffix로 끝나면, 정책이 example 값을 가진 example-header라는 이름의 헤더를 추가해요.
정책 설정하기
컨슘(Consume) 단계
Konnect API
curl -X POST https://{region}.api.konghq.com/v1/event-gateways/{eventGatewayId}/virtual-clusters/{virtualClusterId}/consume-policies \
--header "accept: application/json" \
--header "Content-Type: application/json" \
--header "Authorization: Bearer $KONNECT_TOKEN" \
--data '
{
"name": "set-header-based-on-condition",
"type": "modify_headers",
"condition": "context.topic.name.endsWith(\"my_suffix\")",
"config": {
"actions": [
{
"op": "set",
"key": "example-header",
"value": "example"
}
]
}
}
'
다음 플레이스홀더는 여러분의 값으로 바꿔야 해요:
-
region: Kong Konnect가 호스팅되고 운영되는 지리적 리전이에요. -
KONNECT_TOKEN: Konnect 계정과 연결된 개인 액세스 토큰(PAT)이에요. -
virtualClusterId: Virtual Cluster의id예요. -
eventGatewayId: Event Gateway의id예요. -
eventGatewayListenerId: Event Gateway Listener의id예요.
리전별 URL과 개인 액세스 토큰에 대해 자세히 알아보려면 Konnect Event Gateway API 참조 문서를 확인하세요.
Terraform
사전 조건: 개인 액세스 토큰 구성하기
terraform {
required_providers {
konnect-beta = {
source = "kong/konnect-beta"
}
}
}
provider "konnect-beta" {
personal_access_token = "$KONNECT_TOKEN"
server_url = "https://us.api.konghq.com/"
}
Terraform 구성에 다음 내용을 추가하세요:
resource "konnect_event_gateway_consume_policy_modify_headers" "my_virtual_cluster_policy_modify_headers" {
provider = konnect-beta
type = "modify_headers"
condition = "context.topic.name.endsWith(\"my_suffix\")"
config = {
actions = [
{
op = "set"
key = "example-header"
value = "example"
} ]
}
virtual_cluster_id = konnect_event_gateway_virtual_cluster.my_virtual_cluster.id
gateway_id = konnect_event_gateway.my_event_gateway.id
}
kongctl
다음 예제는 새로운 modify_headers 정책을 생성해요.
event_gateways:
- ref: eventGatewayName
name: eventGatewayName
virtual_clusters:
- ref: virtualClusterName
name: virtualClusterName
consume_policies:
- ref: set-header-based-on-condition
type: modify_headers
modify_headers:
name: set-header-based-on-condition
condition: context.topic.name.endsWith("my_suffix")
config:
actions:
- op: set
key: example-header
value: example
{: data-file="event_gateway_policy.yaml" data-tool="kongctl" }
다음 플레이스홀더는 여러분의 값으로 바꿔야 해요:
-
eventGatewayName: Event Gateway의name이에요. -
virtualClusterName: Virtual Cluster의name이에요.
프로듀스(Produce) 단계
Konnect API
curl -X POST https://{region}.api.konghq.com/v1/event-gateways/{eventGatewayId}/virtual-clusters/{virtualClusterId}/produce-policies \
--header "accept: application/json" \
--header "Content-Type: application/json" \
--header "Authorization: Bearer $KONNECT_TOKEN" \
--data '
{
"name": "set-header-based-on-condition",
"type": "modify_headers",
"condition": "context.topic.name.endsWith(\"my_suffix\")",
"config": {
"actions": [
{
"op": "set",
"key": "example-header",
"value": "example"
}
]
}
}
'
다음 플레이스홀더는 여러분의 값으로 바꿔야 해요:
-
region: Kong Konnect가 호스팅되고 운영되는 지리적 리전이에요. -
KONNECT_TOKEN: Konnect 계정과 연결된 개인 액세스 토큰(PAT)이에요. -
virtualClusterId: Virtual Cluster의id예요. -
eventGatewayId: Event Gateway의id예요. -
eventGatewayListenerId: Event Gateway Listener의id예요.
리전별 URL과 개인 액세스 토큰에 대해 자세히 알아보려면 Konnect Event Gateway API 참조 문서를 확인하세요.
Terraform
사전 조건: 개인 액세스 토큰 구성하기
terraform {
required_providers {
konnect-beta = {
source = "kong/konnect-beta"
}
}
}
provider "konnect-beta" {
personal_access_token = "$KONNECT_TOKEN"
server_url = "https://us.api.konghq.com/"
}
Terraform 구성에 다음 내용을 추가하세요:
resource "konnect_event_gateway_produce_policy_modify_headers" "my_virtual_cluster_policy_modify_headers" {
provider = konnect-beta
type = "modify_headers"
condition = "context.topic.name.endsWith(\"my_suffix\")"
config = {
actions = [
{
op = "set"
key = "example-header"
value = "example"
} ]
}
virtual_cluster_id = konnect_event_gateway_virtual_cluster.my_virtual_cluster.id
gateway_id = konnect_event_gateway.my_event_gateway.id
}
kongctl
다음 예제는 새로운 modify_headers 정책을 생성해요.
event_gateways:
- ref: eventGatewayName
name: eventGatewayName
virtual_clusters:
- ref: virtualClusterName
name: virtualClusterName
produce_policies:
- ref: set-header-based-on-condition
type: modify_headers
modify_headers:
name: set-header-based-on-condition
condition: context.topic.name.endsWith("my_suffix")
config:
actions:
- op: set
key: example-header
value: example
{: data-file="event_gateway_policy.yaml" data-tool="kongctl" }
다음 플레이스홀더는 여러분의 값으로 바꿔야 해요:
-
eventGatewayName: Event Gateway의name이에요. -
virtualClusterName: Virtual Cluster의name이에요.
관련 리소스
더 알아보기 (Learn more)
- 리전별 URL과 퍼스널 액세스 토큰 사용 방법을 확인할 수 있어요 — Konnect Event Gateway API reference
- 정책을 적용할 가상 클러스터의 개념과 구성 방법을 알아봐요 — Virtual clusters
- consume 정책을 포함한 정책 엔티티의 전체 구조를 이해해요 — Policies