Skip to content

Commit 97b0ffe

Browse files
authored
Merge pull request #1419 from merico-dev/fix-ci-plugin-ciLocaiton-error
refactor: ci plugins and gitlab runner
2 parents 59e1e41 + 352dce8 commit 97b0ffe

File tree

30 files changed

+172
-185
lines changed

30 files changed

+172
-185
lines changed

docs/use-cases/gitops-python-flask/4-gitlab-dtm-apps.zh.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ pipelineTemplates:
206206
- name: ci-pipeline
207207
type: gitlab-ci
208208
options:
209+
runner:
210+
enable: true
209211
imageRepo:
210212
user: [[ dockerhubUser ]]
211213
password: [[ env DOCKERHUB_TOKEN ]] # use "DOCKERHUB_TOKEN" env var

internal/pkg/configmanager/pipelineDefault.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ var (
2828
}
2929
gitlabGeneral = pipelineOption{
3030
defaultConfigLocation: "https://raw.githubusercontent.com/devstream-io/dtm-pipeline-templates/main/gitlab-ci/.gitlab-ci.yml",
31-
optionGeneratorFunc: pipelineGeneralGenerator,
31+
optionGeneratorFunc: gitlabCIGenerator,
3232
}
3333
jenkinsGeneral = pipelineOption{
3434
defaultConfigLocation: "https://raw.githubusercontent.com/devstream-io/dtm-pipeline-templates/main/jenkins-pipeline/general/Jenkinsfile",
@@ -50,6 +50,17 @@ func jenkinsGenerator(options RawOptions, globalVars *pipelineGlobalOption) RawO
5050
return newOptions
5151
}
5252

53+
// gitlabGenerator generate gitlab ci config
54+
func gitlabCIGenerator(options RawOptions, globalVars *pipelineGlobalOption) RawOptions {
55+
newOptions := pipelineGeneralGenerator(options, globalVars)
56+
// extract jenkins config from options
57+
gitlabRunnerOption, exist := options["runner"]
58+
if exist {
59+
newOptions["runner"] = gitlabRunnerOption
60+
}
61+
return newOptions
62+
}
63+
5364
// pipelineGeneralGenerator generate pipeline general options from RawOptions
5465
func pipelineGeneralGenerator(options RawOptions, globalVars *pipelineGlobalOption) RawOptions {
5566
if globalVars.AppSpec != nil {

internal/pkg/plugin/githubactions/create.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package general
33
import (
44
"github.com/devstream-io/devstream/internal/pkg/configmanager"
55
"github.com/devstream-io/devstream/internal/pkg/plugin/installer"
6-
"github.com/devstream-io/devstream/internal/pkg/plugin/installer/ci"
76
"github.com/devstream-io/devstream/internal/pkg/plugin/installer/ci/cifile"
87
"github.com/devstream-io/devstream/pkg/util/log"
98
)
@@ -12,7 +11,7 @@ func Create(options map[string]interface{}) (map[string]interface{}, error) {
1211
// Initialize Operator with Operations
1312
operator := &installer.Operator{
1413
PreExecuteOperations: installer.PreExecuteOperations{
15-
ci.SetDefault(ciType),
14+
setDefault,
1615
validate,
1716
},
1817
ExecuteOperations: installer.ExecuteOperations{

internal/pkg/plugin/githubactions/delete.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@ package general
33
import (
44
"github.com/devstream-io/devstream/internal/pkg/configmanager"
55
"github.com/devstream-io/devstream/internal/pkg/plugin/installer"
6-
"github.com/devstream-io/devstream/internal/pkg/plugin/installer/ci"
76
"github.com/devstream-io/devstream/internal/pkg/plugin/installer/ci/cifile"
87
)
98

109
func Delete(options map[string]interface{}) (bool, error) {
1110
// Initialize Operator with Operations
1211
operator := &installer.Operator{
1312
PreExecuteOperations: installer.PreExecuteOperations{
14-
ci.SetDefault(ciType),
13+
setDefault,
1514
validate,
1615
},
1716
ExecuteOperations: installer.ExecuteOperations{

internal/pkg/plugin/githubactions/githubactions.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@ package general
33
import (
44
"github.com/devstream-io/devstream/internal/pkg/configmanager"
55
"github.com/devstream-io/devstream/internal/pkg/plugin/installer/ci"
6-
"github.com/devstream-io/devstream/internal/pkg/plugin/installer/ci/cifile/server"
76
"github.com/devstream-io/devstream/internal/pkg/plugin/installer/ci/step"
7+
"github.com/devstream-io/devstream/internal/pkg/plugin/installer/util"
88
"github.com/devstream-io/devstream/pkg/util/log"
99
"github.com/devstream-io/devstream/pkg/util/scm"
1010
)
1111

12-
var ciType = server.CIGithubType
13-
1412
func preConfigGithub(options configmanager.RawOptions) error {
15-
opts, err := ci.NewCIOptions(options)
16-
if err != nil {
13+
opts := new(ci.CIConfig)
14+
if err := util.DecodePlugin(options, opts); err != nil {
1715
return err
1816
}
1917

internal/pkg/plugin/githubactions/read.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package general
33
import (
44
"github.com/devstream-io/devstream/internal/pkg/configmanager"
55
"github.com/devstream-io/devstream/internal/pkg/plugin/installer"
6-
"github.com/devstream-io/devstream/internal/pkg/plugin/installer/ci"
76
"github.com/devstream-io/devstream/internal/pkg/plugin/installer/ci/cifile"
87
"github.com/devstream-io/devstream/pkg/util/log"
98
)
@@ -12,7 +11,7 @@ func Read(options map[string]interface{}) (map[string]interface{}, error) {
1211
// Initialize Operator with Operations
1312
operator := &installer.Operator{
1413
PreExecuteOperations: installer.PreExecuteOperations{
15-
ci.SetDefault(ciType),
14+
setDefault,
1615
validate,
1716
},
1817
GetStatusOperation: cifile.GetCIFileStatus,

internal/pkg/plugin/githubactions/validate.go

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,39 @@ import (
55

66
"github.com/devstream-io/devstream/internal/pkg/configmanager"
77
"github.com/devstream-io/devstream/internal/pkg/plugin/installer/ci"
8+
"github.com/devstream-io/devstream/internal/pkg/plugin/installer/ci/cifile/server"
9+
"github.com/devstream-io/devstream/internal/pkg/plugin/installer/util"
10+
"github.com/devstream-io/devstream/pkg/util/mapz"
11+
"github.com/devstream-io/devstream/pkg/util/validator"
812
)
913

1014
// validate validates the options provided by the core.
1115
func validate(options configmanager.RawOptions) (configmanager.RawOptions, error) {
12-
opts, err := ci.NewCIOptions(options)
13-
if err != nil {
16+
opts := new(ci.CIConfig)
17+
if err := util.DecodePlugin(options, opts); err != nil {
1418
return nil, err
1519
}
16-
// check basic ci error
17-
_, err = ci.Validate(options)
18-
if err != nil {
20+
// check struct data
21+
if err := validator.CheckStructError(opts).Combine(); err != nil {
1922
return nil, err
2023
}
24+
2125
// check repo is valid
22-
if !opts.ProjectRepo.IsGithubRepo() {
23-
return nil, fmt.Errorf("github action only support other repo")
26+
if opts.ProjectRepo.RepoType != "github" {
27+
return nil, fmt.Errorf("github action only support github repo")
2428
}
2529
return options, nil
2630
}
31+
32+
func setDefault(options configmanager.RawOptions) (configmanager.RawOptions, error) {
33+
opts := new(ci.CIConfig)
34+
if err := util.DecodePlugin(options, opts); err != nil {
35+
return nil, err
36+
}
37+
// set default value of pipeline location
38+
if opts.Pipeline.ConfigLocation == "" {
39+
opts.Pipeline.ConfigLocation = "https://raw.githubusercontent.com/devstream-io/dtm-pipeline-templates/main/github-actions/workflows/main.yml"
40+
}
41+
opts.CIFileConfig = opts.Pipeline.BuildCIFileConfig(server.CIGithubType, opts.ProjectRepo)
42+
return mapz.DecodeStructToMap(opts)
43+
}

internal/pkg/plugin/gitlabci/create.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@ package gitlabci
33
import (
44
"github.com/devstream-io/devstream/internal/pkg/configmanager"
55
"github.com/devstream-io/devstream/internal/pkg/plugin/installer"
6-
"github.com/devstream-io/devstream/internal/pkg/plugin/installer/ci"
76
"github.com/devstream-io/devstream/internal/pkg/plugin/installer/ci/cifile"
8-
"github.com/devstream-io/devstream/internal/pkg/plugin/installer/ci/cifile/server"
97
"github.com/devstream-io/devstream/internal/pkg/statemanager"
108
"github.com/devstream-io/devstream/pkg/util/log"
119
)
1210

1311
func Create(options configmanager.RawOptions) (statemanager.ResourceStatus, error) {
1412
operator := &installer.Operator{
1513
PreExecuteOperations: installer.PreExecuteOperations{
16-
ci.SetDefault(server.CIGitLabType),
14+
setDefault,
1715
validate,
1816
},
1917
ExecuteOperations: installer.ExecuteOperations{

internal/pkg/plugin/gitlabci/delete.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,17 @@ package gitlabci
33
import (
44
"github.com/devstream-io/devstream/internal/pkg/configmanager"
55
"github.com/devstream-io/devstream/internal/pkg/plugin/installer"
6-
"github.com/devstream-io/devstream/internal/pkg/plugin/installer/ci"
76
"github.com/devstream-io/devstream/internal/pkg/plugin/installer/ci/cifile"
8-
"github.com/devstream-io/devstream/internal/pkg/plugin/installer/ci/cifile/server"
97
)
108

119
func Delete(options configmanager.RawOptions) (bool, error) {
1210
operator := &installer.Operator{
1311
PreExecuteOperations: installer.PreExecuteOperations{
14-
ci.SetDefault(server.CIGitLabType),
12+
setDefault,
1513
validate,
1614
},
1715
ExecuteOperations: installer.ExecuteOperations{
16+
//TODO(steinliber): delete gitlab runner if it exists
1817
cifile.DeleteCIFiles,
1918
},
2019
}

internal/pkg/plugin/gitlabci/gitlabci.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66

77
"github.com/devstream-io/devstream/internal/pkg/configmanager"
88
"github.com/devstream-io/devstream/internal/pkg/plugin/helminstaller"
9-
"github.com/devstream-io/devstream/internal/pkg/plugin/installer/ci"
109
"github.com/devstream-io/devstream/internal/pkg/plugin/installer/ci/step"
10+
"github.com/devstream-io/devstream/internal/pkg/plugin/installer/util"
1111
"github.com/devstream-io/devstream/pkg/util/log"
1212
"github.com/devstream-io/devstream/pkg/util/scm"
1313
"github.com/devstream-io/devstream/pkg/util/scm/git"
@@ -18,9 +18,9 @@ import (
1818
//go:embed tpl/helmValue.tpl.yaml
1919
var helmValueTpl string
2020

21-
func preConfigGitlab(options configmanager.RawOptions) error {
22-
opts, err := ci.NewCIOptions(options)
23-
if err != nil {
21+
func preConfigGitlab(rawOptions configmanager.RawOptions) error {
22+
opts := new(options)
23+
if err := util.DecodePlugin(rawOptions, opts); err != nil {
2424
return err
2525
}
2626

@@ -38,7 +38,11 @@ func preConfigGitlab(options configmanager.RawOptions) error {
3838
return err
3939
}
4040
}
41-
return createGitlabRunnerByHelm(opts.ProjectRepo)
41+
// create runner in self-host gitlab
42+
if opts.needCreateRunner() {
43+
return createGitlabRunnerByHelm(opts.ProjectRepo)
44+
}
45+
return nil
4246
}
4347

4448
// createGitlabRunnerByHelm will install gitlab runner if it's not exist
@@ -91,5 +95,10 @@ func createGitlabRunnerByHelm(repoInfo *git.RepoInfo) error {
9195
},
9296
}
9397
_, err = helminstaller.Create(helmOptions)
94-
return err
98+
if err != nil {
99+
log.Debugf("gitlab ci install runner by helm failed: %s", err)
100+
return fmt.Errorf("gitlabci create runner by helm failed, please check your kubeneretes config")
101+
}
102+
// disable gitlab shared runner
103+
return gitlabClient.DisableRepoSharedRunner()
95104
}

0 commit comments

Comments
 (0)