Manos Kounelakis
Manos Kounelakis

Reputation: 3161

Create a hostPath persistent volume with kube client

I am using the go k8s kube client . I am trying to create a persistent volume in my cluster . I want to use the hostPath type. However I cannot find any documentation about how to do that in go . I have the following code


vl := v1.PersistentVolume{

        Spec: v1.PersistentVolumeSpec{
            //VolumeMode: v1.PersistentVolumeMode(),

            StorageClassName: "manual",
            AccessModes: []v1.PersistentVolumeAccessMode{
                "ReadWriteMany",
            },
            Capacity: v1.ResourceList{
                v1.ResourceName(v1.ResourceStorage): resource.MustParse("2Gi"),
            },
        },
    }

    vl.Name = "golang-demo-storage"

If i try to create the above persistent volume as it is I get an error saying PersistentVolume "golang-demo-storage" is invalid: spec: Required value: must specify a volume type which makes sense since I have to define a type

However I cannot find any documentation about where in the struct to actually define the hostpath.

Do you have any reference that would help me ?

Upvotes: 0

Views: 877

Answers (1)

Manos Kounelakis
Manos Kounelakis

Reputation: 3161

So i was looking at the wrong fields. The field i hav to look at is the PersistentVolumeSource at the PersistentVolumeSpec. Here is the snippet of the code that worked for me


    ctx := context.TODO()
    vl := v1.PersistentVolume{

        TypeMeta: metav1.TypeMeta{Kind: "PersistentVolume"},
        ObjectMeta: metav1.ObjectMeta{
            Name: name,
        },

        Spec: v1.PersistentVolumeSpec{
            //VolumeMode: v1.PersistentVolumeMode(),

            StorageClassName: "manual",
            AccessModes: []v1.PersistentVolumeAccessMode{
                "ReadWriteMany",
            },
            Capacity: v1.ResourceList{
                v1.ResourceName(v1.ResourceStorage): resource.MustParse("2Gi"),
            },
            PersistentVolumeSource : v1.PersistentVolumeSource{
                HostPath: &v1.HostPathVolumeSource{
                    Path: "/demo",
                },
            },
        },
    }

    _ , e := clientset.CoreV1().PersistentVolumes().Create(ctx , &vl , metav1.CreateOptions{})

I had to define a PersistentVolumeSource field and in its initialization i passed the host path configuration i needed

Upvotes: 3

Related Questions