-
Notifications
You must be signed in to change notification settings - Fork 450
/
rbac.go
162 lines (151 loc) · 4.92 KB
/
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package nodeagent
import (
coordinationv1 "k8s.io/api/coordination/v1"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apiserver/pkg/authentication/user"
bootstraptokenapi "k8s.io/cluster-bootstrap/token/api"
"github.com/gardener/gardener/pkg/client/kubernetes"
valiconstants "github.com/gardener/gardener/pkg/component/observability/logging/vali/constants"
nodeagentv1alpha1 "github.com/gardener/gardener/pkg/nodeagent/apis/config/v1alpha1"
"github.com/gardener/gardener/pkg/utils/managedresources"
)
// RBACResourcesData returns a map of serialized Kubernetes resources that allow the gardener-node-agent to
// access the list of given secrets. Additionally, serialized resources providing permissions to allow initiating the
// Kubernetes TLS bootstrapping process will be returned.
func RBACResourcesData(secretNames []string) (map[string][]byte, error) {
var (
clusterRole = &rbacv1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{
Name: "gardener-node-agent",
},
Rules: []rbacv1.PolicyRule{
{
APIGroups: []string{""},
Resources: []string{"nodes", "nodes/status"},
Verbs: []string{"get", "list", "watch", "patch", "update"},
},
{
APIGroups: []string{""},
Resources: []string{"events"},
Verbs: []string{"get", "list", "watch", "create", "patch", "update"},
},
},
}
clusterRoleBinding = &rbacv1.ClusterRoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: "gardener-node-agent",
},
RoleRef: rbacv1.RoleRef{
APIGroup: rbacv1.SchemeGroupVersion.Group,
Kind: "ClusterRole",
Name: clusterRole.Name,
},
Subjects: []rbacv1.Subject{{
Kind: rbacv1.ServiceAccountKind,
Name: nodeagentv1alpha1.AccessSecretName,
Namespace: metav1.NamespaceSystem,
}},
}
role = &rbacv1.Role{
ObjectMeta: metav1.ObjectMeta{
Name: "gardener-node-agent",
Namespace: metav1.NamespaceSystem,
},
Rules: []rbacv1.PolicyRule{
{
APIGroups: []string{""},
Resources: []string{"secrets"},
ResourceNames: append([]string{nodeagentv1alpha1.AccessSecretName, valiconstants.ValitailTokenSecretName}, secretNames...),
Verbs: []string{"get", "list", "watch"},
},
{
APIGroups: []string{coordinationv1.GroupName},
Resources: []string{"leases"},
Verbs: []string{"get", "list", "watch", "create", "update"},
},
},
}
roleBinding = &rbacv1.RoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: "gardener-node-agent",
Namespace: metav1.NamespaceSystem,
},
RoleRef: rbacv1.RoleRef{
APIGroup: rbacv1.SchemeGroupVersion.Group,
Kind: "Role",
Name: role.Name,
},
Subjects: []rbacv1.Subject{
{
Kind: rbacv1.GroupKind,
Name: bootstraptokenapi.BootstrapDefaultGroup,
},
{
Kind: rbacv1.ServiceAccountKind,
Name: nodeagentv1alpha1.AccessSecretName,
Namespace: metav1.NamespaceSystem,
},
},
}
clusterRoleBindingNodeBootstrapper = &rbacv1.ClusterRoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: "system:node-bootstrapper",
},
RoleRef: rbacv1.RoleRef{
APIGroup: rbacv1.SchemeGroupVersion.Group,
Kind: "ClusterRole",
Name: "system:node-bootstrapper",
},
Subjects: []rbacv1.Subject{{
APIGroup: rbacv1.SchemeGroupVersion.Group,
Kind: rbacv1.GroupKind,
Name: bootstraptokenapi.BootstrapDefaultGroup,
}},
}
clusterRoleBindingNodeClient = &rbacv1.ClusterRoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: "system:certificates.k8s.io:certificatesigningrequests:nodeclient",
},
RoleRef: rbacv1.RoleRef{
APIGroup: rbacv1.SchemeGroupVersion.Group,
Kind: "ClusterRole",
Name: "system:certificates.k8s.io:certificatesigningrequests:nodeclient",
},
Subjects: []rbacv1.Subject{{
APIGroup: rbacv1.SchemeGroupVersion.Group,
Kind: rbacv1.GroupKind,
Name: bootstraptokenapi.BootstrapDefaultGroup,
}},
}
clusterRoleBindingSelfNodeClient = &rbacv1.ClusterRoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: "system:certificates.k8s.io:certificatesigningrequests:selfnodeclient",
},
RoleRef: rbacv1.RoleRef{
APIGroup: rbacv1.SchemeGroupVersion.Group,
Kind: "ClusterRole",
Name: "system:certificates.k8s.io:certificatesigningrequests:selfnodeclient",
},
Subjects: []rbacv1.Subject{{
APIGroup: rbacv1.SchemeGroupVersion.Group,
Kind: rbacv1.GroupKind,
Name: user.NodesGroup,
}},
}
)
return managedresources.
NewRegistry(kubernetes.ShootScheme, kubernetes.ShootCodec, kubernetes.ShootSerializer).
AddAllAndSerialize(
clusterRole,
clusterRoleBinding,
role,
roleBinding,
clusterRoleBindingNodeBootstrapper,
clusterRoleBindingNodeClient,
clusterRoleBindingSelfNodeClient,
)
}