Skip to content
This repository has been archived by the owner on Nov 20, 2020. It is now read-only.

Commit

Permalink
init client
Browse files Browse the repository at this point in the history
  • Loading branch information
yolossn committed Sep 12, 2020
1 parent aeac9ca commit db7d739
Show file tree
Hide file tree
Showing 7 changed files with 460 additions and 7 deletions.
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ require (
github.com/jesseduffield/gocui v0.3.1-0.20200201013258-57fdcf23edc5
github.com/jesseduffield/termbox-go v0.0.0-20200823212418-a2289ed6aafe // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e // indirect
k8s.io/apimachinery v0.19.0
k8s.io/client-go v0.19.0

)
336 changes: 336 additions & 0 deletions go.sum

Large diffs are not rendered by default.

13 changes: 11 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
package main

import "github.com/yolossn/lazykubernetes/pkg/app"
import (
"github.com/yolossn/lazykubernetes/pkg/app"
"github.com/yolossn/lazykubernetes/pkg/client"
)

func main() {
ui, err := app.NewApp()
// Setup k8sClient
k8sClient, err := client.Newk8s()
if err != nil {
panic(err)
}

ui, err := app.NewApp(k8sClient)
if err != nil {
panic(err)
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/app/app.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app

import (
"github.com/yolossn/lazykubernetes/pkg/client"
"github.com/yolossn/lazykubernetes/pkg/gui"
)

Expand All @@ -12,9 +13,9 @@ func (app *App) Run() error {
return app.Gui.Run()
}

func NewApp() (*App, error) {
func NewApp(k8sClient *client.K8s) (*App, error) {

gui, err := gui.NewGui()
gui, err := gui.NewGui(k8sClient)
if err != nil {
return nil, err
}
Expand Down
73 changes: 73 additions & 0 deletions pkg/client/kube.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package client

import (
"context"
"path/filepath"
"time"

v1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
)

type K8s struct {
client *kubernetes.Clientset
}

func Newk8s() (*K8s, error) {
var kubeconfig string
if home := homedir.HomeDir(); home != "" {
kubeconfig = filepath.Join(home, ".kube", "config")
}
// } else {
// kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
// }

config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
return nil, err
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}

return &K8s{clientset}, nil
}

type NamespaceInfo struct {
Name string
Status string
CreatedAt time.Time
}

func (k *K8s) ListNamespace() ([]NamespaceInfo, error) {
ctx := context.TODO()
opts := v1.ListOptions{}
list, _ := k.client.CoreV1().Namespaces().List(ctx, opts)
// fmt.Println(list, err)
ns := []NamespaceInfo{}
for _, item := range list.Items {
n := NamespaceInfo{
Name: item.ObjectMeta.Name,
Status: string(item.Status.Phase),
CreatedAt: item.ObjectMeta.CreationTimestamp.Time,
}
ns = append(ns, n)
}
return ns, nil
}

// TODO: Create Struct for output similar to namespace
func (k *K8s) ListPods(namespace string) ([]string, error) {
ctx := context.TODO()
opts := v1.ListOptions{}
pods, _ := k.client.CoreV1().Pods(namespace).List(ctx, opts)
podList := []string{}
for _, pod := range pods.Items {
podList = append(podList, pod.Name)
}
return podList, nil
}
11 changes: 8 additions & 3 deletions pkg/gui/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ package gui

import (
"github.com/jesseduffield/gocui"
"github.com/yolossn/lazykubernetes/pkg/client"
)

var OverlappingEdges = false

type Gui struct {
g *gocui.Gui
g *gocui.Gui
k8sClient *client.K8s
}

func NewGui() (*Gui, error) {
return &Gui{}, nil
func NewGui(k8sClient *client.K8s) (*Gui, error) {
return &Gui{k8sClient: k8sClient}, nil
}

func (gui *Gui) Run() error {
Expand Down Expand Up @@ -42,6 +44,9 @@ func (gui *Gui) Run() error {
return err
}

// Init render
go gui.reRenderNamespace()

err = g.MainLoop()
return err
}
25 changes: 25 additions & 0 deletions pkg/gui/namespace.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package gui

import (
"fmt"

"github.com/jesseduffield/gocui"
)

Expand All @@ -20,3 +22,26 @@ func (gui *Gui) onNamespaceClick(g *gocui.Gui, v *gocui.View) error {

return nil
}

func (gui *Gui) reRenderNamespace() error {
nsView := gui.getNamespaceView()
if nsView == nil {
return nil
}

ns, err := gui.k8sClient.ListNamespace()
if err != nil {
return err
}

gui.g.Update(func(*gocui.Gui) error {
nsView.Clear()
for _, n := range ns {
fmt.Fprintln(nsView, n.Name)
fmt.Println(n.Name)
}
return nil
})

return nil
}

0 comments on commit db7d739

Please sign in to comment.