2022-09-05 04:01:14 +00:00
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
v1 "k8s.io/api/core/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/api/resource"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
)
|
|
|
|
|
2023-03-21 19:00:45 +00:00
|
|
|
func PersistentVolumeClaim(namespace, name, storageClass, size string, storageRwx bool) (*v1.PersistentVolumeClaim, error) {
|
2022-09-05 04:01:14 +00:00
|
|
|
_storageClass := &storageClass
|
|
|
|
if storageClass == "" {
|
|
|
|
_storageClass = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var accessMode v1.PersistentVolumeAccessMode
|
|
|
|
|
|
|
|
if storageRwx {
|
|
|
|
accessMode = v1.ReadWriteMany
|
|
|
|
} else {
|
|
|
|
accessMode = v1.ReadWriteOnce
|
|
|
|
}
|
|
|
|
|
2023-03-21 19:00:45 +00:00
|
|
|
volumeName, err := dnsName(strings.Split(name, ":")[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pvc := &v1.PersistentVolumeClaim{
|
2022-09-05 04:01:14 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2023-03-21 19:00:45 +00:00
|
|
|
Name: volumeName,
|
2022-09-05 04:01:14 +00:00
|
|
|
Namespace: namespace,
|
|
|
|
},
|
|
|
|
Spec: v1.PersistentVolumeClaimSpec{
|
|
|
|
AccessModes: []v1.PersistentVolumeAccessMode{accessMode},
|
|
|
|
StorageClassName: _storageClass,
|
|
|
|
Resources: v1.ResourceRequirements{
|
|
|
|
Requests: v1.ResourceList{
|
|
|
|
v1.ResourceStorage: resource.MustParse(size),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-03-21 19:00:45 +00:00
|
|
|
return pvc, nil
|
2022-09-05 04:01:14 +00:00
|
|
|
}
|