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

sql: drop the attachdriver mechanism #97

Merged
merged 6 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
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
64 changes: 0 additions & 64 deletions pkg/cache/sql/attachdriver/driver.go
moio marked this conversation as resolved.
Show resolved Hide resolved

This file was deleted.

45 changes: 0 additions & 45 deletions pkg/cache/sql/attachdriver/driver_test.go

This file was deleted.

10 changes: 1 addition & 9 deletions pkg/cache/sql/db/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"time"

"github.com/pkg/errors"
"github.com/rancher/lasso/pkg/cache/sql/attachdriver"
"github.com/rancher/lasso/pkg/cache/sql/db/transaction"
"k8s.io/apimachinery/pkg/util/wait"
"modernc.org/sqlite"
Expand All @@ -26,9 +25,6 @@ import (
const (
// InformerObjectCacheDBPath is where SQLite's object database file will be stored relative to process running lasso
InformerObjectCacheDBPath = "informer_object_cache.db"
// OnDiskInformerIndexedFieldDBPath is where SQLite's indexed fields database file will be stored if, env var value
// found at EncryptAllEnvVar is "false".
OnDiskInformerIndexedFieldDBPath = "informer_object_fields.db"
)

// Client is a database client that provides encrypting, decrypting, and database resetting.
Expand Down Expand Up @@ -336,11 +332,7 @@ func (c *Client) NewConnection() error {
return err
}

err = os.RemoveAll(OnDiskInformerIndexedFieldDBPath)
if err != nil {
return err
}
sqlDB, err := sql.Open(attachdriver.Name, "file:"+InformerObjectCacheDBPath+"?mode=rwc&cache=shared&_journal_mode=wal&_synchronous=off&_foreign_keys=on&_busy_timeout=1000000")
sqlDB, err := sql.Open("sqlite", "file:"+InformerObjectCacheDBPath+"?mode=rwc&cache=shared&_journal_mode=wal&_synchronous=off&_foreign_keys=on&_busy_timeout=1000000")
if err != nil {
return err
}
Expand Down
17 changes: 4 additions & 13 deletions pkg/cache/sql/informer/factory/informer_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/rancher/lasso/pkg/cache/sql/informer"

"github.com/rancher/lasso/pkg/cache/sql/attachdriver"
"github.com/rancher/lasso/pkg/cache/sql/db"
"github.com/rancher/lasso/pkg/cache/sql/encryption"
sqlStore "github.com/rancher/lasso/pkg/cache/sql/store"
Expand All @@ -20,6 +19,10 @@ import (
"k8s.io/client-go/tools/cache"
)

// EncryptAllEnvVar is set to "true" if users want all types' data blobs to be encrypted in SQLite
// otherwise only variables in defaultEncryptedResourceTypes will have their blobs encrypted
const EncryptAllEnvVar = "CATTLE_ENCRYPT_CACHE_ALL"

// CacheFactory builds Informer instances and keeps a cache of instances it created
type CacheFactory struct {
informerCreateLock sync.RWMutex
Expand Down Expand Up @@ -56,18 +59,6 @@ var defaultEncryptedResourceTypes = map[schema.GroupVersionKind]struct{}{
}: {},
}

const (
EncryptAllEnvVar = "CATTLE_ENCRYPT_CACHE_ALL"
)

func init() {
indexedFieldDBPath := db.OnDiskInformerIndexedFieldDBPath
if os.Getenv(EncryptAllEnvVar) == "true" {
indexedFieldDBPath = ":memory:"
}
attachdriver.Register("file:" + indexedFieldDBPath + "?cache=shared")
}

// NewCacheFactory returns an informer factory instance
func NewCacheFactory() (*CacheFactory, error) {
m, err := encryption.NewManager()
Expand Down
10 changes: 5 additions & 5 deletions pkg/cache/sql/informer/listoption_indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ var (
const (
matchFmt = `%%%s%%`
strictMatchFmt = `%s`
createFieldsTableFmt = `CREATE TABLE db2."%s_fields" (
createFieldsTableFmt = `CREATE TABLE "%s_fields" (
key TEXT NOT NULL PRIMARY KEY,
%s
)`
createFieldsIndexFmt = `CREATE INDEX db2."%s_%s_index" ON "%s_fields"("%s")`
createFieldsIndexFmt = `CREATE INDEX "%s_%s_index" ON "%s_fields"("%s")`

failedToGetFromSliceFmt = "[listoption indexer] failed to get subfield [%s] from slice items: %w"
)
Expand Down Expand Up @@ -127,13 +127,13 @@ func NewListOptionIndexer(fields [][]string, s Store, namespaced bool) (*ListOpt
}

l.addFieldQuery = fmt.Sprintf(
`INSERT INTO db2."%s_fields"(key, %s) VALUES (?, %s) ON CONFLICT DO UPDATE SET %s`,
`INSERT INTO "%s_fields"(key, %s) VALUES (?, %s) ON CONFLICT DO UPDATE SET %s`,
db.Sanitize(i.GetName()),
strings.Join(columns, ", "),
strings.Join(qmarks, ", "),
strings.Join(setStatements, ", "),
)
l.deleteFieldQuery = fmt.Sprintf(`DELETE FROM db2."%s_fields" WHERE key = ?`, db.Sanitize(i.GetName()))
l.deleteFieldQuery = fmt.Sprintf(`DELETE FROM "%s_fields" WHERE key = ?`, db.Sanitize(i.GetName()))

l.addFieldStmt = l.Prepare(l.addFieldQuery)
l.deleteFieldStmt = l.Prepare(l.deleteFieldQuery)
Expand Down Expand Up @@ -195,7 +195,7 @@ func (l *ListOptionIndexer) ListByOptions(ctx context.Context, lo ListOptions, p
// 1- Intro: SELECT and JOIN clauses
query := fmt.Sprintf(`SELECT o.object, o.objectnonce, o.dekid FROM "%s" o`, db.Sanitize(l.GetName()))
query += "\n "
query += fmt.Sprintf(`JOIN db2."%s_fields" f ON o.key = f.key`, db.Sanitize(l.GetName()))
query += fmt.Sprintf(`JOIN "%s_fields" f ON o.key = f.key`, db.Sanitize(l.GetName()))
params := []any{}

// 2- Filtering: WHERE clauses (from lo.Filters)
Expand Down
Loading