@@ -26,6 +26,8 @@ import (
26
26
apierrors "k8s.io/apimachinery/pkg/api/errors"
27
27
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28
28
"sigs.k8s.io/controller-runtime/pkg/client"
29
+
30
+ "sigs.k8s.io/cluster-api/util/version"
29
31
)
30
32
31
33
const (
@@ -35,6 +37,11 @@ const (
35
37
// GetNodesClusterRoleName defines the name of the ClusterRole and ClusterRoleBinding to get nodes.
36
38
GetNodesClusterRoleName = "kubeadm:get-nodes"
37
39
40
+ // ClusterAdminsGroupAndClusterRoleBinding is the name of the Group used for kubeadm generated cluster
41
+ // admin credentials and the name of the ClusterRoleBinding that binds the same Group to the "cluster-admin"
42
+ // built-in ClusterRole.
43
+ ClusterAdminsGroupAndClusterRoleBinding = "kubeadm:cluster-admins"
44
+
38
45
// NodesGroup defines the well-known group for all nodes.
39
46
NodesGroup = "system:nodes"
40
47
@@ -66,6 +73,33 @@ func (w *Workload) EnsureResource(ctx context.Context, obj client.Object) error
66
73
return nil
67
74
}
68
75
76
+ // AllowClusterAdminPermissions creates ClusterRoleBinding rules to use the kubeadm:cluster-admins Cluster Role created in Kubeadm v1.29.
77
+ func (w * Workload ) AllowClusterAdminPermissions (ctx context.Context , targetVersion semver.Version ) error {
78
+ // We intentionally only parse major/minor/patch so that the subsequent code
79
+ // also already applies to pre-release versions of new releases.
80
+ // Do nothing for Kubernetes < 1.29.
81
+ if version .Compare (targetVersion , semver.Version {Major : 1 , Minor : 29 , Patch : 0 }, version .WithoutPreReleases ()) < 0 {
82
+ return nil
83
+ }
84
+ return w .EnsureResource (ctx , & rbacv1.ClusterRoleBinding {
85
+ ObjectMeta : metav1.ObjectMeta {
86
+ Name : ClusterAdminsGroupAndClusterRoleBinding ,
87
+ },
88
+ RoleRef : rbacv1.RoleRef {
89
+ APIGroup : rbacv1 .GroupName ,
90
+ Kind : "ClusterRole" ,
91
+ Name : "cluster-admin" ,
92
+ },
93
+ Subjects : []rbacv1.Subject {
94
+ {
95
+ Kind : rbacv1 .GroupKind ,
96
+ Name : ClusterAdminsGroupAndClusterRoleBinding ,
97
+ },
98
+ },
99
+ },
100
+ )
101
+ }
102
+
69
103
// AllowBootstrapTokensToGetNodes creates RBAC rules to allow Node Bootstrap Tokens to list nodes.
70
104
func (w * Workload ) AllowBootstrapTokensToGetNodes (ctx context.Context ) error {
71
105
if err := w .EnsureResource (ctx , & rbacv1.ClusterRole {
0 commit comments