Skip to content

Commit

Permalink
adjusted ports command to simulate ip and added show-ip flag
Browse files Browse the repository at this point in the history
  • Loading branch information
NimbleArchitect committed Mar 30, 2024
1 parent 736ce22 commit 3af1551
Show file tree
Hide file tree
Showing 15 changed files with 152 additions and 59 deletions.
31 changes: 30 additions & 1 deletion pkg/plugin/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Looper interface {
BuildContainerSpec(container v1.Container, info BuilderInformation) ([][]Cell, error)
BuildEphemeralContainerSpec(container v1.EphemeralContainer, info BuilderInformation) ([][]Cell, error)
BuildContainerStatus(container v1.ContainerStatus, info BuilderInformation) ([][]Cell, error)
BuildPodRow(pod v1.Pod, info BuilderInformation) ([][]Cell, error)
Headers() []string
HideColumns(info BuilderInformation) []int
}
Expand All @@ -36,6 +37,7 @@ type RowBuilder struct {
ShowInitContainers bool
ShowContainerType bool
ShowNodeTree bool // show the tree view with the nodes at the root level rather than just the resource sets at root
DontListContainers bool // dont loop through containers, only the main pod
FilterList map[string]matchValue // used to filter out rows from the table during Print function
CalcFiltered bool // the filterd out rows are included in the branch calculations
DefaultHeaderLen int
Expand Down Expand Up @@ -344,7 +346,7 @@ func (b *RowBuilder) setValuesAnnotationLabel(pod v1.Pod) {
}

func (b *RowBuilder) populateAnnotationsLabels(podList []v1.Pod) error {
log := logger{location: "RowBuilder:BuildContainerTable"}
log := logger{location: "RowBuilder:populateAnnotationsLabels"}
log.Debug("Start")
// type kind pod label value
b.annotationLabel = make(map[string]map[string]map[string]map[string]string)
Expand Down Expand Up @@ -567,6 +569,9 @@ func (b *RowBuilder) setVisibleColumns(info *BuilderInformation) {
b.Table.HideColumn(3)
}

if b.DontListContainers {
b.Table.HideColumn(4)
}
}

// PodLoop given a pod we loop over all containers adding to the table as we go
Expand All @@ -578,6 +583,26 @@ func (b *RowBuilder) podLoop(loop Looper, info BuilderInformation, pod v1.Pod, i
log := logger{location: "RowBuilder:PodLoop"}
log.Debug("Start")

if b.DontListContainers {
log.Debug("skipping containers")
info.ContainerType = TypeIDPod
info.TypeName = TypeNamePod

allRows, err := loop.BuildPodRow(pod, info)
if err != nil {
return [][]Cell{}, err
}
for _, row := range allRows {
rowsOut := b.makeFullRow(&info, indentLevel, row)
if !b.matchShouldExclude(rowsOut) {
b.Table.AddRow(rowsOut...)
}
}
podRowsOut = append(podRowsOut, allRows...)

return podRowsOut, nil
}

if b.ShowInitContainers {
log.Debug("loop init Container")
info.ContainerType = TypeIDInitContainer
Expand Down Expand Up @@ -795,6 +820,10 @@ func (b *RowBuilder) getDefaultHead(info *BuilderInformation) []string {
headList = []string{
"T", "NAMESPACE", "NODE",
}
// } else if b.DontListContainers {
// headList = []string{
// "T", "NAMESPACE", "NODE", "PODNAME",
// }
} else {
headList = []string{
"T", "NAMESPACE", "NODE", "PODNAME", "CONTAINER",
Expand Down
4 changes: 4 additions & 0 deletions pkg/plugin/capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,7 @@ func (s *capabilities) capabilitiesBuildRow(securityContext *v1.SecurityContext,

return cellList
}

func (s *capabilities) BuildPodRow(pod v1.Pod, info BuilderInformation) ([][]Cell, error) {
return [][]Cell{}, nil
}
4 changes: 4 additions & 0 deletions pkg/plugin/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,7 @@ func (s *commands) commandsBuildRow(cmdLine commandLine, info BuilderInformation

return cellList
}

func (s *commands) BuildPodRow(pod v1.Pod, info BuilderInformation) ([][]Cell, error) {
return [][]Cell{}, nil
}
4 changes: 4 additions & 0 deletions pkg/plugin/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,7 @@ func (s *environment) buildEnvFromEphemeral(container v1.EphemeralContainer) []v
}
return container.Env
}

func (s *environment) BuildPodRow(pod v1.Pod, info BuilderInformation) ([][]Cell, error) {
return [][]Cell{}, nil
}
4 changes: 4 additions & 0 deletions pkg/plugin/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,7 @@ func (s *image) imageBuildRow(info BuilderInformation, imageName string, pullPol

return cellList
}

func (s *image) BuildPodRow(pod v1.Pod, info BuilderInformation) ([][]Cell, error) {
return [][]Cell{}, nil
}
51 changes: 1 addition & 50 deletions pkg/plugin/ip.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package plugin

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

var ipShort = "List ip addresses of all pods in the namespace listed"

var ipDescription = ` Prints the known IP addresses of the specified pod(s). if no pod is specified the IP address of
Expand All @@ -25,48 +20,4 @@ var ipExample = ` # List IP address of pods
# List IP address of all pods where the pod label app is either web or mail
%[1]s ip -l "app in (web,mail)"`

func IP(cmd *cobra.Command, kubeFlags *genericclioptions.ConfigFlags, args []string) error {
var podname []string

connect := Connector{}
if err := connect.LoadConfig(kubeFlags); err != nil {
return err
}

// if a single pod is selected we dont need to show its name
if len(args) >= 1 {
podname = args
}
commonFlagList, err := processCommonFlags(cmd)
if err != nil {
return err
}
connect.Flags = commonFlagList

podList, err := connect.GetPods(podname)
if err != nil {
return err
}
table := Table{}
table.ColourOutput = commonFlagList.outputAsColour
table.CustomColours = commonFlagList.useTheseColours
table.SetHeader(
"NAME", "IP",
)

for _, pod := range podList {

table.AddRow(
NewCellText(pod.Name),
NewCellText(pod.Status.PodIP),
)
}

if err := table.SortByNames(commonFlagList.sortList...); err != nil {
return err
}

outputTableAs(table, commonFlagList.outputAs)
return nil

}
// IP subcommand now points to ports command with useIP bool set to true
4 changes: 4 additions & 0 deletions pkg/plugin/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,7 @@ func (s *lifecycle) buildLifecycleAction(lifecycle *v1.LifecycleHandler) lifecyc

return lifecycleAction{}
}

func (s *lifecycle) BuildPodRow(pod v1.Pod, info BuilderInformation) ([][]Cell, error) {
return [][]Cell{}, nil
}
36 changes: 32 additions & 4 deletions pkg/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ func InitSubCommands(rootCmd *cobra.Command) {
var includeInitShort string = "include init container(s) in the output, by default init containers are hidden"
var odditiesShort string = "show only the outlier rows that dont fall within the computed range"
var sizeShort string = "allows conversion to the selected size rather then the default megabyte output"
var treeShort string = "Display tree like view instead of the standard list"
var nodetreeShort string = "Displays the tree with the nodes as the root"
var showIPShort string = "Show the pods IP address column"
// var treeShort string = "Display tree like view instead of the standard list"

log := logger{location: "InitSubCommands"}
Expand All @@ -75,6 +78,8 @@ func InitSubCommands(rootCmd *cobra.Command) {
},
}
KubernetesConfigFlags.AddFlags(cmdCapabilities.Flags())
cmdCapabilities.Flags().BoolP("tree", "t", false, treeShort)
cmdCapabilities.Flags().BoolP("node-tree", "", false, nodetreeShort)
addCommonFlags(cmdCapabilities)
rootCmd.AddCommand(cmdCapabilities)

Expand All @@ -95,6 +100,8 @@ func InitSubCommands(rootCmd *cobra.Command) {
},
}
KubernetesConfigFlags.AddFlags(cmdCommands.Flags())
cmdCommands.Flags().BoolP("tree", "t", false, treeShort)
cmdCommands.Flags().BoolP("node-tree", "", false, nodetreeShort)
addCommonFlags(cmdCommands)
rootCmd.AddCommand(cmdCommands)

Expand All @@ -117,6 +124,8 @@ func InitSubCommands(rootCmd *cobra.Command) {
cmdCPU.Flags().BoolP("include-init", "i", false, includeInitShort)
cmdCPU.Flags().BoolP("oddities", "", false, odditiesShort)
cmdCPU.Flags().BoolP("raw", "r", false, "show raw values")
cmdCPU.Flags().BoolP("tree", "t", false, treeShort)
cmdCPU.Flags().BoolP("node-tree", "", false, nodetreeShort)
addCommonFlags(cmdCPU)
rootCmd.AddCommand(cmdCPU)

Expand All @@ -138,6 +147,8 @@ func InitSubCommands(rootCmd *cobra.Command) {
}
KubernetesConfigFlags.AddFlags(cmdEnvironment.Flags())
cmdEnvironment.Flags().BoolP("translate", "", false, "read the configmap show its values")
cmdEnvironment.Flags().BoolP("tree", "t", false, treeShort)
cmdEnvironment.Flags().BoolP("node-tree", "", false, nodetreeShort)
addCommonFlags(cmdEnvironment)
rootCmd.AddCommand(cmdEnvironment)

Expand All @@ -149,7 +160,7 @@ func InitSubCommands(rootCmd *cobra.Command) {
Example: fmt.Sprintf(ipExample, rootCmd.CommandPath()),
// SuggestFor: []string{""},
RunE: func(cmd *cobra.Command, args []string) error {
if err := IP(cmd, KubernetesConfigFlags, args); err != nil {
if err := Ports(cmd, KubernetesConfigFlags, args, true); err != nil {
return err
}

Expand Down Expand Up @@ -178,6 +189,8 @@ func InitSubCommands(rootCmd *cobra.Command) {
}
KubernetesConfigFlags.AddFlags(cmdImage.Flags())
cmdImage.Flags().BoolP("id", "", false, "Show running containers id")
cmdImage.Flags().BoolP("tree", "t", false, treeShort)
cmdImage.Flags().BoolP("node-tree", "", false, nodetreeShort)
addCommonFlags(cmdImage)
rootCmd.AddCommand(cmdImage)

Expand All @@ -198,6 +211,8 @@ func InitSubCommands(rootCmd *cobra.Command) {
},
}
KubernetesConfigFlags.AddFlags(cmdLifecycle.Flags())
cmdLifecycle.Flags().BoolP("tree", "t", false, treeShort)
cmdLifecycle.Flags().BoolP("node-tree", "", false, nodetreeShort)
addCommonFlags(cmdLifecycle)
rootCmd.AddCommand(cmdLifecycle)

Expand All @@ -223,6 +238,8 @@ func InitSubCommands(rootCmd *cobra.Command) {
cmdMemory.Flags().BoolP("oddities", "", false, odditiesShort)
cmdMemory.Flags().BoolP("raw", "r", false, "show raw values")
cmdMemory.Flags().String("size", "Mi", sizeShort)
cmdMemory.Flags().BoolP("tree", "t", false, treeShort)
cmdMemory.Flags().BoolP("node-tree", "", false, nodetreeShort)
addCommonFlags(cmdMemory)
rootCmd.AddCommand(cmdMemory)

Expand All @@ -235,14 +252,17 @@ func InitSubCommands(rootCmd *cobra.Command) {
Aliases: []string{"port", "po"},
// SuggestFor: []string{""},
RunE: func(cmd *cobra.Command, args []string) error {
if err := Ports(cmd, KubernetesConfigFlags, args); err != nil {
if err := Ports(cmd, KubernetesConfigFlags, args, false); err != nil {
return err
}

return nil
},
}
KubernetesConfigFlags.AddFlags(cmdPorts.Flags())
cmdPorts.Flags().BoolP("tree", "t", false, treeShort)
cmdPorts.Flags().BoolP("node-tree", "", false, nodetreeShort)
cmdPorts.Flags().BoolP("show-ip", "", false, showIPShort)
addCommonFlags(cmdPorts)
rootCmd.AddCommand(cmdPorts)

Expand All @@ -263,6 +283,8 @@ func InitSubCommands(rootCmd *cobra.Command) {
},
}
KubernetesConfigFlags.AddFlags(cmdProbes.Flags())
cmdProbes.Flags().BoolP("tree", "t", false, treeShort)
cmdProbes.Flags().BoolP("node-tree", "", false, nodetreeShort)
addCommonFlags(cmdProbes)
rootCmd.AddCommand(cmdProbes)

Expand All @@ -285,6 +307,8 @@ func InitSubCommands(rootCmd *cobra.Command) {
}
KubernetesConfigFlags.AddFlags(cmdRestart.Flags())
cmdRestart.Flags().BoolP("oddities", "", false, odditiesShort)
cmdRestart.Flags().BoolP("tree", "t", false, treeShort)
cmdRestart.Flags().BoolP("node-tree", "", false, nodetreeShort)
addCommonFlags(cmdRestart)
rootCmd.AddCommand(cmdRestart)

Expand All @@ -306,6 +330,8 @@ func InitSubCommands(rootCmd *cobra.Command) {
}
KubernetesConfigFlags.AddFlags(cmdSecurity.Flags())
cmdSecurity.Flags().BoolP("selinux", "", false, "show the SELinux context thats applied to the containers")
cmdSecurity.Flags().BoolP("tree", "t", false, treeShort)
cmdSecurity.Flags().BoolP("node-tree", "", false, nodetreeShort)
addCommonFlags(cmdSecurity)
rootCmd.AddCommand(cmdSecurity)

Expand Down Expand Up @@ -333,6 +359,8 @@ func InitSubCommands(rootCmd *cobra.Command) {
cmdStatus.Flags().BoolP("oddities", "", false, odditiesShort)
cmdStatus.Flags().BoolP("previous", "p", false, "Show previous state")
cmdStatus.Flags().BoolP("id", "", false, "Show running containers id")
cmdStatus.Flags().BoolP("tree", "t", false, treeShort)
cmdStatus.Flags().BoolP("node-tree", "", false, nodetreeShort)
// TODO: check if I can add labels for service/replicaset/configmap etc.
addCommonFlags(cmdStatus)
rootCmd.AddCommand(cmdStatus)
Expand Down Expand Up @@ -366,6 +394,8 @@ func InitSubCommands(rootCmd *cobra.Command) {
}
KubernetesConfigFlags.AddFlags(cmdVolume.Flags())
cmdVolume.Flags().BoolP("device", "d", false, "show raw block device mappings within a container")
cmdVolume.Flags().BoolP("tree", "t", false, treeShort)
cmdVolume.Flags().BoolP("node-tree", "", false, nodetreeShort)
addCommonFlags(cmdVolume)
rootCmd.AddCommand(cmdVolume)

Expand All @@ -385,8 +415,6 @@ func addCommonFlags(cmdObj *cobra.Command) {
cmdObj.Flags().BoolP("show-node", "", false, `Show the node name column`)
cmdObj.Flags().BoolP("show-type", "T", false, `Show the container type column, where:
I=init container, C=container, E=ephemerial container, P=Pod, D=Deployment, R=ReplicaSet, A=DaemonSet, S=StatefulSet, N=Node`)
cmdObj.Flags().BoolP("tree", "t", false, `Display tree like view instead of the standard list`)
cmdObj.Flags().BoolP("node-tree", "", false, `Displayes the tree with the nodes as the root`)
cmdObj.Flags().StringP("node-label", "", "", `Show the selected node label as a column`)
cmdObj.Flags().StringP("pod-label", "", "", `Show the selected pod label as a column`)
cmdObj.Flags().StringP("annotation", "", "", `Show the selected annotation as a column`)
Expand Down
Loading

0 comments on commit 3af1551

Please sign in to comment.