-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add missing ClusterRoleBindings (#329)
* Update k3s to allow white-labeling k3s controller RBAC * Add missing ClusterRoleBindings Several critical ClusterRoleBindings were being inherited from k3s's rolebindings.yaml. Now that we have purged all k3s content, we need to provide them locally. Signed-off-by: Brad Davidson <[email protected]>
- Loading branch information
Showing
6 changed files
with
187 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package rke2 | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/sirupsen/logrus" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
) | ||
|
||
// setClusterRoles applies common clusterroles and clusterrolebindings that are critical | ||
// to the function of internal controllers. | ||
func setClusterRoles() func(context.Context, <-chan struct{}, string) error { | ||
return func(ctx context.Context, apiServerReady <-chan struct{}, kubeConfigAdmin string) error { | ||
go func() { | ||
<-apiServerReady | ||
logrus.Info("Applying Cluster Role Bindings") | ||
|
||
cs, err := newClient(kubeConfigAdmin, nil) | ||
if err != nil { | ||
logrus.Fatalf("clusterrole: new k8s client: %s", err.Error()) | ||
} | ||
|
||
if err := setKubeletAPIServerRoleBinding(ctx, cs); err != nil { | ||
logrus.Fatalf("psp: set kubeletAPIServerRoleBinding: %s", err.Error()) | ||
} | ||
|
||
if err := setTunnelControllerRoleBinding(ctx, cs); err != nil { | ||
logrus.Fatalf("psp: set tunnelControllerRoleBinding: %s", err.Error()) | ||
} | ||
|
||
logrus.Info("Cluster Role Bindings applied successfully") | ||
}() | ||
return nil | ||
} | ||
} | ||
|
||
// setKubeletAPIServerRoleBinding creates the clusterrolebinding that grants the apiserver full access to the kubelet API | ||
func setKubeletAPIServerRoleBinding(ctx context.Context, cs *kubernetes.Clientset) error { | ||
// check if clusterrolebinding exists | ||
if _, err := cs.RbacV1().ClusterRoleBindings().Get(ctx, kubeletAPIServerRoleBindingName, metav1.GetOptions{}); err != nil { | ||
if apierrors.IsNotFound(err) { | ||
logrus.Infof("Setting Cluster RoleBinding: %s", kubeletAPIServerRoleBindingName) | ||
|
||
tmpl := fmt.Sprintf(kubeletAPIServerRoleBindingTemplate, kubeletAPIServerRoleBindingName) | ||
if err := deployClusterRoleBindingFromYaml(ctx, cs, tmpl); err != nil { | ||
return err | ||
} | ||
} else { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// setTunnelControllerRoleBinding creates the clusterrole and clusterrolebinding used by internal controllers | ||
// such as the agent tunnel controller | ||
func setTunnelControllerRoleBinding(ctx context.Context, cs *kubernetes.Clientset) error { | ||
// check if clusterrole exists | ||
if _, err := cs.RbacV1().ClusterRoles().Get(ctx, tunnelControllerRoleName, metav1.GetOptions{}); err != nil { | ||
if apierrors.IsNotFound(err) { | ||
logrus.Infof("Setting Cluster Role: %s", tunnelControllerRoleName) | ||
|
||
tmpl := fmt.Sprintf(tunnelControllerRoleTemplate, tunnelControllerRoleName) | ||
if err := deployClusterRoleFromYaml(ctx, cs, tmpl); err != nil { | ||
return err | ||
} | ||
} else { | ||
return err | ||
} | ||
} | ||
|
||
// check if clusterrolebinding exists | ||
if _, err := cs.RbacV1().ClusterRoleBindings().Get(ctx, tunnelControllerRoleName, metav1.GetOptions{}); err != nil { | ||
if apierrors.IsNotFound(err) { | ||
logrus.Infof("Setting Cluster RoleBinding: %s", tunnelControllerRoleName) | ||
|
||
tmpl := fmt.Sprintf(tunnelControllerRoleBindingTemplate, tunnelControllerRoleName, tunnelControllerRoleName) | ||
if err := deployClusterRoleBindingFromYaml(ctx, cs, tmpl); err != nil { | ||
return err | ||
} | ||
} else { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package rke2 | ||
|
||
const ( | ||
kubeletAPIServerRoleBindingName = "kube-apiserver-kubelet-admin" | ||
tunnelControllerRoleName = "system:rke2-controller" | ||
) | ||
|
||
const kubeletAPIServerRoleBindingTemplate = `apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: ClusterRoleBinding | ||
metadata: | ||
name: %s | ||
roleRef: | ||
apiGroup: rbac.authorization.k8s.io | ||
kind: ClusterRole | ||
name: system:kubelet-api-admin | ||
subjects: | ||
- apiGroup: rbac.authorization.k8s.io | ||
kind: User | ||
name: kube-apiserver | ||
` | ||
|
||
const tunnelControllerRoleTemplate = `apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: ClusterRole | ||
metadata: | ||
name: %s | ||
rules: | ||
- apiGroups: | ||
- "" | ||
resources: | ||
- nodes | ||
verbs: | ||
- get | ||
- apiGroups: | ||
- "" | ||
resources: | ||
- namespaces | ||
verbs: | ||
- list | ||
- watch | ||
- apiGroups: | ||
- "networking.k8s.io" | ||
resources: | ||
- networkpolicies | ||
verbs: | ||
- list | ||
- watch | ||
- apiGroups: | ||
- "" | ||
resources: | ||
- endpoints | ||
- pods | ||
verbs: | ||
- list | ||
- get | ||
- watch | ||
` | ||
|
||
const tunnelControllerRoleBindingTemplate = `apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: ClusterRoleBinding | ||
metadata: | ||
name: system:k3s-controller | ||
roleRef: | ||
apiGroup: rbac.authorization.k8s.io | ||
kind: ClusterRole | ||
name: %s | ||
subjects: | ||
- apiGroup: rbac.authorization.k8s.io | ||
kind: User | ||
name: %s | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters