我必须从Kubernetes集群的Kubernetes Map中提取每个数据,这些数据响应某些标准。我试着和jq做,但我不能克服它...
以下是我的K8S提取:
{
"apiVersion": "v1",
"items": [
{
"apiVersion": "v1",
"data": {
"DATA_1": false,
"DATA_2": "http://my-cluster-dns.my-service:8080",
"DATA_3": 200
},
"kind": "ConfigMap",
"metadata": {
"name": "cluster-agent-config",
"namespace": "my-namespace",
}
},
{
"apiVersion": "v1",
"data": {
"agent-log.yaml": "log-level: INFO\nmax-filesize-mb: 50\nmax-backups: 5\nwrite-to-stdout: true"
},
"kind": "ConfigMap",
"metadata": {
"name": "cluster-agent-log",
"namespace": "your-namespace",
}
},
{
"apiVersion": "v1",
"data": {
"agent-monitoring.yml": "metric-collection-interval-seconds: 30\ncluster-metric-collection-interval-seconds: 60\nmetadata-collection-interval-seconds: 60\ncontainer-registration-batch-size: 5\npod-registration-batch-size: 6\nmetric-upload-retry-count: 2\nmetric-upload-retry-interval-milliseconds: 5\nmax-pods-to-register-count: 750\nmax-pod-logs-tail-lines-count: 500\ninstrumentation-max-polling-attempts: 10\npod-filter: {}\nns-to-monitor-regex: \".*\""
},
"kind": "ConfigMap",
"metadata": {
"name": "cluster-agent-mon",
"namespace": "his-namespace",
}
},
{
"apiVersion": "v1",
"data": {
"DATA_1": "http://my-cluster-dns.my-service:9000",
"DATA_2": "something",
},
"kind": "ConfigMap",
"metadata": {
"name": "cluster-agent-misc",
"namespace": "her-namespace",
}
}
],
"kind": "List",
"metadata": {
"resourceVersion": ""
}
}
我想在输出(csv)中有这样的东西:
my-namespace,cluster-agent-config,DATA_2,http://my-cluster-dns.my-service:8080
her-namespace,cluster-agent-misc,DATA_1,http://my-cluster-dns.my-service:9000
这里的标准是“数据包含以http://my-cluster-dns开头的url”。
我获得了我正在寻找的值,但我丢失了其他元数据(和相应的键)。例如,使用此jq语句jq -r '.items[].data[] | select(contains("http://my-cluster-dns"))'
或者,我可以获取其他元数据,但过滤器也适用于它们(对于某些标准,这不能满足我的需求),以便使用此jq -r '.items[] | . as $item | .data[] | $item.metadata.name, $item.metadata.namespace, . | select(contains("http://my-cluster-dns"))'
进行匹配
最后,我想我接近这个:.items[] | . as $item | .data[] | [{ name: $item.metadata.name, namespace: $item.metadata.namespace, value: . }]
但我找不到正确的过滤方法。
1条答案
按热度按时间fhity93d1#
下面是一种使用
to_entries
访问键和值的方法,并将其存储在变量中供以后使用:Demo
注意:为了正确的转义(和引用),使用
@csv
而不是join(",")
。