How to version APIs with CRDs
This page explains API versioning with CRDs (Custom Resource Definitions).
Before you begin¶
Before getting started, make sure to read our versioning overview docs.
Custom Resource Definitions¶
Traefik Hub uses the APIVersion CRD to version APIs.
See the following versioning examples.
Examples
---
# Example of versioning an API using `pathPrefix` for URI path.
apiVersion: hub.traefik.io/v1alpha1
kind: APIVersion
metadata:
name: my-flights-api-v2
namespace: apps
spec:
# The API this version is referencing (assumed to be in the same namespace).
apiName: flight-api
# SemVer of the release
release: v2.0.0
title: "An awesome title for this release, like a cheese name"
routes:
# The API will be available on one route using URI path for versioning.
# Example: curl https://api.example.com/flights/v2.0.0
- pathPrefix: "/v2.0.0"
# The path prefix of the route will be removed and not forwarded with the request.
stripPathPrefix: true
service:
name: flights-svc-v2
port:
number: 8080
openApiSpec:
path: /api/v2/openapi.json
port:
number: 8080
---
# Example of versioning an API using `headers`.
apiVersion: hub.traefik.io/v1alpha1
kind: APIVersion
metadata:
name: my-flights-api-v2
namespace: apps
spec:
# The API this version is referencing (assumed to be in the same namespace).
apiName: flight-api
# SemVer of the release
release: v2.0.0
title: "An awesome title for this release, like a cheese name"
routes:
# The API will be available on one route using a custom header for versioning.
# Example: curl -H "Version: v2.0.0" https://api.example.com/flights
- headers:
Version: "v2.0.0"
service:
name: flights-svc-v2
port:
number: 8080
openApiSpec:
path: /api/v2/openapi.json
port:
number: 8080
---
# Example of versioning an API using a media type headers.
apiVersion: hub.traefik.io/v1alpha1
kind: APIVersion
metadata:
name: my-flights-api-v2
namespace: apps
spec:
# The API this version is referencing (assumed to be in the same namespace).
apiName: flight-api
# SemVer of the release
release: v2.0.0
title: "An awesome title for this release, like a cheese name"
routes:
# The API will be available on one route using a `media type` header for versioning.
# Example: curl -H "Content: application/vnd.example.v2.0.0+json" https://api.example.com/flights
- headers:
Content: "application/vnd.example.v1+json"
service:
name: flights-svc-v2
port:
number: 8080
openApiSpec:
path: /api/v2/openapi.json
port:
number: 8080
---
# Example of versioning an API using `queryParams` for query parameter versioning.
apiVersion: hub.traefik.io/v1alpha1
kind: APIVersion
metadata:
name: my-flights-api-v2
namespace: apps
spec:
# The API this version is referencing (assumed to be in the same namespace).
apiName: flight-api
# SemVer of the release
release: v2.0.0
title: "An awesome title for this release, like a cheese name"
routes:
# The API will be available at one route, using a version `query parameter`.
# Example: curl https://api.example.com/flights?version=2.0.0
- queryParams:
version: "v2.0.0"
service:
name: flights-svc-v2
port:
number: 8080
openApiSpec:
path: /api/v2/openapi.json
port:
number: 8080
---
# Example of versioning an API using two `queryParams`, version and language.
apiVersion: hub.traefik.io/v1alpha1
kind: APIVersion
metadata:
name: my-flights-api-v2
namespace: apps
spec:
# The API this version is referencing (assumed to be in the same namespace).
apiName: flight-api
# SemVer of the release
release: v2.0.0
title: "An awesome title for this release, like a cheese name"
routes:
# The API will be available at one route, both `queryParams` constraints must match.
# Example: curl https://api.example.com/flights?version=2.0.0&lang=fr
- queryParams:
version: "v2.0.0"
lang: "fr"
service:
name: flights-svc-v2
port:
number: 8080
openApiSpec:
path: /api/v2/openapi.json
port:
number: 8080
---
# Example of versioning an API using `pathPrefix` for URI path and CORS.
apiVersion: hub.traefik.io/v1alpha1
kind: APIVersion
metadata:
name: my-flights-api-v2
namespace: apps
spec:
# The API this version is referencing (assumed to be in the same namespace).
apiName: my-versioned-flights-api
# SemVer of the release
release: v2.0.0
title: "An awesome title for this release, like a cheese name"
routes:
# The API will be available on one route using URI path for versioning.
# Example: curl https://api.example.com/flights/v2.0.0
- pathPrefix: "/v2.0.0"
# The path prefix of the route will be removed and not forwarded with the request.
stripPathPrefix: true
service:
name: flights-svc-v2
port:
number: 8080
openApiSpec:
path: /api/v2/openapi.json
port:
number: 8080
cors:
allowCredentials: true
allowOriginList:
- "*"
allowHeaders:
- "Accept"
- "Accept-Language"
- "Content-Language"
- "Content-Type"
- "Authorization"
- "X-TraefikLabs-User"
allowMethods:
- "GET"
- "HEAD"
- "POST"
- "PUT"
What's next¶
- Check out the CRD reference about API versioning