jq를 사용하여 두 개의 JSON 객체를 결합하는 방법

jq를 사용하여 두 개의 JSON 객체를 결합하는 방법

두 개의 파일이 있습니다: kube-apiserver.json

{
    "apiVersion": "v1",
    "kind": "Pod",
    "metadata": {
        [...]
    },
    "spec": {
        "containers": [
            {
                "command": [
                    "kube-apiserver",
                    "--advertise-address=192.168.49.2",
                    "--allow-privileged=true",
                    "--authorization-mode=Node,RBAC",
                    "--client-ca-file=/var/lib/minikube/certs/ca.crt",
                    "--enable-admission-plugins=NamespaceLifecycle,LimitRanger,ServiceAccount,DefaultStorageClass,DefaultTolerationSeconds,NodeRestriction,MutatingAdmissionWebhook,ValidatingAdmissionWebhook,ResourceQuota",
                    "--enable-bootstrap-token-auth=true",
                    "--etcd-cafile=/var/lib/minikube/certs/etcd/ca.crt",
                    "--etcd-certfile=/var/lib/minikube/certs/apiserver-etcd-client.crt",
                    "--etcd-keyfile=/var/lib/minikube/certs/apiserver-etcd-client.key",
                    "--etcd-servers=https://127.0.0.1:2379",
                    "--kubelet-client-certificate=/var/lib/minikube/certs/apiserver-kubelet-client.crt",
                    "--kubelet-client-key=/var/lib/minikube/certs/apiserver-kubelet-client.key",
                    "--kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname",
                    "--proxy-client-cert-file=/var/lib/minikube/certs/front-proxy-client.crt",
                    "--proxy-client-key-file=/var/lib/minikube/certs/front-proxy-client.key",
                    "--requestheader-allowed-names=front-proxy-client",
                    "--requestheader-client-ca-file=/var/lib/minikube/certs/front-proxy-ca.crt",
                    "--requestheader-extra-headers-prefix=X-Remote-Extra-",
                    "--requestheader-group-headers=X-Remote-Group",
                    "--requestheader-username-headers=X-Remote-User",
                    "--secure-port=8443",
                    "--service-account-issuer=https://kubernetes.default.svc.cluster.local",
                    "--service-account-key-file=/var/lib/minikube/certs/sa.pub",
                    "--service-account-signing-key-file=/var/lib/minikube/certs/sa.key",
                    "--service-cluster-ip-range=10.96.0.0/12",
                    "--tls-cert-file=/var/lib/minikube/certs/apiserver.crt",
                    "--tls-private-key-file=/var/lib/minikube/certs/apiserver.key"
                ],
                [...]
                "volumeMounts": [
                    {
                        "mountPath": "/etc/ssl/certs",
                        "name": "ca-certs",
                        "readOnly": true
                    },
                    {
                        "mountPath": "/etc/ca-certificates",
                        "name": "etc-ca-certificates",
                        "readOnly": true
                    },
                    {
                        "mountPath": "/var/lib/minikube/certs",
                        "name": "k8s-certs",
                        "readOnly": true
                    },
                    {
                        "mountPath": "/usr/local/share/ca-certificates",
                        "name": "usr-local-share-ca-certificates",
                        "readOnly": true
                    },
                    {
                        "mountPath": "/usr/share/ca-certificates",
                        "name": "usr-share-ca-certificates",
                        "readOnly": true
                    }
                ]
            }
        ],
        [...]
        "volumes": [
            {
                "hostPath": {
                    "path": "/etc/ssl/certs",
                    "type": "DirectoryOrCreate"
                },
                "name": "ca-certs"
            },
            {
                "hostPath": {
                    "path": "/etc/ca-certificates",
                    "type": "DirectoryOrCreate"
                },
                "name": "etc-ca-certificates"
            },
            {
                "hostPath": {
                    "path": "/var/lib/minikube/certs",
                    "type": "DirectoryOrCreate"
                },
                "name": "k8s-certs"
            },
            {
                "hostPath": {
                    "path": "/usr/local/share/ca-certificates",
                    "type": "DirectoryOrCreate"
                },
                "name": "usr-local-share-ca-certificates"
            },
            {
                "hostPath": {
                    "path": "/usr/share/ca-certificates",
                    "type": "DirectoryOrCreate"
                },
                "name": "usr-share-ca-certificates"
            }
        ]
    },
    "status": {
        [...]
    }
}

