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 May 13, 2022
1 parent aa14a23 commit b4d7d90
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
10 changes: 10 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 list of Persistent Volume Claims to be created and mounted. Syntax: [pvcname:storageClassName:requestedQuantity:/container/path]
PVCs []string `property:"pvcs" json:"pvcs,omitempty"`
}

func newMountTrait() Trait {
Expand Down Expand Up @@ -163,6 +165,14 @@ func (t *mountTrait) configureVolumesAndMounts(e *Environment, vols *[]corev1.Vo
return parseErr
}
}
for _, p := range t.PVCs {
if pvc, vol, createAndParseErr := utilResource.CreateAndParseVolume(p); createAndParseErr == nil {
e.Resources.Add(pvc)
t.mountResource(vols, mnts, vol)
} else {
return createAndParseErr
}
}

return nil
}
Expand Down
33 changes: 33 additions & 0 deletions pkg/util/resource/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import (
"github.com/apache/camel-k/pkg/util/kubernetes"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// Config represents a config option.
Expand Down Expand Up @@ -170,6 +172,37 @@ func ParseVolume(item string) (*Config, error) {
}, nil
}

// CreateAndParseVolume will create and parse a volume then return the created volume and a Config.
func CreateAndParseVolume(item string) (*corev1.PersistentVolumeClaim, *Config, error) {
pvcParts := strings.Split(item, ":")

if len(pvcParts) != 4 {
return nil, nil, fmt.Errorf("could not create and mount pvc as %s", item)
}

return &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{
"storage": resource.MustParse(pvcParts[2]),
},
},
},
}, &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 b4d7d90

Please sign in to comment.