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

Sync Space/SpaceResource component with enterprise #225

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
21 changes: 11 additions & 10 deletions _mocks/opencsg.com/csghub-server/component/mock_SpaceComponent.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion api/handler/space.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ func (h *SpaceHandler) Stop(ctx *gin.Context) {
return
}

err = h.c.Stop(ctx, namespace, name)
err = h.c.Stop(ctx, namespace, name, false)
Yiling-J marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
slog.Error("failed to stop space", slog.String("namespace", namespace),
slog.String("name", name), slog.Any("error", err))
Expand Down
2 changes: 1 addition & 1 deletion api/handler/space_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (h *SpaceResourceHandler) Index(ctx *gin.Context) {
httpbase.BadRequest(ctx, err.Error())
return
}
spaceResources, err := h.c.Index(ctx, clusterId, deployType)
spaceResources, err := h.c.Index(ctx, clusterId, deployType, "")
Yiling-J marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
slog.Error("Failed to get space resources", slog.String("cluster_id", clusterId), slog.String("deploy_type", deployTypeStr), slog.Any("error", err))
httpbase.ServerError(ctx, err)
Expand Down
21 changes: 11 additions & 10 deletions common/types/space.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,17 @@ type Space struct {
// the serving endpoint url
Endpoint string `json:"endpoint,omitempty" example:"https://localhost/spaces/myname/myspace"`
// deploying, running, failed
Status string `json:"status"`
RepositoryID int64 `json:"repository_id,omitempty"`
UserLikes bool `json:"user_likes"`
Source RepositorySource `json:"source"`
SyncStatus RepositorySyncStatus `json:"sync_status"`
SKU string `json:"sku,omitempty"`
SvcName string `json:"svc_name,omitempty"`
CanWrite bool `json:"can_write"`
CanManage bool `json:"can_manage"`
Namespace *Namespace `json:"namespace"`
Status string `json:"status"`
RepositoryID int64 `json:"repository_id,omitempty"`
UserLikes bool `json:"user_likes"`
Source RepositorySource `json:"source"`
SyncStatus RepositorySyncStatus `json:"sync_status"`
SKU string `json:"sku,omitempty"`
SvcName string `json:"svc_name,omitempty"`
CanWrite bool `json:"can_write"`
CanManage bool `json:"can_manage"`
Namespace *Namespace `json:"namespace"`
SensitiveCheckStatus string `json:"sensitive_check_status"`
}

