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 1 commit
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
2 changes: 1 addition & 1 deletion pkg/trait/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func (t *mountTrait) mountResource(vols *[]corev1.Volume, mnts *[]corev1.VolumeM
dstFile = conf.Key()
}
}
vol := getVolume(refName, string(conf.StorageType()), conf.Name(), conf.Key(), dstFile)
vol := getVolume(refName, string(conf.StorageType()), conf.Name(), conf.Key(), dstFile, conf.AdditionalParams())
mntPath := getMountPoint(conf.Name(), dstDir, string(conf.StorageType()), string(conf.ContentType()))
readOnly := true
if conf.StorageType() == utilResource.StorageTypePVC ||
Expand Down
49 changes: 49 additions & 0 deletions pkg/trait/mount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,55 @@ func TestEmptyDirVolumeIntegrationPhaseDeploying(t *testing.T) {
})
}

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" {
for _, volumeMount := range container.VolumeMounts {
if volumeMount.Name == "my-empty-dir" {
return true
}
}
}
}
return false
})
}

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

Expand Down
16 changes: 7 additions & 9 deletions pkg/trait/trait_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ func (e *Environment) configureVolumesAndMounts(vols *[]corev1.Volume, mnts *[]c
resName := strings.TrimPrefix(s.Name, "/")
refName := fmt.Sprintf("i-source-%03d", idx)
resPath := filepath.Join(camel.SourcesMountPath, resName)
vol := getVolume(refName, "configmap", cmName, cmKey, resName)
vol := getVolume(refName, "configmap", cmName, cmKey, resName, nil)
mnt := getMount(refName, resPath, resName, true)

*vols = append(*vols, *vol)
Expand All @@ -550,7 +550,7 @@ func (e *Environment) configureVolumesAndMounts(vols *[]corev1.Volume, mnts *[]c

if propertiesType != "" {
refName := propertiesType + "-properties"
vol := getVolume(refName, "configmap", configMap.Name, "application.properties", resName)
vol := getVolume(refName, "configmap", configMap.Name, "application.properties", resName, nil)
mnt := getMount(refName, mountPath, resName, true)

*vols = append(*vols, *vol)
Expand All @@ -562,7 +562,7 @@ func (e *Environment) configureVolumesAndMounts(vols *[]corev1.Volume, mnts *[]c
// Kamelets bundle configmap
kameletMountPoint := configMap.Annotations[kameletMountPointAnnotation]
refName := KameletBundleType
vol := getVolume(refName, "configmap", configMap.Name, "", "")
vol := getVolume(refName, "configmap", configMap.Name, "", "", nil)
mnt := getMount(refName, kameletMountPoint, "", true)

*vols = append(*vols, *vol)
Expand All @@ -572,7 +572,7 @@ func (e *Environment) configureVolumesAndMounts(vols *[]corev1.Volume, mnts *[]c
}
}

func getVolume(volName, storageType, storageName, filterKey, filterValue string) *corev1.Volume {
func getVolume(volName, storageType, storageName, filterKey, filterValue string, additionalConfig map[string]interface{}) *corev1.Volume {
items := convertToKeyToPath(filterKey, filterValue)
volume := corev1.Volume{
Name: volName,
Expand All @@ -596,12 +596,10 @@ func getVolume(volName, storageType, storageName, filterKey, filterValue string)
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")
}
sizeLimit, _ := additionalConfig["SizeLimit"].(*resource.Quantity) //; ok {

volume.VolumeSource.EmptyDir = &corev1.EmptyDirVolumeSource{
SizeLimit: &size,
SizeLimit: sizeLimit,
}
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/trait/trait_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func TestCollectConfigurationPairs(t *testing.T) {
}

func TestVolumeWithKeyAndPath(t *testing.T) {
v := getVolume("SomeVolName", "secret", "SomeSecretName", "SomeKey", "SomePath")
v := getVolume("SomeVolName", "secret", "SomeSecretName", "SomeKey", "SomePath", nil)
assert.NotNil(t, v)
assert.Equal(t, "SomeVolName", v.Name)
s := v.VolumeSource.Secret
Expand All @@ -134,7 +134,7 @@ func TestVolumeWithKeyAndPath(t *testing.T) {
}

func TestVolumeWithPathOnly(t *testing.T) {
v := getVolume("SomeVolName", "secret", "SomeSecretName", "", "SomePath")
v := getVolume("SomeVolName", "secret", "SomeSecretName", "", "SomePath", nil)
assert.NotNil(t, v)
assert.Equal(t, "SomeVolName", v.Name)
s := v.VolumeSource.Secret
Expand All @@ -145,7 +145,7 @@ func TestVolumeWithPathOnly(t *testing.T) {
}

func TestVolumeWithKeyOnly(t *testing.T) {
v := getVolume("SomeVolName", "secret", "SomeSecretName", "SomeKey", "")
v := getVolume("SomeVolName", "secret", "SomeSecretName", "SomeKey", "", nil)
assert.NotNil(t, v)
assert.Equal(t, "SomeVolName", v.Name)
s := v.VolumeSource.Secret
Expand Down
32 changes: 26 additions & 6 deletions pkg/util/resource/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,18 @@ import (
"fmt"
"regexp"
"strings"

"k8s.io/apimachinery/pkg/api/resource"
)

// Config represents a config option.
type Config struct {
storageType StorageType
contentType ContentType
resourceName string
resourceKey string
destinationPath string
storageType StorageType
contentType ContentType
resourceName string
resourceKey string
destinationPath string
additionalParams map[string]interface{}
}

// DestinationPath is the location where the resource will be stored on destination.
Expand Down Expand Up @@ -57,6 +60,11 @@ func (config *Config) Key() string {
return config.resourceKey
}

// Additional parameters for the config
func (config *Config) AdditionalParams() map[string]interface{} {
return config.additionalParams
}

// String represents the unparsed value of the resource.
func (config *Config) String() string {
s := fmt.Sprintf("%s:%s", config.storageType, config.resourceName)
Expand Down Expand Up @@ -140,14 +148,26 @@ func ParseResource(item string) (*Config, error) {
func ParseEmptyDirVolume(item string) (*Config, error) {
configParts := strings.Split(item, ":")

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

var sizeLimit *resource.Quantity
if len(configParts) == 3 {
if parsed, err := resource.ParseQuantity(configParts[2]); err != nil {
return nil, fmt.Errorf("could not parse sizeLimit from emptyDir volume: %s", configParts[2])
} else {
sizeLimit = &parsed
}
}

return &Config{
storageType: StorageTypeEmptyDir,
resourceName: configParts[0],
destinationPath: configParts[1],
additionalParams: map[string]interface{}{
"SizeLimit": sizeLimit,
},
}, nil
}

Expand Down
Loading