Skip to content

Commit

Permalink
Adopt ks devops pipeline in k8s (#145)
Browse files Browse the repository at this point in the history
Co-authored-by: rick <[email protected]>
  • Loading branch information
LinuxSuRen and LinuxSuRen committed Jun 8, 2021
1 parent 6468ea2 commit 4e2bd1c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
26 changes: 19 additions & 7 deletions kubectl-plugin/pipeline/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type pipelineCreateOption struct {
Type string
SCMType string
Batch bool
SkipCheck bool

// Inner fields
Client dynamic.Interface
Expand Down Expand Up @@ -68,6 +69,7 @@ KubeSphere supports multiple types Pipeline. Currently, this CLI only support th
flags.StringVarP(&opt.SCMType, "scm-type", "", "",
"The SCM type of pipeline, could be gitlab, github")
flags.BoolVarP(&opt.Batch, "batch", "b", false, "Create pipeline as batch mode")
flags.BoolVarP(&opt.SkipCheck, "skip-check", "", false, "Skip the resources check")

_ = cmd.RegisterFlagCompletionFunc("template", common.ArrayCompletion("java", "go", "simple",
"multi-branch-gitlab", "multi-branch-github", "multi-branch-git"))
Expand Down Expand Up @@ -196,12 +198,17 @@ func (o *pipelineCreateOption) preRunE(cmd *cobra.Command, args []string) (err e
func (o *pipelineCreateOption) runE(cmd *cobra.Command, args []string) (err error) {
ctx := context.TODO()

var ws *unstructured.Unstructured
if ws, err = o.checkWorkspace(); err != nil {
return
var wdID string
if !o.SkipCheck {
var ws *unstructured.Unstructured
if ws, err = o.checkWorkspace(); err != nil {
return
}
wdID = string(ws.GetUID())
}

var project *unstructured.Unstructured
if project, err = o.checkDevOpsProject(ws); err != nil {
if project, err = o.checkDevOpsProject(wdID); err != nil {
return
}
o.Project = project.GetName() // the previous name is the generate name
Expand Down Expand Up @@ -307,7 +314,7 @@ func (o *pipelineCreateOption) getDevOpsProjectList() (wsList *unstructured.Unst
return
}

func (o *pipelineCreateOption) checkDevOpsProject(ws *unstructured.Unstructured) (project *unstructured.Unstructured, err error) {
func (o *pipelineCreateOption) checkDevOpsProject(wsID string) (project *unstructured.Unstructured, err error) {
ctx := context.TODO()
var list *unstructured.UnstructuredList
if list, err = o.getDevOpsProjectList(); err != nil {
Expand All @@ -325,8 +332,9 @@ func (o *pipelineCreateOption) checkDevOpsProject(ws *unstructured.Unstructured)

if !found {
var tpl *template.Template
o.WorkspaceUID = string(ws.GetUID())
o.WorkspaceUID = wsID
if tpl, err = template.New("project").Parse(devopsProjectTemplate); err != nil {
err = fmt.Errorf("failed to parse devops project template, error is: %v", err)
return
}

Expand All @@ -341,7 +349,9 @@ func (o *pipelineCreateOption) checkDevOpsProject(ws *unstructured.Unstructured)
return
}

project, err = o.Client.Resource(types.GetDevOpsProjectSchema()).Create(ctx, projectObj, metav1.CreateOptions{})
if project, err = o.Client.Resource(types.GetDevOpsProjectSchema()).Create(ctx, projectObj, metav1.CreateOptions{}); err != nil {
err = fmt.Errorf("failed to create devops project with YAML: '%s'. Error is: %v", buf.String(), err)
}
}
return
}
Expand Down Expand Up @@ -382,13 +392,15 @@ metadata:
generateName: {{.Project}}
labels:
kubesphere.io/workspace: {{.Workspace}}
{{if ne .WorkspaceUID ""}}
ownerReferences:
- apiVersion: tenant.kubesphere.io/v1alpha1
blockOwnerDeletion: true
controller: true
kind: Workspace
name: {{.Workspace}}
uid: {{.WorkspaceUID}}
{{end}}
`

var pipelineTemplate = `
Expand Down
10 changes: 5 additions & 5 deletions kubectl-plugin/source2image/s2ibuilder_types.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4e2bd1c

Please sign in to comment.