Merge pull request #36568 from xilabao/add-label-to-rbac-bootstrap-policy

Automatic merge from submit-queue

add default label to rbac bootstrap policy

allow people to retrieve information of bootstrap policy by label :

`kubectl get clusterroles -l key=value` 
`kubectl get clusterrolebindings -l key=value`
pull/6/head
Kubernetes Submit Queue 2016-12-07 06:13:47 -08:00 committed by GitHub
commit 28df1d948f
8 changed files with 174 additions and 3 deletions

View File

@ -32,6 +32,7 @@ go_test(
deps = [ deps = [
"//pkg/api:go_default_library", "//pkg/api:go_default_library",
"//pkg/api/install:go_default_library", "//pkg/api/install:go_default_library",
"//pkg/api/meta:go_default_library",
"//pkg/api/v1:go_default_library", "//pkg/api/v1:go_default_library",
"//pkg/apis/rbac:go_default_library", "//pkg/apis/rbac:go_default_library",
"//pkg/apis/rbac/install:go_default_library", "//pkg/apis/rbac/install:go_default_library",
@ -50,5 +51,8 @@ go_test(
srcs = ["controller_policy_test.go"], srcs = ["controller_policy_test.go"],
library = "go_default_library", library = "go_default_library",
tags = ["automanaged"], tags = ["automanaged"],
deps = ["//pkg/util/sets:go_default_library"], deps = [
"//pkg/api/meta:go_default_library",
"//pkg/util/sets:go_default_library",
],
) )

View File

@ -46,8 +46,11 @@ func addControllerRole(role rbac.ClusterRole) {
} }
controllerRoles = append(controllerRoles, role) controllerRoles = append(controllerRoles, role)
addClusterRoleLabel(controllerRoles)
controllerRoleBindings = append(controllerRoleBindings, controllerRoleBindings = append(controllerRoleBindings,
rbac.NewClusterBinding(role.Name).SAs("kube-system", role.Name[len(saRolePrefix):]).BindingOrDie()) rbac.NewClusterBinding(role.Name).SAs("kube-system", role.Name[len(saRolePrefix):]).BindingOrDie())
addClusterRoleBindingLabel(controllerRoleBindings)
} }
func eventsRule() rbac.PolicyRule { func eventsRule() rbac.PolicyRule {

View File

@ -17,8 +17,10 @@ limitations under the License.
package bootstrappolicy package bootstrappolicy
import ( import (
"reflect"
"testing" "testing"
"k8s.io/kubernetes/pkg/api/meta"
"k8s.io/kubernetes/pkg/util/sets" "k8s.io/kubernetes/pkg/util/sets"
) )
@ -58,3 +60,29 @@ func TestNoStarsForControllers(t *testing.T) {
} }
} }
} }
func TestControllerRoleLabel(t *testing.T) {
roles := ControllerRoles()
for i := range roles {
role := roles[i]
accessor, err := meta.Accessor(&role)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got, want := accessor.GetLabels(), map[string]string{"kubernetes.io/bootstrapping": "rbac-defaults"}; !reflect.DeepEqual(got, want) {
t.Errorf("ClusterRole: %s GetLabels() = %s, want %s", accessor.GetName(), got, want)
}
}
rolebindings := ControllerRoleBindings()
for i := range rolebindings {
rolebinding := rolebindings[i]
accessor, err := meta.Accessor(&rolebinding)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got, want := accessor.GetLabels(), map[string]string{"kubernetes.io/bootstrapping": "rbac-defaults"}; !reflect.DeepEqual(got, want) {
t.Errorf("ClusterRoleBinding: %s GetLabels() = %s, want %s", accessor.GetName(), got, want)
}
}
}

View File

