How to get JsonPath for nodeSelector

9/11/2018

Is there a way to get JsonPath for nodeSelector key word from kubectl command line?

If I run the kubectl explain deployment --recursive command, it will output a list of all key words:

apiVersion  <string>
kind    <string>
metadata    <Object>
   annotations  <map[string]string>
   clusterName  <string>
   creationTimestamp    <string>
   deletionGracePeriodSeconds   <integer>
   deletionTimestamp    <string>
   finalizers   <[]string>
   generateName <string>
   generation   <integer>
   initializers <Object>
      pending   <[]Object>
         name   <string>
      result    <Object>
         apiVersion <string>
         code   <integer>
         details    <Object>
            causes  <[]Object>
               field    <string>
               message  <string>
               reason   <string>
            group   <string>
            kind    <string>
...

This list is quite long and with key word like nodeSelector it is hard to see that JsonPath is spec.template.spec.nodeSelector.

-- danielinclouds
kubectl
kubernetes

1 Answer

9/12/2018

One of the ways to do it is to convert output to correct YAML format like this: (I've done several replacements and deletions to get this output. Unfortunately, there are no native options for kubectl to achieve it.)

   apiVersion: 'string'
   kind: 'string'
   metadata:
      annotations: 'map[string]string'
      clusterName: 'string'
      creationTimestamp: 'string'
      deletionGracePeriodSeconds: 'integer'
      deletionTimestamp: 'string'
      finalizers: '[]string'
      generateName: 'string'
      generation: 'integer'
      initializers:
         pending:
            name: 'string'
         result:
            apiVersion: 'string'
            code: 'integer'
            details:
               causes:
                  field: 'string'
                  message: 'string'
                  reason: 'string'
...

Now you can apply the solution from this stackoverflow question to get flattened output like this:

{
  "apiVersion": "string",
  "kind": "string",
  "metadata.annotations": "map[string]string",
  "metadata.clusterName": "string",
  "metadata.creationTimestamp": "string",
  "metadata.deletionGracePeriodSeconds": "integer",
  "metadata.deletionTimestamp": "string",
  "metadata.finalizers": "[]string",
  "metadata.generateName": "string",
  "metadata.generation": "integer",
  "metadata.initializers.pending.name": "string",
  "metadata.initializers.result.apiVersion": "string",
  "metadata.initializers.result.code": "integer",
  "metadata.initializers.result.details.causes.field": "string",
  "metadata.initializers.result.details.causes.message": "string",
  "metadata.initializers.result.details.causes.reason": "string",
...
}

Then you can filter or search the result to get the values you want.

-- VAS
Source: StackOverflow