Merge pull request #65786 from juanvallejo/jvallejo/update-template-printer-check

Automatic merge from submit-queue (batch tested with PRs 65715, 65786). 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>.

update --template printer defaulting

**Release note**:
```release-note
NONE
```

Depends on https://github.com/kubernetes/kubernetes/pull/65711
The relevant commit for this PR is the last one (`
fix go-template defaulting for commands w default output format`)

cc @deads2k @soltysh
pull/8/head
Kubernetes Submit Queue 2018-07-04 06:18:08 -07:00 committed by GitHub
commit df1826c9d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 7 deletions

View File

@ -1777,10 +1777,6 @@ run_template_output_tests() {
output_message=$(kubectl "${kube_flags[@]}" auth reconcile --dry-run -f test/fixtures/pkg/kubectl/cmd/auth/rbac-resource-plus.yaml --template="{{ .metadata.name }}:")
kube::test::if_has_string "${output_message}" 'testing-CR:testing-CRB:testing-RB:testing-R:'
# check that "config view" command supports --template output
output_message=$(kubectl "${kube_flags[@]}" config view --output=go-template="{{ .kind }}:")
kube::test::if_has_string "${output_message}" 'Config'
# check that "create clusterrole" command supports --template output
output_message=$(kubectl "${kube_flags[@]}" create clusterrole --template="{{ .metadata.name }}:" --verb get myclusterrole --non-resource-url /logs/ --resource pods)
kube::test::if_has_string "${output_message}" 'myclusterrole:'
@ -1901,6 +1897,22 @@ EOF
output_message=$(kubectl "${kube_flags[@]}" create service nodeport foo --dry-run --tcp=8080 --template="{{ .metadata.name }}:")
kube::test::if_has_string "${output_message}" 'foo:'
# check that "config view" ouputs "yaml" as its default output format
output_message=$(kubectl "${kube_flags[@]}" config view)
kube::test::if_has_string "${output_message}" 'kind: Config'
# check that "config view" command supports --template output
# and that commands that set a default output (yaml in this case),
# default to "go-template" as their output format when a --template
# value is provided, but no explicit --output format is given.
output_message=$(kubectl "${kube_flags[@]}" config view --template="{{ .kind }}:")
kube::test::if_has_string "${output_message}" 'Config'
# check that running a command with both a --template flag and a
# non-template --output prefers the non-template output value
output_message=$(kubectl "${kube_flags[@]}" create configmap cm --dry-run --template="{{ .metadata.name }}:" --output yaml)
kube::test::if_has_string "${output_message}" 'kind: ConfigMap'
# cleanup
kubectl delete cronjob pi "${kube_flags[@]}"
kubectl delete pods --all "${kube_flags[@]}"

View File

@ -22,6 +22,8 @@ import (
"strings"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers"
)
@ -62,6 +64,8 @@ type PrintFlags struct {
TypeSetterPrinter *printers.TypeSetterPrinter
OutputFormat *string
outputFlag *pflag.Flag
outputDefaulted bool
}
func (f *PrintFlags) Complete(successTemplate string) error {
@ -81,8 +85,12 @@ func (f *PrintFlags) ToPrinter() (printers.ResourcePrinter, error) {
if f.OutputFormat != nil {
outputFormat = *f.OutputFormat
}
// for backwards compatibility we want to support a --template argument given, even when no --output format is provided
if f.TemplatePrinterFlags != nil && f.TemplatePrinterFlags.TemplateArgument != nil && len(*f.TemplatePrinterFlags.TemplateArgument) > 0 && len(outputFormat) == 0 {
// For backwards compatibility we want to support a --template argument given, even when no --output format is provided.
// If a default output format has been set, but no explicit output format has been provided via the --output flag, fallback
// to honoring the --template argument.
if f.TemplatePrinterFlags != nil && f.TemplatePrinterFlags.TemplateArgument != nil &&
len(*f.TemplatePrinterFlags.TemplateArgument) > 0 &&
(len(outputFormat) == 0 || (f.outputDefaulted && !f.outputFlag.Changed)) {
outputFormat = "go-template"
}
@ -114,12 +122,14 @@ func (f *PrintFlags) AddFlags(cmd *cobra.Command) {
if f.OutputFormat != nil {
cmd.Flags().StringVarP(f.OutputFormat, "output", "o", *f.OutputFormat, fmt.Sprintf("Output format. One of: %s.", strings.Join(f.AllowedFormats(), "|")))
f.outputFlag = cmd.Flag("output")
}
}
// WithDefaultOutput sets a default output format if one is not provided through a flag value
func (f *PrintFlags) WithDefaultOutput(output string) *PrintFlags {
f.OutputFormat = &output
f.outputDefaulted = true
return f
}