@ -25,6 +25,8 @@ import (
var ( var (
ReadWrite = []string{"get", "list", "watch", "create", "update", "patch", "delete", "deletecollection"} ReadWrite = []string{"get", "list", "watch", "create", "update", "patch", "delete", "deletecollection"}
Read = []string{"get", "list", "watch"} Read = []string{"get", "list", "watch"}
Label = map[string]string{"kubernetes.io/bootstrapping": "rbac-defaults"}
) )
const ( const (
@ -41,9 +43,33 @@ const (
storageGroup = "storage.k8s.io" storageGroup = "storage.k8s.io"
) )
func addClusterRoleLabel(roles []rbac.ClusterRole) {
for i := range roles {
if roles[i].ObjectMeta.Labels == nil {
roles[i].ObjectMeta.Labels = make(map[string]string)
}
for k, v := range Label {
roles[i].ObjectMeta.Labels[k] = v
}
}
return
}
func addClusterRoleBindingLabel(rolebindings []rbac.ClusterRoleBinding) {
for i := range rolebindings {
if rolebindings[i].ObjectMeta.Labels == nil {
rolebindings[i].ObjectMeta.Labels = make(map[string]string)
}
for k, v := range Label {
rolebindings[i].ObjectMeta.Labels[k] = v
}
}
return
}
// ClusterRoles returns the cluster roles to bootstrap an API server with // ClusterRoles returns the cluster roles to bootstrap an API server with
func ClusterRoles() []rbac.ClusterRole { func ClusterRoles() []rbac.ClusterRole {
return []rbac.ClusterRole{ roles := []rbac.ClusterRole{
{ {
// a "root" role which can do absolutely anything // a "root" role which can do absolutely anything
ObjectMeta: api.ObjectMeta{Name: "cluster-admin"}, ObjectMeta: api.ObjectMeta{Name: "cluster-admin"},
@ -204,15 +230,19 @@ func ClusterRoles() []rbac.ClusterRole {
}, },
}, },
} }
addClusterRoleLabel(roles)
return roles
} }
// ClusterRoleBindings return default rolebindings to the default roles // ClusterRoleBindings return default rolebindings to the default roles
func ClusterRoleBindings() []rbac.ClusterRoleBinding { func ClusterRoleBindings() []rbac.ClusterRoleBinding {
return []rbac.ClusterRoleBinding{ rolebindings := []rbac.ClusterRoleBinding{
rbac.NewClusterBinding("cluster-admin").Groups(user.SystemPrivilegedGroup).BindingOrDie(), rbac.NewClusterBinding("cluster-admin").Groups(user.SystemPrivilegedGroup).BindingOrDie(),
rbac.NewClusterBinding("system:discovery").Groups(user.AllAuthenticated, user.AllUnauthenticated).BindingOrDie(), rbac.NewClusterBinding("system:discovery").Groups(user.AllAuthenticated, user.AllUnauthenticated).BindingOrDie(),
rbac.NewClusterBinding("system:basic-user").Groups(user.AllAuthenticated, user.AllUnauthenticated).BindingOrDie(), rbac.NewClusterBinding("system:basic-user").Groups(user.AllAuthenticated, user.AllUnauthenticated).BindingOrDie(),
rbac.NewClusterBinding("system:node").Groups(user.NodesGroup).BindingOrDie(), rbac.NewClusterBinding("system:node").Groups(user.NodesGroup).BindingOrDie(),
rbac.NewClusterBinding("system:node-proxier").Groups(user.NodesGroup).BindingOrDie(), rbac.NewClusterBinding("system:node-proxier").Groups(user.NodesGroup).BindingOrDie(),
} }
addClusterRoleBindingLabel(rolebindings)
return rolebindings
} }

View File

@ -20,12 +20,14 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"reflect"
"testing" "testing"
"github.com/ghodss/yaml" "github.com/ghodss/yaml"
"k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/api"
_ "k8s.io/kubernetes/pkg/api/install" _ "k8s.io/kubernetes/pkg/api/install"
"k8s.io/kubernetes/pkg/api/meta"
"k8s.io/kubernetes/pkg/api/v1" "k8s.io/kubernetes/pkg/api/v1"
rbac "k8s.io/kubernetes/pkg/apis/rbac" rbac "k8s.io/kubernetes/pkg/apis/rbac"
_ "k8s.io/kubernetes/pkg/apis/rbac/install" _ "k8s.io/kubernetes/pkg/apis/rbac/install"
@ -233,3 +235,29 @@ func testObjects(t *testing.T, list *api.List, fixtureFilename string) {
} }
} }
} }
func TestClusterRoleLabel(t *testing.T) {
roles := bootstrappolicy.ClusterRoles()
for i := range roles {
role := roles[i]
accessor, err := meta.Accessor(&role)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got, want := accessor.GetLabels(), map[string]string{"kubernetes.io/bootstrapping": "rbac-defaults"}; !reflect.DeepEqual(got, want) {
t.Errorf("ClusterRole: %s GetLabels() = %s, want %s", accessor.GetName(), got, want)
}
}
rolebindings := bootstrappolicy.ClusterRoleBindings()
for i := range rolebindings {
rolebinding := rolebindings[i]
accessor, err := meta.Accessor(&rolebinding)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got, want := accessor.GetLabels(), map[string]string{"kubernetes.io/bootstrapping": "rbac-defaults"}; !reflect.DeepEqual(got, want) {
t.Errorf("ClusterRoleBinding: %s GetLabels() = %s, want %s", accessor.GetName(), got, want)
}
}
}

