mirror of https://github.com/k3s-io/k3s
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 @soltyshpull/8/head
commit
df1826c9d7
|
@ -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 }}:")
|
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:'
|
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
|
# 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)
|
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:'
|
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 }}:")
|
output_message=$(kubectl "${kube_flags[@]}" create service nodeport foo --dry-run --tcp=8080 --template="{{ .metadata.name }}:")
|
||||||
kube::test::if_has_string "${output_message}" 'foo:'
|
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
|
# cleanup
|
||||||
kubectl delete cronjob pi "${kube_flags[@]}"
|
kubectl delete cronjob pi "${kube_flags[@]}"
|
||||||
kubectl delete pods --all "${kube_flags[@]}"
|
kubectl delete pods --all "${kube_flags[@]}"
|
||||||
|
|
|
@ -22,6 +22,8 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/spf13/pflag"
|
||||||
|
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers"
|
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers"
|
||||||
)
|
)
|
||||||
|
@ -62,6 +64,8 @@ type PrintFlags struct {
|
||||||
TypeSetterPrinter *printers.TypeSetterPrinter
|
TypeSetterPrinter *printers.TypeSetterPrinter
|
||||||
|
|
||||||
OutputFormat *string
|
OutputFormat *string
|
||||||
|
outputFlag *pflag.Flag
|
||||||
|
outputDefaulted bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *PrintFlags) Complete(successTemplate string) error {
|
func (f *PrintFlags) Complete(successTemplate string) error {
|
||||||
|
@ -81,8 +85,12 @@ func (f *PrintFlags) ToPrinter() (printers.ResourcePrinter, error) {
|
||||||
if f.OutputFormat != nil {
|
if f.OutputFormat != nil {
|
||||||
outputFormat = *f.OutputFormat
|
outputFormat = *f.OutputFormat
|
||||||
}
|
}
|
||||||
// for backwards compatibility we want to support a --template argument given, even when no --output format is provided
|
// 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 {
|
// 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"
|
outputFormat = "go-template"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -114,12 +122,14 @@ func (f *PrintFlags) AddFlags(cmd *cobra.Command) {
|
||||||
|
|
||||||
if f.OutputFormat != nil {
|
if f.OutputFormat != nil {
|
||||||
cmd.Flags().StringVarP(f.OutputFormat, "output", "o", *f.OutputFormat, fmt.Sprintf("Output format. One of: %s.", strings.Join(f.AllowedFormats(), "|")))
|
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
|
// WithDefaultOutput sets a default output format if one is not provided through a flag value
|
||||||
func (f *PrintFlags) WithDefaultOutput(output string) *PrintFlags {
|
func (f *PrintFlags) WithDefaultOutput(output string) *PrintFlags {
|
||||||
f.OutputFormat = &output
|
f.OutputFormat = &output
|
||||||
|
f.outputDefaulted = true
|
||||||
return f
|
return f
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue