Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Yu-HaoYu committed Jun 9, 2021
1 parent 96de013 commit be7cf78
Show file tree
Hide file tree
Showing 8 changed files with 949 additions and 0 deletions.
53 changes: 53 additions & 0 deletions cmd/capacity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package cmd

import (
"context"
"github.com/spf13/cobra"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"kubemb/pkg/kube"
"kubemb/pkg/utils"
)

func Capacity() error {
clientset := kube.Clientset(KubernetesConfigFlags)

n, _ := rootCmd.Flags().GetString("namespace")
deployList, err := clientset.AppsV1().Deployments(n).List(context.TODO(), v1.ListOptions{})
if err != nil {
return err
}

var itemSlice []string
itemSlice = append(itemSlice, "NAMESPACE\tCONTAINER NAME\tCPU REQUESTS\tMEMORY REQUESTS\tCPU LIMITS\tMEMORY LIMITS")
for i := 0; i < len(deployList.Items); i++ {
d := deployList.Items[i]
n := d.Namespace
name := d.Spec.Template.Spec.Containers[0].Name
rCPU := d.Spec.Template.Spec.Containers[0].Resources.Requests.Cpu().String()
rMem := d.Spec.Template.Spec.Containers[0].Resources.Requests.Memory().String()
lCPU := d.Spec.Template.Spec.Containers[0].Resources.Limits.Cpu().String()
lMem := d.Spec.Template.Spec.Containers[0].Resources.Limits.Memory().String()

itemSlice = append(itemSlice, n+"\t"+name+"\t"+rCPU+"\t"+rMem+"\t"+lCPU+"\t"+lMem)
}

utils.Fprint(itemSlice)

return nil
}

var capacityCmd = &cobra.Command{
Use: "capacity",
Aliases: []string{"cap"},
Short: "Alias \"cap\", view the current resource usage of Node and Pod",
Long: "View the current resource usage of Node and Pod",
Run: func(cmd *cobra.Command, args []string) {
if err := Capacity(); err != nil {
panic(err.Error())
}
},
}

func init() {
rootCmd.AddCommand(capacityCmd)
}
61 changes: 61 additions & 0 deletions cmd/image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package cmd

import (
"context"
"fmt"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"kubemb/pkg/kube"
"kubemb/pkg/utils"
)

var (
example = `# Get deployment image versions of all namespaces
%[1]s image
# Get the deployment image version of the specified namespace
%[1]s image -n default`
)

func Image() error {
clientset := kube.Clientset(KubernetesConfigFlags)

n, _ := rootCmd.Flags().GetString("namespace")
deployList, err := clientset.AppsV1().Deployments(n).List(context.TODO(), v1.ListOptions{})
if err != nil {
return err
}

var itemSlice []string
itemSlice = append(itemSlice, "NAMESPACE\tDEPLOY_NAME\tCONTAINER_NAME\tIMAGE")
for i := 0; i < len(deployList.Items); i++ {
d := deployList.Items[i]
for i := 0; i < len(d.Spec.Template.Spec.Containers); i++ {
c := d.Spec.Template.Spec.Containers[i]
tab := "\t"
item := d.Namespace + tab + d.Name + tab + c.Name + tab + c.Image
itemSlice = append(itemSlice, item)
}
}

utils.Fprint(itemSlice)

return nil
}

var imageCmd = &cobra.Command{
Use: "image",
Short: "View the currently deployed image version",
Long: `View the currently deployed image versiondir
`,
Example: fmt.Sprintf(example, "kubemb"),
Run: func(cmd *cobra.Command, args []string) {
if err := Image(); err != nil {
panic(err.Error())
}
},
}

func init() {
rootCmd.AddCommand(imageCmd)
}
30 changes: 30 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cmd

import (
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
)

const (
version = "0.0.2\nBy John.Yu"
)

var (
KubernetesConfigFlags *genericclioptions.ConfigFlags
)

var rootCmd = &cobra.Command{
Use: "kubemb",
Short: "This is a kubectl magic box plugin",
Long: `This is a kubectl magic box plugin`,
Version: version,
}

func Execute() {
cobra.CheckErr(rootCmd.Execute())
}

func init() {
KubernetesConfigFlags = genericclioptions.NewConfigFlags(true)
KubernetesConfigFlags.AddFlags(rootCmd.PersistentFlags())
}
13 changes: 13 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module kubemb

go 1.16

require (
github.com/pkg/errors v0.9.1 // indirect
github.com/spf13/cobra v1.1.3
github.com/spf13/pflag v1.0.5 // indirect
k8s.io/apimachinery v0.21.1 // indirect
k8s.io/cli-runtime v0.21.1
k8s.io/client-go v0.21.1 // indirect
k8s.io/kubectl v0.21.1 // indirect
)
734 changes: 734 additions & 0 deletions go.sum

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "kubemb/cmd"

func main() {
cmd.Execute()
}
21 changes: 21 additions & 0 deletions pkg/kube/clientset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package kube

import (
"fmt"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/kubernetes"
)

func Clientset(configFlags *genericclioptions.ConfigFlags) *kubernetes.Clientset {
config, err := configFlags.ToRESTConfig()
if err != nil {
fmt.Println(err, "failed to read kubeconfig")
}

clientset, err := kubernetes.NewForConfig(config)
if err != nil {
fmt.Println(err, "failed to create clientset")
}

return clientset
}
30 changes: 30 additions & 0 deletions pkg/utils/fprint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package utils

import (
"fmt"
"os"
"text/tabwriter"
)

type tableWriter struct {
w *tabwriter.Writer
}

func (tw *tableWriter) Print(items []string) {
tw.w.Init(os.Stdout, 0, 8, 1, '\t', 0)

for _, item := range items {
_, err := fmt.Fprintln(tw.w, item)
if err != nil {
}
}

err := tw.w.Flush()
if err != nil {
}
}

func Fprint(items []string) {
tw := &tableWriter{w: new(tabwriter.Writer)}
tw.Print(items)
}

0 comments on commit be7cf78

Please sign in to comment.