<p>While using the go-client API after I use the <code>api.PersistentVolumeClaims(namespace).Create(createOpts)</code> call the PersistentVolumeClaim appears as a resource but stays in the Pending state.  I do not see any events when using <code>kubectl describe pvc</code>, I also don't see any Volumes being created etc.</p>

<pre><code>$ kubectl describe pvc --namespace=test -R
Name:          93007732-9d8c-406e-be99-f48faed3a061
Namespace:     test
StorageClass:  microk8s-hostpath
Status:        Pending
Volume:        93007732-9d8c-406e-be99-f48faed3a061
Labels:        &lt;none&gt;
Annotations:   &lt;none&gt;
Finalizers:    [kubernetes.io/pvc-protection]
Capacity:      0
Access Modes:  
VolumeMode:    Filesystem
Events:        &lt;none&gt;
Mounted By:    &lt;none&gt;
</code></pre>

<p>The code I am using is as follows:</p>

<pre><code>
        volume, errGo := uuid.NewRandom()                                                                                                                                                 
        if errGo != nil {                                                                                                                                                                 
                job.failed = kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime())                                                                                                    
                return job.failed                                                                                                                                                         
        }                                                                                                                                                                                 
        job.volume = volume.String()

        fs := v1.PersistentVolumeFilesystem
        createOpts := &amp;v1.PersistentVolumeClaim{
                ObjectMeta: metav1.ObjectMeta{
                        Name:      job.volume,
                        Namespace: job.namespace,
                        UID:       types.UID(job.volume),
                },
                Spec: v1.PersistentVolumeClaimSpec{
                        AccessModes: []v1.PersistentVolumeAccessMode{v1.ReadWriteOnce},
                        Resources: v1.ResourceRequirements{
                                Requests: v1.ResourceList{
                                        v1.ResourceName(v1.ResourceStorage): resource.MustParse("10Gi"),
                                },
                        },
                        VolumeName: job.volume,
                        VolumeMode: &amp;fs,
                },
                Status: v1.PersistentVolumeClaimStatus{
                        Phase:       v1.ClaimBound,
                        AccessModes: []v1.PersistentVolumeAccessMode{v1.ReadWriteOnce},
                        Capacity: v1.ResourceList{
                                v1.ResourceName(v1.ResourceStorage): resource.MustParse("10Gi"),
                        },
                },
        }

        api := Client().CoreV1()
        if _, errGo = api.PersistentVolumeClaims(namespace).Create(createOpts); errGo != nil {
                job.failed = kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime())
                return job.failed
        }

</code></pre>

<p>I have tried to find good examples for using the Create API with persistent volumes but most examples appear to be for watchers etc and so I spent quite sometime trying to reverse engineer code leading me explicitly setting the <code>Status</code> but this appears to have had zero impact.  I also tried defaulting the VolumeMode within the Spec which did not help.</p>

<p>The examples I have read come from:</p>

<p><a href="https://github.com/kubernetes/kubernetes/blob/master/pkg/controller/volume/persistentvolume/framework_test.go" rel="nofollow noreferrer">https://github.com/kubernetes/kubernetes/blob/master/pkg/controller/volume/persistentvolume/framework_test.go</a>
<br>
<a href="https://godoc.org/k8s.io/api/core/v1#PersistentVolumeSpec" rel="nofollow noreferrer">https://godoc.org/k8s.io/api/core/v1#PersistentVolumeSpec</a>
<br>
<a href="https://github.com/vladimirvivien/k8s-client-examples/tree/master/go/pvcwatch" rel="nofollow noreferrer">https://github.com/vladimirvivien/k8s-client-examples/tree/master/go/pvcwatch</a>
<br>
<a href="https://medium.com/programming-kubernetes/building-stuff-with-the-kubernetes-api-part-4-using-go-b1d0e3c1c899" rel="nofollow noreferrer">https://medium.com/programming-kubernetes/building-stuff-with-the-kubernetes-api-part-4-using-go-b1d0e3c1c899</a>
<br></p>

<p>Does anyone know of actually example code for these APIs that goes beyond unit testing within the _test.go files, or can anyone provide any hints as to how to get the creation process actually rolling within the cluster ?  I have assumed that the downstream resources needed for example the Volume etc are automatically provisioned when I attempt to create the Claim resource.</p>

<p>Many Thanks for taking a look if you got this far...</p>

Karl Mutch
<p>What you are doing in the code looks correct. However, it looks like that your PVC can't find a matching PV to bind together. </p>

<p>It looks like you are using a <a href="https://kubernetes.io/docs/concepts/storage/volumes/#hostpath" rel="nofollow noreferrer"><code>hostPath</code></a> PV (with a <a href="https://kubernetes.io/docs/concepts/storage/storage-classes/" rel="nofollow noreferrer">storage class</a>) that <a href="https://kubernetes.io/docs/concepts/storage/persistent-volumes/#raw-block-volume-support" rel="nofollow noreferrer">doesn't  support dynamic provisioning</a>. Also, documented <a href="https://kubernetes.io/docs/concepts/storage/storage-classes/#provisioner" rel="nofollow noreferrer">here</a>.</p>

<p>So most likely you will have to create a hostPath PV so that your PVC can bind to it. The volume has to be equal or greater in size as what you are requesting in your PVC.</p>

<p>Another option is to use a <a href="https://kubernetes.io/docs/concepts/storage/storage-classes/#local" rel="nofollow noreferrer">Local</a> volume that supports dynamic provisioning which is different from hostPath.</p>

<p>You can debug the dynamic provisioning and binding of the PVC/PV by looking at the kube-controller-manager logs on your K8s control plane leader.</p>
