chore: move to go folder

This commit is contained in:
Nicolas Meienberger
2025-08-20 22:15:43 +02:00
parent 83b4296cfc
commit a0be690eb9
21 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,4 @@
// Package constants defines constants used throughout the application.
package constants
const VolumeRootLocal = "/mounts"

View File

@@ -0,0 +1,34 @@
// Package core provides the configuration loading functionality for the application.
package core
import (
"github.com/go-playground/validator/v10"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
)
type Config struct {
VolumeRootHost string `mapstructure:"volume_root" validate:"required"`
}
func LoadConfig() Config {
var config Config
viper.AutomaticEnv()
viper.BindEnv("volume_root", "VOLUME_ROOT")
if err := viper.Unmarshal(&config); err != nil {
log.Error().Err(err).Msg("Failed to load configuration")
panic("Failed to load configuration: " + err.Error())
}
validator := validator.New()
if err := validator.Struct(config); err != nil {
log.Error().Err(err).Msg("Configuration validation failed")
panic("Configuration validation failed: " + err.Error())
}
log.Info().Msgf("Loaded configuration: %+v", config)
return config
}

68
go/internal/core/log.go Normal file
View File

@@ -0,0 +1,68 @@
package core
import (
stdlog "log"
"os"
"strconv"
"time"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
func init() {
console := zerolog.ConsoleWriter{
Out: os.Stderr,
TimeFormat: time.ANSIC,
}
logger := zerolog.New(console).With().Timestamp().Caller().Logger()
zerolog.SetGlobalLevel(zerolog.InfoLevel)
log.Logger = logger
stdlog.SetFlags(0)
stdlog.SetOutput(console)
}
func colorStatus(code int) string {
switch {
case code >= 200 && code < 300:
// Green background
return "\033[42m" + strconv.Itoa(code) + "\033[0m"
case code >= 300 && code < 400:
// Cyan background
return "\033[46m" + strconv.Itoa(code) + "\033[0m"
case code >= 400 && code < 500:
// Yellow background
return "\033[43m" + strconv.Itoa(code) + "\033[0m"
default:
// Red background
return "\033[41m" + strconv.Itoa(code) + "\033[0m"
}
}
// GinLogger is a middleware for Gin that logs HTTP requests
// using zerolog.
func GinLogger() gin.HandlerFunc {
return func(c *gin.Context) {
c.Next()
code := c.Writer.Status()
method := c.Request.Method
path := c.Request.URL.Path
// logPath check if the path should be logged normally or with debug
switch {
case code >= 200 && code < 300:
log.Info().Str("method", method).Str("path", path).Msgf("Request status=%s", colorStatus(code))
case code >= 300 && code < 400:
log.Warn().Str("method", method).Str("path", path).Msgf("Request status=%s", colorStatus(code))
case code >= 400:
log.Error().Str("method", method).Str("path", path).Msgf("Request status=%s", colorStatus(code))
}
}
}

View File

@@ -0,0 +1,18 @@
package core
import (
"regexp"
"strings"
)
var nonAlnum = regexp.MustCompile(`[^a-z0-9_-]+`)
var hyphenRuns = regexp.MustCompile(`[-_]{2,}`)
func Slugify(input string) string {
s := strings.ToLower(strings.TrimSpace(input))
s = nonAlnum.ReplaceAllString(s, "-")
s = hyphenRuns.ReplaceAllString(s, "-")
s = strings.Trim(s, "-")
return s
}

129
go/internal/core/utils.go Normal file
View File

@@ -0,0 +1,129 @@
package core
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"reflect"
"strings"
"github.com/gin-gonic/gin/binding"
"github.com/go-playground/validator/v10"
)
type FieldError struct {
Field string `json:"field"`
Tag string `json:"tag"`
Param string `json:"param,omitempty"`
Message string `json:"message"`
}
type ValidationError struct {
Errors []FieldError `json:"errors"`
}
func (e *ValidationError) Error() string {
return fmt.Sprintf("validation failed (%d errors)", len(e.Errors))
}
// DecodeStrict decodes a JSON object from raw, rejects unknown fields, and
// validates struct tags (binding:"..."). Returns a ValidationError for
// semantic issues so callers can map it to HTTP 422.
func DecodeStrict[T any](raw json.RawMessage) (T, error) {
var out T
trimmed := bytes.TrimSpace(raw)
if len(trimmed) == 0 || bytes.Equal(trimmed, []byte("null")) {
return out, &ValidationError{
Errors: []FieldError{{
Field: "",
Tag: "required",
Message: "config is required",
}},
}
}
if trimmed[0] != '{' {
return out, fmt.Errorf("config must be a JSON object")
}
dec := json.NewDecoder(bytes.NewReader(raw))
dec.DisallowUnknownFields()
if err := dec.Decode(&out); err != nil {
return out, fmt.Errorf("invalid JSON: %w", err)
}
// Ensure no trailing junk after the object
if err := dec.Decode(&struct{}{}); err != io.EOF {
if err == nil {
return out, errors.New("unexpected trailing data after JSON object")
}
return out, fmt.Errorf("invalid JSON: %w", err)
}
if binding.Validator == nil {
return out, errors.New("validator not initialized")
}
if err := binding.Validator.ValidateStruct(out); err != nil {
if verrs, ok := err.(validator.ValidationErrors); ok {
return out, toValidationError[T](verrs)
}
return out, err
}
return out, nil
}
func toValidationError[T any](verrs validator.ValidationErrors) *ValidationError {
errs := make([]FieldError, 0, len(verrs))
t := reflect.TypeOf((*T)(nil)).Elem()
if t.Kind() == reflect.Pointer {
t = t.Elem()
}
for _, fe := range verrs {
name := fe.Field()
if t.Kind() == reflect.Struct {
if sf, ok := t.FieldByName(fe.StructField()); ok {
if tag := sf.Tag.Get("json"); tag != "" && tag != "-" {
name = strings.Split(tag, ",")[0]
} else {
// fallback to lower-camel for nicer output
name = lowerCamel(name)
}
}
}
errs = append(errs, FieldError{
Field: name,
Tag: fe.Tag(),
Param: fe.Param(),
Message: defaultMsg(fe),
})
}
return &ValidationError{Errors: errs}
}
func defaultMsg(fe validator.FieldError) string {
switch fe.Tag() {
case "required":
return ""
case "min":
return fmt.Sprintf("must be at least %s", fe.Param())
case "max":
return fmt.Sprintf("must be at most %s", fe.Param())
case "oneof":
return "must be one of: " + fe.Param()
case "hostname", "ip":
return "must be a valid " + fe.Tag()
}
return fe.Error()
}
func lowerCamel(s string) string {
if s == "" {
return s
}
r := []rune(s)
r[0] = []rune(strings.ToLower(string(r[0])))[0]
return string(r)
}

22
go/internal/db/db.go Normal file
View File

@@ -0,0 +1,22 @@
// Package db provides database access for the Ironmount application
package db
import (
"github.com/glebarez/sqlite"
"gorm.io/gorm"
)
var DB, err = gorm.Open(sqlite.Open("file:ironmount.db"), &gorm.Config{})
// InitDB initializes the database and creates the volumes table if it doesn't exist
func InitDB() {
if err != nil {
panic(err)
}
err = DB.AutoMigrate(&Volume{})
if err != nil {
panic(err)
}
}

13
go/internal/db/schema.go Normal file
View File

@@ -0,0 +1,13 @@
package db
import (
"gorm.io/gorm"
)
type Volume struct {
gorm.Model
Name string `json:"name"`
Path string `json:"path"`
Type string `json:"type"`
Config string `json:"config"`
}

View File

@@ -0,0 +1,180 @@
// Package driver provides the HTTP handlers for the volume driver API.
package driver
import (
"ironmount/internal/modules/volumes"
"net/http"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
)
func SetupHandlers(router *gin.Engine) {
volumeService := volumes.VolumeService{}
router.POST("/VolumeDriver.Capabilities", func(c *gin.Context) {
c.JSON(200, gin.H{
"Capabilities": map[string]bool{
"Scope": true, // Indicates that the driver supports scope (local/global)
},
})
})
router.POST("/Plugin.Activate", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"Implements": []string{
"VolumeDriver",
},
})
})
router.POST("/VolumeDriver.Create", func(c *gin.Context) {
var req CreateRequest
if err := c.BindJSON(&req); err != nil {
log.Error().Err(err).Msg("Invalid request body for Create")
c.JSON(http.StatusBadRequest, gin.H{"Err": "Invalid request body"})
return
}
volume, status, err := volumeService.CreateVolume(volumes.VolumeCreateRequest{
Name: req.Name,
Type: volumes.VolumeBackendTypeLocal,
})
if err != nil {
log.Error().Err(err).Msg("Failed to create volume")
c.JSON(status, gin.H{"Err": err.Error()})
return
}
c.JSON(status, gin.H{
"Name": volume.Name,
"Mountpoint": volume.Path,
"Err": "",
})
})
router.POST("/VolumeDriver.Remove", func(c *gin.Context) {
var req RemoveRequest
if err := c.BindJSON(&req); err != nil {
log.Error().Err(err).Msg("Invalid request body for Remove")
c.JSON(http.StatusBadRequest, gin.H{"Err": "Invalid request body"})
return
}
status, err := volumeService.DeleteVolume(req.Name)
if err != nil {
c.JSON(status, gin.H{"Err": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"Err": "",
})
})
router.POST("/VolumeDriver.Mount", func(c *gin.Context) {
var req MountRequest
if err := c.BindJSON(&req); err != nil {
log.Error().Err(err).Msg("Invalid request body")
c.JSON(http.StatusBadRequest, gin.H{"Err": "Invalid request body"})
return
}
volume, err := volumeService.GetVolume(req.Name)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
if volume == nil {
c.JSON(http.StatusNotFound, gin.H{"error": "Volume not found"})
return
}
c.JSON(http.StatusOK, gin.H{
"Name": volume.Name,
"Mountpoint": volume.Path,
"Err": "",
})
})
// VolumeDriver.Unmount is a no-op in this implementation
router.POST("/VolumeDriver.Unmount", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"Err": "",
})
})
// VolumeDriver.Path returns the mount point of the volume
router.POST("/VolumeDriver.Path", func(c *gin.Context) {
var req PathRequest
if err := c.BindJSON(&req); err != nil {
log.Error().Err(err).Msg("Invalid request body for Path")
c.JSON(http.StatusBadRequest, gin.H{"Err": "Invalid request body"})
return
}
vol, err := volumeService.GetVolume(req.Name)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"Err": err.Error()})
return
}
if vol == nil {
c.JSON(http.StatusNotFound, gin.H{"Err": "Volume not found"})
return
}
c.JSON(http.StatusOK, gin.H{
"Mountpoint": vol.Path,
"Err": "",
})
})
router.POST("/VolumeDriver.Get", func(c *gin.Context) {
var req GetRequest
if err := c.BindJSON(&req); err != nil {
log.Error().Err(err).Msg("Invalid request body for Get")
c.JSON(http.StatusBadRequest, gin.H{"Err": "Invalid request body"})
return
}
vol, err := volumeService.GetVolume(req.Name)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"Err": err.Error()})
return
}
if vol == nil {
c.JSON(http.StatusNotFound, gin.H{"Err": "Volume not found"})
return
}
data := map[string]any{
"Volume": map[string]string{
"Name": vol.Name,
"Mountpoint": vol.Path,
"CreatedAt": vol.CreatedAt.Format(volumes.DateFormat),
},
"Err": "",
}
c.JSON(http.StatusOK, data)
})
router.POST("/VolumeDriver.List", func(c *gin.Context) {
volumesList := volumeService.ListVolumes()
c.JSON(http.StatusOK, gin.H{
"Volumes": volumesList,
})
})
}

View File

@@ -0,0 +1,26 @@
package driver
// CreateRequest is the JSON request for Create
type CreateRequest struct {
Name string
}
type GetRequest struct {
Name string
}
// RemoveRequest is the JSON request for Remove
type RemoveRequest struct {
Name string
}
// MountRequest is the JSON request for Mount
type MountRequest struct {
Name string
ID string
}
// PathRequest is the JSON request for Path
type PathRequest struct {
Name string
}

View File

@@ -0,0 +1,63 @@
// Package volumes provides tools for managing volumes in the application.
package volumes
import (
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
)
// SetupHandlers sets up the API routes for the application.
func SetupHandlers(router *gin.Engine) {
volumeService := VolumeService{}
router.GET("/api/volumes", func(c *gin.Context) {
volumes := volumeService.ListVolumes()
log.Debug().Msgf("Listing volumes: %v", volumes)
c.JSON(200, gin.H{"volumes": volumes})
})
router.POST("/api/volumes", func(c *gin.Context) {
var body VolumeCreateRequest
if err := c.ShouldBindJSON(&body); err != nil {
log.Error().Err(err).Msg("Failed to bind JSON for volume creation")
c.JSON(400, gin.H{"error": "Invalid request body"})
return
}
volume, status, err := volumeService.CreateVolume(body)
if err != nil {
c.JSON(status, gin.H{"error": err.Error()})
return
}
c.JSON(status, volume)
})
router.GET("/api/volumes/:name", func(c *gin.Context) {
volume, err := volumeService.GetVolume(c.Param("name"))
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
if volume == nil {
c.JSON(404, gin.H{"error": "Volume not found"})
return
}
c.JSON(200, gin.H{
"name": volume.Name,
"mountpoint": volume.Path,
"created_at": volume.CreatedAt.String(),
"err": "",
})
})
router.DELETE("/api/volumes/:name", func(c *gin.Context) {
status, err := volumeService.DeleteVolume(c.Param("name"))
if err != nil {
c.JSON(status, gin.H{"error": err.Error()})
return
}
c.JSON(200, gin.H{"message": "Volume deleted successfully"})
})
}

