我可以使用 kubectl 查看集群角色应用于哪些主题,例如:
kubectl get clusterrolebindings system:node --all-namespaces -o json
{
"apiVersion": "rbac.authorization.k8s.io/v1beta1",
"kind": "ClusterRoleBinding",
....
....
"subjects": [
{
"apiGroup": "rbac.authorization.k8s.io",
"kind": "Group",
"name": "system:nodes"
}
]
}
我想以相反的方式获取此信息,例如:我想列出应用于“system:nodes”主题的所有策略。
我怎样才能做到这一点?
最佳答案
没有用于反向索引的 API。您可以查找绑定(bind)并过滤包含预期主题的绑定(bind)。例如,使用 bash、jq 和 kubectl:
# $1 is kind (User, Group, ServiceAccount)
# $2 is name ("system:nodes", etc)
# $3 is namespace (optional, only applies to kind=ServiceAccount)
function getRoles() {
local kind="${1}"
local name="${2}"
local namespace="${3:-}"
kubectl get clusterrolebinding -o json | jq -r "
.items[]
|
select(
.subjects[]?
|
select(
.kind == \"${kind}\"
and
.name == \"${name}\"
and
(if .namespace then .namespace else \"\" end) == \"${namespace}\"
)
)
|
(.roleRef.kind + \"/\" + .roleRef.name)
"
}
$ getRoles Group system:authenticated
ClusterRole/system:basic-user
ClusterRole/system:discovery
$ getRoles ServiceAccount attachdetach-controller kube-system
ClusterRole/system:controller:attachdetach-controller
关于kubernetes - 分配给主题的 kubectl 和查看(集群)角色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43186611/