diff --git a/pkg/storage/internalstorage/config.go b/pkg/storage/internalstorage/config.go index 81823c32b..3d175bcbe 100644 --- a/pkg/storage/internalstorage/config.go +++ b/pkg/storage/internalstorage/config.go @@ -15,6 +15,8 @@ import ( "gopkg.in/natefinch/lumberjack.v2" "gorm.io/gorm/logger" "k8s.io/klog/v2" + + clusterv1alpha2 "github.com/clusterpedia-io/api/cluster/v1alpha2" ) const ( @@ -24,6 +26,14 @@ const ( databasePasswordEnvName = "DB_PASSWORD" ) +type DivisionPolicy string + +const ( + DivisionPolicyNone DivisionPolicy = "None" + DivisionPolicyGroupResource DivisionPolicy = "GroupResource" + DivisionPolicyCustom DivisionPolicy = "Custom" +) + type Config struct { Type string `env:"DB_TYPE" required:"true"` DSN string `env:"DB_DSN"` @@ -48,9 +58,30 @@ type Config struct { Params map[string]string `yaml:"params"` + AutoMigration *bool `yaml:"autoMigration"` // If set to false, no tables will be created + DivisionPolicy DivisionPolicy `yaml:"divisionPolicy"` + Mapper []ResourceMapper `yaml:"mapper"` // Only DivisionPolicy is DivisionPolicyCustom it need to specify the mapping between resource and table + Log *LogConfig `yaml:"log"` } +type ResourceMapper struct { + Table *Table `yaml:"table"` + Resources []clusterv1alpha2.ClusterGroupResources `yaml:"resources"` +} + +type Table struct { + Name string `yaml:"name"` + ExtraFields []ExtraField `yaml:"extraFields"` +} + +type ExtraField struct { + Name string `yaml:"name"` + PlainPath string `yaml:"plainPath"` + Type string `yaml:"type"` + Index string `yaml:"index"` +} + type LogConfig struct { Stdout bool `yaml:"stdout"` Level string `yaml:"level"` diff --git a/pkg/storage/internalstorage/register.go b/pkg/storage/internalstorage/register.go index a0bb10375..42061499b 100644 --- a/pkg/storage/internalstorage/register.go +++ b/pkg/storage/internalstorage/register.go @@ -93,10 +93,6 @@ func NewStorageFactory(configPath string) (storage.StorageFactory, error) { sqlDB.SetMaxOpenConns(connPool.MaxOpenConns) sqlDB.SetConnMaxLifetime(connPool.ConnMaxLifetime) - if err := db.AutoMigrate(&Resource{}); err != nil { - return nil, err - } - return &StorageFactory{db}, nil } diff --git a/pkg/storage/internalstorage/storage.go b/pkg/storage/internalstorage/storage.go index ce99ca99d..ea221bf2d 100644 --- a/pkg/storage/internalstorage/storage.go +++ b/pkg/storage/internalstorage/storage.go @@ -12,7 +12,31 @@ import ( ) type StorageFactory struct { - db *gorm.DB + db *gorm.DB + AutoMigration *bool + DivisionPolicy DivisionPolicy + Mapper []ResourceMapper +} + +func (s *StorageFactory) AutoMigrate() error { + if s.AutoMigration != nil && *s.AutoMigration { + switch s.DivisionPolicy { + if err := s.db.AutoMigrate(&Resource{}); err != nil { + return err + } + case "", DivisionPolicyNone: + case DivisionPolicyGroupResource: + + } + + if s.DivisionPolicy == "" || s.DivisionPolicy == DivisionPolicyNone { + if err := s.db.AutoMigrate(&Resource{}); err != nil { + return err + } + } + } + + return nil } func (s *StorageFactory) GetSupportedRequestVerbs() []string {