Skip to content

Commit dcdf70a

Browse files
author
Kubernetes Submit Queue
authored
Merge pull request kubernetes#61501 from juanvallejo/jvallejo/add-custom-columns-flags
Automatic merge from submit-queue (batch tested with PRs 61434, 61501, 59609, 61467, 61531). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. wire through custom-column print flags **Release note**: ```release-note NONE ``` Begin implementing pieces needed to retrieve custom-column printers from a set of flags. Proposal: https://docs.google.com/document/d/19ZZFVe9oD1KQmk5uExggRWtRl_hKGfYnBXvHZJlgEro/edit#heading=h.pnvbfi14v4zz cc @soltysh @deads2k @pwittrock
2 parents 88ce605 + e4cdb9f commit dcdf70a

File tree

4 files changed

+252
-12
lines changed

4 files changed

+252
-12
lines changed

pkg/printers/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ go_library(
1010
name = "go_default_library",
1111
srcs = [
1212
"customcolumn.go",
13+
"customcolumn_flags.go",
1314
"humanreadable.go",
1415
"interface.go",
1516
"json.go",
@@ -44,6 +45,7 @@ go_library(
4445
go_test(
4546
name = "go_default_xtest",
4647
srcs = [
48+
"customcolumn_flags_test.go",
4749
"customcolumn_test.go",
4850
"json_yaml_flags_test.go",
4951
"name_flags_test.go",

pkg/printers/customcolumn_flags.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package printers
18+
19+
import (
20+
"fmt"
21+
"os"
22+
"strings"
23+
24+
"github.com/spf13/cobra"
25+
26+
"k8s.io/kubernetes/pkg/kubectl/scheme"
27+
)
28+
29+
// CustomColumnsPrintFlags provides default flags necessary for printing
30+
// custom resource columns from an inline-template or file.
31+
type CustomColumnsPrintFlags struct {
32+
NoHeaders bool
33+
TemplateArgument string
34+
}
35+
36+
// ToPrinter receives an templateFormat and returns a printer capable of
37+
// handling custom-column printing.
38+
// Returns false if the specified templateFormat does not match a supported format.
39+
// Supported format types can be found in pkg/printers/printers.go
40+
func (f *CustomColumnsPrintFlags) ToPrinter(templateFormat string) (ResourcePrinter, bool, error) {
41+
if len(templateFormat) == 0 {
42+
return nil, false, fmt.Errorf("missing output format")
43+
}
44+
45+
templateValue := ""
46+
47+
supportedFormats := map[string]bool{
48+
"custom-columns-file": true,
49+
"custom-columns": true,
50+
}
51+
52+
if len(f.TemplateArgument) == 0 {
53+
for format := range supportedFormats {
54+
format = format + "="
55+
if strings.HasPrefix(templateFormat, format) {
56+
templateValue = templateFormat[len(format):]
57+
templateFormat = format[:len(format)-1]
58+
break
59+
}
60+
}
61+
} else {
62+
templateValue = f.TemplateArgument
63+
}
64+
65+
if _, supportedFormat := supportedFormats[templateFormat]; !supportedFormat {
66+
return nil, false, nil
67+
}
68+
69+
if len(templateValue) == 0 {
70+
return nil, true, fmt.Errorf("custom-columns format specified but no custom columns given")
71+
}
72+
73+
decoder := scheme.Codecs.UniversalDecoder()
74+
75+
if templateFormat == "custom-columns-file" {
76+
file, err := os.Open(templateValue)
77+
if err != nil {
78+
return nil, true, fmt.Errorf("error reading template %s, %v\n", templateValue, err)
79+
}
80+
defer file.Close()
81+
p, err := NewCustomColumnsPrinterFromTemplate(file, decoder)
82+
return p, true, err
83+
}
84+
85+
p, err := NewCustomColumnsPrinterFromSpec(templateValue, decoder, f.NoHeaders)
86+
return p, true, err
87+
}
88+
89+
// AddFlags receives a *cobra.Command reference and binds
90+
// flags related to custom-columns printing
91+
func (f *CustomColumnsPrintFlags) AddFlags(c *cobra.Command) {}
92+
93+
// NewCustomColumnsPrintFlags returns flags associated with
94+
// custom-column printing, with default values set.
95+
// NoHeaders and TemplateArgument should be set by callers.
96+
func NewCustomColumnsPrintFlags(noHeaders bool, templateValue string) *CustomColumnsPrintFlags {
97+
return &CustomColumnsPrintFlags{
98+
NoHeaders: noHeaders,
99+
TemplateArgument: templateValue,
100+
}
101+
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package printers_test
18+
19+
import (
20+
"bytes"
21+
"fmt"
22+
"io/ioutil"
23+
"os"
24+
"strings"
25+
"testing"
26+
27+
"k8s.io/api/core/v1"
28+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29+
"k8s.io/kubernetes/pkg/printers"
30+
)
31+
32+
func TestPrinterSupportsExpectedCustomColumnFormats(t *testing.T) {
33+
testObject := &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}
34+
35+
customColumnsFile, err := ioutil.TempFile("", "printers_jsonpath_flags")
36+
if err != nil {
37+
t.Fatalf("unexpected error: %v", err)
38+
}
39+
defer func(tempFile *os.File) {
40+
tempFile.Close()
41+
os.Remove(tempFile.Name())
42+
}(customColumnsFile)
43+
44+
fmt.Fprintf(customColumnsFile, "NAME\n.metadata.name")
45+
46+
testCases := []struct {
47+
name string
48+
outputFormat string
49+
templateArg string
50+
expectedError string
51+
expectedParseError string
52+
expectedOutput string
53+
expectNoMatch bool
54+
}{
55+
{
56+
name: "valid output format also containing the custom-columns argument succeeds",
57+
outputFormat: "custom-columns=NAME:.metadata.name",
58+
expectedOutput: "foo",
59+
},
60+
{
61+
name: "valid output format and no --template argument results in an error",
62+
outputFormat: "custom-columns",
63+
expectedError: "custom-columns format specified but no custom columns given",
64+
},
65+
{
66+
name: "valid output format and --template argument succeeds",
67+
outputFormat: "custom-columns",
68+
templateArg: "NAME:.metadata.name",
69+
expectedOutput: "foo",
70+
},
71+
{
72+
name: "custom-columns template file should match, and successfully return correct value",
73+
outputFormat: "custom-columns-file",
74+
templateArg: customColumnsFile.Name(),
75+
expectedOutput: "foo",
76+
},
77+
{
78+
name: "valid output format and invalid --template argument results in a parsing error from the printer",
79+
outputFormat: "custom-columns",
80+
templateArg: "invalid",
81+
expectedError: "unexpected custom-columns spec: invalid, expected <header>:<json-path-expr>",
82+
},
83+
{
84+
name: "no printer is matched on an invalid outputFormat",
85+
outputFormat: "invalid",
86+
expectNoMatch: true,
87+
},
88+
{
89+
name: "custom-columns printer should not match on any other format supported by another printer",
90+
outputFormat: "go-template",
91+
expectNoMatch: true,
92+
},
93+
}
94+
95+
for _, tc := range testCases {
96+
t.Run(tc.name, func(t *testing.T) {
97+
printFlags := printers.CustomColumnsPrintFlags{
98+
TemplateArgument: tc.templateArg,
99+
}
100+
101+
p, matched, err := printFlags.ToPrinter(tc.outputFormat)
102+
if tc.expectNoMatch {
103+
if matched {
104+
t.Fatalf("expected no printer matches for output format %q", tc.outputFormat)
105+
}
106+
return
107+
}
108+
if !matched {
109+
t.Fatalf("expected to match template printer for output format %q", tc.outputFormat)
110+
}
111+
112+
if len(tc.expectedError) > 0 {
113+
if err == nil || !strings.Contains(err.Error(), tc.expectedError) {
114+
t.Errorf("expecting error %q, got %v", tc.expectedError, err)
115+
}
116+
return
117+
}
118+
if err != nil {
119+
t.Fatalf("unexpected error: %v", err)
120+
}
121+
122+
out := bytes.NewBuffer([]byte{})
123+
err = p.PrintObj(testObject, out)
124+
if len(tc.expectedParseError) > 0 {
125+
if err == nil || !strings.Contains(err.Error(), tc.expectedParseError) {
126+
t.Errorf("expecting error %q, got %v", tc.expectedError, err)
127+
}
128+
return
129+
}
130+
if err != nil {
131+
t.Errorf("unexpected error: %v", err)
132+
}
133+
134+
if !strings.Contains(out.String(), tc.expectedOutput) {
135+
t.Errorf("unexpected output: expecting %q, got %q", tc.expectedOutput, out.String())
136+
}
137+
})
138+
}
139+
}

pkg/printers/printers.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package printers
1919
import (
2020
"fmt"
2121
"io/ioutil"
22-
"os"
2322

2423
"k8s.io/apimachinery/pkg/runtime"
2524
)
@@ -110,22 +109,21 @@ func GetStandardPrinter(typer runtime.ObjectTyper, encoder runtime.Encoder, deco
110109
jsonpathPrinter.AllowMissingKeys(allowMissingTemplateKeys)
111110
printer = jsonpathPrinter
112111

113-
case "custom-columns":
114-
var err error
115-
if printer, err = NewCustomColumnsPrinterFromSpec(formatArgument, decoders[0], options.NoHeaders); err != nil {
116-
return nil, err
112+
case "custom-columns", "custom-columns-file":
113+
customColumnsFlags := &CustomColumnsPrintFlags{
114+
NoHeaders: options.NoHeaders,
115+
TemplateArgument: formatArgument,
117116
}
118-
119-
case "custom-columns-file":
120-
file, err := os.Open(formatArgument)
121-
if err != nil {
122-
return nil, fmt.Errorf("error reading template %s, %v\n", formatArgument, err)
117+
customColumnsPrinter, matched, err := customColumnsFlags.ToPrinter(format)
118+
if !matched {
119+
return nil, fmt.Errorf("unable to match a name printer to handle current print options")
123120
}
124-
defer file.Close()
125-
if printer, err = NewCustomColumnsPrinterFromTemplate(file, decoders[0]); err != nil {
121+
if err != nil {
126122
return nil, err
127123
}
128124

125+
printer = customColumnsPrinter
126+
129127
case "wide":
130128
fallthrough
131129
case "":

0 commit comments

Comments
 (0)