View File

@ -4,6 +4,8 @@ items:
kind: ClusterRole kind: ClusterRole
metadata: metadata:
creationTimestamp: null creationTimestamp: null
labels:
kubernetes.io/bootstrapping: rbac-defaults
name: admin name: admin
rules: rules:
- apiGroups: - apiGroups:
@ -170,6 +172,8 @@ items:
kind: ClusterRole kind: ClusterRole
metadata: metadata:
creationTimestamp: null creationTimestamp: null
labels:
kubernetes.io/bootstrapping: rbac-defaults
name: cluster-admin name: cluster-admin
rules: rules:
- apiGroups: - apiGroups:
@ -188,6 +192,8 @@ items:
kind: ClusterRole kind: ClusterRole
metadata: metadata:
creationTimestamp: null creationTimestamp: null
labels:
kubernetes.io/bootstrapping: rbac-defaults
name: edit name: edit
rules: rules:
- apiGroups: - apiGroups:
@ -332,6 +338,8 @@ items:
kind: ClusterRole kind: ClusterRole
metadata: metadata:
creationTimestamp: null creationTimestamp: null
labels:
kubernetes.io/bootstrapping: rbac-defaults
name: system:auth-delegator name: system:auth-delegator
rules: rules:
- apiGroups: - apiGroups:
@ -352,6 +360,8 @@ items:
kind: ClusterRole kind: ClusterRole
metadata: metadata:
creationTimestamp: null creationTimestamp: null
labels:
kubernetes.io/bootstrapping: rbac-defaults
name: system:basic-user name: system:basic-user
rules: rules:
- apiGroups: - apiGroups:
@ -365,6 +375,8 @@ items:
kind: ClusterRole kind: ClusterRole
metadata: metadata:
creationTimestamp: null creationTimestamp: null
labels:
kubernetes.io/bootstrapping: rbac-defaults
name: system:discovery name: system:discovery
rules: rules:
- attributeRestrictions: null - attributeRestrictions: null
@ -380,6 +392,8 @@ items:
kind: ClusterRole kind: ClusterRole
metadata: metadata:
creationTimestamp: null creationTimestamp: null
labels:
kubernetes.io/bootstrapping: rbac-defaults
name: system:node name: system:node
rules: rules:
- apiGroups: - apiGroups:
@ -484,6 +498,8 @@ items:
kind: ClusterRole kind: ClusterRole
metadata: metadata:
creationTimestamp: null creationTimestamp: null
labels:
kubernetes.io/bootstrapping: rbac-defaults
name: system:node-proxier name: system:node-proxier
rules: rules:
- apiGroups: - apiGroups:
@ -499,6 +515,8 @@ items:
kind: ClusterRole kind: ClusterRole
metadata: metadata:
creationTimestamp: null creationTimestamp: null
labels:
kubernetes.io/bootstrapping: rbac-defaults
name: view name: view
rules: rules:
- apiGroups: - apiGroups:

View File

