Skip to content

Commit

Permalink
fix: reinit DB to use native sql driver and remove go-sqlite3
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeyberezansky committed Nov 26, 2024
1 parent 4ec1e24 commit 469bbea
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 38 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ require (
golang.org/x/sync v0.9.0
google.golang.org/grpc v1.68.0
google.golang.org/protobuf v1.35.2
gorm.io/driver/sqlite v1.5.6
gorm.io/gorm v1.25.12
k8s.io/apimachinery v0.31.3
k8s.io/helm v2.17.0+incompatible
Expand Down
15 changes: 3 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,6 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntN
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/sqlite v1.5.6 h1:fO/X46qn5NUEEOZtnjJRWRzZMe8nqJiQ9E+0hi+hKQE=
gorm.io/driver/sqlite v1.5.6/go.mod h1:U+J8craQU6Fzkcvu8oLeAQmi50TkwPEhHDEjQZXDah4=
gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8=
gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=
k8s.io/apimachinery v0.31.3 h1:6l0WhcYgasZ/wk9ktLq5vLaoXJJr5ts6lkaQzgeYPq4=
Expand All @@ -141,23 +139,16 @@ k8s.io/mount-utils v0.31.3 h1:CANy3prUYvvDCc2X7ZKgpjpDhAidx4gjGh/WwDrCPq8=
k8s.io/mount-utils v0.31.3/go.mod h1:HV/VYBUGqYUj4vt82YltzpWvgv8FPg0G9ItyInT3NPU=
k8s.io/utils v0.0.0-20241104163129-6fe5fd82f078 h1:jGnCPejIetjiy2gqaJ5V0NLwTpF4wbQ6cZIItJCSHno=
k8s.io/utils v0.0.0-20241104163129-6fe5fd82f078/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
modernc.org/cc/v4 v4.21.4 h1:3Be/Rdo1fpr8GrQ7IVw9OHtplU4gWbb+wNgeoBMmGLQ=
modernc.org/cc/v4 v4.21.4/go.mod h1:HM7VJTZbUCR3rV8EYBi9wxnJ0ZBRiGE5OeGXNA0IsLQ=
modernc.org/cc/v4 v4.23.1 h1:WqJoPL3x4cUufQVHkXpXX7ThFJ1C4ik80i2eXEXbhD8=
modernc.org/ccgo/v4 v4.19.2 h1:lwQZgvboKD0jBwdaeVCTouxhxAyN6iawF3STraAal8Y=
modernc.org/ccgo/v4 v4.19.2/go.mod h1:ysS3mxiMV38XGRTTcgo0DQTeTmAO4oCmJl1nX9VFI3s=
modernc.org/cc/v4 v4.23.1/go.mod h1:HM7VJTZbUCR3rV8EYBi9wxnJ0ZBRiGE5OeGXNA0IsLQ=
modernc.org/ccgo/v4 v4.22.3 h1:C7AW89Zw3kygesTQWBzApwIn9ldM+cb/plrTIKq41Os=
modernc.org/ccgo/v4 v4.22.3/go.mod h1:Dz7n0/UkBbH3pnYaxgi1mFSfF4REqUOZNziphZASx6k=
modernc.org/fileutil v1.3.0 h1:gQ5SIzK3H9kdfai/5x41oQiKValumqNTDXMvKo62HvE=
modernc.org/fileutil v1.3.0/go.mod h1:XatxS8fZi3pS8/hKG2GH/ArUogfxjpEKs3Ku3aK4JyQ=
modernc.org/gc/v2 v2.4.1 h1:9cNzOqPyMJBvrUipmynX0ZohMhcxPtMccYgGOJdOiBw=
modernc.org/gc/v2 v2.4.1/go.mod h1:wzN5dK1AzVGoH6XOzc3YZ+ey/jPgYHLuVckd62P0GYU=
modernc.org/gc/v2 v2.5.0 h1:bJ9ChznK1L1mUtAQtxi0wi5AtAs5jQuw4PrPHO5pb6M=
modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6 h1:5D53IMaUuA5InSeMu9eJtlQXS2NxAhyWQvkKEgXZhHI=
modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6/go.mod h1:Qz0X07sNOR1jWYCrJMEnbW/X55x206Q7Vt4mz6/wHp4=
modernc.org/gc/v2 v2.5.0/go.mod h1:wzN5dK1AzVGoH6XOzc3YZ+ey/jPgYHLuVckd62P0GYU=
modernc.org/gc/v3 v3.0.0-20241004144649-1aea3fae8852 h1:IYXPPTTjjoSHvUClZIYexDiO7g+4x+XveKT4gCIAwiY=
modernc.org/gc/v3 v3.0.0-20241004144649-1aea3fae8852/go.mod h1:Qz0X07sNOR1jWYCrJMEnbW/X55x206Q7Vt4mz6/wHp4=
modernc.org/libc v1.55.3 h1:AzcW1mhlPNrRtjS5sS+eW2ISCgSOLLNyFzRh/V3Qj/U=
modernc.org/libc v1.55.3/go.mod h1:qFXepLhz+JjFThQ4kzwzOjA/y/artDeg+pcYnY+Q83w=
modernc.org/libc v1.61.2 h1:dkO4DlowfClcJYsvf/RiK6fUwvzCQTmB34bJLt0CAGQ=
modernc.org/libc v1.61.2/go.mod h1:4QGjNyX3h+rn7V5oHpJY2yH0QN6frt1X+5BkXzwLPCo=
modernc.org/mathutil v1.6.0 h1:fRe9+AmYlaej+64JsEEhoWuAYBkOtQiMEU7n/XgfYi4=
Expand Down
89 changes: 84 additions & 5 deletions pkg/wekafs/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package db

import (
"context"
"database/sql"
"errors"
"fmt"
"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/rs/zerolog/log"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/migrator"
"gorm.io/gorm/schema"
_ "modernc.org/sqlite" // Import the modernnc sqlite driver
"os"
"path/filepath"
Expand All @@ -24,6 +27,72 @@ type PvcAttachment struct {
AccessType string `json:"access_type"`
}

type ModerncSQLiteDialector struct {
DSN string
Conn *sql.DB
Config *gorm.Config
}

func (dialector ModerncSQLiteDialector) DefaultValueOf(field *schema.Field) clause.Expression {
//TODO implement me
panic("implement me")
}

func (dialector ModerncSQLiteDialector) BindVarTo(writer clause.Writer, stmt *gorm.Statement, v interface{}) {
//TODO implement me
panic("implement me")
}

func (dialector ModerncSQLiteDialector) QuoteTo(writer clause.Writer, s string) {
//TODO implement me
panic("implement me")
}

func (dialector ModerncSQLiteDialector) Explain(sql string, vars ...interface{}) string {
//TODO implement me
panic("implement me")
}

func (dialector ModerncSQLiteDialector) Name() string {
return "sqlite"
}

func (dialector ModerncSQLiteDialector) Initialize(db *gorm.DB) error {
// Set up the database/sql connection
if dialector.Conn != nil {
db.ConnPool = dialector.Conn
} else {
sqlDB, err := sql.Open("sqlite", dialector.DSN)
if err != nil {
return err
}
db.ConnPool = sqlDB
}

// Set up GORM configurations
db.Dialector = dialector
db.Config = dialector.Config
return nil
}

func (dialector ModerncSQLiteDialector) Migrator(db *gorm.DB) gorm.Migrator {
return migrator.Migrator{Config: migrator.Config{DB: db}}
}

func (dialector ModerncSQLiteDialector) DataTypeOf(field *schema.Field) string {
// Basic SQLite type mapping
switch field.DataType {
case schema.Int:
return "INTEGER"
case schema.String:
return "TEXT"
case schema.Bool:
return "BOOLEAN"
default:
return "BLOB"
}
}

func (pal *PvcAttachment) String() string {
return fmt.Sprintf("PVC: %s, Node: %s, TargetPath: %s, BootID: %s, AccessType: %s", pal.VolumeId, pal.Node, pal.TargetPath, pal.BootID, pal.AccessType)
}
Expand Down Expand Up @@ -58,26 +127,36 @@ func (pal *PvcAttachment) IsSingleWriter() bool {
}

// GetDatabase returns a database for pod attachments that will be used on each node to satisfy the ReadWriteOncePod attachment mode
func GetDatabase(ctx context.Context) (Database, error) {
func GetDatabase(ctx context.Context) (*SqliteDatabase, error) {
logger := log.Ctx(ctx)
directory := filepath.Dir(DBPath)
if err := EnsureDirectoryExists(directory); err != nil {
logger.Error().Err(err).Msg("Failed to create directory")
return nil, err
}

dsn := "file:" + DBPath
db, err := gorm.Open(sqlite.Open(dsn), &gorm.Config{})
gormDb, err := gorm.Open(
ModerncSQLiteDialector{
DSN: dsn,
},
&gorm.Config{
NamingStrategy: schema.NamingStrategy{
SingularTable: true,
},
},
)
if err != nil {
logger.Error().Err(err).Msg("Failed to connect to the database")
}

// Auto-migrate the schema
if err := db.AutoMigrate(&PvcAttachment{}); err != nil {
if err := gormDb.AutoMigrate(&PvcAttachment{}); err != nil {
logger.Error().Err(err).Msg("Failed to migrate the database")
}

return &SqliteDatabase{
db,
gormDb,
}, nil
}

Expand Down
9 changes: 2 additions & 7 deletions pkg/wekafs/db/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,14 @@ import (

"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)

func setupTestDB(t *testing.T) *SqliteDatabase {
_ = os.Remove(DBPath)
db, err := gorm.Open(sqlite.Open("file:"+DBPath), &gorm.Config{})
db, err := GetDatabase(context.Background())
assert.NoError(t, err)

err = db.AutoMigrate(&PvcAttachment{})
assert.NoError(t, err)

return &SqliteDatabase{db}
return db
}

func TestDatabaseWrapper_GetAttachmentsByVolumeIdOrTargetPath(t *testing.T) {
Expand Down
12 changes: 0 additions & 12 deletions pkg/wekafs/db/interfaces.go

This file was deleted.

2 changes: 1 addition & 1 deletion pkg/wekafs/nodeserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type NodeServer struct {
config *DriverConfig
semaphores map[string]*semaphore.Weighted
bootId string
database db.Database
database *db.SqliteDatabase
sync.Mutex
}

Expand Down

0 comments on commit 469bbea

Please sign in to comment.