Skip to content

Commit

Permalink
feat: support for checks of rollback and restart
Browse files Browse the repository at this point in the history
Signed-off-by: xu.zhu <[email protected]>
  • Loading branch information
xuzhu-591 committed Dec 11, 2023
1 parent ed83f01 commit 51982fd
Show file tree
Hide file tree
Showing 10 changed files with 408 additions and 114 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
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,
}
}
37 changes: 24 additions & 13 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,8 +683,6 @@ 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 {
Expand All @@ -694,8 +692,16 @@ func (c *controller) createPipelineRun(ctx context.Context, clusterID uint,
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 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,17 @@ 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
configCommitSHA = configCommit.Master

default:
Expand All @@ -805,7 +815,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 +824,6 @@ func (c *controller) createPipelineRun(ctx context.Context, clusterID uint,
ImageURL: imageURL,
LastConfigCommit: lastConfigCommitSHA,
ConfigCommit: configCommitSHA,
RollbackFrom: rollbackFrom,
}, nil
}
86 changes: 5 additions & 81 deletions core/controller/cluster/controller_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func getDeployImage(imageURL, deployTag string) (string, error) {

func (c *controller) Rollback(ctx context.Context,
clusterID uint, r *RollbackRequest) (_ *PipelinerunIDResponse, err error) {
const op = "cluster controller: rollback "
const op = "cluster controller: rollback"
defer wlog.Start(ctx, op).StopPrint()

// 1. get pipelinerun to rollback, and do some validation
Expand Down Expand Up @@ -335,8 +335,8 @@ func (c *controller) Rollback(ctx context.Context,
return nil, err
}

// Deprecated: for internal usage
err = c.checkAndSyncGitOpsBranch(ctx, application.Name, cluster.Name, pipelinerun.ConfigCommit)
// for internal usage
err = c.clusterGitRepo.CheckAndSyncGitOpsBranch(ctx, application.Name, cluster.Name, pipelinerun.ConfigCommit)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -368,7 +368,7 @@ func (c *controller) Rollback(ctx context.Context,

// 6. update template and tags in db
// TODO(zhuxu): remove strong dependencies on db updates, just print an err log when updates fail
cluster, err = c.updateTemplateAndTagsFromFile(ctx, application, cluster)
cluster, err = c.clusterSvc.SyncDBWithGitRepo(ctx, application, cluster)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -671,7 +671,7 @@ func (c *controller) Upgrade(ctx context.Context, clusterID uint) error {
}

// 3. sync gitops branch if restarts occur
err = c.syncGitOpsBranch(ctx, application.Name, cluster.Name)
err = c.clusterGitRepo.SyncGitOpsBranch(ctx, application.Name, cluster.Name)
if err != nil {
return err
}
Expand Down Expand Up @@ -765,79 +765,3 @@ func (c *controller) updateTemplateAndTagsFromFile(ctx context.Context,
}
return cluster, nil
}

func (c *controller) checkAndSyncGitOpsBranch(ctx context.Context, application,
cluster string, commit string) error {
changed, err := c.manifestVersionChanged(ctx, application, cluster, commit)
if err != nil {
return err
}
if changed {
err = c.syncGitOpsBranch(ctx, application, cluster)
if err != nil {
return err
}
}
return nil
}

// Deprecated: for internal usage
// manifestVersionChanged determines whether manifest version is changed
func (c *controller) manifestVersionChanged(ctx context.Context, application,
cluster string, commit string) (bool, error) {
currentManifest, err1 := c.clusterGitRepo.GetManifest(ctx, application, cluster, nil)
if err1 != nil {
if _, ok := perror.Cause(err1).(*herrors.HorizonErrNotFound); !ok {
log.Errorf(ctx, "get cluster manifest error, err = %s", err1.Error())
return false, err1
}
}
targetManifest, err2 := c.clusterGitRepo.GetManifest(ctx, application, cluster, &commit)
if err2 != nil {
if _, ok := perror.Cause(err2).(*herrors.HorizonErrNotFound); !ok {
log.Errorf(ctx, "get cluster manifest error, err = %s", err2.Error())
return false, err2
}
}
if err1 != nil && err2 != nil {
// manifest does not exist in both revisions
return false, nil
}
if err1 != nil || err2 != nil {
// One exists and the other does not exist in two revisions
return true, nil
}
return currentManifest.Version != targetManifest.Version, nil
}

// Deprecated: for internal usage
// syncGitOpsBranch syncs gitOps branch with default branch to avoid merge conflicts.
// Restart updates time in restart.yaml in default branch. When other actions update
// template prefix in gitOps branch, there are merge conflicts in restart.yaml because
// usual context lines of 'git diff' are three. Ref: https://git-scm.com/docs/git-diff
// For example:
//
// <<<<<<< HEAD
// javaapp:
// restartTime: "2025-02-19 10:24:52"
// =======
// rollout:
// restartTime: "2025-02-14 12:12:07"
// >>>>>>> gitops
func (c *controller) syncGitOpsBranch(ctx context.Context, application, cluster string) error {
gitOpsBranch := gitrepo.GitOpsBranch
defaultBranch := c.clusterGitRepo.DefaultBranch()
diff, err := c.clusterGitRepo.CompareConfig(ctx, application, cluster,
&gitOpsBranch, &defaultBranch)
if err != nil {
return err
}
if diff != "" {
_, err = c.clusterGitRepo.MergeBranch(ctx, application,
cluster, defaultBranch, gitOpsBranch, nil)
if err != nil {
return err
}
}
return nil
}
2 changes: 1 addition & 1 deletion core/controller/member/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func createContext(t *testing.T) {
groupSvc = groupservice.NewService(manager)

applicationSvc = applicationservice.NewService(groupSvc, manager)
clusterSvc = clusterservice.NewService(applicationSvc, manager)
clusterSvc = clusterservice.NewService(applicationSvc, nil, manager)
eventSvc = eventservice.New(manager)
}

Expand Down
Loading

0 comments on commit 51982fd

Please sign in to comment.