Skip to content

Instantly share code, notes, and snippets.

@jovemfelix
Last active August 31, 2022 12:44
Show Gist options
  • Save jovemfelix/4ae5dcdcda0340e09c7bd776950e06a7 to your computer and use it in GitHub Desktop.
Save jovemfelix/4ae5dcdcda0340e09c7bd776950e06a7 to your computer and use it in GitHub Desktop.
Examples using jsonpath with Kubernetes or Openshift

Get JSON

$ oc get backup -o json

JSON to be parsed

{
    "apiVersion": "v1",
    "items": [
        {
            "apiVersion": "velero.io/v1",
            "kind": "Backup",
            "metadata": {
                "annotations": {
                    "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"velero.io/v1\",\"kind\":\"Backup\",\"metadata\":{\"annotations\":{},\"name\":\"mysql-bkp-1\",\"namespace\":\"rfelix-oadp\"},\"spec\":{\"includedNamespaces\":[\"rfelix-mysql\"],\"snapshotVolumes\":true,\"ttl\":\"720h0m0s\"}}\n",
                    "velero.io/source-cluster-k8s-gitversion": "v1.23.5+3afdacb",
                    "velero.io/source-cluster-k8s-major-version": "1",
                    "velero.io/source-cluster-k8s-minor-version": "23"
                },
                "creationTimestamp": "2022-08-30T20:32:29Z",
                "generation": 5,
                "labels": {
                    "velero.io/storage-location": "velero-netapp-1"
                },
                "name": "mysql-bkp-1",
                "namespace": "rfelix-oadp",
                "resourceVersion": "269544630",
                "uid": "39c062e0-1f27-4a3e-9894-a4ed5a229181"
            },
            "spec": {
                "defaultVolumesToRestic": false,
                "includedNamespaces": [
                    "rfelix-mysql"
                ],
                "snapshotVolumes": true,
                "storageLocation": "velero-netapp-1",
                "ttl": "720h0m0s",
                "volumeSnapshotLocations": [
                    "velero-netapp-1"
                ]
            },
            "status": {
                "completionTimestamp": "2022-08-30T20:33:08Z",
                "expiration": "2022-09-29T20:32:29Z",
                "formatVersion": "1.1.0",
                "phase": "Completed",
                "progress": {
                    "itemsBackedUp": 116,
                    "totalItems": 116
                },
                "startTimestamp": "2022-08-30T20:32:29Z",
                "version": 1
            }
        },
        {
            "apiVersion": "velero.io/v1",
            "kind": "Backup",
            "metadata": {
                "annotations": {
                    "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"velero.io/v1\",\"kind\":\"Backup\",\"metadata\":{\"annotations\":{},\"name\":\"mysql-bkp-2\",\"namespace\":\"rfelix-oadp\"},\"spec\":{\"includedNamespaces\":[\"rfelix-mysql\"],\"snapshotVolumes\":true,\"ttl\":\"3h0m0s\"}}\n",
                    "velero.io/source-cluster-k8s-gitversion": "v1.23.5+3afdacb",
                    "velero.io/source-cluster-k8s-major-version": "1",
                    "velero.io/source-cluster-k8s-minor-version": "23"
                },
                "creationTimestamp": "2022-08-31T12:02:43Z",
                "generation": 5,
                "labels": {
                    "velero.io/storage-location": "velero-netapp-1"
                },
                "name": "mysql-bkp-2",
                "namespace": "rfelix-oadp",
                "resourceVersion": "271447329",
                "uid": "76ef3707-1e49-419f-ac24-5c2b1f53073f"
            },
            "spec": {
                "defaultVolumesToRestic": false,
                "includedNamespaces": [
                    "rfelix-mysql"
                ],
                "snapshotVolumes": true,
                "storageLocation": "velero-netapp-1",
                "ttl": "3h0m0s",
                "volumeSnapshotLocations": [
                    "velero-netapp-1"
                ]
            },
            "status": {
                "completionTimestamp": "2022-08-31T12:03:22Z",
                "expiration": "2022-08-31T15:02:43Z",
                "formatVersion": "1.1.0",
                "phase": "Completed",
                "progress": {
                    "itemsBackedUp": 65,
                    "totalItems": 65
                },
                "startTimestamp": "2022-08-31T12:02:43Z",
                "version": 1
            }
        }
    ],
    "kind": "List",
    "metadata": {
        "resourceVersion": "",
        "selfLink": ""
    }
}

Output using jsonpath

$ oc get backup -n ${OADP_NS} -o=jsonpath='{"name"}{"\t\t\t"}{"completionTimestamp"}{"\t"}{"expiration"}{"\t\t"}{"phase"}{"\t\t"}{"itemsBackedUp"}{"\t"}{"totalItems"}{"\t"}{"startTimestamp"}{"\n"}{range .items[*]}{.metadata.name}{"\t"}{.status.completionTimestamp}{"\t"}{.status.expiration}{"\t"}{.status.phase}{"\t"}{.status.progress.itemsBackedUp}{"\t\t"}{.status.progress.totalItems}{"\t\t"}{.status.startTimestamp}{"\n"}{end}'

name			completionTimestamp	expiration		phase		itemsBackedUp	totalItems	startTimestamp
mysql-bkp-1	2022-08-30T20:33:08Z	2022-09-29T20:32:29Z	Completed	116		116		2022-08-30T20:32:29Z
mysql-bkp-2	2022-08-31T12:03:22Z	2022-08-31T15:02:43Z	Completed	65		65		2022-08-31T12:02:43Z

Output using jsonpath-file

create template

$ cat <<EOF >template.tpl
{"name"}{"\t\t\t"}{"completionTimestamp"}{"\t"}{"expiration"}{"\t\t"}{"phase"}{"\t\t"}{"itemsBackedUp"}{"\t"}{"totalItems"}{"\t"}{"startTimestamp"}
{range .items[*]}
{.metadata.name}{"\t"}{.status.completionTimestamp}{"\t"}{.status.expiration}{"\t"}{.status.phase}{"\t"}{.status.progress.itemsBackedUp}{"\t\t"}{.status.progress.totalItems}{"\t\t"}{.status.startTimestamp}{end}
EOF

run using template file

$ oc get backup -n ${OADP_NS} -o jsonpath-file=template.tpl
name			completionTimestamp	expiration		phase		itemsBackedUp	totalItems	startTimestamp

mysql-bkp-1	2022-08-30T20:33:08Z	2022-09-29T20:32:29Z	Completed	116		116		2022-08-30T20:32:29Z
mysql-bkp-2	2022-08-31T12:03:22Z	2022-08-31T15:02:43Z	Completed	65		65		2022-08-31T12:02:43Z

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment