Skip to content

Commit

Permalink
Move to tablewriter from tabwriter
Browse files Browse the repository at this point in the history
  • Loading branch information
Kavinraja-G committed Aug 20, 2023
1 parent c188afd commit a6c9c25
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 40 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.20

require (
github.com/Kavinraja-G/kube-bouncer v0.1.0
github.com/olekukonko/tablewriter v0.0.5
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
k8s.io/api v0.28.0
Expand All @@ -30,6 +31,7 @@ require (
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,17 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/onsi/ginkgo/v2 v2.9.4 h1:xR7vG4IXt5RWx6FfIjyAtsoMAtnc3C/rFXBBd2AjZwE=
github.com/onsi/gomega v1.27.6 h1:ENqfyGeS5AX/rlXDd/ETokDz93u0YufY1Pgxuy/PvWE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
32 changes: 28 additions & 4 deletions pkg/cmd/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@ package cmd

import (
"context"
"fmt"
"log"

"github.com/Kavinraja-G/node-gizmo/pkg/outputs"

"github.com/Kavinraja-G/node-gizmo/pkg"
"github.com/Kavinraja-G/node-gizmo/pkg/auth"
"github.com/Kavinraja-G/node-gizmo/pkg/utils"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var showTaints bool

func NewCmdNodeInfo() *cobra.Command {
cmd := &cobra.Command{
Use: "node",
Expand All @@ -22,11 +26,17 @@ func NewCmdNodeInfo() *cobra.Command {
},
}

// flags
cmd.Flags().BoolVarP(&showTaints, "show-taints", "t", false, "Shows taints on Nodes in the output")

return cmd
}

func showNodeInfo(cmd *cobra.Command, args []string) error {
var nodeInfos []pkg.GenericNodeInfo
var outputOpts = pkg.OutputOptsForGenericNodeInfo{
ShowTaints: showTaints,
}

clientset, err := auth.K8sAuth()
if err != nil {
Expand All @@ -35,21 +45,35 @@ func showNodeInfo(cmd *cobra.Command, args []string) error {

nodes, err := clientset.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{})
for _, node := range nodes.Items {
nodeInfos = append(nodeInfos, pkg.GenericNodeInfo{
genericNodeInfo := pkg.GenericNodeInfo{
NodeName: node.Name,
K8sVersion: node.Status.NodeInfo.KubeletVersion,
Image: node.Status.NodeInfo.OSImage,
Os: node.Status.NodeInfo.OperatingSystem,
OsArch: node.Status.NodeInfo.Architecture,
NodeStatus: getNodeStatus(node.Status.Conditions),
})
}

if ok, _ := cmd.Flags().GetBool("show-taints"); ok {
genericNodeInfo.Taints = getNodeTaints(node.Spec.Taints)
}

nodeInfos = append(nodeInfos, genericNodeInfo)
}

utils.OutputGenericNodeInfo(nodeInfos)
outputs.OutputGenericNodeInfo(nodeInfos, outputOpts)

return nil
}

func getNodeTaints(rawTaints []corev1.Taint) []string {
var taints []string
for _, taint := range rawTaints {
taints = append(taints, fmt.Sprintf("%v=%v:%v", taint.Key, taint.Value, taint.Effect))
}
return taints
}

func getNodeStatus(nodeConditions []corev1.NodeCondition) string {
for _, nodeCondition := range nodeConditions {
if nodeCondition.Type == corev1.NodeReady {
Expand Down
43 changes: 43 additions & 0 deletions pkg/outputs/table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package outputs

import (
"os"
"strings"

"github.com/Kavinraja-G/node-gizmo/pkg"
"github.com/olekukonko/tablewriter"
)

func OutputGenericNodeInfo(genericNodeInfos []pkg.GenericNodeInfo, outputOpts pkg.OutputOptsForGenericNodeInfo) {
table := tablewriter.NewWriter(os.Stdout)
table.SetRowLine(false)
table.SetBorder(false)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetAutoWrapText(true)
table.SetColumnSeparator("")
table.SetHeaderLine(false)

// default headers
headers := []string{"NAME", "VERSION", "IMAGE", "OS", "ARCHITECTURE", "STATUS"}
if outputOpts.ShowTaints {
headers = append(headers, "TAINTS")
}
table.SetHeader(headers)

for _, nodeInfo := range genericNodeInfos {
lineItems := []string{
nodeInfo.NodeName,
nodeInfo.K8sVersion,
nodeInfo.Image,
nodeInfo.Os,
nodeInfo.OsArch,
nodeInfo.NodeStatus,
}
if outputOpts.ShowTaints {
lineItems = append(lineItems, strings.Join(nodeInfo.Taints, "\n"))
}
table.Append(lineItems)
}

table.Render()
}
5 changes: 5 additions & 0 deletions pkg/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ type GenericNodeInfo struct {
Os string
OsArch string
NodeStatus string
Taints []string
}

type OutputOptsForGenericNodeInfo struct {
ShowTaints bool
}
36 changes: 0 additions & 36 deletions pkg/utils/table.go

This file was deleted.

0 comments on commit a6c9c25

Please sign in to comment.