type UpdateSpaceReq struct {
Expand Down
106 changes: 43 additions & 63 deletions component/space.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,9 @@ import (

"opencsg.com/csghub-server/builder/deploy"
"opencsg.com/csghub-server/builder/deploy/scheduler"
"opencsg.com/csghub-server/builder/git"
"opencsg.com/csghub-server/builder/git/gitserver"
"opencsg.com/csghub-server/builder/git/membership"
"opencsg.com/csghub-server/builder/rpc"
"opencsg.com/csghub-server/builder/store/database"
"opencsg.com/csghub-server/common/config"
"opencsg.com/csghub-server/common/types"
"opencsg.com/csghub-server/common/utils/common"
)
Expand Down Expand Up @@ -45,7 +42,7 @@ type SpaceComponent interface {
Delete(ctx context.Context, namespace, name, currentUser string) error
Deploy(ctx context.Context, namespace, name, currentUser string) (int64, error)
Wakeup(ctx context.Context, namespace, name string) error
Stop(ctx context.Context, namespace, name string) error
Stop(ctx context.Context, namespace, name string, deleteSpace bool) error
// FixHasEntryFile checks whether git repo has entry point file and update space's HasAppFile property in db
FixHasEntryFile(ctx context.Context, s *database.Space) *database.Space
Status(ctx context.Context, namespace, name string) (string, string, error)
Expand All @@ -54,69 +51,26 @@ type SpaceComponent interface {
HasEntryFile(ctx context.Context, space *database.Space) bool
}

func NewSpaceComponent(config *config.Config) (SpaceComponent, error) {
c := &spaceComponentImpl{}
c.spaceStore = database.NewSpaceStore()
var err error
c.spaceSdkStore = database.NewSpaceSdkStore()
c.spaceResourceStore = database.NewSpaceResourceStore()
c.repoStore = database.NewRepoStore()
c.repoComponent, err = NewRepoComponentImpl(config)
if err != nil {
return nil, err
}
c.deployer = deploy.NewDeployer()
c.publicRootDomain = config.Space.PublicRootDomain
c.userStore = database.NewUserStore()
c.accountingComponent, err = NewAccountingComponent(config)
if err != nil {
return nil, err
}
c.git, err = git.NewGitServer(config)
if err != nil {
return nil, err
}
c.serverBaseUrl = config.APIServer.PublicDomain
c.deployTaskStore = database.NewDeployTaskStore()
c.userSvcClient = rpc.NewUserSvcHttpClient(fmt.Sprintf("%s:%d", config.User.Host, config.User.Port),
rpc.AuthWithApiKey(config.APIToken))
c.userLikesStore = database.NewUserLikesStore()
c.config = config
return c, nil
}

type spaceComponentImpl struct {
config *config.Config
repoComponent RepoComponent
git gitserver.GitServer
spaceStore database.SpaceStore
spaceSdkStore database.SpaceSdkStore
spaceResourceStore database.SpaceResourceStore
repoStore database.RepoStore
userStore database.UserStore
deployer deploy.Deployer
publicRootDomain string
accountingComponent AccountingComponent
serverBaseUrl string
deployTaskStore database.DeployTaskStore
userSvcClient rpc.UserSvcClient
userLikesStore database.UserLikesStore
}

func (c *spaceComponentImpl) Create(ctx context.Context, req types.CreateSpaceReq) (*types.Space, error) {
var nickname string
if req.Nickname != "" {
nickname = req.Nickname
} else {
nickname = req.Name
}

if req.DefaultBranch == "" {
req.DefaultBranch = types.MainBranch
}

req.Nickname = nickname
req.RepoType = types.SpaceRepo
req.Readme = generateReadmeData(req.License)
resource, err := c.spaceResourceStore.FindByID(ctx, req.ResourceID)
if err != nil {
return nil, fmt.Errorf("fail to find resource by id, %w", err)
}
err = c.checkResourcePurchasableForCreate(ctx, req, resource)
if err != nil {
return nil, err
}
Expand All @@ -125,7 +79,7 @@ func (c *spaceComponentImpl) Create(ctx context.Context, req types.CreateSpaceRe
if err != nil {
return nil, fmt.Errorf("invalid hardware setting, %w", err)
}
_, err = c.deployer.CheckResourceAvailable(ctx, req.ClusterID, 0, &hardware)
_, err = c.checkResourceAvailable(ctx, req, hardware)
if err != nil {
return nil, fmt.Errorf("fail to check resource, %w", err)
}
Expand All @@ -145,6 +99,7 @@ func (c *spaceComponentImpl) Create(ctx context.Context, req types.CreateSpaceRe
Secrets: req.Secrets,
SKU: strconv.FormatInt(resource.ID, 10),
}
dbSpace = c.updateSpaceByReq(dbSpace, req)

resSpace, err := c.spaceStore.Create(ctx, dbSpace)
if err != nil {
Expand Down Expand Up @@ -302,12 +257,26 @@ func (c *spaceComponentImpl) Show(ctx context.Context, namespace, name, currentU
CanManage: permission.CanAdmin,
Namespace: ns,
}
if permission.CanAdmin {
resModel.SensitiveCheckStatus = space.Repository.SensitiveCheckStatus.String()
}

return resModel, nil
}

func (c *spaceComponentImpl) Update(ctx context.Context, req *types.UpdateSpaceReq) (*types.Space, error) {
req.RepoType = types.SpaceRepo
if req.ResourceID != nil {
resource, err := c.spaceResourceStore.FindByID(ctx, *req.ResourceID)
if err != nil {
return nil, fmt.Errorf("fail to find resource by id, %w", err)
}

err = c.checkResourcePurchasableForUpdate(ctx, *req, resource)
if err != nil {
return nil, err
}
}
dbRepo, err := c.repoComponent.UpdateRepo(ctx, req.UpdateRepoReq)
if err != nil {
return nil, err
Expand All @@ -317,6 +286,10 @@ func (c *spaceComponentImpl) Update(ctx context.Context, req *types.UpdateSpaceR
if err != nil {
return nil, fmt.Errorf("failed to find space, error: %w", err)
}
// don't support switch reserved resource
if c.resourceReserved(space, req) {
return nil, fmt.Errorf("don't support switch reserved resource so far")
}
err = c.mergeUpdateSpaceRequest(ctx, space, req)
if err != nil {
return nil, fmt.Errorf("failed to merge update space request, error: %w", err)
Expand Down Expand Up @@ -602,7 +575,12 @@ func (c *spaceComponentImpl) Delete(ctx context.Context, namespace, name, curren
}

// stop any running space instance
go func() { _ = c.Stop(ctx, namespace, name) }()
go func() {
err := c.Stop(ctx, namespace, name, true)
if err != nil {
slog.Error("stop space failed", slog.Any("error", err))
Yiling-J marked this conversation as resolved.
Show resolved Hide resolved
}
}()

return nil
}
Expand Down Expand Up @@ -641,7 +619,7 @@ func (c *spaceComponentImpl) Deploy(ctx context.Context, namespace, name, curren
slog.Info("run space with container image", slog.Any("namespace", namespace), slog.Any("name", name), slog.Any("containerImg", containerImg))

// create deploy for space
return c.deployer.Deploy(ctx, types.DeployRepo{
dr := types.DeployRepo{
SpaceID: s.ID,
Path: s.Repository.Path,
GitPath: s.Repository.GitPath,
Expand All @@ -660,7 +638,9 @@ func (c *spaceComponentImpl) Deploy(ctx context.Context, namespace, name, curren
Type: types.SpaceType,
UserUUID: user.UUID,
SKU: s.SKU,
})
}
dr = c.updateDeployRepoBySpace(dr, s)
return c.deployer.Deploy(ctx, dr)
}

func (c *spaceComponentImpl) Wakeup(ctx context.Context, namespace, name string) error {
Expand All @@ -682,7 +662,7 @@ func (c *spaceComponentImpl) Wakeup(ctx context.Context, namespace, name string)
})
}

func (c *spaceComponentImpl) Stop(ctx context.Context, namespace, name string) error {
func (c *spaceComponentImpl) Stop(ctx context.Context, namespace, name string, deleteSpace bool) error {
s, err := c.spaceStore.FindByPath(ctx, namespace, name)
if err != nil {
slog.Error("can't stop space", slog.Any("error", err), slog.String("namespace", namespace), slog.String("name", name))
Expand All @@ -698,12 +678,14 @@ func (c *spaceComponentImpl) Stop(ctx context.Context, namespace, name string) e
return fmt.Errorf("can't get space deployment")
}

err = c.deployer.Stop(ctx, types.DeployRepo{
dr := types.DeployRepo{
SpaceID: s.ID,
Namespace: namespace,
Name: name,
SvcName: deploy.SvcName,
})
}
dr = c.updateDeployRepoByDeploy(dr, deploy)
err = c.deployer.Stop(ctx, dr)
if err != nil {
return fmt.Errorf("can't stop space service deploy for service '%s', %w", deploy.SvcName, err)
}
Expand All @@ -720,9 +702,7 @@ func (c *spaceComponentImpl) FixHasEntryFile(ctx context.Context, s *database.Sp
hasAppFile := c.HasEntryFile(ctx, s)
if s.HasAppFile != hasAppFile {
s.HasAppFile = hasAppFile
if er := c.spaceStore.Update(ctx, *s); er != nil {
slog.Error("update space failed", "error", er)
}
_ = c.spaceStore.Update(ctx, *s)
}

return s
Expand Down
Loading
Loading