如何为nodeSelector获取JsonPath

时间:2018-09-11 21:46:11

标签: kubernetes kubectl

是否可以从kubectl命令行获取用于nodeSelector关键字的JsonPath?

如果我运行kubectl explain deployment --recursive命令,它将输出所有关键字的列表:

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>
...

此列表很长,关键字为nodeSelector,很难看到JsonPath是spec.template.spec.nodeSelector

1 个答案:

答案 0 :(得分:0)

其中一种方法是将输出转换为正确的YAML格式,如下所示: (我已经完成了几次替换和删除操作以获得此输出。不幸的是,kubectl没有实现它的本机选项。)

   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'
...

现在,您可以应用this stackoverflow问题中的解决方案来获得平坦的输出,如下所示:

{
  "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",
...
}

然后,您可以过滤或搜索结果以获取所需的值。