如何从 yaml 文件的一部分创建 Kubernetes configMap?
据我所知,在 Kubernetes 中从文件创建 configMap 的方法是使用:kubectl
的 --from-file
选项
我正在寻找一种仅将部分 yaml 文件加载到 configMap 中的方法。
例子: 假设我有这个 yml 文件:
family:
Boys:
- name: Joe
- name: Bob
- name: dan
Girls:
- name: Alice
- name: Jane
现在我想创建一个名为“boys”的 configMap,其中仅包含“Boys”部分。
可能的?
如果上述不可能,另一件事可能会有所帮助,那就是当我将 configMap 作为环境变量导出到 pod 时(使用 envFrom
),以便能够仅导出 configMap 的一部分。
两种选择都适合我。
有什么想法吗?
As I know the way to create a configMap in Kubernetes from a file is to use:--from-file
option for kubectl
What I am looking for is a way to only load part of the yaml file into the configMap.
Example:
Let's say I have this yml file:
family:
Boys:
- name: Joe
- name: Bob
- name: dan
Girls:
- name: Alice
- name: Jane
Now I want to create a configMap called 'boys' which will include only the 'Boys' section.
Possible?
Another thing that could help if the above is not possible is when I am exporting the configMap as environment variables to a pod (using envFrom
) to be able to only export part of the configMap.
Both options will work for me.
Any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ConfigMap 使用键和值进行配置。根据您的示例,您将获得多个数据数组,其中有多个值及其自己的键。但是您可以从不同的文件创建多个 ConfigMap 来解决这些问题。
第一个文件调用
Boys.yaml
第二个文件调用
Girls.yaml
Boys.yaml 将这些 ConfigMap 传递到 pod 或部署代码>configMapRef
条目:The ConfigMap uses a key and value for its configuration. Based on your example, you get multiple arrays of data where there are multiple values with their own keys. But you can create multiple ConfigMap from different file for these issues.
First file call
Boys.yaml
Second file call
Girls.yaml
configMapRef
entries:Configmap 不能包含丰富的 yaml 数据。仅键值对。因此,如果您想要一个事物列表,则需要将其表示为多行字符串。
考虑到这一点,您可以使用某些工具(例如 yq)来查询输入文件并选择您想要的部分。
例如:
结果如下所示
您还可以使用 base64 对文件或文件的一部分进行编码,并将其用作环境变量,因为您可以从中得到一个易于处理的字符串。例如:
或者使用 set env,如果需要,您可以进一步将其与试运行结合起来。
另一件事是,YAML 是 JSON 的超集,在许多情况下您可以将 YAML 转换为 JSON,或者至少使用类似 JSON 的语法。
这在这种情况下很有用,可以将其表示为单行字符串,而不必使用多行语法。它不那么脆弱。
每个 YAML 解析器都能够很好地解析 JSON。因此,如果您在应用程序中解析字符串,就不会有问题。
免责声明,我创建了上面使用的工具 tpl。如前所述,您也可以使用替代工具,例如 yq。
Configmaps cannot contain rich yaml data. Only key value pairs. So if you want to have a list of things, you need to express this as a multiline string.
With that in mind you could use certain tools, such a yq to query your input file and select the part you want.
For example:
The result looks like this
You could also encode the file or part of the file with base64 and use that as an environment variable, since you get a single string, which is easily processable, out of it. For example:
Or with set env which you could further combine with dry run if required.
Another thing is, that YAML is a superset of JSON, in many cases you are able to convert YAML to JSON or at least use JSON like syntax.
This can be useful in such a scenario in order to express this as a single line string rather than having to use multiline syntax. It's less fragile.
Every YAML parser will be able to parse JSON just fine. So if you are parsing the string in your app, you won't have problems.
Disclaimer, I created the above used tool tpl. As mentioned, you might as well use alternative tools such as yq.