Skip to content

Commit

Permalink
Merge pull request #204 from xuzhu-591/feat-approval
Browse files Browse the repository at this point in the history
feat: support for checks of rollback and restart pipelineruns
  • Loading branch information
xuzhu-591 authored Dec 11, 2023
2 parents ed83f01 + 7b78016 commit aa5e0fa
Show file tree
Hide file tree
Showing 26 changed files with 749 additions and 306 deletions.
2 changes: 1 addition & 1 deletion core/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ func Init(ctx context.Context, flags *Flags, coreConfig *config.Config) {
groupSvc := groupservice.NewService(manager)
eventSvc := eventservice.New(manager)
applicationSvc := applicationservice.NewService(groupSvc, manager)
clusterSvc := clusterservice.NewService(applicationSvc, manager)
clusterSvc := clusterservice.NewService(applicationSvc, clusterGitRepo, manager)
userSvc := userservice.NewService(manager)
tokenSvc := tokenservice.NewService(manager, coreConfig.TokenConfig)

Expand Down
68 changes: 37 additions & 31 deletions core/controller/application/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
trschemamock "github.com/horizoncd/horizon/mock/pkg/templaterelease/schema"
"github.com/horizoncd/horizon/pkg/application/gitrepo"
"github.com/horizoncd/horizon/pkg/application/models"
appregionmodels "github.com/horizoncd/horizon/pkg/applicationregion/models"
userauth "github.com/horizoncd/horizon/pkg/authentication/user"
codemodels "github.com/horizoncd/horizon/pkg/cluster/code"
clustermodels "github.com/horizoncd/horizon/pkg/cluster/models"
Expand All @@ -36,6 +37,7 @@ import (
groupmodels "github.com/horizoncd/horizon/pkg/group/models"
groupservice "github.com/horizoncd/horizon/pkg/group/service"
membermodels "github.com/horizoncd/horizon/pkg/member/models"
"github.com/horizoncd/horizon/pkg/param"
"github.com/horizoncd/horizon/pkg/param/managerparam"
regionmodels "github.com/horizoncd/horizon/pkg/region/models"
tagmodels "github.com/horizoncd/horizon/pkg/tag/models"
Expand Down Expand Up @@ -298,6 +300,9 @@ func TestMain(m *testing.M) {
if err := db.AutoMigrate(&usermodel.User{}); err != nil {
panic(err)
}
if err := db.AutoMigrate(&appregionmodels.ApplicationRegion{}); err != nil {
panic(err)
}
ctx = context.TODO()
ctx = context.WithValue(ctx, common.UserContextKey(), &userauth.DefaultInfo{
Name: "Tony",
Expand Down Expand Up @@ -357,26 +362,28 @@ func Test(t *testing.T) {
_, err := manager.TemplateReleaseMgr.Create(ctx, tr)
assert.Nil(t, err)

c = &controller{
applicationGitRepo: applicationGitRepo,
templateSchemaGetter: templateSchemaGetter,
tagMgr: manager.TagMgr,
applicationMgr: manager.ApplicationMgr,
groupMgr: manager.GroupMgr,
groupSvc: groupservice.NewService(manager),
templateReleaseMgr: manager.TemplateReleaseMgr,
clusterMgr: manager.ClusterMgr,
userSvc: userservice.NewService(manager),
eventSvc: eventservice.New(manager),
memberManager: manager.MemberMgr,
params := &param.Param{
Manager: manager,
UserSvc: userservice.NewService(manager),
EventSvc: eventservice.New(manager),
GroupSvc: groupservice.NewService(manager),
ApplicationGitRepo: applicationGitRepo,
TemplateSchemaGetter: templateSchemaGetter,
}
c = NewController(params)

group, err := manager.GroupMgr.Create(ctx, &groupmodels.Group{
Name: "ABC",
Path: "abc",
})
assert.Nil(t, err)

groupToTransfer, err := manager.GroupMgr.Create(ctx, &groupmodels.Group{
Name: "ABC-transfer",
Path: "abc/transfer",
})
assert.Nil(t, err)

createRequest := &CreateApplicationRequest{
Base: Base{
Description: "this is a description",
Expand Down Expand Up @@ -445,6 +452,12 @@ func Test(t *testing.T) {

assert.Equal(t, resp.Description, updatedDescription)

err = c.Transfer(ctx, resp.ID, groupToTransfer.ID)
assert.Nil(t, err)
resp, err = c.GetApplication(ctx, resp.ID)
assert.Nil(t, err)
assert.Equal(t, resp.GroupID, groupToTransfer.ID)

err = c.DeleteApplication(ctx, resp.ID, false)
assert.Nil(t, err)

Expand Down Expand Up @@ -513,20 +526,15 @@ func TestV2(t *testing.T) {
}
_, err := manager.TemplateReleaseMgr.Create(ctx, tr)
assert.Nil(t, err)
c := &controller{
applicationGitRepo: applicationGitRepo,
templateSchemaGetter: templateSchemaGetter,
applicationMgr: manager.ApplicationMgr,
tagMgr: manager.TagMgr,
groupMgr: manager.GroupMgr,
groupSvc: groupservice.NewService(manager),
templateReleaseMgr: manager.TemplateReleaseMgr,
clusterMgr: manager.ClusterMgr,
userSvc: userservice.NewService(manager),
eventSvc: eventservice.New(manager),
memberManager: manager.MemberMgr,
params := &param.Param{
Manager: manager,
UserSvc: userservice.NewService(manager),
EventSvc: eventservice.New(manager),
GroupSvc: groupservice.NewService(manager),
ApplicationGitRepo: applicationGitRepo,
TemplateSchemaGetter: templateSchemaGetter,
}

c := NewController(params)
group, err := manager.GroupMgr.Create(ctx, &groupmodels.Group{
Name: "cde",
Path: "cde",
Expand Down Expand Up @@ -673,12 +681,10 @@ func TestListUserApplication(t *testing.T) {
applications = append(applications, application)
}

c = &controller{
applicationMgr: manager.ApplicationMgr,
groupMgr: manager.GroupMgr,
groupSvc: groupservice.NewService(manager),
memberManager: manager.MemberMgr,
}
c = NewController(&param.Param{
Manager: manager,
GroupSvc: groupservice.NewService(manager),
})

// nolint
ctx = context.WithValue(ctx, common.UserContextKey(), &userauth.DefaultInfo{
Expand Down
3 changes: 3 additions & 0 deletions core/controller/cluster/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cluster
import (
"context"

clusterservice "github.com/horizoncd/horizon/pkg/cluster/service"
templatemanager "github.com/horizoncd/horizon/pkg/template/manager"
"k8s.io/apimachinery/pkg/runtime/schema"

Expand Down Expand Up @@ -164,6 +165,7 @@ type controller struct {
tokenConfig token.Config
templateUpgradeMapper template.UpgradeMapper
collectionManager collectionmanager.Manager
clusterSvc clusterservice.Service
}

var _ Controller = (*controller)(nil)
Expand Down Expand Up @@ -206,5 +208,6 @@ func NewController(config *config.Config, param *param.Param) Controller {
tokenConfig: config.TokenConfig,
templateUpgradeMapper: config.TemplateUpgradeMapper,
collectionManager: param.CollectionMgr,
clusterSvc: param.ClusterSvc,
}
}
46 changes: 30 additions & 16 deletions core/controller/cluster/controller_basic_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ func (c *controller) CreatePipelineRun(ctx context.Context, clusterID uint,
return nil, err
}

// 找一下是否需要check,如果不需要则直接设为ready
// if checks is empty, set status to ready
checks, err := c.prSvc.GetCheckByResource(ctx, clusterID, common.ResourceCluster)
if err != nil {
return nil, err
Expand All @@ -683,19 +683,22 @@ func (c *controller) CreatePipelineRun(ctx context.Context, clusterID uint,
func (c *controller) createPipelineRun(ctx context.Context, clusterID uint,
r *CreatePipelineRunRequest) (*prmodels.Pipelinerun, error) {
defer wlog.Start(ctx, "cluster controller: create pipeline run").StopPrint()
var action string
var err error

cluster, err := c.clusterMgr.GetByID(ctx, clusterID)
if err != nil {
return nil, err
}
if r.Action == prmodels.ActionBuildDeploy && cluster.GitURL == "" {
return nil, herrors.ErrBuildDeployNotSupported
}

var gitURL, gitRef, gitRefType, imageURL, codeCommitID = cluster.GitURL,
cluster.GitRef, cluster.GitRefType, cluster.Image, cluster.GitRef
var (
title = r.Title
action string
gitURL = cluster.GitURL
gitRefType = cluster.GitRefType
gitRef = cluster.GitRef
codeCommitID string
imageURL = cluster.Image
rollbackFrom *uint
)

application, err := c.applicationMgr.GetByID(ctx, cluster.ApplicationID)
if err != nil {
Expand All @@ -717,6 +720,9 @@ func (c *controller) createPipelineRun(ctx context.Context, clusterID uint,
switch r.Action {
case prmodels.ActionBuildDeploy:
action = prmodels.ActionBuildDeploy
if cluster.GitURL == "" {
return nil, herrors.ErrBuildDeployNotSupported
}

if r.Git != nil {
if r.Git.Commit != "" {
Expand Down Expand Up @@ -768,6 +774,7 @@ func (c *controller) createPipelineRun(ctx context.Context, clusterID uint,
}

case prmodels.ActionRollback:
title = prmodels.ActionRollback
action = prmodels.ActionRollback

// get pipelinerun to rollback, and do some validation
Expand All @@ -787,14 +794,20 @@ func (c *controller) createPipelineRun(ctx context.Context, clusterID uint,
"the pipelinerun with id: %v is not belongs to cluster: %v", r.PipelinerunID, clusterID)
}

// Deprecated: for internal usage
err = c.checkAndSyncGitOpsBranch(ctx, application.Name, cluster.Name, pipelinerun.ConfigCommit)
if err != nil {
return nil, err
}
gitURL = pipelinerun.GitURL
gitRefType = pipelinerun.GitRefType
gitRef = pipelinerun.GitRef
codeCommitID = pipelinerun.GitCommit
imageURL = pipelinerun.ImageURL
rollbackFrom = &pipelinerun.ID
configCommitSHA = configCommit.Master

gitURL, gitRefType, gitRef, codeCommitID, imageURL =
cluster.GitURL, cluster.GitRefType, cluster.GitRef, pipelinerun.GitCommit, pipelinerun.ImageURL
case prmodels.ActionRestart:
title = prmodels.ActionRestart
action = prmodels.ActionRestart
if cluster.Status == common.ClusterStatusFreed {
return nil, herrors.ErrFreedClusterNotSupportedRestart
}
configCommitSHA = configCommit.Master

default:
Expand All @@ -805,7 +818,7 @@ func (c *controller) createPipelineRun(ctx context.Context, clusterID uint,
ClusterID: clusterID,
Action: action,
Status: string(prmodels.StatusPending),
Title: r.Title,
Title: title,
Description: r.Description,
GitURL: gitURL,
GitRefType: gitRefType,
Expand All @@ -814,5 +827,6 @@ func (c *controller) createPipelineRun(ctx context.Context, clusterID uint,
ImageURL: imageURL,
LastConfigCommit: lastConfigCommitSHA,
ConfigCommit: configCommitSHA,
RollbackFrom: rollbackFrom,
}, nil
}
22 changes: 17 additions & 5 deletions core/controller/cluster/controller_internal_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@ func (c *controller) InternalDeployV2(ctx context.Context, clusterID uint,
return nil
}

// get original config commit, and sync pipeline's last_config_commit in DB
originalConfigCommit, err := c.clusterGitRepo.GetConfigCommit(ctx, application.Name, cluster.Name)
if err != nil {
return nil, err
}
if err := c.prMgr.PipelineRun.UpdateColumns(ctx, pr.ID, map[string]interface{}{
"last_config_commit": originalConfigCommit.Master,
}); err != nil {
return nil, err
}

// 3. update pipeline output in git repo if builddeploy for gitImport and deploy for imageDeploy
if (pr.Action == prmodels.ActionBuildDeploy && pr.GitURL != "") ||
(pr.Action == prmodels.ActionDeploy && pr.GitURL == "") {
Expand All @@ -102,20 +113,21 @@ func (c *controller) InternalDeployV2(ctx context.Context, clusterID uint,
if err != nil {
return nil, perror.WithMessage(err, op)
}
// 4. update config commit and status
if err := c.prMgr.PipelineRun.UpdateConfigCommitByID(ctx, pr.ID, commit); err != nil {
return nil, err
}
if err := updatePRStatus(prmodels.StatusCommitted, commit); err != nil {
return nil, err
}
}

// 5. merge branch from gitops to master if diff is not empty and update status
// 4. update config commit
configCommit, err := c.clusterGitRepo.GetConfigCommit(ctx, application.Name, cluster.Name)
if err != nil {
return nil, err
}
if err := c.prMgr.PipelineRun.UpdateConfigCommitByID(ctx, pr.ID, configCommit.Gitops); err != nil {
return nil, err
}

// 5. merge branch from gitops to master if diff is not empty and update status
diff, err := c.clusterGitRepo.CompareConfig(ctx, application.Name, cluster.Name,
&configCommit.Master, &configCommit.Gitops)
if err != nil {
Expand Down
Loading

0 comments on commit aa5e0fa

Please sign in to comment.