-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
workload_cluster_rbac.go
164 lines (145 loc) · 5.29 KB
/
workload_cluster_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
163
164
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package internal
import (
"context"
"fmt"
"github.com/blang/semver"
"github.com/pkg/errors"
rbacv1 "k8s.io/api/rbac/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const (
// NodeBootstrapTokenAuthGroup specifies which group a Node Bootstrap Token should be authenticated in.
NodeBootstrapTokenAuthGroup = "system:bootstrappers:kubeadm:default-node-token"
// GetNodesClusterRoleName defines the name of the ClusterRole and ClusterRoleBinding to get nodes.
GetNodesClusterRoleName = "kubeadm:get-nodes"
// NodesGroup defines the well-known group for all nodes.
NodesGroup = "system:nodes"
// KubeletConfigMapRolePrefix defines base kubelet configuration ConfigMap role prefix.
KubeletConfigMapRolePrefix = "kubeadm:"
// KubeletConfigMapName defines base kubelet configuration ConfigMap name for kubeadm < 1.24.
KubeletConfigMapName = "kubelet-config-%d.%d"
// UnversionedKubeletConfigMapName defines base kubelet configuration ConfigMap for kubeadm >= 1.24.
UnversionedKubeletConfigMapName = "kubelet-config"
)
// EnsureResource creates a resoutce if the target resource doesn't exist. If the resource exists already, this function will ignore the resource instead.
func (w *Workload) EnsureResource(ctx context.Context, obj client.Object) error {
testObj := obj.DeepCopyObject().(client.Object)
key := client.ObjectKeyFromObject(obj)
if err := w.Client.Get(ctx, key, testObj); err != nil && !apierrors.IsNotFound(err) {
return errors.Wrapf(err, "failed to determine if resource %s/%s already exists", key.Namespace, key.Name)
} else if err == nil {
// If object already exists, nothing left to do
return nil
}
if err := w.Client.Create(ctx, obj); err != nil {
if !apierrors.IsAlreadyExists(err) {
return errors.Wrapf(err, "unable to create resource %s/%s on workload cluster", key.Namespace, key.Name)
}
}
return nil
}
// AllowBootstrapTokensToGetNodes creates RBAC rules to allow Node Bootstrap Tokens to list nodes.
func (w *Workload) AllowBootstrapTokensToGetNodes(ctx context.Context) error {
if err := w.EnsureResource(ctx, &rbacv1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{
Name: GetNodesClusterRoleName,
Namespace: metav1.NamespaceSystem,
},
Rules: []rbacv1.PolicyRule{
{
Verbs: []string{"get"},
APIGroups: []string{""},
Resources: []string{"nodes"},
},
},
}); err != nil {
return err
}
return w.EnsureResource(ctx, &rbacv1.ClusterRoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: GetNodesClusterRoleName,
Namespace: metav1.NamespaceSystem,
},
RoleRef: rbacv1.RoleRef{
APIGroup: rbacv1.GroupName,
Kind: "ClusterRole",
Name: GetNodesClusterRoleName,
},
Subjects: []rbacv1.Subject{
{
Kind: rbacv1.GroupKind,
Name: NodeBootstrapTokenAuthGroup,
},
},
})
}
func generateKubeletConfigName(version semver.Version) string {
majorMinor := semver.Version{Major: version.Major, Minor: version.Minor}
if majorMinor.GTE(minVerUnversionedKubeletConfig) {
return UnversionedKubeletConfigMapName
}
return fmt.Sprintf(KubeletConfigMapName, version.Major, version.Minor)
}
func generateKubeletConfigRoleName(version semver.Version) string {
return KubeletConfigMapRolePrefix + generateKubeletConfigName(version)
}
// ReconcileKubeletRBACBinding will create a RoleBinding for the new kubelet version during upgrades.
// If the role binding already exists this function is a no-op.
func (w *Workload) ReconcileKubeletRBACBinding(ctx context.Context, version semver.Version) error {
roleName := generateKubeletConfigRoleName(version)
return w.EnsureResource(ctx, &rbacv1.RoleBinding{
ObjectMeta: metav1.ObjectMeta{
Namespace: metav1.NamespaceSystem,
Name: roleName,
},
Subjects: []rbacv1.Subject{
{
APIGroup: rbacv1.GroupName,
Kind: rbacv1.GroupKind,
Name: NodesGroup,
},
{
APIGroup: rbacv1.GroupName,
Kind: rbacv1.GroupKind,
Name: NodeBootstrapTokenAuthGroup,
},
},
RoleRef: rbacv1.RoleRef{
APIGroup: rbacv1.GroupName,
Kind: "Role",
Name: roleName,
},
})
}
// ReconcileKubeletRBACRole will create a Role for the new kubelet version during upgrades.
// If the role already exists this function is a no-op.
func (w *Workload) ReconcileKubeletRBACRole(ctx context.Context, version semver.Version) error {
return w.EnsureResource(ctx, &rbacv1.Role{
ObjectMeta: metav1.ObjectMeta{
Name: generateKubeletConfigRoleName(version),
Namespace: metav1.NamespaceSystem,
},
Rules: []rbacv1.PolicyRule{
{
Verbs: []string{"get"},
APIGroups: []string{""},
Resources: []string{"configmaps"},
ResourceNames: []string{generateKubeletConfigName(version)},
},
},
})
}