diff --git a/controllers/job/init/internal/util/database/user.go b/controllers/job/init/internal/util/database/user.go index 38f081a1e89..9a4af7f2815 100644 --- a/controllers/job/init/internal/util/database/user.go +++ b/controllers/job/init/internal/util/database/user.go @@ -19,6 +19,10 @@ import ( "os" "time" + "github.com/google/uuid" + + "github.com/labring/sealos/controllers/pkg/utils/retry" + "github.com/labring/sealos/controllers/job/init/internal/util/common" gonanoid "github.com/matoous/go-nanoid/v2" @@ -41,6 +45,32 @@ func PresetAdminUser() error { logger.Warn("failed to close cockroach connection: %v", err) } }() + domain := os.Getenv("DOMAIN") + if domain == "" { + return fmt.Errorf("'DOMAIN' the environment variable is not set. please check") + } + regionUID, err := uuid.Parse(os.Getenv(cockroach.EnvLocalRegion)) + if err != nil { + return fmt.Errorf("failed to parse region %s uid: %v", os.Getenv(cockroach.EnvLocalRegion), err) + } + err = retry.Retry(10, 5*time.Second, func() error { + if !v2Account.DB.Migrator().HasTable(types.User{}) { + return fmt.Errorf("user table is null, please check") + } + return nil + }) + if err != nil { + return fmt.Errorf("failed to check user table: %v", err) + } + if err = v2Account.CreateRegion(&types.Region{ + UID: regionUID, + Domain: domain, + DisplayName: domain, + Location: domain, + }); err != nil { + return fmt.Errorf("failed to create region: %v", err) + } + userNanoID, err := gonanoid.New(10) if err != nil { return fmt.Errorf("failed to generate nano id: %v", err) diff --git a/controllers/pkg/database/cockroach/accountv2.go b/controllers/pkg/database/cockroach/accountv2.go index 060c8120887..0654d31d3bf 100644 --- a/controllers/pkg/database/cockroach/accountv2.go +++ b/controllers/pkg/database/cockroach/accountv2.go @@ -66,6 +66,13 @@ func (g *Cockroach) CreateUser(oAuth *types.OauthProvider, regionUserCr *types.R return nil } +func (g *Cockroach) CreateRegion(region *types.Region) error { + if err := g.DB.Where(&types.Region{UID: region.UID}).FirstOrCreate(region).Error; err != nil { + return fmt.Errorf("failed to create region: %w", err) + } + return nil +} + func (g *Cockroach) GetUserCr(ops *types.UserQueryOpts) (*types.RegionUserCr, error) { if err := checkOps(ops); err != nil { return nil, err @@ -687,7 +694,7 @@ func NewCockRoach(globalURI, localURI string) (*Cockroach, error) { if err != nil { return nil, fmt.Errorf("failed to encrypt zero value") } - if err := CreateTableIfNotExist(db, types.Account{}, types.ErrorAccountCreate{}, types.ErrorPaymentCreate{}, types.Payment{}, types.Transfer{}); err != nil { + if err := CreateTableIfNotExist(db, types.Account{}, types.ErrorAccountCreate{}, types.ErrorPaymentCreate{}, types.Payment{}, types.Transfer{}, types.Region{}); err != nil { return nil, err } cockroach := &Cockroach{DB: db, Localdb: localdb, ZeroAccount: &types.Account{EncryptBalance: *newEncryptBalance, EncryptDeductionBalance: *newEncryptDeductionBalance, Balance: baseBalance, DeductionBalance: 0}} diff --git a/controllers/pkg/types/global.go b/controllers/pkg/types/global.go index 51e67dff76b..38a20c920c1 100644 --- a/controllers/pkg/types/global.go +++ b/controllers/pkg/types/global.go @@ -37,10 +37,10 @@ func (Account) TableName() string { type Region struct { UID uuid.UUID `gorm:"type:uuid;default:gen_random_uuid();primary_key"` - DisplayName string `gorm:"type:text;not null"` - Location string `gorm:"type:text;not null"` - Domain string `gorm:"type:text;not null"` - Description string `gorm:"type:text;not null"` + DisplayName string `gorm:"type:text"` + Location string `gorm:"type:text"` + Domain string `gorm:"type:text;not null;unique"` + Description string `gorm:"type:text"` } // RegionUserCr is located in the region