Skip to content

Commit 3af1551

Browse files
adjusted ports command to simulate ip and added show-ip flag
1 parent 736ce22 commit 3af1551

File tree

15 files changed

+152
-59
lines changed

15 files changed

+152
-59
lines changed

pkg/plugin/builder.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type Looper interface {
1414
BuildContainerSpec(container v1.Container, info BuilderInformation) ([][]Cell, error)
1515
BuildEphemeralContainerSpec(container v1.EphemeralContainer, info BuilderInformation) ([][]Cell, error)
1616
BuildContainerStatus(container v1.ContainerStatus, info BuilderInformation) ([][]Cell, error)
17+
BuildPodRow(pod v1.Pod, info BuilderInformation) ([][]Cell, error)
1718
Headers() []string
1819
HideColumns(info BuilderInformation) []int
1920
}
@@ -36,6 +37,7 @@ type RowBuilder struct {
3637
ShowInitContainers bool
3738
ShowContainerType bool
3839
ShowNodeTree bool // show the tree view with the nodes at the root level rather than just the resource sets at root
40+
DontListContainers bool // dont loop through containers, only the main pod
3941
FilterList map[string]matchValue // used to filter out rows from the table during Print function
4042
CalcFiltered bool // the filterd out rows are included in the branch calculations
4143
DefaultHeaderLen int
@@ -344,7 +346,7 @@ func (b *RowBuilder) setValuesAnnotationLabel(pod v1.Pod) {
344346
}
345347

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

572+
if b.DontListContainers {
573+
b.Table.HideColumn(4)
574+
}
570575
}
571576

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

586+
if b.DontListContainers {
587+
log.Debug("skipping containers")
588+
info.ContainerType = TypeIDPod
589+
info.TypeName = TypeNamePod
590+
591+
allRows, err := loop.BuildPodRow(pod, info)
592+
if err != nil {
593+
return [][]Cell{}, err
594+
}
595+
for _, row := range allRows {
596+
rowsOut := b.makeFullRow(&info, indentLevel, row)
597+
if !b.matchShouldExclude(rowsOut) {
598+
b.Table.AddRow(rowsOut...)
599+
}
600+
}
601+
podRowsOut = append(podRowsOut, allRows...)
602+
603+
return podRowsOut, nil
604+
}
605+
581606
if b.ShowInitContainers {
582607
log.Debug("loop init Container")
583608
info.ContainerType = TypeIDInitContainer
@@ -795,6 +820,10 @@ func (b *RowBuilder) getDefaultHead(info *BuilderInformation) []string {
795820
headList = []string{
796821
"T", "NAMESPACE", "NODE",
797822
}
823+
// } else if b.DontListContainers {
824+
// headList = []string{
825+
// "T", "NAMESPACE", "NODE", "PODNAME",
826+
// }
798827
} else {
799828
headList = []string{
800829
"T", "NAMESPACE", "NODE", "PODNAME", "CONTAINER",

pkg/plugin/capabilities.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,7 @@ func (s *capabilities) capabilitiesBuildRow(securityContext *v1.SecurityContext,
154154

155155
return cellList
156156
}
157+
158+
func (s *capabilities) BuildPodRow(pod v1.Pod, info BuilderInformation) ([][]Cell, error) {
159+
return [][]Cell{}, nil
160+
}

pkg/plugin/commands.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,7 @@ func (s *commands) commandsBuildRow(cmdLine commandLine, info BuilderInformation
146146

147147
return cellList
148148
}
149+
150+
func (s *commands) BuildPodRow(pod v1.Pod, info BuilderInformation) ([][]Cell, error) {
151+
return [][]Cell{}, nil
152+
}

pkg/plugin/environment.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,7 @@ func (s *environment) buildEnvFromEphemeral(container v1.EphemeralContainer) []v
201201
}
202202
return container.Env
203203
}
204+
205+
func (s *environment) BuildPodRow(pod v1.Pod, info BuilderInformation) ([][]Cell, error) {
206+
return [][]Cell{}, nil
207+
}

pkg/plugin/image.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,7 @@ func (s *image) imageBuildRow(info BuilderInformation, imageName string, pullPol
201201

202202
return cellList
203203
}
204+
205+
func (s *image) BuildPodRow(pod v1.Pod, info BuilderInformation) ([][]Cell, error) {
206+
return [][]Cell{}, nil
207+
}

pkg/plugin/ip.go

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
package plugin
22

3-
import (
4-
"github.com/spf13/cobra"
5-
"k8s.io/cli-runtime/pkg/genericclioptions"
6-
)
7-
83
var ipShort = "List ip addresses of all pods in the namespace listed"
94

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

28-
func IP(cmd *cobra.Command, kubeFlags *genericclioptions.ConfigFlags, args []string) error {
29-
var podname []string
30-
31-
connect := Connector{}
32-
if err := connect.LoadConfig(kubeFlags); err != nil {
33-
return err
34-
}
35-
36-
// if a single pod is selected we dont need to show its name
37-
if len(args) >= 1 {
38-
podname = args
39-
}
40-
commonFlagList, err := processCommonFlags(cmd)
41-
if err != nil {
42-
return err
43-
}
44-
connect.Flags = commonFlagList
45-
46-
podList, err := connect.GetPods(podname)
47-
if err != nil {
48-
return err
49-
}
50-
table := Table{}
51-
table.ColourOutput = commonFlagList.outputAsColour
52-
table.CustomColours = commonFlagList.useTheseColours
53-
table.SetHeader(
54-
"NAME", "IP",
55-
)
56-
57-
for _, pod := range podList {
58-
59-
table.AddRow(
60-
NewCellText(pod.Name),
61-
NewCellText(pod.Status.PodIP),
62-
)
63-
}
64-
65-
if err := table.SortByNames(commonFlagList.sortList...); err != nil {
66-
return err
67-
}
68-
69-
outputTableAs(table, commonFlagList.outputAs)
70-
return nil
71-
72-
}
23+
// IP subcommand now points to ports command with useIP bool set to true

pkg/plugin/lifecycle.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,7 @@ func (s *lifecycle) buildLifecycleAction(lifecycle *v1.LifecycleHandler) lifecyc
211211

212212
return lifecycleAction{}
213213
}
214+
215+
func (s *lifecycle) BuildPodRow(pod v1.Pod, info BuilderInformation) ([][]Cell, error) {
216+
return [][]Cell{}, nil
217+
}

pkg/plugin/plugin.go

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ func InitSubCommands(rootCmd *cobra.Command) {
5050
var includeInitShort string = "include init container(s) in the output, by default init containers are hidden"
5151
var odditiesShort string = "show only the outlier rows that dont fall within the computed range"
5252
var sizeShort string = "allows conversion to the selected size rather then the default megabyte output"
53+
var treeShort string = "Display tree like view instead of the standard list"
54+
var nodetreeShort string = "Displays the tree with the nodes as the root"
55+
var showIPShort string = "Show the pods IP address column"
5356
// var treeShort string = "Display tree like view instead of the standard list"
5457

5558
log := logger{location: "InitSubCommands"}
@@ -75,6 +78,8 @@ func InitSubCommands(rootCmd *cobra.Command) {
7578
},
7679
}
7780
KubernetesConfigFlags.AddFlags(cmdCapabilities.Flags())
81+
cmdCapabilities.Flags().BoolP("tree", "t", false, treeShort)
82+
cmdCapabilities.Flags().BoolP("node-tree", "", false, nodetreeShort)
7883
addCommonFlags(cmdCapabilities)
7984
rootCmd.AddCommand(cmdCapabilities)
8085

@@ -95,6 +100,8 @@ func InitSubCommands(rootCmd *cobra.Command) {
95100
},
96101
}
97102
KubernetesConfigFlags.AddFlags(cmdCommands.Flags())
103+
cmdCommands.Flags().BoolP("tree", "t", false, treeShort)
104+
cmdCommands.Flags().BoolP("node-tree", "", false, nodetreeShort)
98105
addCommonFlags(cmdCommands)
99106
rootCmd.AddCommand(cmdCommands)
100107

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

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

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

