Skip to content

Instantly share code, notes, and snippets.

@cmwylie19
Last active December 30, 2024 16:21
Show Gist options
  • Select an option

  • Save cmwylie19/41a7f9092f41a90131c0a2bd24549335 to your computer and use it in GitHub Desktop.

Select an option

Save cmwylie19/41a7f9092f41a90131c0a2bd24549335 to your computer and use it in GitHub Desktop.
Test cascading deletes on cluster scoped resources.

Kubernetes Ownership test

Background

If we delete a namespace that owns a ClusterRole, which owns a ClusterRoleBinding, will they delete in a cascading fashion?

Create a namespace

kubectl create ns starburst

Create ClusterRole

Create a ClusterRole owned by the namespace:

kubectl apply -f -<<EOF
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  ownerReferences:
    - apiVersion: v1
      kind: Namespace
      name: starburst
      uid: $(kubectl get ns starburst --template='{{ .metadata.uid}}')
  creationTimestamp: null
  name: secret-reader
rules:
- apiGroups:
  - ""
  resources:
  - secrets
  verbs:
  - get
  - list
EOF

Create a ClusterRoleBinding

Create a ClusterRoleBinding owned by the clusterrole

kubectl apply -f -<<EOF
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  ownerReferences:
    - apiVersion: rbac.authorization.k8s.io/v1
      kind: ClusterRole
      name: secret-reader
      uid: $(kubectl get clusterrole secret-reader --template='{{ .metadata.uid }}')
  creationTimestamp: null
  name: secret-reader-binding
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: secret-reader
subjects:
- kind: ServiceAccount
  name: secret-watcher
  namespace: default
EOF

Check Validity

If you delete the namespace, then the subsequent "owned" objects will also be deleted:

kubectl get clusterrole secret-reader 

kubectl get clusterrolebinding secret-reader-binding

kubectl delete ns starburst 

kubectl get clusterrole secret-reader 

kubectl get clusterrolebinding secret-reader-binding

Clean Up

Already cleaned because of the cascading delete

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment