1
16
17 package node
18
19 import (
20 "fmt"
21
22 rbac "k8s.io/api/rbac/v1"
23 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24 clientset "k8s.io/client-go/kubernetes"
25
26 "k8s.io/kubernetes/cmd/kubeadm/app/constants"
27 "k8s.io/kubernetes/cmd/kubeadm/app/util/apiclient"
28 )
29
30
31 func AllowBootstrapTokensToPostCSRs(client clientset.Interface) error {
32 fmt.Println("[bootstrap-token] Configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials")
33
34 return apiclient.CreateOrUpdateClusterRoleBinding(client, &rbac.ClusterRoleBinding{
35 ObjectMeta: metav1.ObjectMeta{
36 Name: constants.NodeKubeletBootstrap,
37 },
38 RoleRef: rbac.RoleRef{
39 APIGroup: rbac.GroupName,
40 Kind: "ClusterRole",
41 Name: constants.NodeBootstrapperClusterRoleName,
42 },
43 Subjects: []rbac.Subject{
44 {
45 Kind: rbac.GroupKind,
46 Name: constants.NodeBootstrapTokenAuthGroup,
47 },
48 },
49 })
50 }
51
52
53 func AllowBoostrapTokensToGetNodes(client clientset.Interface) error {
54 fmt.Println("[bootstrap-token] Configured RBAC rules to allow Node Bootstrap tokens to get nodes")
55
56 if err := apiclient.CreateOrUpdateClusterRole(client, &rbac.ClusterRole{
57 ObjectMeta: metav1.ObjectMeta{
58 Name: constants.GetNodesClusterRoleName,
59 },
60 Rules: []rbac.PolicyRule{
61 {
62 Verbs: []string{"get"},
63 APIGroups: []string{""},
64 Resources: []string{"nodes"},
65 },
66 },
67 }); err != nil {
68 return err
69 }
70
71 return apiclient.CreateOrUpdateClusterRoleBinding(client, &rbac.ClusterRoleBinding{
72 ObjectMeta: metav1.ObjectMeta{
73 Name: constants.GetNodesClusterRoleName,
74 },
75 RoleRef: rbac.RoleRef{
76 APIGroup: rbac.GroupName,
77 Kind: "ClusterRole",
78 Name: constants.GetNodesClusterRoleName,
79 },
80 Subjects: []rbac.Subject{
81 {
82 Kind: rbac.GroupKind,
83 Name: constants.NodeBootstrapTokenAuthGroup,
84 },
85 },
86 })
87 }
88
89
90 func AutoApproveNodeBootstrapTokens(client clientset.Interface) error {
91 fmt.Println("[bootstrap-token] Configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token")
92
93
94 return apiclient.CreateOrUpdateClusterRoleBinding(client, &rbac.ClusterRoleBinding{
95 ObjectMeta: metav1.ObjectMeta{
96 Name: constants.NodeAutoApproveBootstrapClusterRoleBinding,
97 },
98 RoleRef: rbac.RoleRef{
99 APIGroup: rbac.GroupName,
100 Kind: "ClusterRole",
101 Name: constants.CSRAutoApprovalClusterRoleName,
102 },
103 Subjects: []rbac.Subject{
104 {
105 Kind: rbac.GroupKind,
106 Name: constants.NodeBootstrapTokenAuthGroup,
107 },
108 },
109 })
110 }
111
112
113 func AutoApproveNodeCertificateRotation(client clientset.Interface) error {
114 fmt.Println("[bootstrap-token] Configured RBAC rules to allow certificate rotation for all node client certificates in the cluster")
115
116 return apiclient.CreateOrUpdateClusterRoleBinding(client, &rbac.ClusterRoleBinding{
117 ObjectMeta: metav1.ObjectMeta{
118 Name: constants.NodeAutoApproveCertificateRotationClusterRoleBinding,
119 },
120 RoleRef: rbac.RoleRef{
121 APIGroup: rbac.GroupName,
122 Kind: "ClusterRole",
123 Name: constants.NodeSelfCSRAutoApprovalClusterRoleName,
124 },
125 Subjects: []rbac.Subject{
126 {
127 Kind: rbac.GroupKind,
128 Name: constants.NodesGroup,
129 },
130 },
131 })
132 }
133
View as plain text