Skip to content

Commit

Permalink
Merge pull request #21524 from andremarianiello/memory-emptydir
Browse files Browse the repository at this point in the history
Use tmpfs mounts when creating a memory-backed emptyDir volume
  • Loading branch information
openshift-merge-bot[bot] authored Feb 7, 2024
2 parents f4f96a2 + d3281cf commit 6b592bd
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
7 changes: 7 additions & 0 deletions pkg/specgen/generate/kube/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,13 @@ func ToSpecGen(ctx context.Context, opts *CtrSpecGenOptions) (*specgen.SpecGener
SubPath: volume.SubPath,
}
s.Volumes = append(s.Volumes, &emptyDirVolume)
case KubeVolumeTypeEmptyDirTmpfs:
memVolume := spec.Mount{
Destination: volume.MountPath,
Type: define.TypeTmpfs,
Source: define.TypeTmpfs,
}
s.Mounts = append(s.Mounts, memVolume)
default:
return nil, errors.New("unsupported volume source type")
}
Expand Down
13 changes: 12 additions & 1 deletion pkg/specgen/generate/kube/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const (
KubeVolumeTypeCharDevice
KubeVolumeTypeSecret
KubeVolumeTypeEmptyDir
KubeVolumeTypeEmptyDirTmpfs
)

//nolint:revive
Expand Down Expand Up @@ -263,7 +264,17 @@ func VolumeFromConfigMap(configMapVolumeSource *v1.ConfigMapVolumeSource, config

// Create a kubeVolume for an emptyDir volume
func VolumeFromEmptyDir(emptyDirVolumeSource *v1.EmptyDirVolumeSource, name string) (*KubeVolume, error) {
return &KubeVolume{Type: KubeVolumeTypeEmptyDir, Source: name}, nil
if emptyDirVolumeSource.Medium == v1.StorageMediumMemory {
return &KubeVolume{
Type: KubeVolumeTypeEmptyDirTmpfs,
Source: name,
}, nil
} else {
return &KubeVolume{
Type: KubeVolumeTypeEmptyDir,
Source: name,
}, nil
}
}

// Create a KubeVolume from one of the supported VolumeSource
Expand Down
24 changes: 24 additions & 0 deletions pkg/specgen/generate/kube/volume_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//go:build !remote

package kube

import (
"testing"

v1 "github.com/containers/podman/v4/pkg/k8s.io/api/core/v1"
"github.com/stretchr/testify/assert"
)

func TestVolumeFromEmptyDir(t *testing.T) {
emptyDirSource := v1.EmptyDirVolumeSource{}
emptyDirVol, err := VolumeFromEmptyDir(&emptyDirSource, "emptydir")
assert.NoError(t, err)
assert.Equal(t, emptyDirVol.Type, KubeVolumeTypeEmptyDir)

memEmptyDirSource := v1.EmptyDirVolumeSource{
Medium: v1.StorageMediumMemory,
}
memEmptyDirVol, err := VolumeFromEmptyDir(&memEmptyDirSource, "emptydir")
assert.NoError(t, err)
assert.Equal(t, memEmptyDirVol.Type, KubeVolumeTypeEmptyDirTmpfs)
}

0 comments on commit 6b592bd

Please sign in to comment.