XML files to ConfigMap yaml

6/28/2018

Is there anyway to convert configuration files like xml to kubernetes configmap yaml file without using kubectl command ? Let’s say if I want to create xml files dynamically which in turn stored in git repo as configmap yaml and some operator can monitor for yaml changes and deploy it to the cluster.

-- srikanth
kubectl
kubernetes

1 Answer

6/28/2018

configuration files like xml to kubernetes configmap yaml file without using kubectl command

Sure, because the only thing kubectl does with yaml is immediately convert it to json and then POST (or PUT or whatever) to the kubernetes api with a content-type: application/json;charset=utf-8 header (you can watch that take place via kubectl --v=100 create -f my-thing.yaml)

So, the answer to your question is to use your favorite programming language that has libraries for json (or the positively amazing jq), package the XML as necessary, the use something like kube-applier to monitor and roll out the change:

# coding=utf-8
import json
import sys

result = {
  "apiVersion": "v1",
  "kind": "ConfigMap",
  # etc etc
  "data": [],
}
for fn in sys.argv[1:]:
    with open(fn) as fh:
       body = fh.read()
    data.append({fn: body})
json.dump(result, sys.stdout)  # or whatever
-- mdaniel
Source: StackOverflow