View File

@@ -0,0 +1,72 @@
package volumes
import (
"context"
"github.com/go-playground/validator/v10"
"ironmount/internal/db"
"github.com/rs/zerolog/log"
"gorm.io/gorm"
)
type VolumeQueries struct{}
func (q *VolumeQueries) QueryVolumeByName(n string) (*db.Volume, error) {
ctx := context.Background()
volume, err := gorm.G[*db.Volume](db.DB).Where("name = ?", n).First(ctx)
if err != nil {
if (err.Error() == "record not found") || (err == gorm.ErrRecordNotFound) {
log.Warn().Str("name", n).Msg("Volume not found")
return nil, nil
}
return nil, err
}
return volume, nil
}
func (q *VolumeQueries) InsertVolume(name string, path string, volType VolumeBackendType, config string) error {
ctx := context.Background()
validate := validator.New(validator.WithRequiredStructEnabled())
data := &db.Volume{}
if err := validate.Struct(data); err != nil {
log.Error().Err(err).Str("name", name).Msg("Validation error while inserting volume")
return err
}
err := gorm.G[db.Volume](db.DB).Create(ctx, &db.Volume{})
if err != nil {
return err
}
return nil
}
func (q *VolumeQueries) RemoveVolume(name string) error {
ctx := context.Background()
log.Info().Str("volume", name).Msg("Removing volume")
_, err := gorm.G[db.Volume](db.DB).Where("name = ?", name).Delete(ctx)
if err != nil {
log.Error().Err(err).Str("volume", name).Msg("Error removing volume")
return err
}
return nil
}
func (q *VolumeQueries) QueryVolumes() ([]db.Volume, error) {
rows, err := gorm.G[db.Volume](db.DB).Select("name", "path", "created_at").Find(context.Background())
if err != nil {
return []db.Volume{}, err
}
return rows, nil
}

View File

