【问题标题】:kubectl and seeing (cluster)roles assigned to subjects分配给主题的 kubectl 和查看(集群)角色
【发布时间】:2017-08-28 10:28:44
【问题描述】:

我可以使用 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”主题的所有策略。

我该怎么做?

【问题讨论】:

    标签: kubernetes kubectl


    【解决方案1】:

    没有用于反向索引的 API。您可以查找绑定并过滤包含预期主题的绑定。例如,使用 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
    

    【讨论】:

    猜你喜欢
    • 2020-08-30
    • 1970-01-01
    • 2023-02-14
    • 2017-02-13
    • 2022-08-19
    • 2022-01-23
    • 1970-01-01
    • 2020-11-13
    • 2019-08-28
    相关资源
    最近更新 更多