Skip to content

Commit

Permalink
Merge pull request #1389 from merico-dev/refactor-scm-config-use-env
Browse files Browse the repository at this point in the history
feat: use config field instead of env
  • Loading branch information
aFlyBird0 authored Dec 27, 2022
2 parents b35405a + 7bd26d0 commit 93367e1
Show file tree
Hide file tree
Showing 35 changed files with 179 additions and 286 deletions.
4 changes: 4 additions & 0 deletions examples/apps.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ apps:
framework: django
repo:
url: github.com/[[ GITHUB_USER ]]/myapp1
token: [[ env GITHUB_TOKEN ]]
repoTemplate:
url: github.com/devstream-io/dtm-repo-scaffolding-python-flask
ci:
- type: github-actions
options:
imageRepo:
user: [[ DOCKERHUB_USER ]]
password: [[ env IMAGE_REPO_PASSWORD ]]
cd:
- type: argocdapp
- name: myapp2
Expand All @@ -34,12 +36,14 @@ apps:
framework: gin
repo:
url: github.com/[[ GITHUB_USER ]]/myapp2
token: [[ env GITHUB_TOKEN ]]
repoTemplate:
url: github.com/devstream-io/dtm-repo-scaffolding-golang-gin
ci:
- type: github-actions
options:
imageRepo:
user: [[ DOCKERHUB_USER ]]
password: [[ env IMAGE_REPO_PASSWORD ]]
cd:
- type: argocdapp
4 changes: 4 additions & 0 deletions examples/gitops.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ tools:
name: [[ app ]]
branch: main
scmType: github
token: [[ env GITHUB_TOKEN ]]
sourceRepo:
org: devstream-io
name: dtm-repo-scaffolding-python-flask
Expand All @@ -29,13 +30,15 @@ tools:
owner: [[ githubUser ]]
name: [[ app ]]
scmType: github
token: [[ env GITHUB_TOKEN ]]
pipeline:
configLocation: https://raw.githubusercontent.com/devstream-io/dtm-pipeline-templates/main/github-actions/workflows/main.yml
language:
name: python
framework: flask
imageRepo:
user: [[ dockerUser ]]
password: [[ env IMAGE_REPO_PASSWORD ]]
- name: helm-installer
instanceID: argocd
- name: argocdapp
Expand All @@ -52,5 +55,6 @@ tools:
valuefile: values.yaml
path: helm/[[ app ]]
repoURL: ${{repo-scaffolding.myapp.outputs.repoURL}}
token: [[ env GITHUB_TOKEN ]]
imageRepo:
user: [[ dockerUser ]]
3 changes: 3 additions & 0 deletions examples/quickstart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ tools:
name: go-webapp-devstream-demo
branch: main
scmType: github
token: [[ env GITHUB_TOKEN ]]
sourceRepo:
org: devstream-io
name: dtm-repo-scaffolding-golang-gin
Expand All @@ -30,10 +31,12 @@ tools:
name: go-webapp-devstream-demo
branch: main
scmType: github
token: [[ env GITHUB_TOKEN ]]
pipeline:
configLocation: https://raw.githubusercontent.com/devstream-io/dtm-pipeline-templates/main/github-actions/workflows/main.yml
language:
name: go
framework: gin
imageRepo:
user: [[ ImageRepoUser ]]
password: [[ env IMAGE_REPO_PASSWORD ]]
2 changes: 2 additions & 0 deletions internal/pkg/configmanager/pipelineDefault.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func pipelineArgocdAppGenerator(options RawOptions, globalVars *pipelineGlobalOp
if _, repoURLExist := sourceMap["repoURL"]; !repoURLExist {
sourceMap["repoURL"] = globalVars.Repo.GetCloneURL()
sourceMap["repoBranch"] = globalVars.Repo.Branch
sourceMap["token"] = globalVars.Repo.Token
}
options["source"] = sourceMap
} else {
Expand All @@ -92,6 +93,7 @@ func pipelineArgocdAppGenerator(options RawOptions, globalVars *pipelineGlobalOp
"path": fmt.Sprintf("helm/%s", globalVars.AppName),
"repoURL": string(globalVars.Repo.GetCloneURL()),
"repoBranch": globalVars.Repo.Branch,
"token": globalVars.Repo.Token,
}
}
// config imageRepo default options
Expand Down
1 change: 1 addition & 0 deletions internal/pkg/configmanager/pipelineDefault_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ var _ = Describe("pipelineArgocdAppGenerator func", func() {
"path": "helm/test_app",
"repoURL": "https://scm.test.com",
"repoBranch": "testBranch",
"token": "",
},
"imageRepo": RawOptions{
"owner": "test_user",
Expand Down
1 change: 1 addition & 0 deletions internal/pkg/plugin/argocdapp/argocdapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func pushArgocdConfigFiles(rawOptions configmanager.RawOptions) error {
repoInfo := &git.RepoInfo{
CloneURL: git.ScmURL(opts.Source.RepoURL),
Branch: opts.Source.RepoBranch,
Token: opts.Source.Token,
}
scmClient, err := scm.NewClientWithAuth(repoInfo)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions internal/pkg/plugin/argocdapp/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type source struct {
Path string `mapstructure:"path" validate:"required"`
RepoURL string `mapstructure:"repoURL" validate:"required"`
RepoBranch string `mapstructure:"repoBranch"`
Token string `mapstructure:"token"`
}

// / newOptions create options by raw options
Expand Down
5 changes: 3 additions & 2 deletions internal/pkg/plugin/gitlabci/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ var _ = Describe("action struct", func() {
"ImageRepoDockerSecret": "image-repo-auth",
"RepoType": "gitlab",
"imageRepo": map[string]interface{}{
"url": "exmaple.com",
"user": "test_user",
"url": "exmaple.com",
"user": "test_user",
"password": "",
},
"dingTalk": nilDingTalkConfig,
"DingTalkSecretKey": "DINGTALK_SECURITY_VALUE",
Expand Down
10 changes: 6 additions & 4 deletions internal/pkg/plugin/installer/ci/ci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ var _ = Describe("PipelineConfig struct", func() {
"ImageRepoSecret": "IMAGE_REPO_SECRET",
"ImageRepoDockerSecret": "image-repo-auth",
"imageRepo": map[string]interface{}{
"url": "exmaple.com",
"user": "test_user",
"url": "exmaple.com",
"user": "test_user",
"password": "",
},
"dingTalk": nilDingTalkConfig,
"DingTalkSecretKey": "DINGTALK_SECURITY_VALUE",
Expand Down Expand Up @@ -94,8 +95,9 @@ var _ = Describe("PipelineConfig struct", func() {
"StepGlobalVars": "",
"RepoType": "gitlab",
"imageRepo": map[string]interface{}{
"url": "exmaple.com",
"user": "test_user",
"url": "exmaple.com",
"user": "test_user",
"password": "",
},
"dingTalk": emptyDingtalk,
"sonarqube": emptySonar,
Expand Down
7 changes: 3 additions & 4 deletions internal/pkg/plugin/installer/ci/step/github.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package step

import (
"os"

"github.com/devstream-io/devstream/pkg/util/jenkins"
"github.com/devstream-io/devstream/pkg/util/log"
"github.com/devstream-io/devstream/pkg/util/scm"
"github.com/devstream-io/devstream/pkg/util/scm/github"
)

const (
Expand All @@ -15,6 +12,7 @@ const (

type GithubStepConfig struct {
RepoOwner string `mapstructure:"repoOwner"`
Token string `mapstructure:"token"`
}

func (g *GithubStepConfig) GetJenkinsPlugins() []*jenkins.JenkinsPlugin {
Expand All @@ -31,7 +29,7 @@ func (g *GithubStepConfig) ConfigJenkins(jenkinsClient jenkins.JenkinsAPI) (*jen
err := jenkinsClient.CreatePasswordCredential(
githubCredentialName,
g.RepoOwner,
os.Getenv(github.TokenEnvKey),
g.Token,
)
if err != nil {
log.Debugf("jenkins preinstall github credentials failed: %s", err)
Expand All @@ -52,5 +50,6 @@ func (g *GithubStepConfig) ConfigSCM(client scm.ClientOperation) error {
func newGithubStep(config *StepGlobalOption) *GithubStepConfig {
return &GithubStepConfig{
RepoOwner: config.RepoInfo.GetRepoOwner(),
Token: config.RepoInfo.Token,
}
}
6 changes: 3 additions & 3 deletions internal/pkg/plugin/installer/ci/step/gitlab.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package step

import (
"os"

"github.com/devstream-io/devstream/pkg/util/jenkins"
"github.com/devstream-io/devstream/pkg/util/log"
"github.com/devstream-io/devstream/pkg/util/scm"
Expand All @@ -18,6 +16,7 @@ type GitlabStepConfig struct {
SSHPrivateKey string `mapstructure:"sshPrivateKey"`
RepoOwner string `mapstructure:"repoOwner"`
BaseURL string `mapstructure:"baseURL"`
Token string `mapstructure:"token"`
}

func (g *GitlabStepConfig) GetJenkinsPlugins() []*jenkins.JenkinsPlugin {
Expand All @@ -42,7 +41,7 @@ func (g *GitlabStepConfig) ConfigJenkins(jenkinsClient jenkins.JenkinsAPI) (*jen
}
}
// 2. create gitlab connection by casc
err := jenkinsClient.CreateGiltabCredential(gitlabCredentialName, os.Getenv("GITLAB_TOKEN"))
err := jenkinsClient.CreateGiltabCredential(gitlabCredentialName, g.Token)
if err != nil {
log.Debugf("jenkins preinstall gitlab credentials failed: %s", err)
return nil, err
Expand All @@ -65,5 +64,6 @@ func newGitlabStep(config *StepGlobalOption) *GitlabStepConfig {
SSHPrivateKey: config.RepoInfo.SSHPrivateKey,
RepoOwner: config.RepoInfo.GetRepoOwner(),
BaseURL: config.RepoInfo.BaseURL,
Token: config.RepoInfo.Token,
}
}
21 changes: 9 additions & 12 deletions internal/pkg/plugin/installer/ci/step/imagerepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package step
import (
"encoding/base64"
"fmt"
"os"

"github.com/devstream-io/devstream/pkg/util/jenkins"
"github.com/devstream-io/devstream/pkg/util/k8s"
Expand All @@ -12,7 +11,6 @@ import (
)

const (
imageRepoPasswordEnv = "IMAGE_REPO_PASSWORD"
// imageRepoDockerSecretName is used for creating k8s secret
// and it will be used by jenkins for mount
imageRepoDockerSecretName = "image-repo-auth"
Expand All @@ -22,8 +20,9 @@ const (
)

type ImageRepoStepConfig struct {
URL string `mapstructure:"url"`
User string `mapstructure:"user"`
URL string `mapstructure:"url"`
User string `mapstructure:"user"`
Password string `mapstructure:"password"`
}

func (g *ImageRepoStepConfig) GetJenkinsPlugins() []*jenkins.JenkinsPlugin {
Expand All @@ -50,23 +49,21 @@ func (g *ImageRepoStepConfig) ConfigJenkins(jenkinsClient jenkins.JenkinsAPI) (*
}

func (g *ImageRepoStepConfig) ConfigSCM(client scm.ClientOperation) error {
imageRepoPasswd := os.Getenv(imageRepoPasswordEnv)
if imageRepoPasswd == "" {
return fmt.Errorf("the environment variable IMAGE_REPO_PASSWORD is not set")
if g.Password == "" {
return fmt.Errorf("config field password is not set")
}

if err := client.AddRepoSecret(imageRepoUserName, g.User); err != nil {
return err
}
return client.AddRepoSecret(imageRepoSecretName, imageRepoPasswd)
return client.AddRepoSecret(imageRepoSecretName, g.Password)
}

func (g *ImageRepoStepConfig) generateDockerAuthSecretData() (map[string][]byte, error) {
imageRepoPasswd := os.Getenv(imageRepoPasswordEnv)
if imageRepoPasswd == "" {
return nil, fmt.Errorf("the environment variable IMAGE_REPO_PASSWORD is not set")
if g.Password == "" {
return nil, fmt.Errorf("config field password is not set")
}
tmpStr := fmt.Sprintf("%s:%s", g.User, imageRepoPasswd)
tmpStr := fmt.Sprintf("%s:%s", g.User, g.Password)
authStr := base64.StdEncoding.EncodeToString([]byte(tmpStr))
authURL := g.GetImageRepoURL()
log.Debugf("jenkins plugin imageRepo auth string: %s.", authStr)
Expand Down
57 changes: 24 additions & 33 deletions internal/pkg/plugin/installer/ci/step/imagerepo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package step

import (
"fmt"
"os"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -32,29 +31,26 @@ var _ = Describe("ImageRepoStepConfig", func() {
})

Context("generateDockerAuthSecretData method", func() {
var (
existImageEnv string
testImageEnvVar string
)
BeforeEach(func() {
existImageEnv = os.Getenv("IMAGE_REPO_PASSWORD")
if existImageEnv != "" {
err := os.Unsetenv("IMAGE_REPO_PASSWORD")
Expect(err).Error().ShouldNot(HaveOccurred())
}
})
When("image env is not set", func() {
BeforeEach(func() {
c = &ImageRepoStepConfig{
URL: url,
User: user,
}
})
It("should return error", func() {
_, err := c.generateDockerAuthSecretData()
Expect(err).Error().Should(HaveOccurred())
Expect(err.Error()).Should(Equal("the environment variable IMAGE_REPO_PASSWORD is not set"))
Expect(err.Error()).Should(Equal("config field password is not set"))
})
})
When("image env is set", func() {
BeforeEach(func() {
testImageEnvVar = "test_image_repo_env"
err := os.Setenv("IMAGE_REPO_PASSWORD", testImageEnvVar)
Expect(err).Error().ShouldNot(HaveOccurred())
c = &ImageRepoStepConfig{
URL: url,
User: user,
Password: "test",
}
})
It("should return image auth data", func() {
d, err := c.generateDockerAuthSecretData()
Expand All @@ -69,48 +65,43 @@ var _ = Describe("ImageRepoStepConfig", func() {
"auth": "%s"
}
}
}`, c.URL, "dGVzdF91c2VyOnRlc3RfaW1hZ2VfcmVwb19lbnY=")
}`, c.URL, "dGVzdF91c2VyOnRlc3Q=")
Expect(string(configJson)).Should(Equal(expectStr))
})
})
})

Context("ConfigSCM method", func() {
var (
scmClient *scm.MockScmClient
errMsg, existImageEnv string
scmClient *scm.MockScmClient
)
BeforeEach(func() {
existImageEnv = os.Getenv("IMAGE_REPO_PASSWORD")
if existImageEnv != "" {
err := os.Unsetenv("IMAGE_REPO_PASSWORD")
Expect(err).Error().ShouldNot(HaveOccurred())
}
})
When("imageRepoPassword is not valid", func() {
BeforeEach(func() {
errMsg = "the environment variable IMAGE_REPO_PASSWORD is not set"
c = &ImageRepoStepConfig{
URL: url,
User: user,
}
scmClient = &scm.MockScmClient{}
})
It("should return error", func() {
err := c.ConfigSCM(scmClient)
Expect(err).Error().Should(HaveOccurred())
Expect(err.Error()).Should(Equal(errMsg))
Expect(err.Error()).Should(Equal("config field password is not set"))
})
})
When("all valid", func() {
BeforeEach(func() {
scmClient = &scm.MockScmClient{}
os.Setenv("IMAGE_REPO_PASSWORD", "test")
c = &ImageRepoStepConfig{
URL: url,
User: user,
Password: "test",
}
})
It("should return nil", func() {
err := c.ConfigSCM(scmClient)
Expect(err).Error().ShouldNot(HaveOccurred())
})
})
AfterEach(func() {
os.Setenv("IMAGE_REPO_PASSWORD", existImageEnv)
})
})

})
Loading

0 comments on commit 93367e1

Please sign in to comment.