如何解析命令的命令行输出并返回特定值



我需要返回kubectl的输出,描述秘密命令输出作为rest响应。我首先使用支持-o json的kubectl get-seret以json形式返回输出。但kubectl describe-seret不支持这一点。我有低于输出

controller-0:~$ kubectl describe secret dashboard-admin-sa-token-blk2w
Name:         dashboard-admin-sa-token-blk2w
Namespace:    default
Labels:       <none>
Annotations:  kubernetes.io/service-account.name: dashboard-admin-sa
kubernetes.io/service-account.uid: aa96a4ec-290b-45cc-aba9-8296f1d67a6c
Type:  kubernetes.io/service-account-token
Data
====
ca.crt:     1090 bytes
namespace:  7 bytes
token:      eyJhbGciOiJSUzI1NiIsImtpZCI6InVnMmJya3pqSVgyRUo4VXJfVnFndXdhd04yR0lsYlYzbHVMUi1JTDVmdlEifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJkZWZhdWx0Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZWNyZXQubmFtZSI6ImRhc2hib2FyZC1hZG1pbi1zYS10b2tlbi1ibGsydyIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50Lm5hbWUiOiJkYXNoYm9hcmQtYWRtaW4tc2EiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC51aWQiOiJhYTk2YTRlYy0yOTBiLTQ1Y2MtYWJhOS04Mjk2ZjFkNjdhNmMiLCJzdWIiOiJzeXN0ZW06c2VydmljZWFjY291bnQ6ZGVmYXVsdDpkYXNoYm9hcmQtYWRtaW4tc2EifQ.xTRKhWvaVHGTrvhDQBIH5HUDUgwIyLJqmUWMhUP3QyrQ9LTVv4_47ssQaSOeQ3Qw_PQjuVME0BSWAos4IpVl0byGJPDi1GC56Vooyx8nXqKBcZ82hOg0CqKH7iXNL7_yg5tKR3MqZLjKpGMK8ac4KVKdITSrM4vFwasPYoxNhJaOmpSvkaVZf7A6Vyhp0XQjsB3ttfnL2XPhELNWonMj7qUvPvRJoyeoqkwG9Ah6FNC2vPTt14RXav7M5NA5hg5Zv4xSNFa0_qlM3W1XMdc0Tc5dslBoS3ew75MKd_69up8O_wxfVTGUOF-TRYQycd04mMumsn6nJSKsm2CglXIvNw
controller-0:~$

我只想获取token:(或任何其他类似Type:的变量(值。

有没有办法只得到某些值作为输出,比如dashboard-admin-sa-to-ken-blk2w或kubernetes.io/service-account-to-ken

您可以使用-o jsonpath和kubectl从kubectl get命令中获取特定值:

示例:

[root@machine ~]# kubectl get secret default-token-fd4wm
NAME                  TYPE                                  DATA   AGE
default-token-fd4wm   kubernetes.io/service-account-token   3      47d
  • 以json格式获取secret的所有值
[root@machine~]# kubectl get secret default-token-fd4wm -o json
{
"apiVersion": "v1",
"data": {
"ca.crt": "LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUMvakNDQWVhZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTVJNd0VRWURWUVFERXdwcmRXSmwKY201bGRHVnpNQjRYRFRJeU1ESXhOVEUwTkRZd09Gb1hEVE15TURJeE16RTBORFl3T0Zvd0ZURVRNQkVHQTFVRQpBeE1LYTNWaVpYSnVaWFJsY3pDQ0FTSXdEUVlKS29aSWh2Y05BUUVCQlFBRGdnRVBBRENDQVFvQ2dnRUJBS0MrCjJhNVV2LzJjMTZ6QkdnRzU5a1p1MW15aEM1V2VrWkUyc1RBSzFCWVBUNDdwdVErMmJwcGRCNnQyYnBsY00zMWwKaENZTDNDM0dXSHU4dElucEJtQlgyN3BBWW5TVlE2OTRYVmpjR0hjZDFVMXcvMTJDRnlLSDlSTjQ5RjVnT21SOAptWlQrZFN6cUYwWmc4cENsZEJiYUJMOXlqZU9CS1hCcmFiWDhONWNlTTZja284MjNRTHY3Z1hEU05aeTBjcFU5CmxLdjZkL2xCNHpDcnRXSlpvTjh4T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THBzTDZNU3l5Z0E1bVZR"
},
"kind": "Secret",
"metadata": {
"annotations": {
"kubernetes.io/service-account.name": "default",
"kubernetes.io/service-account.uid": "3799caff-d105-4a42-95c0-f5d77a79ef4b"
},
"creationTimestamp": "2022-02-15T14:46:30Z",
"name": "default-token-fd4wm",
"namespace": "default",
"resourceVersion": "376",
"uid": "2078ada0-c8a4-4926-b1a1-796e1a8fa12c"
},
"type": "kubernetes.io/service-account-token"
}
  • 从中获取特定值(例如ca.crt或token(
[root@machine ~]# kubectl get secret default-token-fd4wm -o jsonpath='{.data.ca.crt}'
LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUMvakNDQWVhZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTVJNd0VRWURWUVFERXdwcmRXSmwKY201bGRHVnpNQjRYRFRJeU1ESXhOVEUwTkRZd09Gb1hEVE15TURJeE16RTBORFl3T0Zvd0ZURVRNQkVHQTFVRQpBeE1LYTNWaVpYSnVaWFJsY3pDQ0FTSXdEUVlKS29aSWh2Y05BUUVCQlFBRGdnRVBBRENDQVFvQ2dnRUJBS0MrCjJhNVV2LzJjMTZ6QkdnRzU5a1p1MW15aEM1V2VrWkUyc1RBSzFCWVBUNDdwdVErMmJwcGRCNnQyYnBsY00zMWwKaENZTDNDM0dXSHU4dElucEJtQlgyN3BBWW5TVlE2OTRYVmpjR0hjZDFVMXcvMTJDRnlLSDlSTjQ5RjVnT21SOAptWlQrZFN6cUYwWmc4cENsZEJiYUJMOXlqZU9CS1hCcmFiWDhONWNlTTZja284MjNRTHY3Z1hEU05aeTBjcFU5CmxLdjZkL2xCNHpDcnRXSlpvTjh4TjFtRDJORUJDbDFibkh1K1pKNXdXSkVYcEVUaDNpdnczdnoxdVgvMXBRTGUKVUZ5QkNGc1pRdFM5NlB3MzdUL2V1RGNQWFVaMHVSc2hmZ1ZKM3RjNG1LYlpxc2pSc2JHNVlFRWoxQVFUV2RRbwpjZ1hzK3MxQ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