및 patch.json

{
    "apiVersion": "v1",
    "kind": "Pod",
    "metadata": {
    },
    "spec": {
        "containers": [
            {
                "command": [
                    "--audit-policy-file=/etc/kubernetes/audit-policy.yaml",
                    "--audit-log-path=/var/log/kubernetes/audit/audit.log"
                ],
                "volumeMounts": [
                    {
                        "mountPath": "/etc/kubernetes/audit-policy.yaml",
                        "name": "audit",
                        "readOnly": true
                    },
                    {
                        "mountPath": "/var/log/kubernetes/audit/",
                        "name": "audit-log",
                        "readOnly": true
                    }
                ]
            }
        ],
        "volumes": [
            {
                "hostPath": {
                    "path": "/etc/kubernetes/audit-policy.yaml",
                    "type": "FileOrCreate"
                },
                "name": "audit"
            },
            {
                "hostPath": {
                    "path": "/var/log/kubernetes/audit/",
                    "type": "DirectoryOrCreate"
                },
                "name": "audit-log"
            }
        ]
    },
    "status": {
    }
}

patch.json에서 프로젝트를 실행하려고 하면 jq -s '.[0] * .[1]' kube-apiserver.json patch.json > patched-apiserver.jsonkube-apiserver.json의 프로젝트를 덮어쓰므로 다음과 같습니다.

