-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
492 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"path/filepath" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/vs4vijay/lazykubectl/pkg/k8s" | ||
"github.com/vs4vijay/lazykubectl/pkg/tui" | ||
|
||
"os" | ||
|
||
"github.com/spf13/viper" | ||
) | ||
|
||
var ( | ||
cfgFile string | ||
kubeConfigFile string | ||
home = k8s.Home() | ||
) | ||
|
||
var rootCmd = &cobra.Command{ | ||
Use: "lazykubectl", | ||
Short: "A Kubernetes Client", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
fmt.Println("lazykubectl") | ||
|
||
configData, err := ioutil.ReadFile(kubeConfigFile) | ||
if err != nil { | ||
fmt.Errorf("Error in Reading Config: %v", err) | ||
os.Exit(1) | ||
} | ||
|
||
kubeConfig := k8s.KubeConfig{ | ||
Type: "MANIFEST", | ||
Manifest: string(configData), | ||
} | ||
|
||
tui.Start(kubeConfig) | ||
}, | ||
} | ||
|
||
func Execute() { | ||
if err := rootCmd.Execute(); err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func init() { | ||
cobra.OnInitialize(initConfig) | ||
|
||
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.lazykubectl.yaml)") | ||
|
||
flags := rootCmd.Flags() | ||
flags.StringVarP(&kubeConfigFile, "kubeconfig", "c", filepath.Join(home, ".kube", "config"), "") | ||
} | ||
|
||
// initConfig reads in config file and ENV variables if set. | ||
func initConfig() { | ||
if cfgFile != "" { | ||
viper.SetConfigFile(cfgFile) | ||
} else { | ||
viper.AddConfigPath(home) | ||
viper.SetConfigName(".lazykubectl") | ||
} | ||
|
||
viper.AutomaticEnv() | ||
|
||
if err := viper.ReadInConfig(); err == nil { | ||
fmt.Println("Using config file:", viper.ConfigFileUsed()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,7 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/rivo/tview" | ||
) | ||
|
||
const ( | ||
project = "lazykubectl" | ||
) | ||
import "github.com/vs4vijay/lazykubectl/cmd" | ||
|
||
func main() { | ||
app := tview.NewApplication() | ||
box := tview.NewBox().SetBorder(true).SetTitle(project) | ||
button := tview.NewButton("Hit Enter to close").SetSelectedFunc(func() { | ||
app.Stop() | ||
}) | ||
|
||
button.SetBorder(true).SetRect(0, 0, 22, 3) | ||
|
||
dropdown := tview.NewDropDown(). | ||
SetLabel("Select an option (hit Enter): "). | ||
SetOptions([]string{"First", "Second", "Third", "Fourth", "Fifth"}, nil) | ||
|
||
flex := tview.NewFlex(). | ||
AddItem(tview.NewBox().SetBorder(true).SetTitle("Left (1/2 x width of Top)"), 0, 1, false). | ||
AddItem(tview.NewFlex().SetDirection(tview.FlexRow). | ||
AddItem(box, 0, 1, true). | ||
AddItem(button, 0, 3, false). | ||
AddItem(dropdown, 5, 1, false), 0, 2, false). | ||
AddItem(tview.NewBox().SetBorder(true).SetTitle("Right (20 cols)"), 20, 1, false) | ||
|
||
newPrimitive := func(text string) tview.Primitive { | ||
return tview.NewTextView(). | ||
SetTextAlign(tview.AlignCenter). | ||
SetText(text) | ||
} | ||
menu := newPrimitive("Menu") | ||
main := newPrimitive("Main content") | ||
sideBar := newPrimitive("Side Bar") | ||
|
||
grid := tview.NewGrid(). | ||
SetRows(3, 0, 3). | ||
SetColumns(30, 0, 30). | ||
SetBorders(true). | ||
AddItem(button, 0, 0, 1, 3, 0, 0, false). | ||
AddItem(dropdown, 2, 0, 1, 3, 0, 0, false) | ||
|
||
// Layout for screens narrower than 100 cells (menu and side bar are hidden). | ||
grid.AddItem(menu, 0, 0, 0, 0, 0, 0, false). | ||
AddItem(flex, 1, 0, 1, 3, 0, 0, false). | ||
AddItem(sideBar, 0, 0, 0, 0, 0, 0, false) | ||
|
||
// Layout for screens wider than 100 cells. | ||
grid.AddItem(menu, 1, 0, 1, 1, 0, 100, false). | ||
AddItem(main, 1, 1, 1, 1, 0, 100, false). | ||
AddItem(sideBar, 1, 2, 1, 1, 0, 100, false) | ||
|
||
if err := app.SetRoot(grid, true).SetFocus(grid).Run(); err != nil { | ||
panic(err) | ||
} | ||
cmd.Execute() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package k8s | ||
|
||
import ( | ||
"k8s.io/client-go/util/homedir" | ||
) | ||
|
||
func Home() string { | ||
return homedir.HomeDir() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
package tui | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"github.com/jroimartin/gocui" | ||
|
||
"github.com/vs4vijay/lazykubectl/pkg/k8s" | ||
) | ||
|
||
var ( | ||
viewArr = []string{"Info", "Namespaces", "Pods", "Services"} | ||
active = 0 | ||
) | ||
|
||
func Start(kubeConfig k8s.KubeConfig) { | ||
g, err := gocui.NewGui(gocui.Output256) | ||
if err != nil { | ||
log.Panicln(err) | ||
} | ||
defer g.Close() | ||
|
||
g.InputEsc = true | ||
g.Cursor = true | ||
g.Mouse = true | ||
g.Highlight = true | ||
g.SelFgColor = gocui.ColorBlue | ||
|
||
g.SetManagerFunc(layout) | ||
|
||
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil { | ||
log.Panicln("SetKeybinding --- ", err) | ||
} | ||
|
||
if err := g.SetKeybinding("", gocui.KeyTab, gocui.ModNone, nextView); err != nil { | ||
log.Panicln(err) | ||
} | ||
|
||
if err := g.SetKeybinding("Pods", gocui.KeyCtrlP, gocui.ModNone, quit); err != nil { | ||
log.Panicln("SetKeybinding --- ", err) | ||
} | ||
|
||
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit { | ||
log.Panicln("MainLoop --- ", err) | ||
} | ||
|
||
// g.SetViewOnTop("Namespaces") | ||
// g.SetCurrentView("Namespaces") | ||
} | ||
|
||
func layout(g *gocui.Gui) error { | ||
maxX, maxY := g.Size() | ||
|
||
pad := 1 | ||
gridX, gridY := maxX/4, maxY/3 | ||
|
||
if v, err := g.SetView("Info", 0, 0, gridX - pad, gridY - pad); err != nil { | ||
if err != gocui.ErrUnknownView { | ||
return err | ||
} | ||
|
||
v.Title = "Info" | ||
v.Editable = true | ||
v.Wrap = true | ||
|
||
fmt.Fprintf(v, "Context: %v\n", "") | ||
fmt.Fprintf(v, "Cluster: %v\n", "") | ||
fmt.Fprintf(v, "User: %v\n", "") | ||
fmt.Fprintf(v, "Nodes: %v\n", "") | ||
} | ||
|
||
if v, err := g.SetView("Namespaces", 0, gridY, gridX - pad, (gridY * 2) - pad) ; err != nil { | ||
if err != gocui.ErrUnknownView { | ||
return err | ||
} | ||
v.Title = "Namespaces" | ||
v.Editable = true | ||
v.Wrap = true | ||
fmt.Fprintf(v, "Namespaces: %v\n", "") | ||
} | ||
|
||
if v, err := g.SetView("Pods", gridX, 0, (gridX * 4), (gridY * 2) - pad) ; err != nil { | ||
if err != gocui.ErrUnknownView { | ||
return err | ||
} | ||
v.Title = "Pods" | ||
v.Editable = true | ||
v.Autoscroll = true | ||
fmt.Fprintf(v, "Pods: %v\n", "") | ||
} | ||
|
||
if v, err := g.SetView("Services", 0, gridY * 2, (gridX * 4), gridY * 3) ; err != nil { | ||
if err != gocui.ErrUnknownView { | ||
return err | ||
} | ||
v.Title = "Services" | ||
v.Editable = true | ||
v.Wrap = true | ||
fmt.Fprintf(v, "Services: %v\n", "") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func quit(g *gocui.Gui, v *gocui.View) error { | ||
return gocui.ErrQuit | ||
} | ||
|
||
func setCurrentViewOnTop(g *gocui.Gui, name string) (*gocui.View, error) { | ||
if _, err := g.SetCurrentView(name); err != nil { | ||
return nil, err | ||
} | ||
return g.SetViewOnTop(name) | ||
} | ||
|
||
func nextView(g *gocui.Gui, v *gocui.View) error { | ||
nextIndex := (active + 1) % len(viewArr) | ||
name := viewArr[nextIndex] | ||
|
||
// out, err := g.View("Pods") | ||
// if err != nil { | ||
// return err | ||
// } | ||
// fmt.Fprintf(out, "Going from view %v to %v\n", v.Name(), name) | ||
|
||
if _, err := setCurrentViewOnTop(g, name); err != nil { | ||
return err | ||
} | ||
|
||
if nextIndex == 0 || nextIndex == 3 { | ||
g.Cursor = true | ||
} else { | ||
g.Cursor = false | ||
} | ||
|
||
active = nextIndex | ||
return nil | ||
} | ||
|
||
func Try(kubeConfig k8s.KubeConfig) { | ||
fmt.Println("Rendering") | ||
// fmt.Println("kubeConfig", kubeConfig) | ||
|
||
clientset, _ := k8s.GetClientset(kubeConfig) | ||
|
||
k8s.SearchNamespaces(clientset) | ||
|
||
k8s.SearchPods(clientset, "kube-system") | ||
|
||
k8s.GetContainers(clientset, "kube-system", "kube-apiserver-kind-control-plane") | ||
k8s.GetContainers(clientset, "kube-system", "kube-controller-manager-kind-control-plane") | ||
k8s.GetContainers(clientset, "kube-system", "kube-scheduler-kind-control-plane") | ||
|
||
err := k8s.GetContainerLogs(clientset, "kube-system", "kube-apiserver-kind-control-plane", "kube-apiserver", os.Stdout) | ||
|
||
fmt.Println(err) | ||
|
||
// kube-apiserver-kind-control-plane | ||
} |