-
Notifications
You must be signed in to change notification settings - Fork 39.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61501 from juanvallejo/jvallejo/add-custom-column…
…s-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
- Loading branch information
Showing
4 changed files
with
252 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
Copyright 2018 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package printers | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"k8s.io/kubernetes/pkg/kubectl/scheme" | ||
) | ||
|
||
// CustomColumnsPrintFlags provides default flags necessary for printing | ||
// custom resource columns from an inline-template or file. | ||
type CustomColumnsPrintFlags struct { | ||
NoHeaders bool | ||
TemplateArgument string | ||
} | ||
|
||
// ToPrinter receives an templateFormat and returns a printer capable of | ||
// handling custom-column printing. | ||
// Returns false if the specified templateFormat does not match a supported format. | ||
// Supported format types can be found in pkg/printers/printers.go | ||
func (f *CustomColumnsPrintFlags) ToPrinter(templateFormat string) (ResourcePrinter, bool, error) { | ||
if len(templateFormat) == 0 { | ||
return nil, false, fmt.Errorf("missing output format") | ||
} | ||
|
||
templateValue := "" | ||
|
||
supportedFormats := map[string]bool{ | ||
"custom-columns-file": true, | ||
"custom-columns": true, | ||
} | ||
|
||
if len(f.TemplateArgument) == 0 { | ||
for format := range supportedFormats { | ||
format = format + "=" | ||
if strings.HasPrefix(templateFormat, format) { | ||
templateValue = templateFormat[len(format):] | ||
templateFormat = format[:len(format)-1] | ||
break | ||
} | ||
} | ||
} else { | ||
templateValue = f.TemplateArgument | ||
} | ||
|
||
if _, supportedFormat := supportedFormats[templateFormat]; !supportedFormat { | ||
return nil, false, nil | ||
} | ||
|
||
if len(templateValue) == 0 { | ||
return nil, true, fmt.Errorf("custom-columns format specified but no custom columns given") | ||
} | ||
|
||
decoder := scheme.Codecs.UniversalDecoder() | ||
|
||
if templateFormat == "custom-columns-file" { | ||
file, err := os.Open(templateValue) | ||
if err != nil { | ||
return nil, true, fmt.Errorf("error reading template %s, %v\n", templateValue, err) | ||
} | ||
defer file.Close() | ||
p, err := NewCustomColumnsPrinterFromTemplate(file, decoder) | ||
return p, true, err | ||
} | ||
|
||
p, err := NewCustomColumnsPrinterFromSpec(templateValue, decoder, f.NoHeaders) | ||
return p, true, err | ||
} | ||
|
||
// AddFlags receives a *cobra.Command reference and binds | ||
// flags related to custom-columns printing | ||
func (f *CustomColumnsPrintFlags) AddFlags(c *cobra.Command) {} | ||
|
||
// NewCustomColumnsPrintFlags returns flags associated with | ||
// custom-column printing, with default values set. | ||
// NoHeaders and TemplateArgument should be set by callers. | ||
func NewCustomColumnsPrintFlags(noHeaders bool, templateValue string) *CustomColumnsPrintFlags { | ||
return &CustomColumnsPrintFlags{ | ||
NoHeaders: noHeaders, | ||
TemplateArgument: templateValue, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/* | ||
Copyright 2018 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package printers_test | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
"k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/kubernetes/pkg/printers" | ||
) | ||
|
||
func TestPrinterSupportsExpectedCustomColumnFormats(t *testing.T) { | ||
testObject := &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}} | ||
|
||
customColumnsFile, err := ioutil.TempFile("", "printers_jsonpath_flags") | ||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
defer func(tempFile *os.File) { | ||
tempFile.Close() | ||
os.Remove(tempFile.Name()) | ||
}(customColumnsFile) | ||
|
||
fmt.Fprintf(customColumnsFile, "NAME\n.metadata.name") | ||
|
||
testCases := []struct { | ||
name string | ||
outputFormat string | ||
templateArg string | ||
expectedError string | ||
expectedParseError string | ||
expectedOutput string | ||
expectNoMatch bool | ||
}{ | ||
{ | ||
name: "valid output format also containing the custom-columns argument succeeds", | ||
outputFormat: "custom-columns=NAME:.metadata.name", | ||
expectedOutput: "foo", | ||
}, | ||
{ | ||
name: "valid output format and no --template argument results in an error", | ||
outputFormat: "custom-columns", | ||
expectedError: "custom-columns format specified but no custom columns given", | ||
}, | ||
{ | ||
name: "valid output format and --template argument succeeds", | ||
outputFormat: "custom-columns", | ||
templateArg: "NAME:.metadata.name", | ||
expectedOutput: "foo", | ||
}, | ||
{ | ||
name: "custom-columns template file should match, and successfully return correct value", | ||
outputFormat: "custom-columns-file", | ||
templateArg: customColumnsFile.Name(), | ||
expectedOutput: "foo", | ||
}, | ||
{ | ||
name: "valid output format and invalid --template argument results in a parsing error from the printer", | ||
outputFormat: "custom-columns", | ||
templateArg: "invalid", | ||
expectedError: "unexpected custom-columns spec: invalid, expected <header>:<json-path-expr>", | ||
}, | ||
{ | ||
name: "no printer is matched on an invalid outputFormat", | ||
outputFormat: "invalid", | ||
expectNoMatch: true, | ||
}, | ||
{ | ||
name: "custom-columns printer should not match on any other format supported by another printer", | ||
outputFormat: "go-template", | ||
expectNoMatch: true, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
printFlags := printers.CustomColumnsPrintFlags{ | ||
TemplateArgument: tc.templateArg, | ||
} | ||
|
||
p, matched, err := printFlags.ToPrinter(tc.outputFormat) | ||
if tc.expectNoMatch { | ||
if matched { | ||
t.Fatalf("expected no printer matches for output format %q", tc.outputFormat) | ||
} | ||
return | ||
} | ||
if !matched { | ||
t.Fatalf("expected to match template printer for output format %q", tc.outputFormat) | ||
} | ||
|
||
if len(tc.expectedError) > 0 { | ||
if err == nil || !strings.Contains(err.Error(), tc.expectedError) { | ||
t.Errorf("expecting error %q, got %v", tc.expectedError, err) | ||
} | ||
return | ||
} | ||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
|
||
out := bytes.NewBuffer([]byte{}) | ||
err = p.PrintObj(testObject, out) | ||
if len(tc.expectedParseError) > 0 { | ||
if err == nil || !strings.Contains(err.Error(), tc.expectedParseError) { | ||
t.Errorf("expecting error %q, got %v", tc.expectedError, err) | ||
} | ||
return | ||
} | ||
if err != nil { | ||
t.Errorf("unexpected error: %v", err) | ||
} | ||
|
||
if !strings.Contains(out.String(), tc.expectedOutput) { | ||
t.Errorf("unexpected output: expecting %q, got %q", tc.expectedOutput, out.String()) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters