Gautam Moulik
Gautam Moulik

Reputation: 95

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

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.

Upvotes: 1

Views: 20156

Answers (4)

viliokas
viliokas

Reputation: 1

To get deployed image name as a string, you can choose from any attribute in deployment yaml.

   kubectl get deploy/${image.name} -o jsonpath="{..image}"

Upvotes: 0

P Ekambaram
P Ekambaram

Reputation: 17631

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

Upvotes: 0

Shudipta Sharma
Shudipta Sharma

Reputation: 5862

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.

Upvotes: 2

Nitishkumar Singh
Nitishkumar Singh

Reputation: 1839

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.

Upvotes: 4

Related Questions