简体   繁体   English

如何在 kubernetes 中获取对象的元数据名称

[英]how to get objects's metadata name in kubernetes

I can get all the things of a list of objects, such as Secrets and ConfigMaps .我可以获得对象列表的所有内容,例如SecretsConfigMaps

{
"kind": "SecretList",
"apiVersion": "v1",
"metadata": {
    "selfLink": "/api/v1/namespaces/kube-system/secrets",
    "resourceVersion": "499638"
},
"items": [{
    "metadata": {
        "name": "aaa",
        "namespace": "kube-system",
        "selfLink": "/api/v1/namespaces/kube-system/secrets/aaa",
        "uid": "96b0fbee-f14c-423d-9734-53fed20ae9f9",
        "resourceVersion": "1354",
        "creationTimestamp": "2020-02-24T11:20:23Z"
    },
    "data": "aaa"
}]
}

but I only want the name list, for this example : "aaa" .但我只想要名称列表,例如: "aaa" Is there any way?有什么办法吗?

Yes, you can achieve it by using jsonpath output.是的,您可以通过使用jsonpath输出来实现它。 Note that the specification you posted will look quite differently once applied.请注意,您发布的规范一旦应用就会大不相同。 It will create one Secret object in your kube-system namespace and when you run:它将在您的kube-system命名空间中创建一个Secret对象,并在您运行时:

$ kubectl get secret -n kube-system aaa -o json

the output will look similar to the following:输出将类似于以下内容:

{
    "apiVersion": "v1",
    "kind": "Secret",
    "metadata": {
        "creationTimestamp": "2020-02-25T11:08:21Z",
        "name": "aaa",
        "namespace": "kube-system",
        "resourceVersion": "34488887",
        "selfLink": "/api/v1/namespaces/kube-system/secrets/aaa",
        "uid": "229edeb3-57bf-11ea-b366-42010a9c0093"
    },
    "type": "Opaque"
}

To get only the name of your Secret you need to run:要仅获取您的Secretname ,您需要运行:

kubectl get secret aaa -n kube-system -o jsonpath='{.metadata.name}'

i think this should work.我认为这应该有效。

kubectl get secretlist -o=jsonpath="{.items[*].metadata.name]}" | grep -v HEAD | head -n1

check link in the below for more info.查看下面的链接以获取更多信息。
https://kubernetes.io/docs/reference/kubectl/jsonpath/ https://kubernetes.io/docs/reference/kubectl/jsonpath/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM