无法将名称空间字段添加到RoleBinding

时间:2018-08-22 07:07:04

标签: kubernetes rbac

我想像这样从kube-system文件中的命名空间MyRoleBinding.yaml添加角色:

kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata: 
  name: myrolebinding
  namespace: default
subjects: 
- kind: ServiceAccount 
  name: myservice
  namespace: default
  apiGroup: ""
roleRef: 
  kind: Role
  name: system:controller:token-cleaner
  namespace: kube-system
  apiGroup: "" 

但是当我运行kubectl apply -f MyRoleBinding.yaml时,我得到:

  

错误:验证“ MyRoleBinding.yaml”时出错:验证数据时出错:   ValidationError(RoleBinding.roleRef):未知字段中的“命名空间”   io.k8s.api.rbac.v1.RoleRef;如果您选择忽略这些错误,请转   通过--validate = false

进行验证

我正在default命名空间中运行,是因为这个原因吗?
我尝试跑步:
kubectl apply -f MyRoleBinding.yaml --namespace=kube-system,但出现相同的错误。

我还尝试使用以下方法在defaul名称空间中添加现有角色:

roleRef: 
  kind: Role
  name: read-pods
  namespace: default
  apiGroup: "" 

,我遇到了同样的错误。

2 个答案:

答案 0 :(得分:1)

RoleRef不支持名称空间子句,摘自source code

// RoleRef contains information that points to the role being used
type RoleRef struct {
    // APIGroup is the group for the resource being referenced
    APIGroup string `json:"apiGroup" protobuf:"bytes,1,opt,name=apiGroup"`
    // Kind is the type of resource being referenced
    Kind string `json:"kind" protobuf:"bytes,2,opt,name=kind"`
    // Name is the name of resource being referenced
    Name string `json:"name" protobuf:"bytes,3,opt,name=name"`
}

答案 1 :(得分:1)

roleRef字段不支持名称空间。您可以将roleRef与未命名空间的ClusterRoles或必须始终与RoleBinding位于相同命名空间的Roles一起使用。另请参见reference

相关问题