Skip to content

Commit

Permalink
The init job waits for the user table to be created before execution. (
Browse files Browse the repository at this point in the history
…#4619)

* retry table create

* create region
  • Loading branch information
bxy4543 authored Mar 22, 2024
1 parent ac53c02 commit 0d9d19d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
30 changes: 30 additions & 0 deletions controllers/job/init/internal/util/database/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down
9 changes: 8 additions & 1 deletion controllers/pkg/database/cockroach/accountv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}}
Expand Down
8 changes: 4 additions & 4 deletions controllers/pkg/types/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0d9d19d

Please sign in to comment.