Commit 7df60d6b authored by juanvallejo's avatar juanvallejo

display resource type as part of its name

parent 8cabbcbd
...@@ -423,6 +423,7 @@ session-affinity ...@@ -423,6 +423,7 @@ session-affinity
setup-node setup-node
show-all show-all
show-events show-events
show-kind
show-labels show-labels
shutdown-fd shutdown-fd
shutdown-fifo shutdown-fifo
......
...@@ -103,6 +103,7 @@ func NewCmdGet(f *cmdutil.Factory, out io.Writer) *cobra.Command { ...@@ -103,6 +103,7 @@ func NewCmdGet(f *cmdutil.Factory, out io.Writer) *cobra.Command {
cmd.Flags().StringP("selector", "l", "", "Selector (label query) to filter on") cmd.Flags().StringP("selector", "l", "", "Selector (label query) to filter on")
cmd.Flags().BoolP("watch", "w", false, "After listing/getting the requested object, watch for changes.") cmd.Flags().BoolP("watch", "w", false, "After listing/getting the requested object, watch for changes.")
cmd.Flags().Bool("watch-only", false, "Watch for changes to the requested object(s), without listing/getting first.") cmd.Flags().Bool("watch-only", false, "Watch for changes to the requested object(s), without listing/getting first.")
cmd.Flags().Bool("show-kind", false, "If present, list the resource type for the requested object(s).")
cmd.Flags().Bool("all-namespaces", false, "If present, list the requested object(s) across all namespaces. Namespace in current context is ignored even if specified with --namespace.") cmd.Flags().Bool("all-namespaces", false, "If present, list the requested object(s) across all namespaces. Namespace in current context is ignored even if specified with --namespace.")
cmd.Flags().StringSliceP("label-columns", "L", []string{}, "Accepts a comma separated list of labels that are going to be presented as columns. Names are case-sensitive. You can also use multiple flag statements like -L label1 -L label2...") cmd.Flags().StringSliceP("label-columns", "L", []string{}, "Accepts a comma separated list of labels that are going to be presented as columns. Names are case-sensitive. You can also use multiple flag statements like -L label1 -L label2...")
cmd.Flags().Bool("export", false, "If true, use 'export' for the resources. Exported resources are stripped of cluster-specific information.") cmd.Flags().Bool("export", false, "If true, use 'export' for the resources. Exported resources are stripped of cluster-specific information.")
...@@ -118,6 +119,7 @@ func NewCmdGet(f *cmdutil.Factory, out io.Writer) *cobra.Command { ...@@ -118,6 +119,7 @@ func NewCmdGet(f *cmdutil.Factory, out io.Writer) *cobra.Command {
func RunGet(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string, options *GetOptions) error { func RunGet(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string, options *GetOptions) error {
selector := cmdutil.GetFlagString(cmd, "selector") selector := cmdutil.GetFlagString(cmd, "selector")
allNamespaces := cmdutil.GetFlagBool(cmd, "all-namespaces") allNamespaces := cmdutil.GetFlagBool(cmd, "all-namespaces")
allKinds := cmdutil.GetFlagBool(cmd, "show-kind")
mapper, typer := f.Object(cmdutil.GetIncludeThirdPartyAPIs(cmd)) mapper, typer := f.Object(cmdutil.GetIncludeThirdPartyAPIs(cmd))
cmdNamespace, enforceNamespace, err := f.DefaultNamespace() cmdNamespace, enforceNamespace, err := f.DefaultNamespace()
...@@ -294,9 +296,29 @@ func RunGet(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string ...@@ -294,9 +296,29 @@ func RunGet(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string
// use the default printer for each object // use the default printer for each object
printer = nil printer = nil
var lastMapping *meta.RESTMapping var lastMapping *meta.RESTMapping
var withKind bool = allKinds
w := kubectl.GetNewTabWriter(out) w := kubectl.GetNewTabWriter(out)
defer w.Flush() defer w.Flush()
// determine if printing multiple kinds of
// objects and enforce "show-kinds" flag if so
for ix := range objs {
var mapping *meta.RESTMapping
if sorter != nil {
mapping = infos[sorter.OriginalPosition(ix)].Mapping
} else {
mapping = infos[ix].Mapping
}
// display "kind" column only if we have mixed resources
if lastMapping != nil && mapping.Resource != lastMapping.Resource {
withKind = true
}
lastMapping = mapping
}
lastMapping = nil
for ix := range objs { for ix := range objs {
var mapping *meta.RESTMapping var mapping *meta.RESTMapping
var original runtime.Object var original runtime.Object
...@@ -315,7 +337,16 @@ func RunGet(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string ...@@ -315,7 +337,16 @@ func RunGet(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string
} }
lastMapping = mapping lastMapping = mapping
} }
if _, found := printer.(*kubectl.HumanReadablePrinter); found { if resourcePrinter, found := printer.(*kubectl.HumanReadablePrinter); found {
resourceName := mapping.Resource
if alias, ok := kubectl.ResourceShortFormFor(mapping.Resource); ok {
resourceName = alias
} else if resourceName == "" {
resourceName = "none"
}
resourcePrinter.Options.WithKind = withKind
resourcePrinter.Options.KindName = resourceName
if err := printer.PrintObj(original, w); err != nil { if err := printer.PrintObj(original, w); err != nil {
allErrs = append(allErrs, err) allErrs = append(allErrs, err)
} }
......
...@@ -164,6 +164,19 @@ var shortForms = map[string]string{ ...@@ -164,6 +164,19 @@ var shortForms = map[string]string{
"svc": "services", "svc": "services",
} }
// Look-up for resource short forms by value
func ResourceShortFormFor(resource string) (string, bool) {
var alias string
exists := false
for k, val := range shortForms {
if val == resource {
alias = k
exists = true
}
}
return alias, exists
}
// expandResourceShortcut will return the expanded version of resource // expandResourceShortcut will return the expanded version of resource
// (something that a pkg/api/meta.RESTMapper can understand), if it is // (something that a pkg/api/meta.RESTMapper can understand), if it is
// indeed a shortcut. Otherwise, will return resource unmodified. // indeed a shortcut. Otherwise, will return resource unmodified.
......
...@@ -216,7 +216,12 @@ func TestJSONPrinter(t *testing.T) { ...@@ -216,7 +216,12 @@ func TestJSONPrinter(t *testing.T) {
} }
func PrintCustomType(obj *TestPrintType, w io.Writer, options PrintOptions) error { func PrintCustomType(obj *TestPrintType, w io.Writer, options PrintOptions) error {
_, err := fmt.Fprintf(w, "%s", obj.Data) data := obj.Data
kind := options.KindName
if options.WithKind {
data = kind + "/" + data
}
_, err := fmt.Fprintf(w, "%s", data)
return err return err
} }
...@@ -241,6 +246,25 @@ func TestCustomTypePrinting(t *testing.T) { ...@@ -241,6 +246,25 @@ func TestCustomTypePrinting(t *testing.T) {
} }
} }
func TestCustomTypePrintingWithKind(t *testing.T) {
columns := []string{"Data"}
printer := NewHumanReadablePrinter(false, false, false, false, false, false, []string{})
printer.Handler(columns, PrintCustomType)
printer.Options.WithKind = true
printer.Options.KindName = "test"
obj := TestPrintType{"test object"}
buffer := &bytes.Buffer{}
err := printer.PrintObj(&obj, buffer)
if err != nil {
t.Fatalf("An error occurred printing the custom type: %#v", err)
}
expectedOutput := "Data\ntest/test object"
if buffer.String() != expectedOutput {
t.Errorf("The data was not printed as expected. Expected:\n%s\nGot:\n%s", expectedOutput, buffer.String())
}
}
func TestPrintHandlerError(t *testing.T) { func TestPrintHandlerError(t *testing.T) {
columns := []string{"Data"} columns := []string{"Data"}
printer := NewHumanReadablePrinter(false, false, false, false, false, false, []string{}) printer := NewHumanReadablePrinter(false, false, false, false, false, false, []string{})
...@@ -670,7 +694,7 @@ func TestPrintHunmanReadableIngressWithColumnLabels(t *testing.T) { ...@@ -670,7 +694,7 @@ func TestPrintHunmanReadableIngressWithColumnLabels(t *testing.T) {
}, },
} }
buff := bytes.Buffer{} buff := bytes.Buffer{}
printIngress(&ingress, &buff, PrintOptions{false, false, false, false, false, false, []string{"app_name"}}) printIngress(&ingress, &buff, PrintOptions{false, false, false, false, false, false, false, "", []string{"app_name"}})
output := string(buff.Bytes()) output := string(buff.Bytes())
appName := ingress.ObjectMeta.Labels["app_name"] appName := ingress.ObjectMeta.Labels["app_name"]
if !strings.Contains(output, appName) { if !strings.Contains(output, appName) {
...@@ -793,7 +817,7 @@ func TestPrintHumanReadableService(t *testing.T) { ...@@ -793,7 +817,7 @@ func TestPrintHumanReadableService(t *testing.T) {
for _, svc := range tests { for _, svc := range tests {
for _, wide := range []bool{false, true} { for _, wide := range []bool{false, true} {
buff := bytes.Buffer{} buff := bytes.Buffer{}
printService(&svc, &buff, PrintOptions{false, false, wide, false, false, false, []string{}}) printService(&svc, &buff, PrintOptions{false, false, false, wide, false, false, false, "", []string{}})
output := string(buff.Bytes()) output := string(buff.Bytes())
ip := svc.Spec.ClusterIP ip := svc.Spec.ClusterIP
if !strings.Contains(output, ip) { if !strings.Contains(output, ip) {
...@@ -1084,7 +1108,7 @@ func TestPrintPod(t *testing.T) { ...@@ -1084,7 +1108,7 @@ func TestPrintPod(t *testing.T) {
buf := bytes.NewBuffer([]byte{}) buf := bytes.NewBuffer([]byte{})
for _, test := range tests { for _, test := range tests {
printPod(&test.pod, buf, PrintOptions{false, false, false, true, false, false, []string{}}) printPod(&test.pod, buf, PrintOptions{false, false, false, false, true, false, false, "", []string{}})
// We ignore time // We ignore time
if !strings.HasPrefix(buf.String(), test.expect) { if !strings.HasPrefix(buf.String(), test.expect) {
t.Fatalf("Expected: %s, got: %s", test.expect, buf.String()) t.Fatalf("Expected: %s, got: %s", test.expect, buf.String())
...@@ -1177,7 +1201,7 @@ func TestPrintNonTerminatedPod(t *testing.T) { ...@@ -1177,7 +1201,7 @@ func TestPrintNonTerminatedPod(t *testing.T) {
buf := bytes.NewBuffer([]byte{}) buf := bytes.NewBuffer([]byte{})
for _, test := range tests { for _, test := range tests {
printPod(&test.pod, buf, PrintOptions{false, false, false, false, false, false, []string{}}) printPod(&test.pod, buf, PrintOptions{false, false, false, false, false, false, false, "", []string{}})
// We ignore time // We ignore time
if !strings.HasPrefix(buf.String(), test.expect) { if !strings.HasPrefix(buf.String(), test.expect) {
t.Fatalf("Expected: %s, got: %s", test.expect, buf.String()) t.Fatalf("Expected: %s, got: %s", test.expect, buf.String())
...@@ -1237,7 +1261,7 @@ func TestPrintPodWithLabels(t *testing.T) { ...@@ -1237,7 +1261,7 @@ func TestPrintPodWithLabels(t *testing.T) {
buf := bytes.NewBuffer([]byte{}) buf := bytes.NewBuffer([]byte{})
for _, test := range tests { for _, test := range tests {
printPod(&test.pod, buf, PrintOptions{false, false, false, false, false, false, test.labelColumns}) printPod(&test.pod, buf, PrintOptions{false, false, false, false, false, false, false, "", test.labelColumns})
// We ignore time // We ignore time
if !strings.HasPrefix(buf.String(), test.startsWith) || !strings.HasSuffix(buf.String(), test.endsWith) { if !strings.HasPrefix(buf.String(), test.startsWith) || !strings.HasSuffix(buf.String(), test.endsWith) {
t.Fatalf("Expected to start with: %s and end with: %s, but got: %s", test.startsWith, test.endsWith, buf.String()) t.Fatalf("Expected to start with: %s and end with: %s, but got: %s", test.startsWith, test.endsWith, buf.String())
...@@ -1301,7 +1325,7 @@ func TestPrintDeployment(t *testing.T) { ...@@ -1301,7 +1325,7 @@ func TestPrintDeployment(t *testing.T) {
buf := bytes.NewBuffer([]byte{}) buf := bytes.NewBuffer([]byte{})
for _, test := range tests { for _, test := range tests {
printDeployment(&test.deployment, buf, PrintOptions{false, false, false, true, false, false, []string{}}) printDeployment(&test.deployment, buf, PrintOptions{false, false, false, false, true, false, false, "", []string{}})
if buf.String() != test.expect { if buf.String() != test.expect {
t.Fatalf("Expected: %s, got: %s", test.expect, buf.String()) t.Fatalf("Expected: %s, got: %s", test.expect, buf.String())
} }
...@@ -1336,7 +1360,7 @@ func TestPrintDaemonSet(t *testing.T) { ...@@ -1336,7 +1360,7 @@ func TestPrintDaemonSet(t *testing.T) {
buf := bytes.NewBuffer([]byte{}) buf := bytes.NewBuffer([]byte{})
for _, test := range tests { for _, test := range tests {
printDaemonSet(&test.ds, buf, PrintOptions{false, false, false, false, false, false, []string{}}) printDaemonSet(&test.ds, buf, PrintOptions{false, false, false, false, false, false, false, "", []string{}})
if !strings.HasPrefix(buf.String(), test.startsWith) { if !strings.HasPrefix(buf.String(), test.startsWith) {
t.Fatalf("Expected to start with %s but got %s", test.startsWith, buf.String()) t.Fatalf("Expected to start with %s but got %s", test.startsWith, buf.String())
} }
...@@ -1384,7 +1408,7 @@ func TestPrintJob(t *testing.T) { ...@@ -1384,7 +1408,7 @@ func TestPrintJob(t *testing.T) {
buf := bytes.NewBuffer([]byte{}) buf := bytes.NewBuffer([]byte{})
for _, test := range tests { for _, test := range tests {
printJob(&test.job, buf, PrintOptions{false, false, false, true, false, false, []string{}}) printJob(&test.job, buf, PrintOptions{false, false, false, false, true, false, false, "", []string{}})
if buf.String() != test.expect { if buf.String() != test.expect {
t.Fatalf("Expected: %s, got: %s", test.expect, buf.String()) t.Fatalf("Expected: %s, got: %s", test.expect, buf.String())
} }
...@@ -1443,7 +1467,7 @@ func TestPrintPodShowLabels(t *testing.T) { ...@@ -1443,7 +1467,7 @@ func TestPrintPodShowLabels(t *testing.T) {
buf := bytes.NewBuffer([]byte{}) buf := bytes.NewBuffer([]byte{})
for _, test := range tests { for _, test := range tests {
printPod(&test.pod, buf, PrintOptions{false, false, false, false, test.showLabels, false, []string{}}) printPod(&test.pod, buf, PrintOptions{false, false, false, false, false, test.showLabels, false, "", []string{}})
// We ignore time // We ignore time
if !strings.HasPrefix(buf.String(), test.startsWith) || !strings.HasSuffix(buf.String(), test.endsWith) { if !strings.HasPrefix(buf.String(), test.startsWith) || !strings.HasSuffix(buf.String(), test.endsWith) {
t.Fatalf("Expected to start with: %s and end with: %s, but got: %s", test.startsWith, test.endsWith, buf.String()) t.Fatalf("Expected to start with: %s and end with: %s, but got: %s", test.startsWith, test.endsWith, buf.String())
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment