-
Notifications
You must be signed in to change notification settings - Fork 7
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
GitOps plugin system and a "kanvas" plugin placeholder #973
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package main | ||
|
||
// GitOpsPlugin is the extension point for InteractorGitOps | ||
// It is used to support various GitOps tools. | ||
type GitOpsPlugin interface { | ||
Prepare(pj DeployProject, phase string, branch string, user User, message string) (o GitOpsPrepareOutput, err error) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package main | ||
|
||
// GitOpsPluginKanvas is a gocat gitops plugin to prepare | ||
// deployments using kanvas. | ||
// This is used when you want to use gocat as a workflow engine | ||
// with a chatops interface, while using kanvas as a deployment tool. | ||
// | ||
// Unlike GitOpsPluginKustomize which uses gocat's builtin Git and GitHub support, | ||
// GitOpsPluginKanvas uses kanvas's Git and GitHub support. | ||
type GitOpsPluginKanvas struct { | ||
github *GitHub | ||
git *GitOperator | ||
} | ||
|
||
func NewGitOpsPluginKanvas(github *GitHub, git *GitOperator) GitOpsPlugin { | ||
return &GitOpsPluginKanvas{github: github, git: git} | ||
} | ||
|
||
func (k GitOpsPluginKanvas) Prepare(pj DeployProject, phase string, branch string, assigner User, tag string) (o GitOpsPrepareOutput, err error) { | ||
o.status = DeployStatusFail | ||
if tag == "" { | ||
ecr, err := CreateECRInstance() | ||
if err != nil { | ||
return o, err | ||
} | ||
tag, err = ecr.FindImageTagByRegexp(pj.ECRRegistryId(), pj.ECRRepository(), pj.ImageTagRegexp(), pj.TargetRegexp(), ImageTagVars{Branch: branch, Phase: phase}) | ||
if err != nil { | ||
return o, err | ||
} | ||
} | ||
|
||
// TODO Enhance kanvas to create a git commit and branch from the image tag | ||
_ = tag | ||
|
||
// TODO Do kanvas deployment using specific PR title and description | ||
|
||
// TODO if the result code is "No difference", we should not create a pull request. | ||
// if tag == currentTag { | ||
// o.status = DeployStatusAlready | ||
// return | ||
// } | ||
|
||
// TODO Enhance kanvas to support setting assigner for the pull request | ||
// TODO Enhance kanvas to returning: | ||
// - pull request id | ||
// - pull request number | ||
// - pull request head branch | ||
|
||
o = GitOpsPrepareOutput{ | ||
// PullRequestID: prID, | ||
// PullRequestNumber: prNum, | ||
// Branch: prBranch, | ||
// status: DeployStatusSuccess, | ||
} | ||
return | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package main | ||
|
||
// GitOpsPluginKustomize is a gocat gitops plugin to prepare | ||
// deployments using kustomize and gocat's builtin Git and GitHub support. | ||
// This is used when you want to use gocat as a workflow engine | ||
// with a chatops interface, while using kustomize along with | ||
// the gocat native features as a deployment tool. | ||
type GitOpsPluginKustomize struct { | ||
github *GitHub | ||
git *GitOperator | ||
} | ||
|
||
func NewGitOpsPluginKustomize(github *GitHub, git *GitOperator) GitOpsPlugin { | ||
return &GitOpsPluginKustomize{github: github, git: git} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package main | ||
|
||
func NewInteractorKanavs(i InteractorContext) (o InteractorGitOps) { | ||
o = InteractorGitOps{ | ||
InteractorContext: i, | ||
model: NewGitOpsPluginKanvas(&o.github, &o.git), | ||
} | ||
o.kind = "kanvas" | ||
return | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package main | ||
|
||
func NewModelKanvas(github *GitHub, git *GitOperator) ModelGitOps { | ||
return ModelGitOps{ | ||
github: github, | ||
git: git, | ||
plugin: NewGitOpsPluginKanvas(github, git), | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,31 +5,36 @@ import ( | |
"strings" | ||
) | ||
|
||
type ModelKustomize struct { | ||
type ModelGitOps struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We turn the existing |
||
github *GitHub | ||
git *GitOperator | ||
plugin GitOpsPlugin | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is how we make it reusable; All the reusable logics reside within A |
||
} | ||
|
||
func NewModelKustomize(github *GitHub, git *GitOperator) ModelKustomize { | ||
return ModelKustomize{github: github, git: git} | ||
func NewModelKustomize(github *GitHub, git *GitOperator) ModelGitOps { | ||
return ModelGitOps{ | ||
github: github, | ||
git: git, | ||
plugin: NewGitOpsPluginKustomize(github, git), | ||
} | ||
} | ||
|
||
type ModelKustomizePrepareOutput struct { | ||
type GitOpsPrepareOutput struct { | ||
PullRequestID string | ||
PullRequestNumber int | ||
Branch string | ||
status DeployStatus | ||
} | ||
|
||
func (self ModelKustomizePrepareOutput) Status() DeployStatus { | ||
func (self GitOpsPrepareOutput) Status() DeployStatus { | ||
return self.status | ||
} | ||
|
||
func (self ModelKustomizePrepareOutput) Message() string { | ||
func (self GitOpsPrepareOutput) Message() string { | ||
return "Success to deploy" | ||
} | ||
|
||
func (self ModelKustomize) Prepare(pj DeployProject, phase string, branch string, assigner User, tag string) (o ModelKustomizePrepareOutput, err error) { | ||
func (k GitOpsPluginKustomize) Prepare(pj DeployProject, phase string, branch string, assigner User, tag string) (o GitOpsPrepareOutput, err error) { | ||
o.status = DeployStatusFail | ||
if tag == "" { | ||
ecr, err := CreateECRInstance() | ||
|
@@ -43,7 +48,7 @@ func (self ModelKustomize) Prepare(pj DeployProject, phase string, branch string | |
} | ||
|
||
ph := pj.FindPhase(phase) | ||
currentTag, err := ph.Destination.GetCurrentRevision(GetCurrentRevisionInput{github: self.github}) | ||
currentTag, err := ph.Destination.GetCurrentRevision(GetCurrentRevisionInput{github: k.github}) | ||
if err != nil { | ||
return | ||
} | ||
|
@@ -53,7 +58,7 @@ func (self ModelKustomize) Prepare(pj DeployProject, phase string, branch string | |
return | ||
} | ||
|
||
commits, err := self.github.CommitsBetween(GitHubCommitsBetweenInput{ | ||
commits, err := k.github.CommitsBetween(GitHubCommitsBetweenInput{ | ||
Repository: pj.GitHubRepository(), | ||
Branch: branch, | ||
FirstCommitID: currentTag, | ||
|
@@ -66,24 +71,24 @@ func (self ModelKustomize) Prepare(pj DeployProject, phase string, branch string | |
commitlog = commitlog + "- " + m + "\n" | ||
} | ||
|
||
prBranch, err := self.git.PushDockerImageTag(pj.ID, ph, tag, pj.DockerRepository()) | ||
prBranch, err := k.git.PushDockerImageTag(pj.ID, ph, tag, pj.DockerRepository()) | ||
if err != nil { | ||
return | ||
} | ||
|
||
prID, prNum, err := self.github.CreatePullRequest(prBranch, fmt.Sprintf("Deploy %s %s", pj.ID, branch), commitlog) | ||
prID, prNum, err := k.github.CreatePullRequest(prBranch, fmt.Sprintf("Deploy %s %s", pj.ID, branch), commitlog) | ||
if err != nil { | ||
return | ||
} | ||
|
||
if assigner.GitHubNodeID != "" { | ||
err = self.github.UpdatePullRequest(prID, assigner.GitHubNodeID) | ||
err = k.github.UpdatePullRequest(prID, assigner.GitHubNodeID) | ||
if err != nil { | ||
return | ||
} | ||
} | ||
|
||
o = ModelKustomizePrepareOutput{ | ||
o = GitOpsPrepareOutput{ | ||
PullRequestID: prID, | ||
PullRequestNumber: prNum, | ||
Branch: prBranch, | ||
|
@@ -92,13 +97,13 @@ func (self ModelKustomize) Prepare(pj DeployProject, phase string, branch string | |
return | ||
} | ||
|
||
func (self ModelKustomize) Commit(pullRequestID string) error { | ||
func (self ModelGitOps) Commit(pullRequestID string) error { | ||
return self.github.MergePullRequest(pullRequestID) | ||
|
||
} | ||
|
||
func (self ModelKustomize) Deploy(pj DeployProject, phase string, option DeployOption) (do DeployOutput, err error) { | ||
o, err := self.Prepare(pj, phase, option.Branch, option.Assigner, option.Tag) | ||
func (self ModelGitOps) Deploy(pj DeployProject, phase string, option DeployOption) (do DeployOutput, err error) { | ||
o, err := self.plugin.Prepare(pj, phase, option.Branch, option.Assigner, option.Tag) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This is the only implementation-specific part of ModelGitOps, and therefore this is the only place you see For example, |
||
if err != nil { | ||
return | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is how we make it reusable; All the reusable logics reside within
InteractorGitOps
, and tool-specific logic resides within eachGitOpsPlugin
likeGitOpsPluginKustomize
andGitOpsPluginKanvas
.A
InteractorGitOps
instance along withGitOpsPluginKustomize
works exactly same as the currentInteractorKustomize
, so this change is backward-compatible.This is about Interactor. For Model, see https://github.com/zaiminc/gocat/pull/973/files#r1395058220