kubernetes python client get value of a specific annotation

1/29/2019

I'm trying to get the value of a node annotation with kubernetes python client.

This code print all the annotations for nodes with etcd nodes :

#!/usr/bin/python

from kubernetes import client, config

def main():
    config.load_kube_config("./backup_kubeconfig_prod")
    label_selector = 'node-role.kubernetes.io/etcd'

    v1 = client.CoreV1Api()
    print("Listing nodes with their IPs:")
    ret = v1.list_node(watch=False, label_selector=label_selector)

    for i in ret.items:
      print(i.metadata.annotations)

if __name__ == '__main__':
    main()

Output example :

{'flannel.alpha.coreos.com/kube-subnet-manager': 'true', 'flannel.alpha.coreos.com/backend-type': 'vxlan', 'flannel.alpha.coreos.com/backend-data': '{"VtepMAC":"96:70:f6:ab:4f:30"}', 'rke.cattle.io/internal-ip': '1.2.3.4', 'volumes.kubernetes.io/controller-managed-attach-detach': 'true', 'flannel.alpha.coreos.com/public-ip': '1.2.3.4', 'rke.cattle.io/external-ip': '1.2.3.4', 'node.alpha.kubernetes.io/ttl': '0'}

How can I print the value of flannel.alpha.coreos.com/public-ip for example ?

-- Nicolas Pepinster
kubernetes
python
python-3.x

1 Answer

1/29/2019

Data in i.metadata.annotations are dictionary type.

You can print the value of the key flannel.alpha.coreos.com/public-ip using:

print(i.metadata.annotations["flannel.alpha.coreos.com/public-ip"])
-- arsho
Source: StackOverflow