@ -4,6 +4,8 @@ items:
kind: ClusterRoleBinding kind: ClusterRoleBinding
metadata: metadata:
creationTimestamp: null creationTimestamp: null
labels:
kubernetes.io/bootstrapping: rbac-defaults
name: system:controller:attachdetach-controller name: system:controller:attachdetach-controller
roleRef: roleRef:
apiGroup: rbac.authorization.k8s.io apiGroup: rbac.authorization.k8s.io
@ -17,6 +19,8 @@ items:
kind: ClusterRoleBinding kind: ClusterRoleBinding
metadata: metadata:
creationTimestamp: null creationTimestamp: null
labels:
kubernetes.io/bootstrapping: rbac-defaults
name: system:controller:cronjob-controller name: system:controller:cronjob-controller
roleRef: roleRef:
apiGroup: rbac.authorization.k8s.io apiGroup: rbac.authorization.k8s.io
@ -30,6 +34,8 @@ items:
kind: ClusterRoleBinding kind: ClusterRoleBinding
metadata: metadata:
creationTimestamp: null creationTimestamp: null
labels:
kubernetes.io/bootstrapping: rbac-defaults
name: system:controller:daemon-set-controller name: system:controller:daemon-set-controller
roleRef: roleRef:
apiGroup: rbac.authorization.k8s.io apiGroup: rbac.authorization.k8s.io
@ -43,6 +49,8 @@ items:
kind: ClusterRoleBinding kind: ClusterRoleBinding
metadata: metadata:
creationTimestamp: null creationTimestamp: null
labels:
kubernetes.io/bootstrapping: rbac-defaults
name: system:controller:deployment-controller name: system:controller:deployment-controller
roleRef: roleRef:
apiGroup: rbac.authorization.k8s.io apiGroup: rbac.authorization.k8s.io
@ -56,6 +64,8 @@ items:
kind: ClusterRoleBinding kind: ClusterRoleBinding
metadata: metadata:
creationTimestamp: null creationTimestamp: null
labels:
kubernetes.io/bootstrapping: rbac-defaults
name: system:controller:disruption-controller name: system:controller:disruption-controller
roleRef: roleRef:
apiGroup: rbac.authorization.k8s.io apiGroup: rbac.authorization.k8s.io
@ -69,6 +79,8 @@ items:
kind: ClusterRoleBinding kind: ClusterRoleBinding
metadata: metadata:
creationTimestamp: null creationTimestamp: null
labels:
kubernetes.io/bootstrapping: rbac-defaults
name: system:controller:endpoint-controller name: system:controller:endpoint-controller
roleRef: roleRef:
apiGroup: rbac.authorization.k8s.io apiGroup: rbac.authorization.k8s.io
@ -82,6 +94,8 @@ items:
kind: ClusterRoleBinding kind: ClusterRoleBinding
metadata: metadata:
creationTimestamp: null creationTimestamp: null
labels:
kubernetes.io/bootstrapping: rbac-defaults
name: system:controller:horizontal-pod-autoscaler name: system:controller:horizontal-pod-autoscaler
roleRef: roleRef:
apiGroup: rbac.authorization.k8s.io apiGroup: rbac.authorization.k8s.io
@ -95,6 +109,8 @@ items:
kind: ClusterRoleBinding kind: ClusterRoleBinding
metadata: metadata:
creationTimestamp: null creationTimestamp: null
labels:
kubernetes.io/bootstrapping: rbac-defaults
name: system:controller:job-controller name: system:controller:job-controller
roleRef: roleRef:
apiGroup: rbac.authorization.k8s.io apiGroup: rbac.authorization.k8s.io
@ -108,6 +124,8 @@ items:
kind: ClusterRoleBinding kind: ClusterRoleBinding
metadata: metadata:
creationTimestamp: null creationTimestamp: null
labels:
kubernetes.io/bootstrapping: rbac-defaults
name: system:controller:namespace-controller name: system:controller:namespace-controller
roleRef: roleRef:
apiGroup: rbac.authorization.k8s.io apiGroup: rbac.authorization.k8s.io
@ -121,6 +139,8 @@ items:
kind: ClusterRoleBinding kind: ClusterRoleBinding
metadata: metadata:
creationTimestamp: null creationTimestamp: null
labels:
kubernetes.io/bootstrapping: rbac-defaults
name: system:controller:persistent-volume-binder name: system:controller:persistent-volume-binder
roleRef: roleRef:
apiGroup: rbac.authorization.k8s.io apiGroup: rbac.authorization.k8s.io
@ -134,6 +154,8 @@ items:
kind: ClusterRoleBinding kind: ClusterRoleBinding
metadata: metadata:
creationTimestamp: null creationTimestamp: null
labels:
kubernetes.io/bootstrapping: rbac-defaults
name: system:controller:pod-garbage-controller name: system:controller:pod-garbage-controller
roleRef: roleRef:
apiGroup: rbac.authorization.k8s.io apiGroup: rbac.authorization.k8s.io
@ -147,6 +169,8 @@ items:
kind: ClusterRoleBinding kind: ClusterRoleBinding
metadata: metadata:
creationTimestamp: null creationTimestamp: null
labels:
kubernetes.io/bootstrapping: rbac-defaults
name: system:controller:replicaset-controller name: system:controller:replicaset-controller
roleRef: roleRef:
apiGroup: rbac.authorization.k8s.io apiGroup: rbac.authorization.k8s.io
@ -160,6 +184,8 @@ items:
kind: ClusterRoleBinding kind: ClusterRoleBinding
metadata: metadata:
creationTimestamp: null creationTimestamp: null
labels:
kubernetes.io/bootstrapping: rbac-defaults
name: system:controller:replication-controller name: system:controller:replication-controller
roleRef: roleRef:
apiGroup: rbac.authorization.k8s.io apiGroup: rbac.authorization.k8s.io
@ -173,6 +199,8 @@ items:
kind: ClusterRoleBinding kind: ClusterRoleBinding
metadata: metadata:
creationTimestamp: null creationTimestamp: null
labels:
kubernetes.io/bootstrapping: rbac-defaults
name: system:controller:service-controller name: system:controller:service-controller
roleRef: roleRef:
apiGroup: rbac.authorization.k8s.io apiGroup: rbac.authorization.k8s.io
@ -186,6 +214,8 @@ items:
kind: ClusterRoleBinding kind: ClusterRoleBinding
metadata: metadata:
creationTimestamp: null creationTimestamp: null
labels:
kubernetes.io/bootstrapping: rbac-defaults
name: system:controller:statefulset-controller name: system:controller:statefulset-controller
roleRef: roleRef:
apiGroup: rbac.authorization.k8s.io apiGroup: rbac.authorization.k8s.io

