-
Notifications
You must be signed in to change notification settings - Fork 14
/
kube_rbac.go
134 lines (118 loc) · 3.09 KB
/
kube_rbac.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package op
import (
"context"
"github.com/cybozu-go/cke"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type kubeRBACRoleInstallOp struct {
apiserver *cke.Node
roleExists bool
bindingExists bool
}
// KubeRBACRoleInstallOp returns an Operator to install ClusterRole and binding for RBAC.
func KubeRBACRoleInstallOp(apiserver *cke.Node, roleExists bool) cke.Operator {
return &kubeRBACRoleInstallOp{
apiserver: apiserver,
roleExists: roleExists,
}
}
func (o *kubeRBACRoleInstallOp) Name() string {
return "install-rbac-role"
}
func (o *kubeRBACRoleInstallOp) NextCommand() cke.Commander {
switch {
case !o.roleExists:
o.roleExists = true
return makeRBACRoleCommand{o.apiserver}
case !o.bindingExists:
o.bindingExists = true
return makeRBACRoleBindingCommand{o.apiserver}
}
return nil
}
type makeRBACRoleCommand struct {
apiserver *cke.Node
}
func (c makeRBACRoleCommand) Run(ctx context.Context, inf cke.Infrastructure) error {
cs, err := inf.K8sClient(ctx, c.apiserver)
if err != nil {
return err
}
_, err = cs.RbacV1().ClusterRoles().Create(&rbacv1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{
Name: rbacRoleName,
Labels: map[string]string{
"kubernetes.io/bootstrapping": "rbac-defaults",
},
Annotations: map[string]string{
// turn on auto-reconciliation
// https://kubernetes.io/docs/reference/access-authn-authz/rbac/#auto-reconciliation
"rbac.authorization.kubernetes.io/autoupdate": "true",
},
},
Rules: []rbacv1.PolicyRule{
{
APIGroups: []string{""},
// these are virtual resources.
// see https://github.com/kubernetes/kubernetes/issues/44330#issuecomment-293768369
Resources: []string{
"nodes/proxy",
"nodes/stats",
"nodes/log",
"nodes/spec",
"nodes/metrics",
},
Verbs: []string{"*"},
},
},
})
return err
}
func (c makeRBACRoleCommand) Command() cke.Command {
return cke.Command{
Name: "makeClusterRole",
Target: rbacRoleName,
}
}
type makeRBACRoleBindingCommand struct {
apiserver *cke.Node
}
func (c makeRBACRoleBindingCommand) Run(ctx context.Context, inf cke.Infrastructure) error {
cs, err := inf.K8sClient(ctx, c.apiserver)
if err != nil {
return err
}
_, err = cs.RbacV1().ClusterRoleBindings().Create(&rbacv1.ClusterRoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: rbacRoleBindingName,
Labels: map[string]string{
"kubernetes.io/bootstrapping": "rbac-defaults",
},
Annotations: map[string]string{
// turn on auto-reconciliation
// https://kubernetes.io/docs/reference/access-authn-authz/rbac/#auto-reconciliation
"rbac.authorization.kubernetes.io/autoupdate": "true",
},
},
RoleRef: rbacv1.RoleRef{
APIGroup: "rbac.authorization.k8s.io",
Kind: "ClusterRole",
Name: rbacRoleName,
},
Subjects: []rbacv1.Subject{
{
APIGroup: "rbac.authorization.k8s.io",
Kind: "User",
Name: "kubernetes",
},
},
})
return err
}
func (c makeRBACRoleBindingCommand) Command() cke.Command {
return cke.Command{
Name: "makeClusterRoleBinding",
Target: rbacRoleBindingName,
}
}