如何使用 oc 客户端从 configmap 中提取特定值
how to extract specific value from a configmap using oc client
我的配置图如下所示:
apiVersion: v1
data:
my-data.yaml |2-
#data comes here
kind: ConfigMap
是否可以通过
提取my-data.yaml
键的内容
oc get configmap
或任何其他 oc
命令?
例如
oc get configmap myconfigmap -o=yaml <[only my-data.yaml]>
没有。在 kube 看来,这只是一个长字符串。不过,您可以使用 json 路径输出模式来过滤到一个值。然后用 jq 或 yq 解析它。或者只使用 jq 两次 :)
有一些 shell 解决方法来解析 yaml
个文件:
yq
您可以使用 yq
,一个构建在 jq
之上的命令行 YAML 处理器。
您可以在 http://mikefarah.github.io/yq/ 下载它并查找文档。
niet
Another tool is openuado/niet
Niet is like xmllint or jq but for YAML and JSON data - you can use it to slice and filter and map and transform structured data.
You can easily retrieve data by using simple expressions or using xpath advanced features to access non-trivial data.
You can easily convert YAML format into JSON format and vice versa.
bash-yaml
纯 bash
纯bash
可以试试:
jasperes/bash-yaml
Read a yaml file and create variables in bash
简单的 41 行 bash script 仅使用 sed
和 awk
来解析 yaml
文件并从中创建变量。
mrbaseman/parse_yaml
parse_yaml
provides a bash function that allows parsing simple YAML files. The output is shell code that defines shell variables which contain the parsed values. bash
doesn't support multidimensional arrays. Therefore a separate variable is created for each value, and the name of the variable consists of the names of all levels in the yaml file, glued together with a separator character which defaults to _
我想演示一个示例命令,"coderanger" 之前提到过。
此示例从 yaml 转换为 json,然后使用 "jq" 命令过滤“.keyname”。
您也可以使用 "yq" 命令代替 python 一行和 jq 组合。
oc get configmap/myconfigmap \
-o "jsonpath={ .data['my-data\.yaml']}" | \
python -c 'import sys, yaml, json; y=yaml.load(sys.stdin.read()); print json.dumps(y)' | \
jq '. | .keyname'
希望对你有所帮助
我的配置图如下所示:
apiVersion: v1
data:
my-data.yaml |2-
#data comes here
kind: ConfigMap
是否可以通过
提取my-data.yaml
键的内容
oc get configmap
或任何其他 oc
命令?
例如
oc get configmap myconfigmap -o=yaml <[only my-data.yaml]>
没有。在 kube 看来,这只是一个长字符串。不过,您可以使用 json 路径输出模式来过滤到一个值。然后用 jq 或 yq 解析它。或者只使用 jq 两次 :)
有一些 shell 解决方法来解析 yaml
个文件:
yq
您可以使用 yq
,一个构建在 jq
之上的命令行 YAML 处理器。
您可以在 http://mikefarah.github.io/yq/ 下载它并查找文档。
niet
Another tool is openuado/niet
Niet is like xmllint or jq but for YAML and JSON data - you can use it to slice and filter and map and transform structured data.
You can easily retrieve data by using simple expressions or using xpath advanced features to access non-trivial data.
You can easily convert YAML format into JSON format and vice versa.
bash-yaml
纯 bash
纯bash
可以试试:
jasperes/bash-yaml
Read a yaml file and create variables in bash
简单的 41 行 bash script 仅使用 sed
和 awk
来解析 yaml
文件并从中创建变量。
mrbaseman/parse_yaml
parse_yaml
provides a bash function that allows parsing simple YAML files. The output is shell code that defines shell variables which contain the parsed values.bash
doesn't support multidimensional arrays. Therefore a separate variable is created for each value, and the name of the variable consists of the names of all levels in the yaml file, glued together with a separator character which defaults to_
我想演示一个示例命令,"coderanger" 之前提到过。
此示例从 yaml 转换为 json,然后使用 "jq" 命令过滤“.keyname”。 您也可以使用 "yq" 命令代替 python 一行和 jq 组合。
oc get configmap/myconfigmap \
-o "jsonpath={ .data['my-data\.yaml']}" | \
python -c 'import sys, yaml, json; y=yaml.load(sys.stdin.read()); print json.dumps(y)' | \
jq '. | .keyname'
希望对你有所帮助