From 4e2bd1c5f40fc3d5e448e715e4624145ef25b0c2 Mon Sep 17 00:00:00 2001 From: Rick <1450685+LinuxSuRen@users.noreply.github.com> Date: Tue, 8 Jun 2021 08:48:08 +0800 Subject: [PATCH] Adopt ks devops pipeline in k8s (#145) Co-authored-by: rick --- kubectl-plugin/pipeline/create.go | 26 ++++++++++++++----- .../source2image/s2ibuilder_types.go | 10 +++---- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/kubectl-plugin/pipeline/create.go b/kubectl-plugin/pipeline/create.go index 6c16357..3ab17cb 100644 --- a/kubectl-plugin/pipeline/create.go +++ b/kubectl-plugin/pipeline/create.go @@ -28,6 +28,7 @@ type pipelineCreateOption struct { Type string SCMType string Batch bool + SkipCheck bool // Inner fields Client dynamic.Interface @@ -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")) @@ -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 @@ -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 { @@ -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 } @@ -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 } @@ -382,6 +392,7 @@ metadata: generateName: {{.Project}} labels: kubesphere.io/workspace: {{.Workspace}} + {{if ne .WorkspaceUID ""}} ownerReferences: - apiVersion: tenant.kubesphere.io/v1alpha1 blockOwnerDeletion: true @@ -389,6 +400,7 @@ metadata: kind: Workspace name: {{.Workspace}} uid: {{.WorkspaceUID}} + {{end}} ` var pipelineTemplate = ` diff --git a/kubectl-plugin/source2image/s2ibuilder_types.go b/kubectl-plugin/source2image/s2ibuilder_types.go index 913d1a4..24d70f4 100644 --- a/kubectl-plugin/source2image/s2ibuilder_types.go +++ b/kubectl-plugin/source2image/s2ibuilder_types.go @@ -18,24 +18,24 @@ type RunState string const ( // ResourceKindS2iBuilder s2i builder name - ResourceKindS2iBuilder = "S2iBuilder" + ResourceKindS2iBuilder = "S2iBuilder" // ResourceSingularS2iBuilder s2i builder singular name ResourceSingularS2iBuilder = "s2ibuilder" // ResourcePluralS2iBuilder s2i builder plural name - ResourcePluralS2iBuilder = "s2ibuilders" + ResourcePluralS2iBuilder = "s2ibuilders" ) const ( // NotRunning means not run yet NotRunning RunState = "Not Running Yet" // Running means running - Running RunState = "Running" + Running RunState = "Running" // Successful means success Successful RunState = "Successful" // Failed means failed - Failed RunState = "Failed" + Failed RunState = "Failed" // Unknown means unknow - Unknown RunState = "Unknown" + Unknown RunState = "Unknown" ) const ( AutoScaleAnnotations = "devops.kubesphere.io/autoscale"