Created
May 30, 2022 08:14
-
-
Save ubergesundheit/b340b82a9a82f18bbc1d28b4a1dbfcd7 to your computer and use it in GitHub Desktop.
RBAC namespace permissions
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
# The script returns a kubeconfig for the service account given | |
# you need to have kubectl on PATH with the context set to the cluster you want to create the config for | |
# Cosmetics for the created config | |
clusterName=.... | |
# your server address goes here get it via `kubectl cluster-info` | |
server=https://.... | |
# the Namespace and ServiceAccount name that is used for the config | |
namespace=mynamespace | |
serviceAccount=mynamespace-user | |
###################### | |
# actual script starts | |
set -o errexit | |
secretName=$(kubectl --namespace $namespace get serviceAccount $serviceAccount -o jsonpath='{.secrets[0].name}') | |
ca=$(kubectl --namespace $namespace get secret/$secretName -o jsonpath='{.data.ca\.crt}') | |
token=$(kubectl --namespace $namespace get secret/$secretName -o jsonpath='{.data.token}' | base64 --decode) | |
echo " | |
--- | |
apiVersion: v1 | |
kind: Config | |
clusters: | |
- name: ${clusterName} | |
cluster: | |
certificate-authority-data: ${ca} | |
server: ${server} | |
contexts: | |
- name: ${serviceAccount}@${clusterName} | |
context: | |
cluster: ${clusterName} | |
namespace: ${namespace} | |
user: ${serviceAccount} | |
users: | |
- name: ${serviceAccount} | |
user: | |
token: ${token} | |
current-context: ${serviceAccount}@${clusterName} | |
" |
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
apiVersion: v1 | |
kind: ServiceAccount | |
metadata: | |
name: mynamespace-user | |
namespace: mynamespace | |
--- | |
kind: Role | |
apiVersion: rbac.authorization.k8s.io/v1beta1 | |
metadata: | |
name: mynamespace-user-full-access | |
namespace: mynamespace | |
rules: | |
- apiGroups: ["", "extensions", "apps", "networking.k8s.io"] | |
resources: ["*"] | |
verbs: ["*"] | |
- apiGroups: ["batch"] | |
resources: | |
- jobs | |
- cronjobs | |
verbs: ["*"] | |
--- | |
kind: RoleBinding | |
apiVersion: rbac.authorization.k8s.io/v1beta1 | |
metadata: | |
name: mynamespace-user-view | |
namespace: mynamespace | |
subjects: | |
- kind: ServiceAccount | |
name: mynamespace-user | |
namespace: mynamespace | |
roleRef: | |
apiGroup: rbac.authorization.k8s.io | |
kind: Role | |
name: mynamespace-user-full-access |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment