Skip to content

Commit

Permalink
Working Proof Of Concept
Browse files Browse the repository at this point in the history
  • Loading branch information
rossedman committed Jul 2, 2021
0 parents commit 7cbf07b
Show file tree
Hide file tree
Showing 3,134 changed files with 1,123,543 additions and 0 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
11 changes: 11 additions & 0 deletions cmd/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package cmd

import "github.com/spf13/cobra"

func init() {
rootCmd.AddCommand(getCmd)
}

var getCmd = &cobra.Command{
Use: "get",
}
95 changes: 95 additions & 0 deletions cmd/get_nodes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package cmd

import (
"context"
"os"
"sort"
"strings"

"code.hq.twilio.com/redman/kubectl-tks/pkg/client"
"github.com/spf13/cobra"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/cli-runtime/pkg/printers"
)

var (
kubeconfig string
)

func init() {
getCmd.AddCommand(getNodesCmd)
getNodesCmd.PersistentFlags().StringVarP(&kubeconfig, "kubeconfig", "c", "$HOME/.kube/config", "path to kube config file")
}

// ByRole fulfills the package sort interface
// so we can sort by custom values from the v1.Node
// that is returned
type ByRole []v1.Node

func (r ByRole) Len() int { return len(r) }
func (r ByRole) Swap(i, j int) { r[i], r[j] = r[j], r[i] }

// Less if the strings are already sorted it would mean
// the first value passed in is "lesser" on the alphabet
// making this true
func (r ByRole) Less(i, j int) bool {
return sort.StringsAreSorted([]string{
r[i].GetLabels()["twilio.com/role"],
r[j].GetLabels()["twilio.com/role"],
})
}

var getNodesCmd = &cobra.Command{
Use: "nodes",
Short: "retrieve cluster nodes",
RunE: func(c *cobra.Command, args []string) error {
clientset := client.InitClient()
// get all nodes
nodes, err := clientset.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{})
if err != nil {
return err
}

// sort the nodes by role
sort.Sort(ByRole(nodes.Items))

// setup table schema
t := &metav1.Table{
ColumnDefinitions: []metav1.TableColumnDefinition{
{Name: "Role", Type: "string"},
{Name: "Name", Type: "string"},
{Name: "SID", Type: "string"},
{Name: "Status", Type: "string"},
},
}
// add rows
for _, n := range nodes.Items {
// deduce status
status := ""
for _, c := range n.Status.Conditions {
if (c.Status == "True") && (c.Type == "Ready") {
status = string(c.Type)
}
if (c.Status == "True") && (c.Type != "Ready") {
status = strings.Join([]string{status, string(c.Type)}, ",")
}
}
// create row
t.Rows = append(t.Rows, metav1.TableRow{
Cells: []interface{}{
n.GetLabels()["twilio.com/role"],
n.Name,
n.GetLabels()["twilio.com/host-sid"],
status,
},
})
}

// print
p := printers.NewTablePrinter(printers.PrintOptions{})
p.PrintObj(t, os.Stdout)

return nil
},
}
19 changes: 19 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
Use: "tks",
}

func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
40 changes: 40 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module code.hq.twilio.com/redman/kubectl-tks

go 1.16

require (
cloud.google.com/go v0.86.0 // indirect
github.com/Azure/go-autorest/autorest v0.11.19 // indirect
github.com/Azure/go-autorest/autorest/adal v0.9.14 // indirect
github.com/form3tech-oss/jwt-go v3.2.3+incompatible // indirect
github.com/go-errors/errors v1.4.0 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.19.6 // indirect
github.com/go-openapi/swag v0.19.15 // indirect
github.com/google/btree v1.0.1 // indirect
github.com/google/go-cmp v0.5.6 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.2.0 // indirect
github.com/googleapis/gnostic v0.5.5 // indirect
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/spf13/cobra v1.2.1
github.com/xlab/treeprint v1.1.0 // indirect
go.starlark.net v0.0.0-20210602144842-1cdb82c9e17a // indirect
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e // indirect
golang.org/x/net v0.0.0-20210614182718-04defd469f4e // indirect
golang.org/x/oauth2 v0.0.0-20210628180205-a41e5a781914 // indirect
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b // indirect
golang.org/x/time v0.0.0-20210611083556-38a9dc6acbc6 // indirect
google.golang.org/protobuf v1.27.1 // indirect
k8s.io/apimachinery v0.21.2
k8s.io/cli-runtime v0.21.2
k8s.io/client-go v0.21.2
k8s.io/klog/v2 v2.9.0 // indirect
k8s.io/kube-openapi v0.0.0-20210527164424-3c818078ee3d // indirect
k8s.io/utils v0.0.0-20210629042839-4a2b36d8d73f // indirect
sigs.k8s.io/kustomize/api v0.8.11 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.1.2 // indirect
)
Loading

0 comments on commit 7cbf07b

Please sign in to comment.