본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

API 네임스페이스 구성

원문 보기 위키 갱신

API 네임스페이스 구성 (deck file namespace)

여러 팀이 연합 구성 관리를 하다 보면 서로 다른 팀이 우연히 같은 API 경로를 쓰는 엔드포인트 충돌이 생기기 쉬워요. deck file namespace 명령어를 사용하면 경로 접두사를 붙이거나 API마다 고유한 호스트를 지정해서 이런 충돌을 예방할 수 있어요. 이 문서에서는 경로 접두사 방식과 커스텀 호스트 방식의 차이, 백엔드로 요청을 보내기 전에 접두사를 제거하는 동작 방식, 그리고 --selector나 --clear-hosts 같은 주요 플래그 사용법까지 차근차근 설명해요

출처: 문서

본문

이를 피하려면 경로 접두사를 사용해 각 API에 네임스페이스를 지정하거나, 각 API가 특정 호스트에서 리슨하도록 설정할 수 있어요.

기본적으로 deck file namespace 명령은 파일의 모든 Route에 적용돼요. 특정 Route만 대상으로 하려면 --selector 플래그를 전달하세요.

경로 접두사

충돌을 방지하는 가장 간단한 방법은 각 API Route에 정적 경로를 접두사로 붙이는 거예요. 이 경우 /path/to/config의 모든 Route는 /billing 경로 아래에 노출돼요:

deck file namespace --path-prefix=/billing -s ./config.yaml

백엔드 Service에 투명하게 유지되도록 하려면, 요청이 Service로 라우팅되기 전에 추가된 경로 접두사를 경로에서 제거해야 해요. 접두사를 제거하기 위해 다음 방법들이 (순서대로) 사용돼요:

  1. Route에 strip_path=true가 설정되어 있으면, 추가된 접두사는 이미 제거된 상태예요.
  2. 관련 Service에 접두사와 일치하는 path 속성이 있으면, 접두사를 제거하도록 service.path 속성이 업데이트돼요.
  3. 경로에서 접두사를 제거하기 위해 pre-function 플러그인이 추가돼요.

pre-function이 사용되면, 설정해 둔 전역 pre-function 플러그인보다 우선 적용돼요.

커스텀 호스트

API에 네임스페이스를 지정하는 또 다른 방법은 각 API가 서로 다른 호스트명에서 리슨하도록 하는 거예요. 예를 들어 http://service1.api.example.com/somepath, http://service2.api.example.com/somepath처럼요.

다음 명령은 파일의 모든 Route 정의를 업데이트해서 service1.api.example.com으로 요청이 들어올 때만 리슨하도록 해요. Route에 이미 hosts 항목이 있으면, 새 도메인이 목록에 추가돼요.

deck file namespace --host service1.api.example.com

API가 제공된 호스트명에서만 리슨하도록 보장해야 한다면, --clear-hosts 플래그를 전달할 수 있어요:

deck file namespace --host service1.api.example.com --clear-hosts

명령 사용법

Usage:
  deck file namespace [flags]

Examples:
# Apply namespace to a deckfile, path and host:
deck file namespace --path-prefix=/kong --host=konghq.com --state=deckfile.yaml

# Apply namespace to a deckfile, and write to a new file
# Example file 'kong.yaml':
routes:
- paths:
  - ~/tracks/system$
  strip_path: true
- paths:
  - ~/list$
  strip_path: false

# Apply namespace to the deckfile, and write to stdout:
cat kong.yaml | deck file namespace --path-prefix=/kong

# Output:
routes:
- paths:
  - ~/kong/tracks/system$
  strip_path: true
  hosts:
  - konghq.com
- paths:
  - ~/kong/list$
  strip_path: false
  hosts:
  - konghq.com
  plugins:
  - name: pre-function
    config:
      access:
      - "local ns='/kong' -- this strips the '/kong' namespace from the path\nlocal <more code here>"



Flags:
      --allow-empty-selectors   Do not error out if the selectors return empty
  -c, --clear-hosts             Clear existing hosts.
      --format string           Output format: yaml or json. (default "yaml")
  -h, --help                    help for namespace
      --host stringArray        Hostname to add for host-based namespacing. Repeat for multiple hosts.
  -o, --output-file string      Output file to write. Use - to write to stdout. (default "-")
  -p, --path-prefix string      The path based namespace to apply.
      --selector stringArray    json-pointer identifying element to patch. Repeat for multiple selectors. Defaults to selecting all routes.
  -s, --state string            decK file to process. Use - to read from stdin. (default "-")

관련 리소스

더 알아보기 (Learn more)

  • 연합 구성 관리의 개념을 먼저 이해하면 이 명령어가 왜 필요한지 더 잘 알 수 있어요 — federated configuration management
  • 이 명령어가 경로 접두사와 호스트를 수정하는 대상인 Route 엔티티의 동작을 자세히 살펴봐요 — Route entity