How to extract Kubernestes PODS mac addresses from annotations object

5/24/2020

I am trying to extract the mac or ips addresses under metadata.annotations using either kubectl get po in json filter or using jq. other objects are easy to manipulate to get those values.

kubectl get po -o json -n multus|jq -r .items Under annotations, there is duplication CNI info but it is ok. I like to extract those MAC addresses using jq. it seems to be tricky on this one.

[
  {
    "apiVersion": "v1",
    "kind": "Pod",
    "metadata": {
      "annotations": {
        "k8s.v1.cni.cncf.io/network-status": "[{\n    \"name\": \"eps-cni\",\n    \"ips\": [\n        \"172.31.83.216\"\n    ],\n    \"default\": true,\n    \"dns\": {}\n},{\n    \"name\": \"ipvlan1-busybox1\",\n    \"interface\": \"net1\",\n    \"ips\": [\n        \"172.31.230.70\"\n    ],\n    \"mac\": \"0a:2d:40:c6:f8:ea\",\n    \"dns\": {}\n},{\n    \"name\": \"ipvlan2-busybox1\",\n    \"interface\": \"net2\",\n    \"ips\": [\n        \"172.31.232.70\"\n    ],\n    \"mac\": \"0a:52:8a:62:5d:f4\",\n    \"dns\": {}\n}]",
        "k8s.v1.cni.cncf.io/networks": "ipvlan1-busybox1, ipvlan2-busybox1",
        "k8s.v1.cni.cncf.io/networks-status": "[{\n    \"name\": \"eps-cni\",\n    \"ips\": [\n        \"172.31.83.216\"\n    ],\n    \"default\": true,\n    \"dns\": {}\n},{\n    \"name\": \"ipvlan1-busybox1\",\n    \"interface\": \"net1\",\n    \"ips\": [\n        \"172.31.230.70\"\n    ],\n    \"mac\": \"0a:2d:40:c6:f8:ea\",\n    \"dns\": {}\n},{\n    \"name\": \"ipvlan2-busybox1\",\n    \"interface\": \"net2\",\n    \"ips\": [\n        \"172.31.232.70\"\n    ],\n    \"mac\": \"0a:52:8a:62:5d:f4\",\n    \"dns\": {}\n}]",
        "kubernetes.io/psp": "eps.privileged"
      },
      "creationTimestamp": "2020-05-24T17:09:10Z",
      "generateName": "busybox1-f476958bd-",
      "labels": {
        "app": "busybox",
        "pod-template-hash": "f476958bd"
      },
      "name": "busybox1-f476958bd-hds4w",
      "namespace": "multus",
      "ownerReferences": [
        {
          "apiVersion": "apps/v1",
          "blockOwnerDeletion": true,
          "controller": true,
          "kind": "ReplicaSet",
          "name": "busybox1-f476958bd",
          "uid": "5daf9b52-e1b3-4df7-b5a1-028b48e7fcc0"
        }
      ],
      "resourceVersion": "965176",
      "selfLink": "/api/v1/namespaces/multus/pods/busybox1-f476958bd-hds4w",
      "uid": "0051b85d-9774-4f89-8658-f34065222bf0"
    },

for basic jq,

[root@ip-172-31-103-214 ~]# kubectl get po -o json -n multus|jq -r '.items[] | .spec.volumes'
[
  {
    "name": "test-busybox1-token-f6bdj",
    "secret": {
      "defaultMode": 420,
      "secretName": "test-busybox1-token-f6bdj"
    }
  }
]

I can switch the get pod to yaml format then using normal grep cmd.

kubectl get po -o yaml  -n multus|egrep 'mac'|sort -u
"mac": "0a:2d:40:c6:f8:ea",
"mac": "0a:52:8a:62:5d:f4",

Thanks

-- AndrewS
annotations
jq
kubernetes

2 Answers

5/24/2020

Please try the below command and should get the expected output.

cat abc.json | jq -r  '.metadata.annotations."k8s.v1.cni.cncf.io/networks-status" | fromjson | .[].mac ' 

where abc.json is your son file.

-- nischay goyal
Source: StackOverflow

5/24/2020

Starting with the original JSON and using jq's -r command-line option, the following jq filter yields the output shown below:

.[]
| .metadata.annotations[]
| (fromjson? // empty)
| .[]
| select(has("mac"))
| {mac}

Output:

{"mac":"0a:2d:40:c6:f8:ea"}
{"mac":"0a:52:8a:62:5d:f4"}
{"mac":"0a:2d:40:c6:f8:ea"}
{"mac":"0a:52:8a:62:5d:f4"}
-- peak
Source: StackOverflow