@@ -178,6 +189,8 @@ func InitSubCommands(rootCmd *cobra.Command) {
178189
}
179190
KubernetesConfigFlags.AddFlags(cmdImage.Flags())
180191
cmdImage.Flags().BoolP("id", "", false, "Show running containers id")
192+
cmdImage.Flags().BoolP("tree", "t", false, treeShort)
193+
cmdImage.Flags().BoolP("node-tree", "", false, nodetreeShort)
181194
addCommonFlags(cmdImage)
182195
rootCmd.AddCommand(cmdImage)
183196

@@ -198,6 +211,8 @@ func InitSubCommands(rootCmd *cobra.Command) {
198211
},
199212
}
200213
KubernetesConfigFlags.AddFlags(cmdLifecycle.Flags())
214+
cmdLifecycle.Flags().BoolP("tree", "t", false, treeShort)
215+
cmdLifecycle.Flags().BoolP("node-tree", "", false, nodetreeShort)
201216
addCommonFlags(cmdLifecycle)
202217
rootCmd.AddCommand(cmdLifecycle)
203218

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

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

242259
return nil
243260
},
244261
}
245262
KubernetesConfigFlags.AddFlags(cmdPorts.Flags())
263+
cmdPorts.Flags().BoolP("tree", "t", false, treeShort)
264+
cmdPorts.Flags().BoolP("node-tree", "", false, nodetreeShort)
265+
cmdPorts.Flags().BoolP("show-ip", "", false, showIPShort)
246266
addCommonFlags(cmdPorts)
247267
rootCmd.AddCommand(cmdPorts)
248268

@@ -263,6 +283,8 @@ func InitSubCommands(rootCmd *cobra.Command) {
263283
},
264284
}
265285
KubernetesConfigFlags.AddFlags(cmdProbes.Flags())
286+
cmdProbes.Flags().BoolP("tree", "t", false, treeShort)
287+
cmdProbes.Flags().BoolP("node-tree", "", false, nodetreeShort)
266288
addCommonFlags(cmdProbes)
267289
rootCmd.AddCommand(cmdProbes)
268290

@@ -285,6 +307,8 @@ func InitSubCommands(rootCmd *cobra.Command) {
285307
}
286308
KubernetesConfigFlags.AddFlags(cmdRestart.Flags())
287309
cmdRestart.Flags().BoolP("oddities", "", false, odditiesShort)
310+
cmdRestart.Flags().BoolP("tree", "t", false, treeShort)
311+
cmdRestart.Flags().BoolP("node-tree", "", false, nodetreeShort)
288312
addCommonFlags(cmdRestart)
289313
rootCmd.AddCommand(cmdRestart)
290314

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

@@ -333,6 +359,8 @@ func InitSubCommands(rootCmd *cobra.Command) {
333359
cmdStatus.Flags().BoolP("oddities", "", false, odditiesShort)
334360
cmdStatus.Flags().BoolP("previous", "p", false, "Show previous state")
335361
cmdStatus.Flags().BoolP("id", "", false, "Show running containers id")
362+
cmdStatus.Flags().BoolP("tree", "t", false, treeShort)
363+
cmdStatus.Flags().BoolP("node-tree", "", false, nodetreeShort)
336364
// TODO: check if I can add labels for service/replicaset/configmap etc.
337365
addCommonFlags(cmdStatus)
338366
rootCmd.AddCommand(cmdStatus)
@@ -366,6 +394,8 @@ func InitSubCommands(rootCmd *cobra.Command) {
366394
}
367395
KubernetesConfigFlags.AddFlags(cmdVolume.Flags())
368396
cmdVolume.Flags().BoolP("device", "d", false, "show raw block device mappings within a container")
397+
cmdVolume.Flags().BoolP("tree", "t", false, treeShort)
398+
cmdVolume.Flags().BoolP("node-tree", "", false, nodetreeShort)
369399
addCommonFlags(cmdVolume)
370400
rootCmd.AddCommand(cmdVolume)
371401

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

0 commit comments

Comments
 (0)