Kubernetes:如何按复杂键(例如node-role.kubernetes.io/general)排序?

huangapple go评论67阅读模式
英文:

Kubernetes: How to sort by complex keys (eg, node-role.kubernetes.io/general)?

问题

按照简单键进行排序很简单和容易。这两种方法都可以正常工作。

kubectl get nodes --sort-by=.metadata.name -o wide
kubectl get nodes --sort-by=.metadata.labels.affinity -o wide

我现在尝试按照复杂键进行排序,比如.metadata.labels."clm.status"或者更重要的.metadata.labels."kubernetes.io/roles"。

从功能上讲,我可以使用不同的方式按照kubernetes.io/roles进行排序,即排序加上其他Linux命令,所以请不要提供非K8s替代方案。我只想看到如何使用原生K8s *get命令*按照这些复杂键进行排序。

我尝试了许多排列组合,包括但不限于:

$ kubectl get nodes --sort-by=.metadata.labels.kubernetes.io.roles -o wide
未找到资源

$ kubectl get nodes --sort-by=.metadata.labels.'kubernetes.io.roles' -o wide
未找到资源

$ kubectl get nodes --sort-by=.metadata.labels.'kubernetes.io/roles' -o wide
未找到资源

$ kubectl get nodes --sort-by=.metadata.labels."kubernetes.io/roles" -o wide
未找到资源
英文:

Sorting by simple keys is straightforward and easy. Both of these work just fine.

kubectl get nodes --sort-by=.metadata.name -o wide
kubectl get nodes --sort-by=.metadata.labels.affinity -o wide

I'm trying to now sort by complex keys such as .metadata.labels."clm.status" or, more importantly, .metadata.labels."kubernetes.io/roles".

Functionally speaking, I am able to sort by the kubernetes.io/roles using a different way, ie, sort plus other linux commands, so plzzzzz don't give me non-k8s alternatives. I just want to see how to sort using native k8s get command with these complex keys.

I tried many permutations, including but not limited to:

$ kubectl get nodes --sort-by=.metadata.labels.kubernetes.io.roles -o wide
No resources found

$ kubectl get nodes --sort-by=.metadata.labels.'kubernetes.io.roles' -o wide
No resources found

$ kubectl get nodes --sort-by=.metadata.labels.'kubernetes.io/roles' -o wide
No resources found

$ kubectl get nodes --sort-by=.metadata.labels."kubernetes.io/roles" -o wide
No resources found

答案1

得分: 2

看起来解决方法是在标签中使用\转义点,例如:

kubectl get nodes \
  --sort=by=.metadata.labels.kubernetes\\.io/roles

或者如果您更喜欢,可以在外部使用单引号以避免不必要的转义转义字符:

kubectl get nodes \
  --sort-by='.metadata.labels.kubernetes\.io/roles'
英文:

It looks like the solution is to escape the dots in your label with \, so for example:

kubectl get nodes \
  --sort=by=.metadata.labels.kubernetes\\.io/roles

Or if you prefer, use single quotes on the outside to avoid having to escape the escape character:

kubectl get nodes \
  --sort-by='.metadata.labels.kubernetes\.io/roles`

huangapple
  • 本文由 发表于 2023年8月4日 08:04:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76832251.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定