How to get Deployments ($ kubectl get deployments) by specifying custom property value?

2/7/2019

I have deployed a Kubernetes service and when I query to get the Deployment $ kubectl get deployments, I can see the Deployment. the json of the Deployment looks like below --

apiVersion: v1
kind: Deployment
metadata:
 name: test
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: test
        release: testRelease
        customProp: xyz

My question is how can I frame a query by which I can get the Deployment by specifying the 'customProp' value. Does kubectl support to pass a jsonpath as part of the query? so that I can pass a json path like jsonpath='{$.spec.template.metadata.labels.customProp}' and value against this jsonPath as 'xyz'.

This is what I am thinking to execute:

$ kubectl get deployments -n <namespace> <json path query>

However not sure how to frame the json path query and pass along with $kubectl get deployments.

-- Gautam Moulik
kubectl
kubernetes

3 Answers

2/7/2019

Add a label to your deployment object. Then with below command to query specific deployment
kubectl get deploy - l labelname=labelvalue

-- P Ekambaram
Source: StackOverflow

2/7/2019

Kubectl does support query feature, you can use below query

kubectl get pods --selector=customProp=xyz

Kubectl also supports JSON path expressions too, to get more details, follow the link. You can write query following the syntax shown on the link.

-- Nitishkumar Singh
Source: StackOverflow

2/7/2019

Yes, one can query to the kube-apiserver for a resource using jsonpath. Run following command to get what you want:

$ kubectl get deploy test -o=jsonpath='{.spec.template.metadata.labels.customProp}'

For more usage, see https://kubernetes.io/docs/reference/kubectl/jsonpath.

-- Shudipta Sharma
Source: StackOverflow