@@ -0,0 +1,133 @@
package volumes
import (
"fmt"
"ironmount/internal/constants"
"ironmount/internal/core"
"ironmount/internal/db"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/rs/zerolog/log"
"k8s.io/utils/mount"
)
type VolumeService struct{}
var volumeQueries = VolumeQueries{}
// CreateVolume handles the creation of a new volume.
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 {
return nil, http.StatusConflict, fmt.Errorf("volume %s already exists", name)
}
cfg := core.LoadConfig()
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)
}
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)
}
return nil, http.StatusInternalServerError, fmt.Errorf("failed to create volume in database: %w", err)
}
return &db.Volume{
Name: name,
Path: volPathHost,
}, http.StatusOK, nil
}
func (v *VolumeService) GetVolume(name string) (*db.Volume, error) {
vol, err := volumeQueries.QueryVolumeByName(name)
return vol, err
}
func (v *VolumeService) ListVolumes() []VolumeInfo {
vols, _ := volumeQueries.QueryVolumes()
volumes := []VolumeInfo{}
for _, vol := range vols {
volumes = append(volumes, VolumeInfo{
Name: vol.Name,
Mountpoint: vol.Path,
CreatedAt: vol.CreatedAt.Format(DateFormat),
Err: "",
})
}
return volumes
}
func (v *VolumeService) DeleteVolume(name string) (int, error) {
vol, _ := volumeQueries.QueryVolumeByName(name)
if vol == nil {
return http.StatusNotFound, fmt.Errorf("volume %s not found", name)
}
if err := volumeQueries.RemoveVolume(name); err != nil {
return http.StatusInternalServerError, fmt.Errorf("failed to remove volume from database: %w", err)
}
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
}

View File

@@ -0,0 +1,87 @@
package volumes
import (
"encoding/json"
"fmt"
"strings"
)
var DateFormat = "2006-01-02T15:04:05Z"
type CreateVolumeResponse struct {
Name string `json:"name"`
Mountpoint string `json:"mountpoint"`
Err string `json:"err,omitempty"`
}
type GetVolumeResponse struct {
Name string `json:"name"`
Mountpoint string `json:"mountpoint"`
Err string `json:"err,omitempty"`
}
type VolumeInfo struct {
Name string `json:"name"`
Mountpoint string `json:"mountpoint"`
Err string `json:"err,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
}
type ListVolumesResponse struct {
Volumes []VolumeInfo `json:"volumes"`
Err string `json:"err,omitempty"`
}
type VolumeBackendType string
const (
VolumeBackendTypeSMB VolumeBackendType = "smb"
VolumeBackendTypeNFS VolumeBackendType = "nfs"
VolumeBackendTypeLocal VolumeBackendType = "local"
)
func (vbt VolumeBackendType) String() string {
return string(vbt)
}
func (vbt *VolumeBackendType) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return fmt.Errorf("volume backend type should be a string: %w", err)
}
lower := strings.ToLower(s)
switch VolumeBackendType(lower) {
case VolumeBackendTypeSMB, VolumeBackendTypeNFS, VolumeBackendTypeLocal:
*vbt = VolumeBackendType(lower)
return nil
default:
return fmt.Errorf("invalid volume backend type: '%s'. Allowed types are: smb, nfs, local", lower)
}
}
type VolumeCreateRequest struct {
Name string `json:"name" binding:"required"`
Type VolumeBackendType `json:"type" binding:"required,oneof=nfs smb directory"`
Config json.RawMessage `json:"config" binding:"required"`
}
type NFSConfig struct {
Server string `json:"server" binding:"required,hostname|ip"`
ExportPath string `json:"exportPath" binding:"required"`
Port int `json:"port" binding:"required,min=1,max=65535"`
Version string `json:"version" binding:"required,oneof=3 4"`
}
type SMBConfig struct {
Server string `json:"server" binding:"required"`
Share string `json:"share" binding:"required"`
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
Domain string `json:"domain,omitempty"`
}
type DirectoryConfig struct {
Path string `json:"path" binding:"required"`
}

View File

@@ -0,0 +1,21 @@
package volumes
import (
"fmt"
"strings"
"k8s.io/utils/mount"
)
func UnmountVolume(path string) error {
mounter := mount.New("")
if err := mounter.Unmount(path); err != nil {
if strings.Contains(err.Error(), "not mounted") || strings.Contains(err.Error(), "No such file or directory") || strings.Contains(err.Error(), "Invalid argument") {
// Volume is not mounted
return nil
}
return fmt.Errorf("failed to unmount volume at %s: %w", path, err)
}
return nil
}