Skip to content
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

refactor: the logic refactor with name/instance/plugin #430

Merged
merged 11 commits into from
Apr 23, 2022
Merged
9 changes: 9 additions & 0 deletions examples/gitops-variables.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defaultBranch: main
githubUsername: daniel-hutao
repoName: dtm-test-go
jiraID: merico
jiraUserEmail: tao.hu@merico.dev
jiraProjectKey: DT
dockerhubUsername: exploitht
argocdNameSpace: argocd
argocdDeployTimeout: 10m
116 changes: 34 additions & 82 deletions examples/gitops.yaml
Original file line number Diff line number Diff line change
@@ -1,119 +1,71 @@
tools:
# name of the instance with github-repo-scaffolding-golang
- name: go-webapp-repo
plugin: github-repo-scaffolding-golang
# options for the plugin
- name: github-repo-scaffolding-golang
instanceID: default
options:
# the repo's owner. It should be case-sensitive here; strictly use your GitHub username; please change the value below.
owner: YOUR_GITHUB_USERNAME
# the repo which you'd like to create; please change the value below.
repo: YOUR_REPO_NAME
# the branch of the repo you'd like to hold the code
branch: main
# the image repo you'd like to push the container image; please change the value below.
image_repo: YOUR_DOCKERHUB_USERNAME/YOUR_DOCKERHUB_REPOSITORY
- name: jira-github-default
plugin: jira-github-integ
# optional; if specified, dtm will make sure the dependency is applied first before handling this tool.
dependsOn: [ "go-webapp-repo.github-repo-scaffolding-golang" ]
# options for the plugin
owner: [[ githubUsername ]]
org: ""
repo: [[ repoName ]]
branch: [[ defaultBranch ]]
image_repo: [[ dockerhubUsername ]]/[[ repoName ]]
- name: jira-github-integ
instanceID: default
dependsOn: [ "github-repo-scaffolding-golang.default" ]
options:
# the repo's owner
owner: YOUR_GITHUB_USERNAME
# the repo where you'd like to setup GitHub Actions
repo: YOUR_REPO_NAME
# "base url: https://id.atlassian.net"
jiraBaseUrl: https://JIRA_ID.atlassian.net
# "need real user email in cloud Jira"
jiraUserEmail: JIRA_USER_EMAIL
# "get it from project url, like 'HEAP' from https://merico.atlassian.net/jira/software/projects/HEAP/pages"
jiraProjectKey: JIRA_PROJECT_KEY
# main branch of the repo (to which branch the plugin will submit the workflows)
owner: [[ githubUsername ]]
repo: [[ repoName ]]
jiraBaseUrl: https://[[ jiraID ]].atlassian.net
jiraUserEmail: [[ jiraUserEmail ]]
jiraProjectKey: [[ jiraProjectKey ]]
branch: main
- name: golang-webapp-actions
plugin: githubactions-golang
# optional; if specified, dtm will make sure the dependency is applied first before handling this tool.
dependsOn: [ "go-webapp-repo.github-repo-scaffolding-golang" ]
# options for the plugin
- name: githubactions-golang
instanceID: default
dependsOn: [ "github-repo-scaffolding-golang.default" ]
options:
# the repo's owner. It should be case-sensitive here; strictly use your GitHub user name; please change the value below.
owner: YOUR_GITHUB_USERNAME
# the repo where you'd like to setup GitHub Actions; please change the value below to an existing repo.
repo: YOUR_REPO_NAME
# programming language specific settings
owner: ${{github-repo-scaffolding-golang.default.outputs.owner}}
org: ""
repo: ${{github-repo-scaffolding-golang.default.outputs.repo}}
language:
name: go
# version of the language
version: "1.17"
# main branch of the repo (to which branch the plugin will submit the workflows)
branch: main
branch: [[ defaultBranch ]]
build:
# default to false
enable: True
# build command, OPTIONAL, the given value below is default value
command: "go build ./..."
test:
# default to false
enable: True
# test command, OPTIONAL, the given value below is default value
command: "go test ./..."
coverage:
# default to false
enable: True
# go test profile subcommand, OPTIONAL, the given value below is default value
profile: "-race -covermode=atomic"
output: "coverage.out"
docker:
# docker build/push related, default to false
enable: False
# dockerhub image repo; please change the value below.
repo: YOUR_DOCKERHUB_REPOSITORY
- name: argocd-dev
plugin: argocd
repo: ${{github-repo-scaffolding-golang.default.outputs.repo}}
- name: argocd
instanceID: default
options:
# need to create the namespace or not, default: false
create_namespace: true
repo:
# name of the Helm repo
name: argo
# url of the Helm repo
url: https://argoproj.github.io/argo-helm
# Helm chart information
chart:
# name of the chart
chart_name: argo/argo-cd
# release name of the chart
release_name: argocd
# k8s namespace where ArgoCD will be installed
namespace: argocd
# whether to wait for the release to be deployed or not
namespace: [[ argocdNameSpace ]]
wait: true
# the time to wait for any individual Kubernetes operation (like Jobs for hooks). This defaults to 5m0s
timeout: 5m
# whether to perform a CRD upgrade during installation
timeout: [[ argocdDeployTimeout ]]
upgradeCRDs: true
- name: helloworld
plugin: argocdapp
dependsOn: [ "argocd-dev.argocd" ]
# options for the plugin
- name: argocdapp
instanceID: default
dependsOn: ["argocd.default", "github-repo-scaffolding-golang.default"]
options:
# information on the ArgoCD application
app:
# name of the ArgoCD application
name: hello
# where the ArgoCD application CRD will be created
namespace: argocd
# destination of the application
name: ${{github-repo-scaffolding-golang.default.outputs.repo}}
namespace: [[ argocdNameSpace ]]
destination:
# on which server to deploy
server: https://kubernetes.default.svc
# in which namespace to deploy
namespace: default
# source of the application
source:
# which values file to use in the Helm chart
valuefile: values.yaml
# path of the Helm chart
path: charts/go-hello-http
# Helm chart repo URL
repoURL: YOUR_CHART_REPO_URL
path: helm/${{github-repo-scaffolding-golang.default.outputs.repo}}
repoURL: ${{github-repo-scaffolding-golang.default.outputs.repoURL}}
10 changes: 5 additions & 5 deletions examples/quickstart.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
---
tools:
- name: go-webapp-repo
plugin: github-repo-scaffolding-golang
- name: github-repo-scaffolding-golang
instanceID: default
options:
owner: YOUR_GITHUB_USERNAME_CASE_SENSITIVE
repo: go-webapp-devstream-demo
branch: main
image_repo: YOUR_DOCKER_USERNAME/go-webapp-devstream-demo
- name: golang-demo-actions
plugin: githubactions-golang
dependsOn: ["go-webapp-repo.github-repo-scaffolding-golang"]
- name: githubactions-golang
instanceID: default
dependsOn: ["github-repo-scaffolding-golang.default"]
options:
owner: YOUR_GITHUB_USERNAME_CASE_SENSITIVE
repo: go-webapp-devstream-demo
Expand Down
9 changes: 9 additions & 0 deletions gitops-variables.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defaultBranch: main
githubUsername: daniel-hutao
repoName: dtm-test-go
jiraID: merico
jiraUserEmail: tao.hu@merico.dev
jiraProjectKey: DT
dockerhubUsername: exploitht
argocdNameSpace: argocd
argocdDeployTimeout: 10m
20 changes: 10 additions & 10 deletions internal/pkg/configloader/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ type Config struct {

// Tool is the struct for one section of the DevStream configuration file.
type Tool struct {
Name string `yaml:"name"`
// RFC 1123 - DNS Subdomain Names style
// contain no more than 253 characters
// contain only lowercase alphanumeric characters, '-' or '.'
// start with an alphanumeric character
// end with an alphanumeric character
Name string `yaml:"name"`
Plugin string `yaml:"plugin"`
DependsOn []string `yaml:"dependsOn"`
Options map[string]interface{} `yaml:"options"`
InstanceID string `yaml:"instanceID"`
DependsOn []string `yaml:"dependsOn"`
Options map[string]interface{} `yaml:"options"`
}

func (t *Tool) DeepCopy() *Tool {
var retTool = Tool{
Name: t.Name,
Plugin: t.Plugin,
DependsOn: t.DependsOn,
Options: map[string]interface{}{},
Name: t.Name,
InstanceID: t.InstanceID,
DependsOn: t.DependsOn,
Options: map[string]interface{}{},
}
for k, v := range t.Options {
retTool.Options[k] = v
Expand Down Expand Up @@ -91,12 +91,12 @@ func LoadConf(configFileName, varFileName string) *Config {
// GetPluginFileName creates the file name based on the tool's name and version
// If the plugin {githubactions 0.0.1}, the generated name will be "githubactions_0.0.1.so"
func GetPluginFileName(t *Tool) string {
return fmt.Sprintf("%s-%s-%s_%s.so", t.Plugin, GOOS, GOARCH, version.Version)
return fmt.Sprintf("%s-%s-%s_%s.so", t.Name, GOOS, GOARCH, version.Version)
}

// GetPluginMD5FileName If the plugin {githubactions 0.0.1}, the generated name will be "githubactions_0.0.1.md5"
func GetPluginMD5FileName(t *Tool) string {
return fmt.Sprintf("%s-%s-%s_%s.md5", t.Plugin, GOOS, GOARCH, version.Version)
return fmt.Sprintf("%s-%s-%s_%s.md5", t.Name, GOOS, GOARCH, version.Version)
}

// GetDtmMD5FileName format likes dtm-linux-amd64
Expand Down
16 changes: 8 additions & 8 deletions internal/pkg/configloader/configloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (

func TestDependencyPass(t *testing.T) {
tools := []Tool{
{Name: "argocd", Plugin: "argocd"},
{Name: "argocdapp", Plugin: "argocdapp", DependsOn: []string{"argocd.argocd"}},
{InstanceID: "argocd", Name: "argocd"},
{InstanceID: "argocdapp", Name: "argocdapp", DependsOn: []string{"argocd.argocd"}},
}
errors := validateDependency(tools)
assert.Equal(t, len(errors), 0, "Dependency check passed.")
Expand All @@ -18,7 +18,7 @@ func TestDependencyPass(t *testing.T) {

func TestDependencyNotExist(t *testing.T) {
tools := []Tool{
{Name: "argocdapp", Plugin: "argocdapp", DependsOn: []string{"argocd.argocd"}},
{InstanceID: "argocdapp", Name: "argocdapp", DependsOn: []string{"argocd.argocd"}},
}
errors := validateDependency(tools)
assert.Equal(t, len(errors), 1)
Expand All @@ -27,18 +27,18 @@ func TestDependencyNotExist(t *testing.T) {

func TestMultipleDependencies(t *testing.T) {
tools := []Tool{
{Name: "argocd", Plugin: "argocd"},
{Name: "repo", Plugin: "github"},
{Name: "argocdapp", Plugin: "argocdapp", DependsOn: []string{"argocd.argocd", "repo.github"}},
{InstanceID: "argocd", Name: "argocd"},
{InstanceID: "repo", Name: "github"},
{InstanceID: "argocdapp", Name: "argocdapp", DependsOn: []string{"argocd.argocd", "github.repo"}},
}
errors := validateDependency(tools)
assert.Equal(t, len(errors), 0)
}

func TestEmptyDependency(t *testing.T) {
tools := []Tool{
{Name: "argocd", Plugin: "argocd"},
{Name: "argocdapp", Plugin: "argocdapp", DependsOn: []string{}},
{InstanceID: "argocd", Name: "argocd"},
{InstanceID: "argocdapp", Name: "argocdapp", DependsOn: []string{}},
}
errors := validateDependency(tools)
assert.Equal(t, len(errors), 0)
Expand Down
16 changes: 8 additions & 8 deletions internal/pkg/configloader/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ func validateConfig(config *Config) []error {
func validateTool(t *Tool) []error {
errors := make([]error, 0)

// Name
if t.Name == "" {
// InstanceID
if t.InstanceID == "" {
errors = append(errors, fmt.Errorf("name is empty"))
}

errs := validation.IsDNS1123Subdomain(t.Name)
errs := validation.IsDNS1123Subdomain(t.InstanceID)
for _, e := range errs {
errors = append(errors, fmt.Errorf("name %s is invalid: %s", t.Name, e))
errors = append(errors, fmt.Errorf("name %s is invalid: %s", t.InstanceID, e))
}

// Plugin
if t.Plugin == "" {
// InstanceID
if t.Name == "" {
errors = append(errors, fmt.Errorf("plugin is empty"))
}

Expand All @@ -47,7 +47,7 @@ func validateDependency(tools []Tool) []error {
toolMap := make(map[string]bool)
// creating the set
for _, tool := range tools {
key := fmt.Sprintf("%s.%s", tool.Name, tool.Plugin)
key := fmt.Sprintf("%s.%s", tool.Name, tool.InstanceID)
toolMap[key] = true
}

Expand All @@ -67,7 +67,7 @@ func validateDependency(tools []Tool) []error {

// generate an error if the dependency isn't in the config set,
if _, ok := toolMap[dependency]; !ok {
errors = append(errors, fmt.Errorf("tool %s's dependency %s doesn't exist in the config", tool.Name, dependency))
errors = append(errors, fmt.Errorf("tool %s's dependency %s doesn't exist in the config", tool.InstanceID, dependency))
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions internal/pkg/plugin/githubactions/nodejs/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/mitchellh/mapstructure"

"github.com/devstream-io/devstream/internal/pkg/plugin/githubactions"
ga "github.com/devstream-io/devstream/internal/pkg/plugin/githubactions"
"github.com/devstream-io/devstream/pkg/util/github"
"github.com/devstream-io/devstream/pkg/util/log"
)
Expand Down Expand Up @@ -38,13 +38,13 @@ func Create(options map[string]interface{}) (map[string]interface{}, error) {
return nil, err
}

log.Debugf("Language is: %s.", githubactions.GetLanguage(opts.Language))
log.Debugf("Language is: %s.", ga.GetLanguage(opts.Language))

for _, w := range workflows {
if err := ghClient.AddWorkflow(w, opts.Branch); err != nil {
return nil, err
}
}

return githubactions.BuildState(opts.Owner, opts.Org, opts.Repo), nil
return ga.BuildState(opts.Owner, opts.Org, opts.Repo), nil
}
4 changes: 2 additions & 2 deletions internal/pkg/plugin/githubactions/nodejs/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/mitchellh/mapstructure"

"github.com/devstream-io/devstream/internal/pkg/plugin/githubactions"
ga "github.com/devstream-io/devstream/internal/pkg/plugin/githubactions"
"github.com/devstream-io/devstream/pkg/util/github"
"github.com/devstream-io/devstream/pkg/util/log"
)
Expand Down Expand Up @@ -37,7 +37,7 @@ func Delete(options map[string]interface{}) (bool, error) {
return false, err
}

log.Debugf("Language is %s.", githubactions.GetLanguage(opts.Language))
log.Debugf("Language is %s.", ga.GetLanguage(opts.Language))

for _, pipeline := range workflows {
err := ghClient.DeleteWorkflow(pipeline, opts.Branch)
Expand Down
6 changes: 3 additions & 3 deletions internal/pkg/plugin/githubactions/nodejs/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/mitchellh/mapstructure"

"github.com/devstream-io/devstream/internal/pkg/plugin/githubactions"
ga "github.com/devstream-io/devstream/internal/pkg/plugin/githubactions"
"github.com/devstream-io/devstream/pkg/util/github"
"github.com/devstream-io/devstream/pkg/util/log"
)
Expand Down Expand Up @@ -45,7 +45,7 @@ func Read(options map[string]interface{}) (map[string]interface{}, error) {
return nil, nil
}

log.Debugf("Language is: %s.", githubactions.GetLanguage(opts.Language))
log.Debugf("Language is: %s.", ga.GetLanguage(opts.Language))

return githubactions.BuildReadState(path), nil
return ga.BuildReadState(path), nil
}
Loading