Rad4
Rad4

Reputation: 2384

Extract a particular args value from Kubernetes Deployment object

I have the Kubernetes deployment object args displayed as below

kubectl get  deploy external-dns --output=jsonpath='{.spec.template.spec.containers[0].args}'

Output:

[--log-level=info --log-format=text --policy=sync --provider=aws --registry=txt --interval=1m --txt-owner-id=us-west-2:testcluster --source=service --source=ingress --aws-batch-change-size=1000]%  

I want to extract only this field --txt-owner-id=us-west-2:testcluster value..

The output I am expecting is (the value of that field),

us-west-2:testcluster

I am not sure if using sed or awk helps..Or if we have jsonpath itself for it.Please let me know.

Upvotes: 0

Views: 2171

Answers (2)

BertramLAU
BertramLAU

Reputation: 430

1: the simple way use awk and sed you can try this

kubectl get  deploy external-dns --output=jsonpath='{.spec.template.spec.containers[0].args}'  |awk -F"=" '{print $8}'|sed 's/--source//'

2: use grep:

kubectl get  deploy external-dns --output=jsonpath='{.spec.template.spec.containers[0].args}'|grep -oE  '\-\-txt-owner-id=(.*\:\w+){1}[[:space:]]'|sed 's/ //g'|awk -F"=" '{print $2}' 

Upvotes: 2

Rad4
Rad4

Reputation: 2384

Thankyou..I am not sure if print $8 is always txt-owner-id..

Your answer gave me this idea..

this works too..

kubectl get  deploy external-dns -n add-ons  --output=jsonpath='{.spec.template.spec.containers[0].args}' |awk -F"txt-owner-id=" '{print $2}'| cut -d " " -f1 

Upvotes: 0

Related Questions