PVC Volume not detached if pod deleted via namespace deletion
Problem:
An attachable volume referenced by a pod via a PVC object (instead of directly) is not detached if the pod is deleted via namespace deletion (i.e. deleting the pod's namespace instead of the pod object directly).
Repro steps:
- Create a new volume
gcloud compute disks create --zone=us-central1-b test-0b
- Create a new namespace.
kubectl create ns testnsnamespace "testns" created
- Create PV and PVC objects:
kubectl create -f volumetest_pvc-pv.yamlpersistentvolume "pv-test-detach" createdpersistentvolumeclaim "claim-test-detach" created
- Verify PV/PVC objects are in bound state
kubectl get pvpv-test-detach 50Gi RWO Bound testns/claim-test-detach 2m
- Create pod object that references the volume via PVC
kubectl create -f volumetest_pod_pvc.yamlreplicationcontroller "sleepypod" created
- Verify pod gets into running state without issue (i.e. volume is attached)
kubectl get pods --namespace testnssleepypod-0b0eq 1/1 Running 0 3m
- Delete the namespace
kubectl delete ns testnsnamespace "testns" deleted
- Wait for pod to terminate
kubectl get pods --namespace testns
- Check if volume remains attached after a few minutes
- Expected: Volume is detached.
- Actual: Volume remains attached indefinitely.
Details
`volumetest_pvc-pv.yaml` is:apiVersion: v1
kind: PersistentVolume
metadata:
name : pv-test-detach
spec:
claimRef:
name: claim-test-detach
namespace: testns
accessModes:
- ReadWriteOnce
capacity:
storage: 50Gi
persistentVolumeReclaimPolicy: Retain
gcePersistentDisk:
fsType: ext4
pdName: test-0b
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: claim-test-detach
namespace: testns
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 50Gi
And volumetest_pod_pvc.yaml is:
apiVersion: v1
kind: ReplicationController
metadata:
name: sleepypod
namespace: testns
spec:
replicas: 1
selector:
name: sleepy
template:
metadata:
labels:
name: sleepy
spec:
volumes:
- name: data
persistentVolumeClaim:
claimName: claim-test-detach
containers:
- name: sleepycontainer1
image: saadali/sleepy:v0.2
env:
- name: "FOO"
value: " "
resources:
limits:
cpu: "0.002"
memory: "4Mi"
volumeMounts:
- name: data
mountPath: /data
readOnly: false
Workarounds:
- Delete the pod object directly before deleting the namespace.
- In step 7 above do
kubectl delete -f volumetest_pod_pvc.yamlinstead - If the attachable volume is referenced by a pod using a PVC object, and the pod object is deleted directly (instead of the namespace first), the volume is correctly detached.
- In step 7 above do
- Reference volume directly in pod (without a PVC object).
- If the attachable volume is referenced by a pod directly (without a PVC object), namespace deletion results in the volume being correctly detached.