Skip to content

Commit

Permalink
Add support to select workspace when creating pipeline (#143)
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 4, 2021
1 parent 20c83ae commit 6468ea2
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 11 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver v1.5.0 // indirect
github.com/Masterminds/sprig v2.22.0+incompatible
github.com/Pallinder/go-randomdata v1.2.0
github.com/huandu/xstrings v1.3.2 // indirect
github.com/linuxsuren/cobra-extension v0.0.10
github.com/linuxsuren/go-cli-alias v0.0.6
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46/go.mod h1:3wb0
github.com/Netflix/go-expect v0.0.0-20180615182759-c93bf25de8e8 h1:xzYJEypr/85nBpB11F9br+3HUrpgb+fcm5iADzXXYEw=
github.com/Netflix/go-expect v0.0.0-20180615182759-c93bf25de8e8/go.mod h1:oX5x61PbNXchhh0oikYAH+4Pcfw5LKv21+Jnpr6r6Pc=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/Pallinder/go-randomdata v1.2.0 h1:DZ41wBchNRb/0GfsePLiSwb0PHZmT67XY00lCDlaYPg=
github.com/Pallinder/go-randomdata v1.2.0/go.mod h1:yHmJgulpD2Nfrm0cR9tI/+oAgRqCQQixsA8HyRZfV9Y=
github.com/PuerkitoBio/purell v1.0.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
github.com/PuerkitoBio/purell v1.1.1 h1:WEQqlqaGbrPkxLJWfBwQmfEAE1Z7ONdDLqrN38tNFfI=
Expand Down
86 changes: 75 additions & 11 deletions kubectl-plugin/pipeline/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import (
"fmt"
"github.com/AlecAivazis/survey/v2"
"github.com/Masterminds/sprig"
"github.com/Pallinder/go-randomdata"
"github.com/linuxsuren/ks/kubectl-plugin/common"
"github.com/linuxsuren/ks/kubectl-plugin/types"
"github.com/spf13/cobra"
"html/template"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"strings"
)
Expand Down Expand Up @@ -91,13 +93,23 @@ func (o *pipelineCreateOption) wizard(_ *cobra.Command, _ []string) (err error)
}

if o.Workspace == "" {
if o.Workspace, err = getInput("Please input the workspace name"); err != nil {
var wsNames []string
if wsNames, err = o.getWorkspaceTemplateNameList(); err == nil {
if o.Workspace, err = chooseObjectFromArray("workspace name", wsNames); err != nil {
return
}
} else {
return
}
}

if o.Project == "" {
if o.Project, err = getInput("Please input the project name"); err != nil {
var projectNames []string
if projectNames, err = o.getDevOpsProjectNameList(); err == nil {
if o.Project, err = chooseObjectFromArray("project name", projectNames); err != nil {
return
}
} else {
return
}
}
Expand All @@ -117,18 +129,24 @@ func (o *pipelineCreateOption) wizard(_ *cobra.Command, _ []string) (err error)
return
}

func chooseOneFromArray(options []string) (result string, err error) {
func chooseObjectFromArray(object string, options []string) (result string, err error) {
prompt := &survey.Select{
Message: "Please select:",
Message: fmt.Sprintf("Please select %s:", object),
Options: options,
}
err = survey.AskOne(prompt, &result)
return
}

func chooseOneFromArray(options []string) (result string, err error) {
result, err = chooseObjectFromArray("", options)
return
}

func getInput(title string) (result string, err error) {
prompt := &survey.Input{
Message: title,
Default: strings.ToLower(randomdata.SillyName()),
}
err = survey.AskOne(prompt, &result)
return
Expand Down Expand Up @@ -197,27 +215,73 @@ func (o *pipelineCreateOption) runE(cmd *cobra.Command, args []string) (err erro
return
}

func (o *pipelineCreateOption) getDevOpsProjectNameList() (names []string, err error) {
names, err = o.getUnstructuredNameList(false, []string{}, types.GetDevOpsProjectSchema())
return
}

func (o *pipelineCreateOption) getWorkspaceNameList() (names []string, err error) {
names, err = o.getUnstructuredNameList(true, []string{"system-workspace"}, types.GetWorkspaceSchema())
return
}

func (o *pipelineCreateOption) getWorkspaceTemplateNameList() (names []string, err error) {
names, err = o.getUnstructuredNameList(true, []string{"system-workspace"}, types.GetWorkspaceTemplate())
return
}

func (o *pipelineCreateOption) getUnstructuredNameList(originalName bool, excludes []string, schemaType schema.GroupVersionResource) (names []string, err error) {
var wsList *unstructured.UnstructuredList
if wsList, err = o.getWorkspaceList(); err == nil {
names = make([]string, len(wsList.Items))
if wsList, err = o.getUnstructuredList(schemaType); err == nil {
names = make([]string, 0)
for i := range wsList.Items {
names[i] = wsList.Items[i].GetName()
var name string
if originalName {
name = wsList.Items[i].GetName()
} else {
name = wsList.Items[i].GetGenerateName()
}

exclude := false
for j := range excludes {
if name == excludes[j] {
exclude = true
break
}
}

if !exclude {
names = append(names, name)
}
}
}
return
}

func (o *pipelineCreateOption) getWorkspaceList() (wsList *unstructured.UnstructuredList, err error) {
func (o *pipelineCreateOption) getUnstructuredList(schemaType schema.GroupVersionResource) (wsList *unstructured.UnstructuredList, err error) {
ctx := context.TODO()
wsList, err = o.Client.Resource(types.GetWorkspaceSchema()).List(ctx, metav1.ListOptions{})
wsList, err = o.Client.Resource(schemaType).List(ctx, metav1.ListOptions{})
return
}

func (o *pipelineCreateOption) getWorkspaceList() (wsList *unstructured.UnstructuredList, err error) {
wsList, err = o.getUnstructuredList(types.GetWorkspaceSchema())
return
}

func (o *pipelineCreateOption) getWorkspaceTemplateList() (wsList *unstructured.UnstructuredList, err error) {
wsList, err = o.getUnstructuredList(types.GetWorkspaceTemplate())
return
}

func (o *pipelineCreateOption) checkWorkspace() (ws *unstructured.Unstructured, err error) {
ctx := context.TODO()
ws, err = o.Client.Resource(types.GetWorkspaceSchema()).Get(ctx, o.Workspace, metav1.GetOptions{})
// TODO create the workspace if it's not exists
if ws, err = o.Client.Resource(types.GetWorkspaceSchema()).Get(ctx, o.Workspace, metav1.GetOptions{}); err == nil {
return
}

// TODO check workspaceTemplate when ks in a multi-cluster environment
ws, err = o.Client.Resource(types.GetWorkspaceTemplate()).Get(ctx, o.Workspace, metav1.GetOptions{})
return
}

Expand Down
9 changes: 9 additions & 0 deletions kubectl-plugin/types/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ func GetWorkspaceSchema() schema.GroupVersionResource {
}
}

// GetWorkspaceTemplate returns the schema of WorkspaceTemplate
func GetWorkspaceTemplate() schema.GroupVersionResource {
return schema.GroupVersionResource{
Group: "tenant.kubesphere.io",
Version: "v1alpha2",
Resource: "workspacetemplates",
}
}

// GetNamespaceSchema returns the schema of namespaces
func GetNamespaceSchema() schema.GroupVersionResource {
return schema.GroupVersionResource{
Expand Down

0 comments on commit 6468ea2

Please sign in to comment.