{
  "apiVersion": "v1",
  "kind": "Pod",
  "metadata": {
    [...]
  "spec": {
    "containers": [
      {
        "command": [
          "--audit-policy-file=/etc/kubernetes/audit-policy.yaml",
          "--audit-log-path=/var/log/kubernetes/audit/audit.log"
        ],
        "volumeMounts": [
          {
            "mountPath": "/etc/kubernetes/audit-policy.yaml",
            "name": "audit",
            "readOnly": true
          },
          {
            "mountPath": "/var/log/kubernetes/audit/",
            "name": "audit-log",
            "readOnly": true
          }
        ]
      }
    ],
    [..].
    "volumes": [
      {
        "hostPath": {
          "path": "/etc/kubernetes/audit-policy.yaml",
          "type": "FileOrCreate"
        },
        "name": "audit"
      },
      {
        "hostPath": {
          "path": "/var/log/kubernetes/audit/",
          "type": "DirectoryOrCreate"
        },
        "name": "audit-log"
      }
    ]
  },
  "status": {
    [...]
  }
}

내 파일이 다음과 같기를 원합니다.

{
    "apiVersion": "v1",
    "kind": "Pod",
    "metadata": {
        [...]
    },
    "spec": {
        "containers": [
            {
                "command": [
                    "kube-apiserver",
                    "--advertise-address=192.168.49.2",
                    "--allow-privileged=true",
                    "--authorization-mode=Node,RBAC",
                    "--client-ca-file=/var/lib/minikube/certs/ca.crt",
                    "--enable-admission-plugins=NamespaceLifecycle,LimitRanger,ServiceAccount,DefaultStorageClass,DefaultTolerationSeconds,NodeRestriction,MutatingAdmissionWebhook,ValidatingAdmissionWebhook,ResourceQuota",
                    "--enable-bootstrap-token-auth=true",
                    "--etcd-cafile=/var/lib/minikube/certs/etcd/ca.crt",
                    "--etcd-certfile=/var/lib/minikube/certs/apiserver-etcd-client.crt",
                    "--etcd-keyfile=/var/lib/minikube/certs/apiserver-etcd-client.key",
                    "--etcd-servers=https://127.0.0.1:2379",
                    "--kubelet-client-certificate=/var/lib/minikube/certs/apiserver-kubelet-client.crt",
                    "--kubelet-client-key=/var/lib/minikube/certs/apiserver-kubelet-client.key",
                    "--kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname",
                    "--proxy-client-cert-file=/var/lib/minikube/certs/front-proxy-client.crt",
                    "--proxy-client-key-file=/var/lib/minikube/certs/front-proxy-client.key",
                    "--requestheader-allowed-names=front-proxy-client",
                    "--requestheader-client-ca-file=/var/lib/minikube/certs/front-proxy-ca.crt",
                    "--requestheader-extra-headers-prefix=X-Remote-Extra-",
                    "--requestheader-group-headers=X-Remote-Group",
                    "--requestheader-username-headers=X-Remote-User",
                    "--secure-port=8443",
                    "--service-account-issuer=https://kubernetes.default.svc.cluster.local",
                    "--service-account-key-file=/var/lib/minikube/certs/sa.pub",
                    "--service-account-signing-key-file=/var/lib/minikube/certs/sa.key",
                    "--service-cluster-ip-range=10.96.0.0/12",
                    "--tls-cert-file=/var/lib/minikube/certs/apiserver.crt",
                    "--tls-private-key-file=/var/lib/minikube/certs/apiserver.key",
                    "--audit-policy-file=/etc/kubernetes/audit-policy.yaml",
                    "--audit-log-path=/var/log/kubernetes/audit/audit.log"
                ],
                [...]
                "volumeMounts": [
                    {
                        "mountPath": "/etc/ssl/certs",
                        "name": "ca-certs",
                        "readOnly": true
                    },
                    {
                        "mountPath": "/etc/ca-certificates",
                        "name": "etc-ca-certificates",
                        "readOnly": true
                    },
                    {
                        "mountPath": "/var/lib/minikube/certs",
                        "name": "k8s-certs",
                        "readOnly": true
                    },
                    {
                        "mountPath": "/usr/local/share/ca-certificates",
                        "name": "usr-local-share-ca-certificates",
                        "readOnly": true
                    },
                    {
                        "mountPath": "/usr/share/ca-certificates",
                        "name": "usr-share-ca-certificates",
                        "readOnly": true
                    },
                    {
                        "mountPath": "/etc/kubernetes/audit-policy.yaml",
                        "name": "audit",
                        "readOnly": true
                    },
                    {
                        "mountPath": "/var/log/kubernetes/audit/",
                        "name": "audit-log",
                        "readOnly": true
                    }
                ]
            }
        ],
        [...]
        "volumes": [
            {
                "hostPath": {
                    "path": "/etc/ssl/certs",
                    "type": "DirectoryOrCreate"
                },
                "name": "ca-certs"
            },
            {
                "hostPath": {
                    "path": "/etc/ca-certificates",
                    "type": "DirectoryOrCreate"
                },
                "name": "etc-ca-certificates"
            },
            {
                "hostPath": {
                    "path": "/var/lib/minikube/certs",
                    "type": "DirectoryOrCreate"
                },
                "name": "k8s-certs"
            },
            {
                "hostPath": {
                    "path": "/usr/local/share/ca-certificates",
                    "type": "DirectoryOrCreate"
                },
                "name": "usr-local-share-ca-certificates"
            },
            {
                "hostPath": {
                    "path": "/usr/share/ca-certificates",
                    "type": "DirectoryOrCreate"
                },
                "name": "usr-share-ca-certificates"
            },
            {
                "hostPath": {
                    "path": "/etc/kubernetes/audit-policy.yaml",
                    "type": "FileOrCreate"
                },
                "name": "audit"
            },
            {
                "hostPath": {
                    "path": "/var/log/kubernetes/audit/",
                    "type": "DirectoryOrCreate"
                },
                "name": "audit-log"
            }
        ]
    },
    "status": {
        [...]
    }
}

jq/python/bash/무엇이든 해결하는 방법을 아는 사람이 있나요?

관련 정보