I have a Kubernetes deployment which uses a ConfigMap with some configuration which is frequently updated. Currently I have to update this configuration manually, by running a script on my local machine that updates the ConfigMap via kubectl
.
Is there a way to do this in a more automated fashion using the Kubernetes API (from inside or outside Kubernetes)?
def updateConfigMap(token):
print(token)
token = "Bearer {}".format(token)
headers = {"Content-Type": "application/merge-patch+json", "authorization":token}
r = requests.patch("{}/api/v1/namespaces/default/configmaps/CONFIMAPNAME".format(KUBERNETES_MASTER), verify=False, headers=headers, json=configData)
return r.content
I've a some problem at before but when changed the header of PATCH request . I can update my configmaps . But attention to the token permission (Service Account)
There are a couple of Kubernetes clients in many languages if you take a look here. Python and Go are officially supported. You can automate the steps by calling the client.
If you know Python, you may refer to the sample below.
from __future__ import print_statement
import time
import kubernetes.client from kubernetes.client.rest
import ApiException from pprint import pprint
# Configure API key authorization: BearerToken
kubernetes.client.configuration.api_key['authorization'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# kubernetes.client.configuration.api_key_prefix['authorization'] = 'Bearer'
# create an instance of the API class api_instance =
kubernetes.client.CoreV1Api()
name = 'name_example' # str | name of the ConfigMap
namespace = 'namespace_example' # str | object name and auth scope, such as for teams and projects
body = NULL # object |
pretty = 'pretty_example' # str | If 'true', then the output is pretty printed. (optional)
try:
api_response = api_instance.patch_namespaced_config_map(name, namespace, body, pretty=pretty)
pprint(api_response)
except ApiException as e:
print("Exception when calling CoreV1Api->patch_namespaced_config_map: %s\n" % e)
With respect to use APIs internally and externally, you can take a look at the wiki. Especially this thread explains how to access APIs from a pod.
KUBE_TOKEN=$(</var/run/secrets/kubernetes.io/serviceaccount/token)
curl -sSk -H "Authorization: Bearer $KUBE_TOKEN" https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_PORT_443_TCP_PORT/api/v1/namespaces/iot2cloud/configmaps