Skip to content

Commit

Permalink
Add support create and delete a project (#223)
Browse files Browse the repository at this point in the history
* Add support create and delete a project

* Remove unused imports
  • Loading branch information
LinuxSuRen committed Oct 14, 2021
1 parent 1767f34 commit 6b7d53a
Show file tree
Hide file tree
Showing 9 changed files with 733 additions and 485 deletions.
443 changes: 12 additions & 431 deletions kubectl-plugin/pipeline/create.go

Large diffs are not rendered by default.

73 changes: 31 additions & 42 deletions kubectl-plugin/pipeline/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@ import (
"github.com/Pallinder/go-randomdata"
"github.com/gdamore/tcell/v2"
"github.com/kubesphere-sigs/ks/kubectl-plugin/common"
"github.com/kubesphere-sigs/ks/kubectl-plugin/pipeline/option"
"github.com/kubesphere-sigs/ks/kubectl-plugin/pipeline/tpl"
"github.com/kubesphere-sigs/ks/kubectl-plugin/pipeline/ui"
"github.com/kubesphere-sigs/ks/kubectl-plugin/pipeline/ui/project"
"github.com/kubesphere-sigs/ks/kubectl-plugin/types"
"github.com/rivo/tview"
"github.com/spf13/cobra"
v1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/cli-runtime/pkg/printers"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -216,7 +215,7 @@ func (o *dashboardOption) pipelineCreationForm() {
templateField := templateItem.(*tview.DropDown)
_, templateName := templateField.GetCurrentOption()

opt := &pipelineCreateOption{
opt := &option.PipelineCreateOption{
Name: nameField.GetText(),
Project: o.namespaceProjectMap[o.namespace],
Template: templateName,
Expand All @@ -225,8 +224,8 @@ func (o *dashboardOption) pipelineCreationForm() {
Type: "pipeline",
Client: o.client,
}
_ = opt.parseTemplate()
_ = opt.createPipeline() // need to find a way to show the errors
_ = opt.ParseTemplate()
_ = opt.CreatePipeline() // need to find a way to show the errors
}

o.stack.Pop()
Expand Down Expand Up @@ -260,43 +259,15 @@ func (o *dashboardOption) listPipelines(index int, mainText string, secondaryTex
}

func (o *dashboardOption) createNamespaceList() (listView tview.Primitive) {
list := tview.NewList()
list.SetBorder(true).SetTitle("namespaces")
go func() {
if watchEvent, err := o.client.Resource(types.GetNamespaceSchema()).Watch(context.TODO(), metav1.ListOptions{
LabelSelector: "kubesphere.io/devopsproject",
}); err == nil {
for event := range watchEvent.ResultChan() {
switch event.Type {
case watch.Added:
unss := event.Object.(*unstructured.Unstructured)
ss := &corev1.Namespace{}
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(unss.Object, ss); err == nil {
list.AddItem(ss.Name, "", 0, nil)
}

if devopsProject, err := o.client.Resource(types.GetDevOpsProjectSchema()).
Get(context.TODO(), ss.Name, metav1.GetOptions{}); err == nil {
o.namespaceWorkspaceMap[ss.Name] = devopsProject.GetLabels()["kubesphere.io/workspace"]
o.namespaceProjectMap[ss.Name] = devopsProject.GetGenerateName()
}
case watch.Deleted:
for i := 0; i < list.GetItemCount(); i++ {
name, _ := list.GetItemText(i)
unss := event.Object.(*unstructured.Unstructured)
ss := &corev1.Namespace{}
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(unss.Object, ss); err == nil {
if name == ss.Name {
list.RemoveItem(i)
break
}
}
}
}
o.app.Draw()
}
list := ui.NewResourceList(o.client, o.app, o.stack)
list.PutItemAddingListener(func(name string) {
if devopsProject, err := o.client.Resource(types.GetDevOpsProjectSchema()).
Get(context.TODO(), name, metav1.GetOptions{}); err == nil {
o.namespaceWorkspaceMap[name] = devopsProject.GetLabels()["kubesphere.io/workspace"]
o.namespaceProjectMap[name] = devopsProject.GetGenerateName()
}
}()
})
list.Load("", types.GetNamespaceSchema(), "kubesphere.io/devopsproject")
list.SetChangedFunc(o.listPipelines)
o.app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch key := event.Rune(); key {
Expand All @@ -311,7 +282,25 @@ func (o *dashboardOption) createNamespaceList() (listView tview.Primitive) {
}
return event
})
inputCapture := list.GetInputCapture()
list.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch key := event.Rune(); key {
case 'p':
o.createProject()
}
return inputCapture(event)
})
o.app.SetFocus(list)
listView = list
return
}

func (o *dashboardOption) createProject() {
form := project.NewProjectForm(o.client)
form.SetConfirmEvent(func() {
o.stack.Pop()
}).SetCancelEvent(func() {
o.stack.Pop()
})
o.stack.Push(form)
}
7 changes: 4 additions & 3 deletions kubectl-plugin/pipeline/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"github.com/kubesphere-sigs/ks/kubectl-plugin/common"
"github.com/kubesphere-sigs/ks/kubectl-plugin/pipeline/option"
"github.com/kubesphere-sigs/ks/kubectl-plugin/types"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -16,7 +17,7 @@ import (
func newGCCmd(client dynamic.Interface) (cmd *cobra.Command) {
opt := &gcOption{
client: client,
pipelineCreateOption: pipelineCreateOption{
PipelineCreateOption: option.PipelineCreateOption{
Client: client,
},
}
Expand Down Expand Up @@ -55,7 +56,7 @@ type gcOption struct {

// inner fields
client dynamic.Interface
pipelineCreateOption
option.PipelineCreateOption
}

func (o *gcOption) preRunE(cmd *cobra.Command, args []string) (err error) {
Expand All @@ -67,7 +68,7 @@ func (o *gcOption) preRunE(cmd *cobra.Command, args []string) (err error) {

func (o *gcOption) cleanPipelineRunInNamespace(namespace string) (err error) {
var pipelineList *unstructured.UnstructuredList
if pipelineList, err = o.getUnstructuredListInNamespace(namespace, types.GetPipelineRunSchema()); err != nil {
if pipelineList, err = o.GetUnstructuredListInNamespace(namespace, types.GetPipelineRunSchema()); err != nil {
err = fmt.Errorf("failed to get PipelineRun list, error: %v", err)
return
}
Expand Down
Loading

0 comments on commit 6b7d53a

Please sign in to comment.