Skip to content

Commit

Permalink
Add a property to Mount trait that creates then mounts a PVC
Browse files Browse the repository at this point in the history
  • Loading branch information
haanhvu committed Apr 22, 2022
1 parent 42d9efc commit dd99e75
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/trait/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
36 changes: 36 additions & 0 deletions pkg/util/resource/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit dd99e75

Please sign in to comment.