Skip to content


Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time


Create a sudo kubeconfig for your current kubernetes context.

Demo gif

For questions or suggestions you are welcome to join us at our myCloudogu community forum.

Discuss it on myCloudogu


The kubectl sudo and helm sudo plugins use a powerful concept to prevent accidental kubectl apply or helm install to clusters: Using kuberentes' impersonate functionality as a sudo mechanism.

The plugins provide a good developer experience but are restricted to kubectl and helm.

What about other CLIs that rely on kubeconfig such as k9s, velero, fluxctl, istioctl, etc.? Can this mechanism be used for them as well? This repo provides an option for that: a "sudo-context". The sudo-context is a duplicate of your usual context in kubeconfig that uses the same cluster but a different user. This user sets as and as-groups just like kubectl sudo does.

Creating a sudo-context

One option for creating a "sudo context" is It guides you through the "sudo context" creation interactively.

chmod +x /tmp/

Using a sudo-context

See bellow for an example using local KIND/k3s/k3d cluster.

  • Create an impersonator ClusterRole (see kubectl-sudo for details of the concept) kubectl apply -f "${SUDO_KUBECONTEXT_VERSION}/clusterrole-sudoer.yaml"
  • Authorize users via ClusterRoleBinding, e.g. like kubectl create clusterrolebinding cluster-sudoers --clusterrole=sudoer --user=you
  • Restrict your user to read-only permissions (e.g. using the built-in viewer clusterrole)
  • Create sudo-kubeconfig
chmod +x /tmp/

Once you created a sudo context, you can use it like so:

fluxctl --context SUDO-context     
k9s --context SUDO-context #  Hint: You can also change the context from within k9s using ":ctx"

⚠️ Please note

  • The SUDO-context also contains a namespace. This might be different from your current context. So: better your -n in your commands or kubernetes ressources, or use kubectl sudo and helm sudo plugins.
  • It's good practice not to use the "sudo context" as current context, but to use it explicitly via an additional parameter.

By the way, you can also use this context for kubectl or helm, as an alternative to kubectl sudo plugin:

kubectl--context SUDO-context  # Hint use auto completion for the context
# This also works with aliases  ...
kgpo --context SUDO-context
#  ... and plugins
kubectl whoami --context SUDO-context
helm --kube-context SUDO-context # Hint use auto completion for the context

Trying sudo-kubeconfig in KIND, k3s/k3d

The kubeconfig used by k3s/k3d and KIND uses a client cert that already is in the system:masters group. This makes it difficult to restrict privileges using RBAC.

One option to try out sudo-kubeconfig is to create a service account and authenticate with its token.

# Preparations
# Create unprivileged service account
kubectl create sa unpriv --namespace default
# Enable sudo for service account
kubectl create clusterrolebinding cluster-sudoers \
    --clusterrole=sudoer \
# Optional: Allow read-only access by default
kubectl create clusterrolebinding cluster-viewers \
    --clusterrole=view \

# Create kubeconfig to authenticate using service account's token
wget -P /tmp
chmod +x /tmp/create-kubeconfig
/tmp/create-kubeconfig unpriv --namespace=default > ${tmpConfig}
export KUBECONFIG=${tmpConfig}


# Fails with
# error: failed to create deployment: deployments.apps is forbidden: User "system:serviceaccount:default:unpriv" cannot create resource "deployments" in API group "apps" in the namespace "default"
kubectl create deployment nginx --image=nginx
# Success: deployment.apps/nginx created
kubectl create deploy nginx --image=nginx --context=SUDO-kind 

# Fail
helm install nginx bitnami/nginx
# Success
helm install nginx bitnami/nginx --kube-context=SUDO-kind 

# Reset to default kubeconfig


Via Environment Variables.

  • SUDO_PREFIX - Prefix added to current kubecontext and user to flag it as "sudo". Default: SUDO-
  • SUDO_CONTEXT_POSTFIX - Postfix added to current kubecontext to raise attention to it being for sudo only. Default: ``
  • DEBUG - prints echo of commands (set -x)


Create a sudo kubeconfig for your current kubernetes context