feat: add NFS volume type

This commit is contained in:
Nicolas Meienberger
2025-08-20 21:15:30 +02:00
parent d13763995e
commit 83b4296cfc
12 changed files with 310 additions and 31 deletions

View File

@@ -9,6 +9,9 @@ import (
"os"
"path/filepath"
"strings"
"github.com/rs/zerolog/log"
"k8s.io/utils/mount"
)
type VolumeService struct{}
@@ -16,7 +19,12 @@ type VolumeService struct{}
var volumeQueries = VolumeQueries{}
// CreateVolume handles the creation of a new volume.
func (v *VolumeService) CreateVolume(name string) (*db.Volume, int, error) {
func (v *VolumeService) CreateVolume(body VolumeCreateRequest) (*db.Volume, int, error) {
name := core.Slugify(body.Name)
if name == "" || name != body.Name {
return nil, http.StatusBadRequest, fmt.Errorf("invalid volume name: %s", body.Name)
}
existingVol, _ := volumeQueries.QueryVolumeByName(name)
if existingVol != nil {
@@ -25,14 +33,48 @@ func (v *VolumeService) CreateVolume(name string) (*db.Volume, int, error) {
cfg := core.LoadConfig()
volPathHost := filepath.Join(cfg.VolumeRootHost, name)
volPathLocal := filepath.Join(constants.VolumeRootLocal, name)
volPathHost := filepath.Join(cfg.VolumeRootHost, name, "_data")
volPathLocal := filepath.Join(constants.VolumeRootLocal, name, "_data")
if err := os.MkdirAll(volPathLocal, 0755); err != nil {
return nil, http.StatusInternalServerError, fmt.Errorf("failed to create volume directory: %w", err)
}
if err := volumeQueries.InsertVolume(name, volPathHost); err != nil {
switch body.Type {
case VolumeBackendTypeNFS:
var cfg NFSConfig
cfg, err := core.DecodeStrict[NFSConfig](body.Config)
if err != nil {
return nil, http.StatusBadRequest, fmt.Errorf("invalid NFS configuration: %w", err)
}
mounter := mount.New("")
source := fmt.Sprintf("%s:%s", cfg.Server, cfg.ExportPath)
options := []string{"vers=" + cfg.Version, "port=" + fmt.Sprintf("%d", cfg.Port)}
if err := UnmountVolume(volPathLocal); err != nil {
return nil, http.StatusInternalServerError, fmt.Errorf("failed to unmount existing volume: %w", err)
}
if err := mounter.Mount(source, volPathLocal, "nfs", options); err != nil {
return nil, http.StatusInternalServerError, fmt.Errorf("failed to mount NFS volume: %w", err)
}
case VolumeBackendTypeSMB:
var _ SMBConfig
case VolumeBackendTypeLocal:
var cfg DirectoryConfig
log.Debug().Str("directory_path", cfg.Path).Msg("Using local directory for volume")
}
bytesConfig, err := body.Config.MarshalJSON()
if err != nil {
return nil, http.StatusBadRequest, fmt.Errorf("failed to marshal volume configuration: %w", err)
}
stringConfig := string(bytesConfig)
if err := volumeQueries.InsertVolume(name, volPathHost, stringConfig); err != nil {
if strings.Contains(err.Error(), "UNIQUE") {
return nil, http.StatusConflict, fmt.Errorf("volume %s already exists", name)
}
@@ -79,7 +121,13 @@ func (v *VolumeService) DeleteVolume(name string) (int, error) {
return http.StatusInternalServerError, fmt.Errorf("failed to remove volume from database: %w", err)
}
// os.RemoveAll(vol.Path) ?? depends on whether we want to delete the actual directory
volPathLocal := filepath.Join(constants.VolumeRootLocal, name)
log.Debug().Str("volume_path", volPathLocal).Msg("Deleting volume directory")
if err := UnmountVolume(volPathLocal); err != nil {
return http.StatusInternalServerError, fmt.Errorf("failed to unmount volume: %w", err)
}
os.RemoveAll(volPathLocal)
return http.StatusOK, nil
}