Kubernetes ConfigMap Update

9/5/2018

I'm working on a python script for update the configmaps programmatically.

Example script at shown as below.

import requests

headers = {"Content-Type": "application/json-patch+json"}
configData = {
"apiVersion": "v1",
"kind": "ConfigMap",
"data": {
    "test2.load": "testimtest"
},
"metadata": {
    "name": "nginx2"
}
}

r = requests.patch("http://localhost:8080/api/v1/namespaces/default/configmaps/nginx2", json=configData)

The interesting side of this problem is that I have no problem with POST and GET methods but when I want to update kubernetes configmaps with PATCH method of HTTP I'm getting

 "reason":"UnsupportedMediaType" //STATUS_CODE 415

How I can handle this problem.

-- ColossusMark1
http
kubernetes
python
rest

2 Answers

9/8/2018

You'd have to read a bit to figure out if in fact this HTTP verb is supported, maybe you're having a permissions issue?

Regardless, my suggestion to you is to use the verbose option of kubectl to figure out stuff like that.

For example, let's say I want to delete a pod, I would:

kubectl -v=9 delete pod myapp

The output (I cleaned up a bit) should contain the information you're looking for:

I1315 14:42:19.153664 30452 round_trippers.go:417]
   curl -k -v -XDELETE -H "Accept: application/json, */*"
   -H "User-Agent: kubectl/v1.8.5 (linux/amd64) kubernetes/cce11c6"
   https://<ip>:<port>/api/v1/namespaces/default/pods/myapp
...

so now if I have all the details I need to construct the methods in python or whatever, hope that makes sense and/or helps, try it with the patch command.

-- Naim Salameh
Source: StackOverflow

9/8/2018

I suggest you use a Kubernetes client library, instead of making the raw HTTP calls yourself. Then you don't need to figure out the low-level connection stuff, as the library will abstract that away for you.

I've been using Pykube, which provides a nice pythonic API, though it does appear to be abandoned now.

You can also use the official client-python, which is actively maintained. The library is a bit more clunky, as it's based on an autogenerated OpenAPI client, but it covers lots of use-cases like streaming results.

-- Symmetric
Source: StackOverflow