我有一个任务。我需要编写 python 代码来为 kubernetes 生成 yaml 文件。到目前为止我一直在使用 pyyaml 并且效果很好。这是我生成的 yaml 文件:
apiVersion: v1
kind: ConfigMap
data:
info:
name: hostname.com
aio-max-nr: 262144
cpu:
cpuLogicalCores: 4
memory:
memTotal: 33567170560
net.core.somaxconn: 1024
...
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试创建此 configMap 时,错误是 info 需要 string() 而不是地图。所以我探索了一下,解决这个问题的最简单方法似乎是在信息后面添加一个管道,如下所示:
apiVersion: v1
kind: ConfigMap
data:
info: | # this will translate everything in data into a string but still keep the format in yaml file for readability
name: hostname.com
aio-max-nr: 262144
cpu:
cpuLogicalCores: 4
memory:
memTotal: 33567170560
net.core.somaxconn: 1024
...
Run Code Online (Sandbox Code Playgroud)
这样我的configmap就创建成功了。我的苦恼是我不知道如何从 python 代码中添加管道栏。这里我手动添加了它,但我想自动化整个过程。 …