mirror of https://github.com/k3s-io/k3s
Merge pull request #65711 from deads2k/cli-81-template-generic
Automatic merge from submit-queue (batch tested with PRs 65677, 65711, 65150, 65726). 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>. make template printers a recommended printer WIP because it needs tests. Apparently there weren't any before. @juanvallejo open a pull to this branch adding tests for commands that need `--template` support and I'll squash them in. @liggitt since you think it's widespread, here's an option to make it "normal" @kubernetes/sig-cli-maintainerspull/8/head
commit
82eb501782
|
@ -1710,6 +1710,208 @@ run_kubectl_request_timeout_tests() {
|
|||
set +o errexit
|
||||
}
|
||||
|
||||
run_template_output_tests() {
|
||||
set -o nounset
|
||||
set -o errexit
|
||||
|
||||
kube::log::status "Testing --template support on commands"
|
||||
### Test global request timeout option
|
||||
# Pre-condition: no POD exists
|
||||
create_and_use_new_namespace
|
||||
kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" ''
|
||||
# Command
|
||||
# check that create supports --template output
|
||||
kubectl create "${kube_flags[@]}" -f test/fixtures/doc-yaml/admin/limitrange/valid-pod.yaml
|
||||
# Post-condition: valid-pod POD is created
|
||||
kubectl get "${kube_flags[@]}" pods -o json
|
||||
kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" 'valid-pod:'
|
||||
|
||||
# check that patch command supports --template output
|
||||
output_message=$(kubectl "${kube_flags[@]}" patch --dry-run pods/valid-pod -p '{"patched":"value3"}' --type=merge --template="{{ .metadata.name }}:")
|
||||
kube::test::if_has_string "${output_message}" 'valid-pod:'
|
||||
|
||||
# check that label command supports --template output
|
||||
output_message=$(kubectl "${kube_flags[@]}" label --dry-run pods/valid-pod label=value --template="{{ .metadata.name }}:")
|
||||
kube::test::if_has_string "${output_message}" 'valid-pod:'
|
||||
|
||||
# check that annotate command supports --template output
|
||||
output_message=$(kubectl "${kube_flags[@]}" annotate --dry-run pods/valid-pod annotation=value --template="{{ .metadata.name }}:")
|
||||
kube::test::if_has_string "${output_message}" 'valid-pod:'
|
||||
|
||||
# check that apply command supports --template output
|
||||
output_message=$(kubectl "${kube_flags[@]}" apply --dry-run -f test/fixtures/doc-yaml/admin/limitrange/valid-pod.yaml --template="{{ .metadata.name }}:")
|
||||
kube::test::if_has_string "${output_message}" 'valid-pod:'
|
||||
|
||||
# check that create command supports --template output
|
||||
output_message=$(kubectl "${kube_flags[@]}" create -f test/fixtures/doc-yaml/admin/limitrange/valid-pod.yaml --dry-run --template="{{ .metadata.name }}:")
|
||||
kube::test::if_has_string "${output_message}" 'valid-pod:'
|
||||
|
||||
# check that autoscale command supports --template output
|
||||
output_message=$(kubectl "${kube_flags[@]}" autoscale --max=2 -f hack/testdata/scale-deploy-1.yaml --dry-run --template="{{ .metadata.name }}:")
|
||||
kube::test::if_has_string "${output_message}" 'scale-1:'
|
||||
|
||||
# check that expose command supports --template output
|
||||
output_message=$(kubectl "${kube_flags[@]}" expose -f hack/testdata/redis-slave-replicaset.yaml --save-config --port=80 --target-port=8000 --dry-run --template="{{ .metadata.name }}:")
|
||||
kube::test::if_has_string "${output_message}" 'redis-slave:'
|
||||
|
||||
# check that convert command supports --template output
|
||||
output_message=$(kubectl "${kube_flags[@]}" convert -f hack/testdata/deployment-revision1.yaml --output-version=apps/v1beta1 --template="{{ .metadata.name }}:")
|
||||
kube::test::if_has_string "${output_message}" 'nginx:'
|
||||
|
||||
# check that run command supports --template output
|
||||
output_message=$(kubectl "${kube_flags[@]}" run --dry-run --template="{{ .metadata.name }}:" pi --image=perl --restart=OnFailure -- perl -Mbignum=bpi -wle 'print bpi(2000)')
|
||||
kube::test::if_has_string "${output_message}" 'pi:'
|
||||
|
||||
# check that taint command supports --template output
|
||||
output_message=$(kubectl "${kube_flags[@]}" taint node 127.0.0.1 dedicated=foo:PreferNoSchedule --template="{{ .metadata.name }}:")
|
||||
kube::test::if_has_string "${output_message}" '127.0.0.1:'
|
||||
# untaint node
|
||||
kubectl taint node 127.0.0.1 dedicated-
|
||||
|
||||
# check that "apply set-last-applied" command supports --template output
|
||||
kubectl "${kube_flags[@]}" create -f test/e2e/testing-manifests/statefulset/cassandra/controller.yaml
|
||||
output_message=$(kubectl "${kube_flags[@]}" apply set-last-applied -f test/e2e/testing-manifests/statefulset/cassandra/controller.yaml --dry-run --create-annotation --template="{{ .metadata.name }}:")
|
||||
kube::test::if_has_string "${output_message}" 'cassandra:'
|
||||
|
||||
# check that "auth reconcile" command supports --template output
|
||||
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:'
|
||||
|
||||
# check that "create clusterrolebinding" command supports --template output
|
||||
output_message=$(kubectl "${kube_flags[@]}" create clusterrolebinding foo --clusterrole=myclusterrole --template="{{ .metadata.name }}:")
|
||||
kube::test::if_has_string "${output_message}" 'foo:'
|
||||
|
||||
# check that "create configmap" command supports --template output
|
||||
output_message=$(kubectl "${kube_flags[@]}" create configmap cm --dry-run --template="{{ .metadata.name }}:")
|
||||
kube::test::if_has_string "${output_message}" 'cm:'
|
||||
|
||||
# check that "create deployment" command supports --template output
|
||||
output_message=$(kubectl "${kube_flags[@]}" create deployment deploy --dry-run --image=nginx --template="{{ .metadata.name }}:")
|
||||
kube::test::if_has_string "${output_message}" 'deploy:'
|
||||
|
||||
# check that "create job" command supports --template output
|
||||
kubectl create "${kube_flags[@]}" -f - <<EOF
|
||||
apiVersion: batch/v1beta1
|
||||
kind: CronJob
|
||||
metadata:
|
||||
name: pi
|
||||
spec:
|
||||
schedule: "*/10 * * * *"
|
||||
jobTemplate:
|
||||
spec:
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
parent: "pi"
|
||||
spec:
|
||||
containers:
|
||||
- name: pi
|
||||
image: perl
|
||||
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
|
||||
restartPolicy: OnFailure
|
||||
EOF
|
||||
output_message=$(kubectl "${kube_flags[@]}" create job foo --from=cronjob/pi --dry-run --template="{{ .metadata.name }}:")
|
||||
kube::test::if_has_string "${output_message}" 'foo:'
|
||||
|
||||
# check that "create namespace" command supports --template output
|
||||
output_message=$(kubectl "${kube_flags[@]}" create ns bar --dry-run --template="{{ .metadata.name }}:")
|
||||
kube::test::if_has_string "${output_message}" 'bar:'
|
||||
|
||||
# check that "create namespace" command supports --template output
|
||||
output_message=$(kubectl "${kube_flags[@]}" create rolebinding foo --clusterrole=myclusterrole --dry-run --template="{{ .metadata.name }}:")
|
||||
kube::test::if_has_string "${output_message}" 'foo:'
|
||||
|
||||
# check that "create role" command supports --template output
|
||||
output_message=$(kubectl "${kube_flags[@]}" create role --dry-run --template="{{ .metadata.name }}:" --verb get myrole --resource pods)
|
||||
kube::test::if_has_string "${output_message}" 'myrole:'
|
||||
|
||||
# check that "create quota" command supports --template output
|
||||
output_message=$(kubectl "${kube_flags[@]}" create quota foo --dry-run --template="{{ .metadata.name }}:")
|
||||
kube::test::if_has_string "${output_message}" 'foo:'
|
||||
|
||||
# check that "create priorityclass" command supports --template output
|
||||
output_message=$(kubectl "${kube_flags[@]}" create priorityclass foo --dry-run --template="{{ .metadata.name }}:")
|
||||
kube::test::if_has_string "${output_message}" 'foo:'
|
||||
|
||||
# check that "create poddisruptionbudget" command supports --template output
|
||||
output_message=$(kubectl "${kube_flags[@]}" create poddisruptionbudget foo --dry-run --selector=foo --min-available=1 --template="{{ .metadata.name }}:")
|
||||
kube::test::if_has_string "${output_message}" 'foo:'
|
||||
|
||||
# check that "create serviceaccount" command supports --template output
|
||||
output_message=$(kubectl "${kube_flags[@]}" create serviceaccount foo --dry-run --template="{{ .metadata.name }}:")
|
||||
kube::test::if_has_string "${output_message}" 'foo:'
|
||||
|
||||
# check that "set env" command supports --template output
|
||||
output_message=$(kubectl "${kube_flags[@]}" set env pod/valid-pod --dry-run A=B --template="{{ .metadata.name }}:")
|
||||
kube::test::if_has_string "${output_message}" 'valid-pod:'
|
||||
|
||||
# check that "set image" command supports --template output
|
||||
output_message=$(kubectl "${kube_flags[@]}" set image pod/valid-pod --dry-run kubernetes-serve-hostname=nginx --template="{{ .metadata.name }}:")
|
||||
kube::test::if_has_string "${output_message}" 'valid-pod:'
|
||||
|
||||
# check that "set resources" command supports --template output
|
||||
output_message=$(kubectl "${kube_flags[@]}" set resources pod/valid-pod --limits=memory=256Mi --dry-run --template="{{ .metadata.name }}:")
|
||||
kube::test::if_has_string "${output_message}" 'valid-pod:'
|
||||
|
||||
# check that "set selector" command supports --template output
|
||||
output_message=$(kubectl "${kube_flags[@]}" set selector -f hack/testdata/kubernetes-service.yaml A=B --local --dry-run --template="{{ .metadata.name }}:")
|
||||
kube::test::if_has_string "${output_message}" 'kubernetes:'
|
||||
|
||||
# check that "set serviceaccount" command supports --template output
|
||||
output_message=$(kubectl "${kube_flags[@]}" set serviceaccount pod/valid-pod deployer --dry-run --template="{{ .metadata.name }}:")
|
||||
kube::test::if_has_string "${output_message}" 'valid-pod:'
|
||||
|
||||
# check that "set subject" command supports --template output
|
||||
output_message=$(kubectl "${kube_flags[@]}" set subject clusterrolebinding/foo --user=foo --dry-run --template="{{ .metadata.name }}:")
|
||||
kube::test::if_has_string "${output_message}" 'foo:'
|
||||
|
||||
# check that "create secret docker-registry" command supports --template output
|
||||
output_message=$(kubectl "${kube_flags[@]}" create secret docker-registry foo --docker-username user --docker-password pass --docker-email foo@bar.baz --dry-run --template="{{ .metadata.name }}:")
|
||||
kube::test::if_has_string "${output_message}" 'foo:'
|
||||
|
||||
# check that "create secret generic" command supports --template output
|
||||
output_message=$(kubectl "${kube_flags[@]}" create secret generic foo --from-literal=key1=value1 --dry-run --template="{{ .metadata.name }}:")
|
||||
kube::test::if_has_string "${output_message}" 'foo:'
|
||||
|
||||
# check that "create secret tls" command supports --template output
|
||||
output_message=$(kubectl "${kube_flags[@]}" create secret tls --dry-run foo --key=hack/testdata/tls.key --cert=hack/testdata/tls.crt --template="{{ .metadata.name }}:")
|
||||
kube::test::if_has_string "${output_message}" 'foo:'
|
||||
|
||||
# check that "create service clusterip" command supports --template output
|
||||
output_message=$(kubectl "${kube_flags[@]}" create service clusterip foo --dry-run --tcp=8080 --template="{{ .metadata.name }}:")
|
||||
kube::test::if_has_string "${output_message}" 'foo:'
|
||||
|
||||
# check that "create service externalname" command supports --template output
|
||||
output_message=$(kubectl "${kube_flags[@]}" create service externalname foo --dry-run --external-name=bar --template="{{ .metadata.name }}:")
|
||||
kube::test::if_has_string "${output_message}" 'foo:'
|
||||
|
||||
# check that "create service loadbalancer" command supports --template output
|
||||
output_message=$(kubectl "${kube_flags[@]}" create service loadbalancer foo --dry-run --tcp=8080 --template="{{ .metadata.name }}:")
|
||||
kube::test::if_has_string "${output_message}" 'foo:'
|
||||
|
||||
# check that "create service nodeport" command supports --template output
|
||||
output_message=$(kubectl "${kube_flags[@]}" create service nodeport foo --dry-run --tcp=8080 --template="{{ .metadata.name }}:")
|
||||
kube::test::if_has_string "${output_message}" 'foo:'
|
||||
|
||||
# cleanup
|
||||
kubectl delete cronjob pi "${kube_flags[@]}"
|
||||
kubectl delete pods --all "${kube_flags[@]}"
|
||||
kubectl delete rc cassandra "${kube_flags[@]}"
|
||||
kubectl delete clusterrole myclusterrole "${kube_flags[@]}"
|
||||
kubectl delete clusterrolebinding foo "${kube_flags[@]}"
|
||||
|
||||
set +o nounset
|
||||
set +o errexit
|
||||
}
|
||||
|
||||
run_crd_tests() {
|
||||
set -o nounset
|
||||
set -o errexit
|
||||
|
@ -5398,6 +5600,14 @@ runTests() {
|
|||
fi
|
||||
fi
|
||||
|
||||
######################
|
||||
# kubectl --template #
|
||||
######################
|
||||
|
||||
if kube::test::if_supports_resource "${pods}" ; then
|
||||
record_command run_template_output_tests
|
||||
fi
|
||||
|
||||
################
|
||||
# Certificates #
|
||||
################
|
||||
|
|
|
@ -122,12 +122,12 @@ func TestIllegalPackageSourceCheckerThroughPrintFlags(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestIllegalPackageSourceCheckerDirectlyThroughPrinters(t *testing.T) {
|
||||
jsonPathPrinter, err := printers.NewJSONPathPrinter("{ .metadata.name }")
|
||||
jsonPathPrinter, err := genericprinters.NewJSONPathPrinter("{ .metadata.name }")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
goTemplatePrinter, err := printers.NewGoTemplatePrinter([]byte("{{ .metadata.name }}"))
|
||||
goTemplatePrinter, err := genericprinters.NewGoTemplatePrinter([]byte("{{ .metadata.name }}"))
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
@ -140,7 +140,7 @@ func TestIllegalPackageSourceCheckerDirectlyThroughPrinters(t *testing.T) {
|
|||
testCases := []struct {
|
||||
name string
|
||||
expectInternalObjErr bool
|
||||
printer printers.ResourcePrinter
|
||||
printer genericprinters.ResourcePrinter
|
||||
obj runtime.Object
|
||||
expectedOutput string
|
||||
}{
|
||||
|
|
|
@ -14,7 +14,6 @@ go_library(
|
|||
"current_context.go",
|
||||
"delete_cluster.go",
|
||||
"delete_context.go",
|
||||
"flags.go",
|
||||
"get_clusters.go",
|
||||
"get_contexts.go",
|
||||
"navigation_step_parser.go",
|
||||
|
@ -33,10 +32,8 @@ go_library(
|
|||
"//pkg/kubectl/cmd/templates:go_default_library",
|
||||
"//pkg/kubectl/cmd/util:go_default_library",
|
||||
"//pkg/kubectl/genericclioptions:go_default_library",
|
||||
"//pkg/kubectl/genericclioptions/printers:go_default_library",
|
||||
"//pkg/kubectl/util/i18n:go_default_library",
|
||||
"//pkg/printers:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/util/errors:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library",
|
||||
"//staging/src/k8s.io/apiserver/pkg/util/flag:go_default_library",
|
||||
|
|
|
@ -1,101 +0,0 @@
|
|||
/*
|
||||
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 config
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
|
||||
genericprinters "k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers"
|
||||
"k8s.io/kubernetes/pkg/printers"
|
||||
)
|
||||
|
||||
// kubectlConfigPrintFlags composes common printer flag structs
|
||||
// used across all config commands, and provides a method
|
||||
// of retrieving a known printer based on flag values provided.
|
||||
type kubectlConfigPrintFlags struct {
|
||||
JSONYamlPrintFlags *genericclioptions.JSONYamlPrintFlags
|
||||
NamePrintFlags *genericclioptions.NamePrintFlags
|
||||
TemplateFlags *printers.KubeTemplatePrintFlags
|
||||
|
||||
TypeSetter *genericprinters.TypeSetterPrinter
|
||||
|
||||
OutputFormat *string
|
||||
}
|
||||
|
||||
func (f *kubectlConfigPrintFlags) Complete(successTemplate string) error {
|
||||
return f.NamePrintFlags.Complete(successTemplate)
|
||||
}
|
||||
|
||||
func (f *kubectlConfigPrintFlags) AllowedFormats() []string {
|
||||
formats := f.JSONYamlPrintFlags.AllowedFormats()
|
||||
formats = append(formats, f.NamePrintFlags.AllowedFormats()...)
|
||||
formats = append(formats, f.TemplateFlags.AllowedFormats()...)
|
||||
return formats
|
||||
}
|
||||
|
||||
func (f *kubectlConfigPrintFlags) ToPrinter() (printers.ResourcePrinter, error) {
|
||||
outputFormat := ""
|
||||
if f.OutputFormat != nil {
|
||||
outputFormat = *f.OutputFormat
|
||||
}
|
||||
|
||||
if p, err := f.JSONYamlPrintFlags.ToPrinter(outputFormat); !genericclioptions.IsNoCompatiblePrinterError(err) {
|
||||
return f.TypeSetter.WrapToPrinter(p, err)
|
||||
}
|
||||
|
||||
if p, err := f.NamePrintFlags.ToPrinter(outputFormat); !genericclioptions.IsNoCompatiblePrinterError(err) {
|
||||
return f.TypeSetter.WrapToPrinter(p, err)
|
||||
}
|
||||
|
||||
if p, err := f.TemplateFlags.ToPrinter(outputFormat); !genericclioptions.IsNoCompatiblePrinterError(err) {
|
||||
return f.TypeSetter.WrapToPrinter(p, err)
|
||||
}
|
||||
|
||||
return nil, genericclioptions.NoCompatiblePrinterError{OutputFormat: &outputFormat, AllowedFormats: f.AllowedFormats()}
|
||||
}
|
||||
|
||||
func (f *kubectlConfigPrintFlags) AddFlags(cmd *cobra.Command) {
|
||||
f.JSONYamlPrintFlags.AddFlags(cmd)
|
||||
f.NamePrintFlags.AddFlags(cmd)
|
||||
f.TemplateFlags.AddFlags(cmd)
|
||||
|
||||
if f.OutputFormat != nil {
|
||||
cmd.Flags().StringVarP(f.OutputFormat, "output", "o", *f.OutputFormat, "Output format. One of: json|yaml|name|go-template=...|go-template-file=...|jsonpath=...|jsonpath-file=... See custom columns [http://kubernetes.io/docs/user-guide/kubectl-overview/#custom-columns], golang template [http://golang.org/pkg/text/template/#pkg-overview] and jsonpath template [http://kubernetes.io/docs/user-guide/jsonpath].")
|
||||
}
|
||||
}
|
||||
|
||||
// WithDefaultOutput sets a default output format if one is not provided through a flag value
|
||||
func (f *kubectlConfigPrintFlags) WithDefaultOutput(output string) *kubectlConfigPrintFlags {
|
||||
f.OutputFormat = &output
|
||||
return f
|
||||
}
|
||||
|
||||
func newKubeConfigPrintFlags(scheme runtime.ObjectTyper) *kubectlConfigPrintFlags {
|
||||
outputFormat := ""
|
||||
|
||||
return &kubectlConfigPrintFlags{
|
||||
OutputFormat: &outputFormat,
|
||||
|
||||
JSONYamlPrintFlags: genericclioptions.NewJSONYamlPrintFlags(),
|
||||
NamePrintFlags: genericclioptions.NewNamePrintFlags(""),
|
||||
TemplateFlags: printers.NewKubeTemplatePrintFlags(),
|
||||
|
||||
TypeSetter: genericprinters.NewTypeSetter(scheme),
|
||||
}
|
||||
}
|
|
@ -34,7 +34,7 @@ import (
|
|||
)
|
||||
|
||||
type ViewOptions struct {
|
||||
PrintFlags *kubectlConfigPrintFlags
|
||||
PrintFlags *genericclioptions.PrintFlags
|
||||
PrintObject printers.ResourcePrinterFunc
|
||||
|
||||
ConfigAccess clientcmd.ConfigAccess
|
||||
|
@ -70,7 +70,7 @@ var (
|
|||
|
||||
func NewCmdConfigView(f cmdutil.Factory, streams genericclioptions.IOStreams, ConfigAccess clientcmd.ConfigAccess) *cobra.Command {
|
||||
o := &ViewOptions{
|
||||
PrintFlags: newKubeConfigPrintFlags(legacyscheme.Scheme).WithDefaultOutput("yaml"),
|
||||
PrintFlags: genericclioptions.NewPrintFlags("").WithTypeSetter(legacyscheme.Scheme).WithDefaultOutput("yaml"),
|
||||
ConfigAccess: ConfigAccess,
|
||||
|
||||
IOStreams: streams,
|
||||
|
|
|
@ -18,7 +18,6 @@ go_library(
|
|||
"create_secret.go",
|
||||
"create_service.go",
|
||||
"create_serviceaccount.go",
|
||||
"flags.go",
|
||||
],
|
||||
importpath = "k8s.io/kubernetes/pkg/kubectl/cmd/create",
|
||||
visibility = ["//build/visible_to:pkg_kubectl_cmd_create_CONSUMERS"],
|
||||
|
@ -29,7 +28,6 @@ go_library(
|
|||
"//pkg/kubectl/cmd/util:go_default_library",
|
||||
"//pkg/kubectl/cmd/util/editor:go_default_library",
|
||||
"//pkg/kubectl/genericclioptions:go_default_library",
|
||||
"//pkg/kubectl/genericclioptions/printers:go_default_library",
|
||||
"//pkg/kubectl/genericclioptions/resource:go_default_library",
|
||||
"//pkg/kubectl/scheme:go_default_library",
|
||||
"//pkg/kubectl/util/i18n:go_default_library",
|
||||
|
|
|
@ -44,7 +44,7 @@ import (
|
|||
)
|
||||
|
||||
type CreateOptions struct {
|
||||
PrintFlags *PrintFlags
|
||||
PrintFlags *genericclioptions.PrintFlags
|
||||
RecordFlags *genericclioptions.RecordFlags
|
||||
|
||||
DryRun bool
|
||||
|
@ -79,7 +79,7 @@ var (
|
|||
|
||||
func NewCreateOptions(ioStreams genericclioptions.IOStreams) *CreateOptions {
|
||||
return &CreateOptions{
|
||||
PrintFlags: NewPrintFlags("created", legacyscheme.Scheme),
|
||||
PrintFlags: genericclioptions.NewPrintFlags("created").WithTypeSetter(legacyscheme.Scheme),
|
||||
RecordFlags: genericclioptions.NewRecordFlags(),
|
||||
|
||||
Recorder: genericclioptions.NoopRecorder{},
|
||||
|
@ -336,7 +336,7 @@ func NameFromCommandArgs(cmd *cobra.Command, args []string) (string, error) {
|
|||
// CreateSubcommandOptions is an options struct to support create subcommands
|
||||
type CreateSubcommandOptions struct {
|
||||
// PrintFlags holds options necessary for obtaining a printer
|
||||
PrintFlags *PrintFlags
|
||||
PrintFlags *genericclioptions.PrintFlags
|
||||
// Name of resource being created
|
||||
Name string
|
||||
// StructuredGenerator is the resource generator for the object being created
|
||||
|
@ -358,7 +358,7 @@ type CreateSubcommandOptions struct {
|
|||
|
||||
func NewCreateSubcommandOptions(ioStreams genericclioptions.IOStreams) *CreateSubcommandOptions {
|
||||
return &CreateSubcommandOptions{
|
||||
PrintFlags: NewPrintFlags("created", legacyscheme.Scheme),
|
||||
PrintFlags: genericclioptions.NewPrintFlags("created").WithTypeSetter(legacyscheme.Scheme),
|
||||
IOStreams: ioStreams,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -140,7 +140,7 @@ func TestCreateDeploymentNoImage(t *testing.T) {
|
|||
cmd.Flags().Set("output", "name")
|
||||
options := &DeploymentOpts{
|
||||
CreateSubcommandOptions: &CreateSubcommandOptions{
|
||||
PrintFlags: NewPrintFlags("created", legacyscheme.Scheme),
|
||||
PrintFlags: genericclioptions.NewPrintFlags("created").WithTypeSetter(legacyscheme.Scheme),
|
||||
DryRun: true,
|
||||
IOStreams: ioStreams,
|
||||
},
|
||||
|
|
|
@ -45,7 +45,7 @@ var (
|
|||
)
|
||||
|
||||
type CreateJobOptions struct {
|
||||
PrintFlags *PrintFlags
|
||||
PrintFlags *genericclioptions.PrintFlags
|
||||
|
||||
PrintObj func(obj runtime.Object) error
|
||||
|
||||
|
@ -64,7 +64,7 @@ type CreateJobOptions struct {
|
|||
|
||||
func NewCreateJobOptions(ioStreams genericclioptions.IOStreams) *CreateJobOptions {
|
||||
return &CreateJobOptions{
|
||||
PrintFlags: NewPrintFlags("created", legacyscheme.Scheme),
|
||||
PrintFlags: genericclioptions.NewPrintFlags("created").WithTypeSetter(legacyscheme.Scheme),
|
||||
IOStreams: ioStreams,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -85,7 +85,7 @@ func TestCreateJobFromCronJob(t *testing.T) {
|
|||
f := cmdtesting.NewTestFactory()
|
||||
defer f.Cleanup()
|
||||
|
||||
printFlags := NewPrintFlags("created", legacyscheme.Scheme)
|
||||
printFlags := genericclioptions.NewPrintFlags("created").WithTypeSetter(legacyscheme.Scheme)
|
||||
|
||||
ioStreams, _, buf, _ := genericclioptions.NewTestIOStreams()
|
||||
cmdOptions := &CreateJobOptions{
|
||||
|
|
|
@ -58,7 +58,7 @@ func TestCreatePdb(t *testing.T) {
|
|||
cmd.Flags().Set("dry-run", "true")
|
||||
cmd.Flags().Set("output", outputFormat)
|
||||
|
||||
printFlags := NewPrintFlags("created", legacyscheme.Scheme)
|
||||
printFlags := genericclioptions.NewPrintFlags("created").WithTypeSetter(legacyscheme.Scheme)
|
||||
printFlags.OutputFormat = &outputFormat
|
||||
|
||||
options := &PodDisruptionBudgetOpts{
|
||||
|
|
|
@ -59,7 +59,7 @@ func TestCreatePriorityClass(t *testing.T) {
|
|||
cmd.Flags().Set("dry-run", "true")
|
||||
cmd.Flags().Set("output", outputFormat)
|
||||
|
||||
printFlags := NewPrintFlags("created", legacyscheme.Scheme)
|
||||
printFlags := genericclioptions.NewPrintFlags("created").WithTypeSetter(legacyscheme.Scheme)
|
||||
printFlags.OutputFormat = &outputFormat
|
||||
|
||||
options := &PriorityClassOpts{
|
||||
|
|
|
@ -112,7 +112,7 @@ type ResourceOptions struct {
|
|||
}
|
||||
|
||||
type CreateRoleOptions struct {
|
||||
PrintFlags *PrintFlags
|
||||
PrintFlags *genericclioptions.PrintFlags
|
||||
|
||||
Name string
|
||||
Verbs []string
|
||||
|
@ -131,7 +131,7 @@ type CreateRoleOptions struct {
|
|||
|
||||
func NewCreateRoleOptions(ioStreams genericclioptions.IOStreams) *CreateRoleOptions {
|
||||
return &CreateRoleOptions{
|
||||
PrintFlags: NewPrintFlags("created", legacyscheme.Scheme),
|
||||
PrintFlags: genericclioptions.NewPrintFlags("created").WithTypeSetter(legacyscheme.Scheme),
|
||||
|
||||
IOStreams: ioStreams,
|
||||
}
|
||||
|
|
|
@ -372,14 +372,14 @@ func TestComplete(t *testing.T) {
|
|||
"test-missing-name": {
|
||||
params: []string{},
|
||||
roleOptions: &CreateRoleOptions{
|
||||
PrintFlags: NewPrintFlags("created", legacyscheme.Scheme),
|
||||
PrintFlags: genericclioptions.NewPrintFlags("created").WithTypeSetter(legacyscheme.Scheme),
|
||||
},
|
||||
expectErr: true,
|
||||
},
|
||||
"test-duplicate-verbs": {
|
||||
params: []string{roleName},
|
||||
roleOptions: &CreateRoleOptions{
|
||||
PrintFlags: NewPrintFlags("created", legacyscheme.Scheme),
|
||||
PrintFlags: genericclioptions.NewPrintFlags("created").WithTypeSetter(legacyscheme.Scheme),
|
||||
Name: roleName,
|
||||
Verbs: []string{
|
||||
"get",
|
||||
|
@ -412,7 +412,7 @@ func TestComplete(t *testing.T) {
|
|||
"test-verball": {
|
||||
params: []string{roleName},
|
||||
roleOptions: &CreateRoleOptions{
|
||||
PrintFlags: NewPrintFlags("created", legacyscheme.Scheme),
|
||||
PrintFlags: genericclioptions.NewPrintFlags("created").WithTypeSetter(legacyscheme.Scheme),
|
||||
Name: roleName,
|
||||
Verbs: []string{
|
||||
"get",
|
||||
|
@ -441,7 +441,7 @@ func TestComplete(t *testing.T) {
|
|||
"test-duplicate-resourcenames": {
|
||||
params: []string{roleName},
|
||||
roleOptions: &CreateRoleOptions{
|
||||
PrintFlags: NewPrintFlags("created", legacyscheme.Scheme),
|
||||
PrintFlags: genericclioptions.NewPrintFlags("created").WithTypeSetter(legacyscheme.Scheme),
|
||||
Name: roleName,
|
||||
Verbs: []string{"*"},
|
||||
ResourceNames: []string{"foo", "foo"},
|
||||
|
@ -466,7 +466,7 @@ func TestComplete(t *testing.T) {
|
|||
"test-valid-complete-case": {
|
||||
params: []string{roleName},
|
||||
roleOptions: &CreateRoleOptions{
|
||||
PrintFlags: NewPrintFlags("created", legacyscheme.Scheme),
|
||||
PrintFlags: genericclioptions.NewPrintFlags("created").WithTypeSetter(legacyscheme.Scheme),
|
||||
Name: roleName,
|
||||
Verbs: []string{"*"},
|
||||
ResourceNames: []string{"foo"},
|
||||
|
|
|
@ -1,93 +0,0 @@
|
|||
/*
|
||||
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 create
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
|
||||
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
genericprinters "k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers"
|
||||
"k8s.io/kubernetes/pkg/printers"
|
||||
)
|
||||
|
||||
// PrintFlags composes common printer flag structs
|
||||
// used across all create commands, and provides a method
|
||||
// of retrieving a known printer based on flag values provided.
|
||||
type PrintFlags struct {
|
||||
JSONYamlPrintFlags *genericclioptions.JSONYamlPrintFlags
|
||||
NamePrintFlags *genericclioptions.NamePrintFlags
|
||||
TemplateFlags *printers.KubeTemplatePrintFlags
|
||||
|
||||
TypeSetter *genericprinters.TypeSetterPrinter
|
||||
|
||||
OutputFormat *string
|
||||
}
|
||||
|
||||
func (f *PrintFlags) AllowedFormats() []string {
|
||||
return append(append(f.JSONYamlPrintFlags.AllowedFormats(), f.NamePrintFlags.AllowedFormats()...),
|
||||
f.TemplateFlags.AllowedFormats()...)
|
||||
}
|
||||
|
||||
func (f *PrintFlags) Complete(successTemplate string) error {
|
||||
return f.NamePrintFlags.Complete(successTemplate)
|
||||
}
|
||||
|
||||
func (f *PrintFlags) ToPrinter() (printers.ResourcePrinter, error) {
|
||||
outputFormat := ""
|
||||
if f.OutputFormat != nil {
|
||||
outputFormat = *f.OutputFormat
|
||||
}
|
||||
|
||||
if p, err := f.JSONYamlPrintFlags.ToPrinter(outputFormat); !genericclioptions.IsNoCompatiblePrinterError(err) {
|
||||
return f.TypeSetter.WrapToPrinter(p, err)
|
||||
}
|
||||
|
||||
if p, err := f.NamePrintFlags.ToPrinter(outputFormat); !genericclioptions.IsNoCompatiblePrinterError(err) {
|
||||
return f.TypeSetter.WrapToPrinter(p, err)
|
||||
}
|
||||
|
||||
if p, err := f.TemplateFlags.ToPrinter(outputFormat); !genericclioptions.IsNoCompatiblePrinterError(err) {
|
||||
return f.TypeSetter.WrapToPrinter(p, err)
|
||||
}
|
||||
|
||||
return nil, genericclioptions.NoCompatiblePrinterError{OutputFormat: &outputFormat, AllowedFormats: f.AllowedFormats()}
|
||||
}
|
||||
|
||||
func (f *PrintFlags) AddFlags(cmd *cobra.Command) {
|
||||
f.JSONYamlPrintFlags.AddFlags(cmd)
|
||||
f.NamePrintFlags.AddFlags(cmd)
|
||||
f.TemplateFlags.AddFlags(cmd)
|
||||
|
||||
if f.OutputFormat != nil {
|
||||
cmd.Flags().StringVarP(f.OutputFormat, "output", "o", *f.OutputFormat, "Output format. One of: json|yaml|wide|name|custom-columns=...|custom-columns-file=...|go-template=...|go-template-file=...|jsonpath=...|jsonpath-file=... See custom columns [http://kubernetes.io/docs/user-guide/kubectl-overview/#custom-columns], golang template [http://golang.org/pkg/text/template/#pkg-overview] and jsonpath template [http://kubernetes.io/docs/user-guide/jsonpath].")
|
||||
}
|
||||
}
|
||||
|
||||
func NewPrintFlags(operation string, scheme runtime.ObjectTyper) *PrintFlags {
|
||||
outputFormat := ""
|
||||
|
||||
return &PrintFlags{
|
||||
OutputFormat: &outputFormat,
|
||||
|
||||
JSONYamlPrintFlags: genericclioptions.NewJSONYamlPrintFlags(),
|
||||
NamePrintFlags: genericclioptions.NewNamePrintFlags(operation),
|
||||
TemplateFlags: printers.NewKubeTemplatePrintFlags(),
|
||||
|
||||
TypeSetter: genericprinters.NewTypeSetter(scheme),
|
||||
}
|
||||
}
|
|
@ -35,7 +35,7 @@ type PrintFlags struct {
|
|||
NamePrintFlags *genericclioptions.NamePrintFlags
|
||||
CustomColumnsFlags *printers.CustomColumnsPrintFlags
|
||||
HumanReadableFlags *HumanPrintFlags
|
||||
TemplateFlags *printers.KubeTemplatePrintFlags
|
||||
TemplateFlags *genericclioptions.KubeTemplatePrintFlags
|
||||
|
||||
NoHeaders *bool
|
||||
OutputFormat *string
|
||||
|
@ -182,7 +182,7 @@ func NewGetPrintFlags() *PrintFlags {
|
|||
|
||||
JSONYamlPrintFlags: genericclioptions.NewJSONYamlPrintFlags(),
|
||||
NamePrintFlags: genericclioptions.NewNamePrintFlags(""),
|
||||
TemplateFlags: printers.NewKubeTemplatePrintFlags(),
|
||||
TemplateFlags: genericclioptions.NewKubeTemplatePrintFlags(),
|
||||
|
||||
HumanReadableFlags: NewHumanPrintFlags(),
|
||||
CustomColumnsFlags: printers.NewCustomColumnsPrintFlags(),
|
||||
|
|
|
@ -89,15 +89,8 @@ type ReplaceOptions struct {
|
|||
}
|
||||
|
||||
func NewReplaceOptions(streams genericclioptions.IOStreams) *ReplaceOptions {
|
||||
outputFormat := ""
|
||||
|
||||
return &ReplaceOptions{
|
||||
// TODO(juanvallejo): figure out why we only support the "name" outputFormat in this command
|
||||
// we only support "-o name" for this command, so only register the name printer
|
||||
PrintFlags: &genericclioptions.PrintFlags{
|
||||
OutputFormat: &outputFormat,
|
||||
NamePrintFlags: genericclioptions.NewNamePrintFlags("replaced"),
|
||||
},
|
||||
PrintFlags: genericclioptions.NewPrintFlags("replaced"),
|
||||
DeleteFlags: NewDeleteFlags("to use to replace the resource."),
|
||||
|
||||
IOStreams: streams,
|
||||
|
|
|
@ -93,15 +93,8 @@ type ScaleOptions struct {
|
|||
}
|
||||
|
||||
func NewScaleOptions(ioStreams genericclioptions.IOStreams) *ScaleOptions {
|
||||
outputFormat := ""
|
||||
|
||||
return &ScaleOptions{
|
||||
// TODO(juanvallejo): figure out why we only support the "name" outputFormat in this command
|
||||
// we only support "-o name" for this command, so only register the name printer
|
||||
PrintFlags: &genericclioptions.PrintFlags{
|
||||
OutputFormat: &outputFormat,
|
||||
NamePrintFlags: genericclioptions.NewNamePrintFlags("scaled"),
|
||||
},
|
||||
PrintFlags: genericclioptions.NewPrintFlags("scaled"),
|
||||
RecordFlags: genericclioptions.NewRecordFlags(),
|
||||
CurrentReplicas: -1,
|
||||
Recorder: genericclioptions.NoopRecorder{},
|
||||
|
|
|
@ -11,9 +11,12 @@ go_library(
|
|||
"filename_flags.go",
|
||||
"io_options.go",
|
||||
"json_yaml_flags.go",
|
||||
"jsonpath_flags.go",
|
||||
"kube_template_flags.go",
|
||||
"name_flags.go",
|
||||
"print_flags.go",
|
||||
"record_flags.go",
|
||||
"template_flags.go",
|
||||
],
|
||||
importpath = "k8s.io/kubernetes/pkg/kubectl/genericclioptions",
|
||||
visibility = ["//visibility:public"],
|
||||
|
@ -57,7 +60,9 @@ go_test(
|
|||
name = "go_default_test",
|
||||
srcs = [
|
||||
"json_yaml_flags_test.go",
|
||||
"jsonpath_flags_test.go",
|
||||
"name_flags_test.go",
|
||||
"template_flags_test.go",
|
||||
],
|
||||
embed = [":go_default_library"],
|
||||
deps = [
|
||||
|
|
|
@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
|||
limitations under the License.
|
||||
*/
|
||||
|
||||
package printers
|
||||
package genericclioptions
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
@ -23,7 +23,7 @@ import (
|
|||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
|
||||
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers"
|
||||
)
|
||||
|
||||
// templates are logically optional for specifying a format.
|
||||
|
@ -55,9 +55,9 @@ func (f *JSONPathPrintFlags) AllowedFormats() []string {
|
|||
// ToPrinter receives an templateFormat and returns a printer capable of
|
||||
// handling --template format printing.
|
||||
// Returns false if the specified templateFormat does not match a template format.
|
||||
func (f *JSONPathPrintFlags) ToPrinter(templateFormat string) (ResourcePrinter, error) {
|
||||
func (f *JSONPathPrintFlags) ToPrinter(templateFormat string) (printers.ResourcePrinter, error) {
|
||||
if (f.TemplateArgument == nil || len(*f.TemplateArgument) == 0) && len(templateFormat) == 0 {
|
||||
return nil, genericclioptions.NoCompatiblePrinterError{Options: f, OutputFormat: &templateFormat}
|
||||
return nil, NoCompatiblePrinterError{Options: f, OutputFormat: &templateFormat}
|
||||
}
|
||||
|
||||
templateValue := ""
|
||||
|
@ -76,7 +76,7 @@ func (f *JSONPathPrintFlags) ToPrinter(templateFormat string) (ResourcePrinter,
|
|||
}
|
||||
|
||||
if _, supportedFormat := jsonFormats[templateFormat]; !supportedFormat {
|
||||
return nil, genericclioptions.NoCompatiblePrinterError{OutputFormat: &templateFormat, AllowedFormats: f.AllowedFormats()}
|
||||
return nil, NoCompatiblePrinterError{OutputFormat: &templateFormat, AllowedFormats: f.AllowedFormats()}
|
||||
}
|
||||
|
||||
if len(templateValue) == 0 {
|
||||
|
@ -92,7 +92,7 @@ func (f *JSONPathPrintFlags) ToPrinter(templateFormat string) (ResourcePrinter,
|
|||
templateValue = string(data)
|
||||
}
|
||||
|
||||
p, err := NewJSONPathPrinter(templateValue)
|
||||
p, err := printers.NewJSONPathPrinter(templateValue)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error parsing jsonpath %s, %v\n", templateValue, err)
|
||||
}
|
|
@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
|||
limitations under the License.
|
||||
*/
|
||||
|
||||
package printers
|
||||
package genericclioptions
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
@ -26,7 +26,6 @@ import (
|
|||
|
||||
"k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
|
||||
)
|
||||
|
||||
func TestPrinterSupportsExpectedJSONPathFormats(t *testing.T) {
|
||||
|
@ -105,12 +104,12 @@ func TestPrinterSupportsExpectedJSONPathFormats(t *testing.T) {
|
|||
|
||||
p, err := printFlags.ToPrinter(tc.outputFormat)
|
||||
if tc.expectNoMatch {
|
||||
if !genericclioptions.IsNoCompatiblePrinterError(err) {
|
||||
if !IsNoCompatiblePrinterError(err) {
|
||||
t.Fatalf("expected no printer matches for output format %q", tc.outputFormat)
|
||||
}
|
||||
return
|
||||
}
|
||||
if genericclioptions.IsNoCompatiblePrinterError(err) {
|
||||
if IsNoCompatiblePrinterError(err) {
|
||||
t.Fatalf("expected to match template printer for output format %q", tc.outputFormat)
|
||||
}
|
||||
|
||||
|
@ -184,7 +183,7 @@ func TestJSONPathPrinterDefaultsAllowMissingKeysToTrue(t *testing.T) {
|
|||
|
||||
outputFormat := "jsonpath"
|
||||
p, err := printFlags.ToPrinter(outputFormat)
|
||||
if genericclioptions.IsNoCompatiblePrinterError(err) {
|
||||
if IsNoCompatiblePrinterError(err) {
|
||||
t.Fatalf("expected to match template printer for output format %q", outputFormat)
|
||||
}
|
||||
if err != nil {
|
|
@ -14,31 +14,38 @@ See the License for the specific language governing permissions and
|
|||
limitations under the License.
|
||||
*/
|
||||
|
||||
package printers
|
||||
package genericclioptions
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
|
||||
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers"
|
||||
)
|
||||
|
||||
// KubeTemplatePrintFlags composes print flags that provide both a JSONPath and a go-template printer.
|
||||
// This is necessary if dealing with cases that require support both both printers, since both sets of flags
|
||||
// require overlapping flags.
|
||||
type KubeTemplatePrintFlags struct {
|
||||
*GoTemplatePrintFlags
|
||||
*JSONPathPrintFlags
|
||||
GoTemplatePrintFlags *GoTemplatePrintFlags
|
||||
JSONPathPrintFlags *JSONPathPrintFlags
|
||||
|
||||
AllowMissingKeys *bool
|
||||
TemplateArgument *string
|
||||
}
|
||||
|
||||
func (f *KubeTemplatePrintFlags) AllowedFormats() []string {
|
||||
if f == nil {
|
||||
return []string{}
|
||||
}
|
||||
return append(f.GoTemplatePrintFlags.AllowedFormats(), f.JSONPathPrintFlags.AllowedFormats()...)
|
||||
}
|
||||
|
||||
func (f *KubeTemplatePrintFlags) ToPrinter(outputFormat string) (ResourcePrinter, error) {
|
||||
if p, err := f.JSONPathPrintFlags.ToPrinter(outputFormat); !genericclioptions.IsNoCompatiblePrinterError(err) {
|
||||
func (f *KubeTemplatePrintFlags) ToPrinter(outputFormat string) (printers.ResourcePrinter, error) {
|
||||
if f == nil {
|
||||
return nil, NoCompatiblePrinterError{}
|
||||
}
|
||||
|
||||
if p, err := f.JSONPathPrintFlags.ToPrinter(outputFormat); !IsNoCompatiblePrinterError(err) {
|
||||
return p, err
|
||||
}
|
||||
return f.GoTemplatePrintFlags.ToPrinter(outputFormat)
|
||||
|
@ -47,6 +54,10 @@ func (f *KubeTemplatePrintFlags) ToPrinter(outputFormat string) (ResourcePrinter
|
|||
// AddFlags receives a *cobra.Command reference and binds
|
||||
// flags related to template printing to it
|
||||
func (f *KubeTemplatePrintFlags) AddFlags(c *cobra.Command) {
|
||||
if f == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if f.TemplateArgument != nil {
|
||||
c.Flags().StringVar(f.TemplateArgument, "template", *f.TemplateArgument, "Template string or path to template file to use when -o=go-template, -o=go-template-file. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].")
|
||||
c.MarkFlagFilename("template")
|
|
@ -55,8 +55,9 @@ func IsNoCompatiblePrinterError(err error) bool {
|
|||
// used across all commands, and provides a method
|
||||
// of retrieving a known printer based on flag values provided.
|
||||
type PrintFlags struct {
|
||||
JSONYamlPrintFlags *JSONYamlPrintFlags
|
||||
NamePrintFlags *NamePrintFlags
|
||||
JSONYamlPrintFlags *JSONYamlPrintFlags
|
||||
NamePrintFlags *NamePrintFlags
|
||||
TemplatePrinterFlags *KubeTemplatePrintFlags
|
||||
|
||||
TypeSetterPrinter *printers.TypeSetterPrinter
|
||||
|
||||
|
@ -68,7 +69,11 @@ func (f *PrintFlags) Complete(successTemplate string) error {
|
|||
}
|
||||
|
||||
func (f *PrintFlags) AllowedFormats() []string {
|
||||
return append(f.JSONYamlPrintFlags.AllowedFormats(), f.NamePrintFlags.AllowedFormats()...)
|
||||
ret := []string{}
|
||||
ret = append(ret, f.JSONYamlPrintFlags.AllowedFormats()...)
|
||||
ret = append(ret, f.NamePrintFlags.AllowedFormats()...)
|
||||
ret = append(ret, f.TemplatePrinterFlags.AllowedFormats()...)
|
||||
return ret
|
||||
}
|
||||
|
||||
func (f *PrintFlags) ToPrinter() (printers.ResourcePrinter, error) {
|
||||
|
@ -76,6 +81,10 @@ 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 {
|
||||
outputFormat = "go-template"
|
||||
}
|
||||
|
||||
if f.JSONYamlPrintFlags != nil {
|
||||
if p, err := f.JSONYamlPrintFlags.ToPrinter(outputFormat); !IsNoCompatiblePrinterError(err) {
|
||||
|
@ -89,12 +98,19 @@ func (f *PrintFlags) ToPrinter() (printers.ResourcePrinter, error) {
|
|||
}
|
||||
}
|
||||
|
||||
if f.TemplatePrinterFlags != nil {
|
||||
if p, err := f.TemplatePrinterFlags.ToPrinter(outputFormat); !IsNoCompatiblePrinterError(err) {
|
||||
return f.TypeSetterPrinter.WrapToPrinter(p, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil, NoCompatiblePrinterError{OutputFormat: f.OutputFormat, AllowedFormats: f.AllowedFormats()}
|
||||
}
|
||||
|
||||
func (f *PrintFlags) AddFlags(cmd *cobra.Command) {
|
||||
f.JSONYamlPrintFlags.AddFlags(cmd)
|
||||
f.NamePrintFlags.AddFlags(cmd)
|
||||
f.TemplatePrinterFlags.AddFlags(cmd)
|
||||
|
||||
if f.OutputFormat != nil {
|
||||
cmd.Flags().StringVarP(f.OutputFormat, "output", "o", *f.OutputFormat, fmt.Sprintf("Output format. One of: %s.", strings.Join(f.AllowedFormats(), "|")))
|
||||
|
@ -119,7 +135,8 @@ func NewPrintFlags(operation string) *PrintFlags {
|
|||
return &PrintFlags{
|
||||
OutputFormat: &outputFormat,
|
||||
|
||||
JSONYamlPrintFlags: NewJSONYamlPrintFlags(),
|
||||
NamePrintFlags: NewNamePrintFlags(operation),
|
||||
JSONYamlPrintFlags: NewJSONYamlPrintFlags(),
|
||||
NamePrintFlags: NewNamePrintFlags(operation),
|
||||
TemplatePrinterFlags: NewKubeTemplatePrintFlags(),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,8 +6,10 @@ go_library(
|
|||
"discard.go",
|
||||
"interface.go",
|
||||
"json.go",
|
||||
"jsonpath.go",
|
||||
"name.go",
|
||||
"sourcechecker.go",
|
||||
"template.go",
|
||||
"typesetter.go",
|
||||
],
|
||||
importpath = "k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers",
|
||||
|
@ -17,14 +19,22 @@ go_library(
|
|||
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/util/jsonpath:go_default_library",
|
||||
"//vendor/github.com/ghodss/yaml:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = ["sourcechecker_test.go"],
|
||||
srcs = [
|
||||
"sourcechecker_test.go",
|
||||
"template_test.go",
|
||||
],
|
||||
embed = [":go_default_library"],
|
||||
deps = [
|
||||
"//staging/src/k8s.io/api/core/v1:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
|
|
|
@ -24,7 +24,6 @@ import (
|
|||
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/client-go/util/jsonpath"
|
||||
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers"
|
||||
)
|
||||
|
||||
// exists returns true if it would be possible to call the index function
|
||||
|
@ -118,8 +117,8 @@ func (j *JSONPathPrinter) PrintObj(obj runtime.Object, w io.Writer) error {
|
|||
// we use reflect.Indirect here in order to obtain the actual value from a pointer.
|
||||
// we need an actual value in order to retrieve the package path for an object.
|
||||
// using reflect.Indirect indiscriminately is valid here, as all runtime.Objects are supposed to be pointers.
|
||||
if printers.InternalObjectPreventer.IsForbidden(reflect.Indirect(reflect.ValueOf(obj)).Type().PkgPath()) {
|
||||
return fmt.Errorf(printers.InternalObjectPrinterErr)
|
||||
if InternalObjectPreventer.IsForbidden(reflect.Indirect(reflect.ValueOf(obj)).Type().PkgPath()) {
|
||||
return fmt.Errorf(InternalObjectPrinterErr)
|
||||
}
|
||||
|
||||
var queryObj interface{} = obj
|
|
@ -25,7 +25,6 @@ import (
|
|||
"text/template"
|
||||
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers"
|
||||
)
|
||||
|
||||
// GoTemplatePrinter is an implementation of ResourcePrinter which formats data with a Go Template.
|
||||
|
@ -61,8 +60,8 @@ func (p *GoTemplatePrinter) AllowMissingKeys(allow bool) {
|
|||
|
||||
// PrintObj formats the obj with the Go Template.
|
||||
func (p *GoTemplatePrinter) PrintObj(obj runtime.Object, w io.Writer) error {
|
||||
if printers.InternalObjectPreventer.IsForbidden(reflect.Indirect(reflect.ValueOf(obj)).Type().PkgPath()) {
|
||||
return fmt.Errorf(printers.InternalObjectPrinterErr)
|
||||
if InternalObjectPreventer.IsForbidden(reflect.Indirect(reflect.ValueOf(obj)).Type().PkgPath()) {
|
||||
return fmt.Errorf(InternalObjectPrinterErr)
|
||||
}
|
||||
|
||||
var data []byte
|
|
@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
|||
limitations under the License.
|
||||
*/
|
||||
|
||||
package printers
|
||||
package genericclioptions
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
@ -23,7 +23,7 @@ import (
|
|||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
|
||||
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers"
|
||||
)
|
||||
|
||||
// templates are logically optional for specifying a format.
|
||||
|
@ -57,9 +57,9 @@ func (f *GoTemplatePrintFlags) AllowedFormats() []string {
|
|||
// ToPrinter receives an templateFormat and returns a printer capable of
|
||||
// handling --template format printing.
|
||||
// Returns false if the specified templateFormat does not match a template format.
|
||||
func (f *GoTemplatePrintFlags) ToPrinter(templateFormat string) (ResourcePrinter, error) {
|
||||
func (f *GoTemplatePrintFlags) ToPrinter(templateFormat string) (printers.ResourcePrinter, error) {
|
||||
if (f.TemplateArgument == nil || len(*f.TemplateArgument) == 0) && len(templateFormat) == 0 {
|
||||
return nil, genericclioptions.NoCompatiblePrinterError{Options: f, OutputFormat: &templateFormat}
|
||||
return nil, NoCompatiblePrinterError{Options: f, OutputFormat: &templateFormat}
|
||||
}
|
||||
|
||||
templateValue := ""
|
||||
|
@ -78,7 +78,7 @@ func (f *GoTemplatePrintFlags) ToPrinter(templateFormat string) (ResourcePrinter
|
|||
}
|
||||
|
||||
if _, supportedFormat := templateFormats[templateFormat]; !supportedFormat {
|
||||
return nil, genericclioptions.NoCompatiblePrinterError{OutputFormat: &templateFormat, AllowedFormats: f.AllowedFormats()}
|
||||
return nil, NoCompatiblePrinterError{OutputFormat: &templateFormat, AllowedFormats: f.AllowedFormats()}
|
||||
}
|
||||
|
||||
if len(templateValue) == 0 {
|
||||
|
@ -94,7 +94,7 @@ func (f *GoTemplatePrintFlags) ToPrinter(templateFormat string) (ResourcePrinter
|
|||
templateValue = string(data)
|
||||
}
|
||||
|
||||
p, err := NewGoTemplatePrinter([]byte(templateValue))
|
||||
p, err := printers.NewGoTemplatePrinter([]byte(templateValue))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error parsing template %s, %v\n", templateValue, err)
|
||||
}
|
|
@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
|||
limitations under the License.
|
||||
*/
|
||||
|
||||
package printers
|
||||
package genericclioptions
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
@ -26,7 +26,6 @@ import (
|
|||
|
||||
"k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
|
||||
)
|
||||
|
||||
func TestPrinterSupportsExpectedTemplateFormats(t *testing.T) {
|
||||
|
@ -105,12 +104,12 @@ func TestPrinterSupportsExpectedTemplateFormats(t *testing.T) {
|
|||
|
||||
p, err := printFlags.ToPrinter(tc.outputFormat)
|
||||
if tc.expectNoMatch {
|
||||
if !genericclioptions.IsNoCompatiblePrinterError(err) {
|
||||
if !IsNoCompatiblePrinterError(err) {
|
||||
t.Fatalf("expected no printer matches for output format %q", tc.outputFormat)
|
||||
}
|
||||
return
|
||||
}
|
||||
if genericclioptions.IsNoCompatiblePrinterError(err) {
|
||||
if IsNoCompatiblePrinterError(err) {
|
||||
t.Fatalf("expected to match template printer for output format %q", tc.outputFormat)
|
||||
}
|
||||
|
||||
|
@ -178,7 +177,7 @@ func TestTemplatePrinterDefaultsAllowMissingKeysToTrue(t *testing.T) {
|
|||
|
||||
outputFormat := "template"
|
||||
p, err := printFlags.ToPrinter(outputFormat)
|
||||
if genericclioptions.IsNoCompatiblePrinterError(err) {
|
||||
if IsNoCompatiblePrinterError(err) {
|
||||
t.Fatalf("expected to match template printer for output format %q", outputFormat)
|
||||
}
|
||||
if err != nil {
|
|
@ -13,12 +13,7 @@ go_library(
|
|||
"customcolumn_flags.go",
|
||||
"humanreadable.go",
|
||||
"interface.go",
|
||||
"jsonpath.go",
|
||||
"jsonpath_flags.go",
|
||||
"kube_template_flags.go",
|
||||
"tabwriter.go",
|
||||
"template.go",
|
||||
"template_flags.go",
|
||||
],
|
||||
importpath = "k8s.io/kubernetes/pkg/printers",
|
||||
deps = [
|
||||
|
@ -61,9 +56,6 @@ go_test(
|
|||
"customcolumn_flags_test.go",
|
||||
"customcolumn_test.go",
|
||||
"humanreadable_test.go",
|
||||
"jsonpath_flags_test.go",
|
||||
"template_flags_test.go",
|
||||
"template_test.go",
|
||||
],
|
||||
embed = [":go_default_library"],
|
||||
deps = [
|
||||
|
|
|
@ -338,7 +338,7 @@ func TestUnknownTypePrinting(t *testing.T) {
|
|||
|
||||
func TestTemplatePanic(t *testing.T) {
|
||||
tmpl := `{{and ((index .currentState.info "foo").state.running.startedAt) .currentState.info.net.state.running.startedAt}}`
|
||||
printer, err := printers.NewGoTemplatePrinter([]byte(tmpl))
|
||||
printer, err := genericprinters.NewGoTemplatePrinter([]byte(tmpl))
|
||||
if err != nil {
|
||||
t.Fatalf("tmpl fail: %v", err)
|
||||
}
|
||||
|
@ -503,7 +503,7 @@ func TestTemplateStrings(t *testing.T) {
|
|||
}
|
||||
// The point of this test is to verify that the below template works.
|
||||
tmpl := `{{if (exists . "status" "containerStatuses")}}{{range .status.containerStatuses}}{{if (and (eq .name "foo") (exists . "state" "running"))}}true{{end}}{{end}}{{end}}`
|
||||
printer, err := printers.NewGoTemplatePrinter([]byte(tmpl))
|
||||
printer, err := genericprinters.NewGoTemplatePrinter([]byte(tmpl))
|
||||
if err != nil {
|
||||
t.Fatalf("tmpl fail: %v", err)
|
||||
}
|
||||
|
@ -535,17 +535,17 @@ func TestPrinters(t *testing.T) {
|
|||
jsonpathPrinter printers.ResourcePrinter
|
||||
)
|
||||
|
||||
templatePrinter, err = printers.NewGoTemplatePrinter([]byte("{{.name}}"))
|
||||
templatePrinter, err = genericprinters.NewGoTemplatePrinter([]byte("{{.name}}"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
templatePrinter2, err = printers.NewGoTemplatePrinter([]byte("{{len .items}}"))
|
||||
templatePrinter2, err = genericprinters.NewGoTemplatePrinter([]byte("{{len .items}}"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
jsonpathPrinter, err = printers.NewJSONPathPrinter("{.metadata.name}")
|
||||
jsonpathPrinter, err = genericprinters.NewJSONPathPrinter("{.metadata.name}")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue