With command, I can add label as below
kubectl label pod POD_NAME KEY1=VALUE1
How could I do that from kubernetes API?
I guess it can be done by PATCH /api/v1/namespaces/{namespace}/pods/{name}
Here is pod.json
{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"labels": {
"key1": "value1"
}
}
}
I tried with following command
KUBE_TOKEN=$(</var/run/secrets/kubernetes.io/serviceaccount/token)
curl --request PATCH --insecure \
--header "Authorization: Bearer $KUBE_TOKEN" \
--data "$(cat pod.json)" \
https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_PORT_443_TCP_PORT/api/v1/namespaces/$POD_NAMESPACE/pods/$POD_NAME
And it returns
{
"kind": "Status",
"apiVersion": "v1",
"metadata": {},
"status": "Failure",
"message": "the server responded with the status code 415 but did not return more information",
"details": {},
"code": 415
}
Set content-type to application/json-patch+json and specify the patch in http://jsonpatch.org format.
$ cat > patch.json <<EOF
[
{
"op": "add", "path": "/metadata/labels/hello", "value": "world"
}
]
EOF
$ curl --request PATCH --data "$(cat patch.json)" -H "Content-Type:application/json-patch+json" https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_PORT_443_TCP_PORT/api/v1/namespaces/$POD_NAMESPACE/pods/$POD_NAME
In order to use JSON Patch properly, you have to set content type to application/json-patch+json as defined in RFC6902. Works for me.
Related
According to the Replacement section of Kubernetes API reference v1.24 I should be able to create a deployment with a PUT /apis/apps/v1/namespaces/{namespace}/deployments/{name} HTTP request. The success response here is 201 Created. However, when I try the following, I get a 404 Not Found which is of course correct but unwanted: PUT requests should be treated as Create statements if the resource does not yet exist as documented. Updating a deployment does work (and returns the expected 200 OK HTTP response). Is there any documentation regarding this? Or is the request somehow incorrect? Ty.
➜ ~ curl --request PUT \
--url http://localhost:8080/apis/apps/v1/namespaces/ns/deployments/nginx-deployment \
--header 'content-type: application/json' \
--data '{
"apiVersion":"apps/v1",
"kind":"Deployment",
"metadata":{
"name":"nginx-deployment",
"labels":{
"app":"nginx"
}
},
"spec": {
"replicas" : 3,
"selector": {
"matchLabels" : {
"app":"nginx"
}
},
"template" : {
"metadata" : {
"labels" : {
"app":"nginx"
}
},
"spec":{
"containers":[
{
"name":"ngnix",
"image":"nginx:1.7.9",
"ports":[
{
"containerPort": 80
}
]
}
]
}
}
}
}'
{
"kind": "Status",
"apiVersion": "v1",
"metadata": {},
"status": "Failure",
"message": "deployments.apps \"nginx-deployment\" not found",
"reason": "NotFound",
"details": {
"name": "nginx-deployment",
"group": "apps",
"kind": "deployments"
},
"code": 404
}%
According to the documentation you provided,
PUT /apis/apps/v1/namespaces/{namespace}/deployments/{name}
is meant to "replace the specified Deployment", while a Deployment is created with a POST:
create a Deployment
HTTP Request
POST /apis/apps/v1/namespaces/{namespace}/deployments
You are correct that the documentation also states:
For PUT requests, Kubernetes internally classifies these as either create or update based on the state of the existing object
so there seems to be a contradiction, but the Deployment API spec states that POST should be used to create a deployment and PUT to update it.
I am trying to patch the node condition type status, for example can I turn/replace the Whatever Node Condition type status from false to true and vice versa?
Edit: When I try to patch I am not getting any errors by yet not being updated.
Using Curl - I was able to update:
curl -k -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json-patch+json" -X PATCH APIserver:6443/api/v1/nodes/<node-name>/status --data '[{ "op": "replace", "path": "/status/conditions/-","value": { "type": "WhateverName", "status": "False" }}]'
Patch Example (not working):
oc patch node/<Node-Name> --type='json' -p '[{ "op": "replace", "path": "/status/conditions/0","value": { "type": "QuayState", "status": "True" }}]'
It's not giving errors but it's not changing anything, I am getting this output:
node/<nodeName> patched (no change)
I am trying to patch horizontal pod autoscaler by setting minimum replica through kubernetes api
Here is the curl which I am using
curl -k \
--request PATCH \
--header "Authorization: Bearer $KUBE_TOKEN" \
--header "Content-Type: application/strategic-merge-patch+json" \
--data '{
"apiVersion": "autoscaling/v1",
"kind": "HorizontalPodAutoscaler",
"metadata": {
"labels": {
"app.kubernetes.io/instance": "test"
},
"name": "test",
"namespace": "default"
},
"spec": {
"maxReplicas": 2,
"minReplicas": 1,
"scaleTargetRef": {
"apiVersion": "apps/v1",
"kind": "Deployment",
"name": "test"
},
"targetCPUUtilizationPercentage": 60
}
}' \
https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_PORT_443_TCP_PORT/apis/autoscaling/v1/namespaces/default/horizontalpodautoscalers
I receive following response
{
"kind": "Status",
"apiVersion": "v1",
"metadata": {
},
"status": "Failure",
"message": "the server does not allow this method on the requested resource",
"reason": "MethodNotAllowed",
"details": {
},
"code": 405
}
Can anyone know where am I missing?
Thanks
The URL path must contain the name:
/apis/autoscaling/v1/namespaces/{namespace}/horizontalpodautoscalers/{name}
Its documented on this page https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.23/#horizontalpodautoscaler-v1-autoscaling
After running
curl --request POST
--url https://api.twitter.com/1.1/direct_messages/events/new.json
--header 'authorization: OAuth oauth_consumer_key="YOUR_CONSUMER_KEY", oauth_nonce="AUTO_GENERATED_NONCE", oauth_signature="AUTO_GENERATED_SIGNATURE", oauth_signature_method="HMAC-SHA1", oauth_timestamp="AUTO_GENERATED_TIMESTAMP", oauth_token="USERS_ACCESS_TOKEN", oauth_version="1.0"'
--header 'content-type: application/json'
--data '{"event": {"type": "message_create", "message_create": {"target": {"recipient_id": "RECIPIENT_USER_ID"}, "message_data": {"text": "Hello World!"}}}}'
I got this error:
{"errors":[{"code":215,"message":"Bad Authentication data."}]} back.
Where do you set your access token & access token secret? And how do you add Quick Replies?
Oh got it! YES!
curl --request POST \
--url https://api.twitter.com/1.1/direct_messages/events/new.json \
--header 'authorization: OAuth oauth_callback="CALLBACK_URL", oauth_consumer_key="KEY", oauth_nonce="auto generated", oauth_signature="autogenerated", oauth_signature_method="HMAC-SHA1", oauth_timestamp="current time stamp", oauth_token="TOKEN", oauth_version="1.0"' \
--header 'content-type: application/json' \
--cookie 'personalization_id=SOME COOKIE; guest_id=SOME ID; lang=en' \
--data '{
"event": {
"type": "message_create",
"message_create": {
"target": {
"recipient_id": "ID"
},
"message_data": {
"text": "What'\''s your favorite type of bird?",
"quick_reply": {
"type": "options",
"options": [
{
"label": "Red Bird",
"description": "A description about the red bird.",
"metadata": "external_id_1"
},
{
"label": "Blue Bird",
"description": "A description about the blue bird.",
"metadata": "external_id_2"
},
{
"label": "Black Bird",
"description": "A description about the black bird.",
"metadata": "external_id_3"
},
{
"label": "White Bird",
"description": "A description about the white bird.",
"metadata": "external_id_4"
}
]
}
}
}
}
}'```
I can add labels to a pod as described here
But no luck to create annotations likewise
$ KUBE_TOKEN=$(</var/run/secrets/kubernetes.io/serviceaccount/token)
$ cat > patch.json <<EOF
]
{
"op": "add", "path": "/metadata/annotations/test", "value": "world"
}
]
EOF
$ curl -sSk -H "Authorization: Bearer $KUBE_TOKEN" --request PATCH --data "$(cat patch.json)" -H "Content-Type:application/json-patch+json" https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_PORT_443_TCP_PORT/api/v1/namespaces/$POD_NAMESPACE/pods/$POD_NAME
the response is:
{
"kind": "Status",
"apiVersion": "v1",
"metadata": {
},
"status": "Failure",
"message": "jsonpatch add operation does not apply: doc is missing path: /metadata/annotations/test",
"code": 500
}
The error is related to non-existing path.
[
{
"op": "add", "path": "/metadata/annotations/", "value": { "test" : "world" }
}
]
Check out https://www.rfc-editor.org/rfc/rfc6902#section-4.1
AFAIR, you cannot add new annotations on existing resources. You can only update existing ones.