Skip to content
Snippets Groups Projects

k8s: PersistentVolumeClaim satisfier

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by Furkan Mustafa

    This script generates PersistentVolumes using hostPath for all pending PVCs.

    Volumes will be places under $PVROOTDIR/$NAMESPACE/$PVC-NAME

    by default; PVROOTDIR=/srv/tmp-volumes

    Edited
    satisfy-pvc.sh 705 B
    #!/bin/sh -e
    
    PVROOTDIR=/srv/tmp-volumes
    
    # Volumes will be places under $PVROOTDIR/$NAMESPACE/$PVC-NAME
    
    makevol() {
    
    cat <<EOF
    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: $2
      namespace: $1
    spec:
      accessModes:
      - ReadWriteOnce
      capacity:
        storage: $3
      # volumeMode: Filesystem
      hostPath:
        path: $PVROOTDIR/$1/$2
    EOF
    
    }
    
    kubectl get pvc --all-namespaces | grep Pending | while read pvc; do
      namespace=$(echo $pvc | awk '{ print $1 }')
      name=$(echo $pvc | awk '{ print $2 }')
      size=$(kubectl get pvc/$name -n $namespace -o=yaml | grep -A1 "requests" | grep "storage:" | awk '{ print $2}')
      echo "PVC: $name@$namespace"
      makevol $namespace $name $size | kubectl apply -f -
    done
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment