-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1259 from merico-dev/feat-app-spec-option
feat: app support ci/cd default config for app.spec
- Loading branch information
Showing
23 changed files
with
542 additions
and
399 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package configmanager | ||
|
||
import ( | ||
"github.com/imdario/mergo" | ||
|
||
"github.com/devstream-io/devstream/pkg/util/log" | ||
"github.com/devstream-io/devstream/pkg/util/mapz" | ||
) | ||
|
||
// appSpec is app special options | ||
type appSpec struct { | ||
// language config | ||
Language string `yaml:"language" mapstructure:"language"` | ||
FrameWork string `yaml:"framework" mapstructure:"framework"` | ||
} | ||
|
||
// merge will merge vars and appSpec | ||
func (s *appSpec) merge(vars map[string]any) map[string]any { | ||
specMap, err := mapz.DecodeStructToMap(s) | ||
if err != nil { | ||
log.Warnf("appspec %+v decode failed: %+v", s, err) | ||
return map[string]any{} | ||
} | ||
if err := mergo.Merge(&specMap, vars); err != nil { | ||
log.Warnf("appSpec %+v merge map failed: %+v", s, err) | ||
return vars | ||
} | ||
return specMap | ||
} | ||
|
||
func (s *appSpec) updatePiplineOption(options RawOptions) { | ||
if _, exist := options["language"]; !exist && s.hasLanguageConfig() { | ||
options["language"] = RawOptions{ | ||
"name": s.Language, | ||
"framework": s.FrameWork, | ||
} | ||
} | ||
} | ||
|
||
func (s *appSpec) hasLanguageConfig() bool { | ||
return s.Language != "" || s.FrameWork != "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package configmanager | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/devstream-io/devstream/pkg/util/log" | ||
) | ||
|
||
var optionConfiguratorMap = map[string]pipelineOption{ | ||
"github-actions": githubGeneral, | ||
"gitlab-ci": gitlabGeneral, | ||
"jenkins-pipeline": jenkinsGeneral, | ||
"argocdapp": argocdApp, | ||
} | ||
|
||
type pipelineOptionGenerator func(originOption RawOptions, app *app) RawOptions | ||
|
||
type pipelineOption struct { | ||
defaultConfigLocation string | ||
optionGeneratorFunc pipelineOptionGenerator | ||
} | ||
|
||
// TODO(steinliber) unify all ci/cd config to same config options | ||
var ( | ||
// github actions pipeline options | ||
githubGeneral = pipelineOption{ | ||
defaultConfigLocation: "git@github.com:devstream-io/ci-template.git//github-actions", | ||
optionGeneratorFunc: pipelineGeneralGenerator, | ||
} | ||
gitlabGeneral = pipelineOption{ | ||
defaultConfigLocation: "https://raw.githubusercontent.com/devstream-io/ci-template/main/gitlab-ci/.gitlab-ci.yml", | ||
optionGeneratorFunc: pipelineGeneralGenerator, | ||
} | ||
jenkinsGeneral = pipelineOption{ | ||
defaultConfigLocation: "https://raw.githubusercontent.com/devstream-io/ci-template/main/jenkins-pipeline/general/Jenkinsfile", | ||
optionGeneratorFunc: pipelineGeneralGenerator, | ||
} | ||
argocdApp = pipelineOption{ | ||
optionGeneratorFunc: pipelineArgocdAppGenerator, | ||
} | ||
) | ||
|
||
// pipelineGeneralGenerator generate pipeline general options from RawOptions | ||
func pipelineGeneralGenerator(options RawOptions, app *app) RawOptions { | ||
if app.Spec != nil { | ||
app.Spec.updatePiplineOption(options) | ||
} | ||
// update image related config | ||
newOption := make(RawOptions) | ||
newOption["pipeline"] = options | ||
newOption["scm"] = RawOptions(app.Repo.Encode()) | ||
return newOption | ||
} | ||
|
||
// pipelineArgocdAppGenerator generate argocdApp options from RawOptions | ||
func pipelineArgocdAppGenerator(options RawOptions, app *app) RawOptions { | ||
// config app default options | ||
if _, exist := options["app"]; !exist { | ||
options["app"] = RawOptions{ | ||
"name": app.Name, | ||
"namespace": "argocd", | ||
} | ||
} | ||
// config destination options | ||
if _, exist := options["destination"]; !exist { | ||
options["destination"] = RawOptions{ | ||
"server": "https://kubernetes.default.svc", | ||
"namespace": "default", | ||
} | ||
} | ||
// config source default options | ||
repoInfo, err := app.Repo.BuildRepoInfo() | ||
if err != nil { | ||
log.Errorf("parse argocd repoInfo failed: %+v", err) | ||
return options | ||
} | ||
if source, sourceExist := options["source"]; sourceExist { | ||
sourceMap := source.(RawOptions) | ||
if _, repoURLExist := sourceMap["repoURL"]; !repoURLExist { | ||
sourceMap["repoURL"] = repoInfo.CloneURL | ||
} | ||
options["source"] = sourceMap | ||
} else { | ||
options["source"] = RawOptions{ | ||
"valuefile": "values.yaml", | ||
"path": fmt.Sprintf("helm/%s", app.Name), | ||
"repoURL": repoInfo.CloneURL, | ||
} | ||
} | ||
return options | ||
} | ||
|
||
// hasDefaultConfig check whether | ||
func (o *pipelineOption) hasDefaultConfig() bool { | ||
return o.defaultConfigLocation != "" | ||
} |
Oops, something went wrong.