Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#5864): Added configuration for sizeLimit on empty-dirs in mount trait #5865

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 38 additions & 5 deletions pkg/trait/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/apache/camel-k/v2/pkg/util/boolean"
"github.com/apache/camel-k/v2/pkg/util/kubernetes"
utilResource "github.com/apache/camel-k/v2/pkg/util/resource"
"k8s.io/apimachinery/pkg/api/resource"
)

const (
Expand Down Expand Up @@ -145,11 +146,12 @@ func (t *mountTrait) configureVolumesAndMounts(vols *[]corev1.Volume, mnts *[]co
}
}
for _, v := range t.EmptyDirs {
if vol, parseErr := utilResource.ParseEmptyDirVolume(v); parseErr == nil {
t.mountResource(vols, mnts, vol)
} else {
volume, volumeMount, parseErr := t.ParseEmptyDirVolume(v)
if parseErr != nil {
return parseErr
}
*vols = append(*vols, *volume)
*mnts = append(*mnts, *volumeMount)
}

return nil
Expand All @@ -169,8 +171,7 @@ func (t *mountTrait) mountResource(vols *[]corev1.Volume, mnts *[]corev1.VolumeM
vol := getVolume(refName, string(conf.StorageType()), conf.Name(), conf.Key(), dstFile)
mntPath := getMountPoint(conf.Name(), dstDir, string(conf.StorageType()), string(conf.ContentType()))
readOnly := true
if conf.StorageType() == utilResource.StorageTypePVC ||
conf.StorageType() == utilResource.StorageTypeEmptyDir {
if conf.StorageType() == utilResource.StorageTypePVC {
readOnly = false
}
mnt := getMount(refName, mntPath, dstFile, readOnly)
Expand All @@ -186,3 +187,35 @@ func (t *mountTrait) addServiceBindingSecret(e *Environment) {
}
})
}

// ParseEmptyDirVolume will parse and return an empty-dir volume
func (t *mountTrait) ParseEmptyDirVolume(item string) (*corev1.Volume, *corev1.VolumeMount, error) {
volumeParts := strings.Split(item, ":")

if len(volumeParts) != 2 && len(volumeParts) != 3 {
return nil, nil, fmt.Errorf("could not match emptyDir volume as %s", item)
}

refName := kubernetes.SanitizeLabel(volumeParts[0])
sizeLimit := "500Mi"
if len(volumeParts) == 3 {
sizeLimit = volumeParts[2]
}

parsed, err := resource.ParseQuantity(sizeLimit)
if err != nil {
return nil, nil, fmt.Errorf("could not parse sizeLimit from emptyDir volume: %s", volumeParts[2])
}

volume := &corev1.Volume{
Name: refName,
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{
SizeLimit: &parsed,
},
},
}

volumeMount := getMount(refName, volumeParts[1], "", false)
return volume, volumeMount, nil
}
59 changes: 56 additions & 3 deletions pkg/trait/mount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,67 @@ func TestEmptyDirVolumeIntegrationPhaseDeploying(t *testing.T) {
assert.Len(t, spec.Containers[0].VolumeMounts, 3)
assert.Len(t, spec.Volumes, 3)

var emptyDirVolume *corev1.Volume
for _, v := range spec.Volumes {
if v.Name == "my-empty-dir" {
emptyDirVolume = &v
break
}
}

assert.NotNil(t, emptyDirVolume)
// Default applied by operator
assert.Equal(t, "500Mi", emptyDirVolume.EmptyDir.SizeLimit.String())

assert.Condition(t, func() bool {
for _, v := range spec.Volumes {
if v.Name == "my-empty-dir" {
return true
for _, container := range spec.Containers {
if container.Name == "integration" {
for _, volumeMount := range container.VolumeMounts {
if volumeMount.Name == "my-empty-dir" {
return true
}
}
}
}
return false
})
}

func TestEmptyDirVolumeWithSizeLimitIntegrationPhaseDeploying(t *testing.T) {
traitCatalog := NewCatalog(nil)

environment := getNominalEnv(t, traitCatalog)
environment.Integration.Spec.Traits.Mount = &traitv1.MountTrait{
EmptyDirs: []string{"my-empty-dir:/some/path:450Mi"},
}
environment.Platform.ResyncStatusFullConfig()
conditions, traits, err := traitCatalog.apply(environment)

require.NoError(t, err)
assert.NotEmpty(t, traits)
assert.NotEmpty(t, conditions)
assert.NotEmpty(t, environment.ExecutedTraits)
assert.NotNil(t, environment.GetTrait("mount"))

deployment := environment.Resources.GetDeployment(func(service *appsv1.Deployment) bool {
return service.Name == "hello"
})
assert.NotNil(t, deployment)
spec := deployment.Spec.Template.Spec

assert.Len(t, spec.Containers[0].VolumeMounts, 3)
assert.Len(t, spec.Volumes, 3)

var emptyDirVolume *corev1.Volume
for _, v := range spec.Volumes {
if v.Name == "my-empty-dir" {
emptyDirVolume = &v
break
}
}
assert.NotNil(t, emptyDirVolume)
assert.Equal(t, "450Mi", emptyDirVolume.EmptyDir.SizeLimit.String())

assert.Condition(t, func() bool {
for _, container := range spec.Containers {
if container.Name == "integration" {
Expand Down
9 changes: 0 additions & 9 deletions pkg/trait/trait_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

serving "knative.dev/serving/pkg/apis/serving/v1"
Expand Down Expand Up @@ -595,14 +594,6 @@ func getVolume(volName, storageType, storageName, filterKey, filterValue string)
volume.VolumeSource.PersistentVolumeClaim = &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: storageName,
}
case emptyDirStorageType:
size, err := resource.ParseQuantity("1Gi")
if err != nil {
log.WithValues("Function", "trait.getVolume").Errorf(err, "could not parse empty dir quantity, skipping")
}
volume.VolumeSource.EmptyDir = &corev1.EmptyDirVolumeSource{
SizeLimit: &size,
}
}

return &volume
Expand Down
15 changes: 0 additions & 15 deletions pkg/util/resource/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,21 +136,6 @@ func ParseResource(item string) (*Config, error) {
return parse(item, ContentTypeData)
}

// ParseEmptyDirVolume will parse an empty dir volume and return a Config.
func ParseEmptyDirVolume(item string) (*Config, error) {
configParts := strings.Split(item, ":")

if len(configParts) != 2 {
return nil, fmt.Errorf("could not match emptyDir volume as %s", item)
}

return &Config{
storageType: StorageTypeEmptyDir,
resourceName: configParts[0],
destinationPath: configParts[1],
}, nil
}

// ParseVolume will parse a volume and return a Config.
func ParseVolume(item string) (*Config, error) {
configParts := strings.Split(item, ":")
Expand Down
Loading