diff --git a/pkg/trait/mount.go b/pkg/trait/mount.go index 6c8976a856..ac30fa9ce1 100644 --- a/pkg/trait/mount.go +++ b/pkg/trait/mount.go @@ -52,6 +52,8 @@ type mountTrait struct { Resources []string `property:"resources" json:"resources,omitempty"` // A list of Persistent Volume Claims to be mounted. Syntax: [pvcname:/container/path] Volumes []string `property:"volumes" json:"volumes,omitempty"` + // A Persistent Volume Claim to be created and mounted. Syntax: pvcname:storageClassName:requestedQuantity:/container/path + PVC string `property:"pvc" json:"pvc,omitempty"` } func newMountTrait() Trait { @@ -163,6 +165,11 @@ func (t *mountTrait) configureVolumesAndMounts(e *Environment, vols *[]corev1.Vo return parseErr } } + if vol, createAndParseErr := utilResource.CreateAndParseVolume(t.PVC); createAndParseErr == nil { + t.mountResource(vols, mnts, vol) + } else { + return createAndParseErr + } return nil } diff --git a/pkg/util/resource/config.go b/pkg/util/resource/config.go index 83d2b145e3..0cfa95c75e 100644 --- a/pkg/util/resource/config.go +++ b/pkg/util/resource/config.go @@ -29,6 +29,8 @@ import ( "github.com/apache/camel-k/pkg/util/camel" "github.com/apache/camel-k/pkg/util/kubernetes" corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/api/resource" k8serrors "k8s.io/apimachinery/pkg/api/errors" ) @@ -170,6 +172,40 @@ func ParseVolume(item string) (*Config, error) { }, nil } +// CreateAndParseVolume will create and parse a volume and return a Config. +func CreateAndParseVolume(item string) (*Config, error) { + pvcParts := strings.Split(item, ":") + + if len(pvcParts) != 4 { + return nil, fmt.Errorf("could not create and mount pvc as %s", item) + } + + pvc := corev1.PersistentVolumeClaim{ + TypeMeta: metav1.TypeMeta{ + Kind: "PersistentVolumeClaim", + APIVersion: "v1", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: pvcParts[0], + }, + Spec: corev1.PersistentVolumeClaimSpec{ + StorageClassName: pvcParts[1], + Resources: corev1.ResourceRequirements{ + Requests: corev1.ResourceList: + map[corev1.ResourceName]resource.Quantity{ + "storage": pvcParts[2], + } + }, + }, + } + + return &Config{ + storageType: StorageTypePVC, + resourceName: pvcParts[0], + destinationPath: pvcParts[3], + }, nil +} + // ParseConfig will parse a config and return a Config. func ParseConfig(item string) (*Config, error) { return parse(item, ContentTypeText)