我必须从Kubernetes集群的配置映射中提取符合特定标准的每个数据.我试着和JQ一起做这件事,但我无法克服.
以下是我的K8摘录:
{
"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: . }]
,但我找不到正确过滤的方法.