View File

@ -4,6 +4,8 @@ items:
kind: ClusterRole kind: ClusterRole
metadata: metadata:
creationTimestamp: null creationTimestamp: null
labels:
kubernetes.io/bootstrapping: rbac-defaults
name: system:controller:attachdetach-controller name: system:controller:attachdetach-controller
rules: rules:
- apiGroups: - apiGroups:
@ -53,6 +55,8 @@ items:
kind: ClusterRole kind: ClusterRole
metadata: metadata:
creationTimestamp: null creationTimestamp: null
labels:
kubernetes.io/bootstrapping: rbac-defaults
name: system:controller:cronjob-controller name: system:controller:cronjob-controller
rules: rules:
- apiGroups: - apiGroups:
@ -96,6 +100,8 @@ items:
kind: ClusterRole kind: ClusterRole
metadata: metadata:
creationTimestamp: null creationTimestamp: null
labels:
kubernetes.io/bootstrapping: rbac-defaults
name: system:controller:daemon-set-controller name: system:controller:daemon-set-controller
rules: rules:
- apiGroups: - apiGroups:
@ -152,6 +158,8 @@ items:
kind: ClusterRole kind: ClusterRole
metadata: metadata:
creationTimestamp: null creationTimestamp: null
labels:
kubernetes.io/bootstrapping: rbac-defaults
name: system:controller:deployment-controller name: system:controller:deployment-controller
rules: rules:
- apiGroups: - apiGroups:
@ -206,6 +214,8 @@ items:
kind: ClusterRole kind: ClusterRole
metadata: metadata:
creationTimestamp: null creationTimestamp: null
labels:
kubernetes.io/bootstrapping: rbac-defaults
name: system:controller:disruption-controller name: system:controller:disruption-controller
rules: rules:
- apiGroups: - apiGroups:
@ -252,6 +262,8 @@ items:
kind: ClusterRole kind: ClusterRole
metadata: metadata:
creationTimestamp: null creationTimestamp: null
labels:
kubernetes.io/bootstrapping: rbac-defaults
name: system:controller:endpoint-controller name: system:controller:endpoint-controller
rules: rules:
- apiGroups: - apiGroups:
@ -295,6 +307,8 @@ items:
kind: ClusterRole kind: ClusterRole
metadata: metadata:
creationTimestamp: null creationTimestamp: null
labels:
kubernetes.io/bootstrapping: rbac-defaults
name: system:controller:horizontal-pod-autoscaler name: system:controller:horizontal-pod-autoscaler
rules: rules:
- apiGroups: - apiGroups:
@ -361,6 +375,8 @@ items:
kind: ClusterRole kind: ClusterRole
metadata: metadata:
creationTimestamp: null creationTimestamp: null
labels:
kubernetes.io/bootstrapping: rbac-defaults
name: system:controller:job-controller name: system:controller:job-controller
rules: rules:
- apiGroups: - apiGroups:
@ -405,6 +421,8 @@ items:
kind: ClusterRole kind: ClusterRole
metadata: metadata:
creationTimestamp: null creationTimestamp: null
labels:
kubernetes.io/bootstrapping: rbac-defaults
name: system:controller:namespace-controller name: system:controller:namespace-controller
rules: rules:
- apiGroups: - apiGroups:
@ -439,6 +457,8 @@ items:
kind: ClusterRole kind: ClusterRole
metadata: metadata:
creationTimestamp: null creationTimestamp: null
labels:
kubernetes.io/bootstrapping: rbac-defaults
name: system:controller:persistent-volume-binder name: system:controller:persistent-volume-binder
rules: rules:
- apiGroups: - apiGroups:
@ -527,6 +547,8 @@ items:
kind: ClusterRole kind: ClusterRole
metadata: metadata:
creationTimestamp: null creationTimestamp: null
labels:
kubernetes.io/bootstrapping: rbac-defaults
name: system:controller:pod-garbage-controller name: system:controller:pod-garbage-controller
rules: rules:
- apiGroups: - apiGroups:
@ -542,6 +564,8 @@ items:
kind: ClusterRole kind: ClusterRole
metadata: metadata:
creationTimestamp: null creationTimestamp: null
labels:
kubernetes.io/bootstrapping: rbac-defaults
name: system:controller:replicaset-controller name: system:controller:replicaset-controller
rules: rules:
- apiGroups: - apiGroups:
@ -584,6 +608,8 @@ items:
kind: ClusterRole kind: ClusterRole
metadata: metadata:
creationTimestamp: null creationTimestamp: null
labels:
kubernetes.io/bootstrapping: rbac-defaults
name: system:controller:replication-controller name: system:controller:replication-controller
rules: rules:
- apiGroups: - apiGroups:
@ -626,6 +652,8 @@ items:
kind: ClusterRole kind: ClusterRole
metadata: metadata:
creationTimestamp: null creationTimestamp: null
labels:
kubernetes.io/bootstrapping: rbac-defaults
name: system:controller:service-controller name: system:controller:service-controller
rules: rules:
- apiGroups: - apiGroups:
@ -665,6 +693,8 @@ items:
kind: ClusterRole kind: ClusterRole
metadata: metadata:
creationTimestamp: null creationTimestamp: null
labels:
kubernetes.io/bootstrapping: rbac-defaults
name: system:controller:statefulset-controller name: system:controller:statefulset-controller
rules: rules:
- apiGroups: - apiGroups: