-
Notifications
You must be signed in to change notification settings - Fork 3
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
8 changed files
with
949 additions
and
0 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,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) | ||
} |
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,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) | ||
} |
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,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()) | ||
} |
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,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 | ||
) |
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,7 @@ | ||
package main | ||
|
||
import "kubemb/cmd" | ||
|
||
func main() { | ||
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,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 | ||
} |
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,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) | ||
} |