When accessing the API server directly (i.e. not with kubectl, but with direct HTTP requests), what formats for the resource specifications does the API server support?
In all examples I have seen so far, the resource specifications are in JSON (for example here). But I couldn't find any general information about this.
Does the API server also accept resource specifications in other formats, such as YAML or protobuf?
Similarly, when the API server returns resources in response to a GET request, are the resources always returned in JSON or are there any other formats supported?
Managing Kubernetes, Chapter 4 (section "Alternate encodings") says that the API server supports three data formats for resource specifications:
I tested creating resources in these formats using curl
and it works, as shown in the following.
For easily talking to the API server, start a proxy to the API server with kubectl:
kubectl proxy
Now the API server is accessible on http://127.0.0.1:8001.
The Kubernetes API specification is accessible on http://127.0.0.1:8001/openapi/v2.
You have to specify the format of the HTTP POST request body (i.e. the resource specification) in the Content-Type
header.
The following data formats are supported:
application/json
application/yaml
application/vnd.kubernetes.protobuf
Below are concrete examples of requests.
Define a resource specification in JSON and save it in a file.
For example, pod.json
:
{
"apiVersion":"v1",
"kind":"Pod",
"metadata":{
"name":"test-pod"
},
"spec":{
"containers":[
{
"image":"nginx",
"name":"nginx-container"
}
]
}
}
Call API server to create the resource:
curl -H "Content-Type: application/json" -d "$(cat pod.json)" -X POST http://127.0.0.1:8001/api/v1/namespaces/default/pods
Define a resource specification in YAML and save it in a file.
For example, pod.yaml
:
apiVersion: v1
kind: Pod
metadata:
name: test-pod
spec:
containers:
- image: nginx
name: nginx-container
Call API server to create the resource:
curl -H "Content-Type: application/yaml" -d "$(cat pod.yaml)" -X POST http://127.0.0.1:8001/api/v1/namespaces/default/pods
I didn't test this, because the Kubernetes protobuf wire format uses a custom wrapper around the protobuf serialisation of the resource (see here and here). But, in principle, it should work.
When creating a resource as shown above, the API server returns the complete specification of the same resource in the HTTP response (that is, the specification that you submitted, initialised with all the default values, a status
field, etc.).
You can choose the format for this response data with the Accept
header in the request.
The accepted formats for the Accept
header are the same as for the Content-Type
header:
application/json
(default)application/yaml
application/vnd.kubernetes.protobuf
For example:
curl -H "Content-Type: application/json" -H "Accept: application/yaml" -d "$(cat pod.json)" -X POST http://127.0.0.1:8001/api/v1/namespaces/default/pods
All combinations of formats in the Content-Type
and Accept
headers are possible.
In Kubernetes both json and YAML formats are supported. am assuming you create and update the resources using kubectl. kubectl accepts both these formats.
if you pass YAML format, Internally the kubectl converts it to json and post it to API server.