mirror of
https://github.com/optim-enterprises-bv/kubernetes.git
synced 2025-11-26 19:35:10 +00:00
begin wiring printflags through set cmds
This commit is contained in:
@@ -26,6 +26,7 @@ go_library(
|
||||
"//pkg/kubectl/cmd/util/env:go_default_library",
|
||||
"//pkg/kubectl/resource:go_default_library",
|
||||
"//pkg/kubectl/util/i18n:go_default_library",
|
||||
"//pkg/printers:go_default_library",
|
||||
"//vendor/github.com/spf13/cobra:go_default_library",
|
||||
"//vendor/k8s.io/api/core/v1:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",
|
||||
@@ -66,6 +67,7 @@ go_test(
|
||||
"//pkg/kubectl/cmd/util:go_default_library",
|
||||
"//pkg/kubectl/resource:go_default_library",
|
||||
"//pkg/kubectl/scheme:go_default_library",
|
||||
"//pkg/printers:go_default_library",
|
||||
"//vendor/github.com/spf13/cobra:go_default_library",
|
||||
"//vendor/github.com/stretchr/testify/assert:go_default_library",
|
||||
"//vendor/k8s.io/api/apps/v1:go_default_library",
|
||||
|
||||
@@ -33,6 +33,7 @@ import (
|
||||
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
|
||||
envutil "k8s.io/kubernetes/pkg/kubectl/cmd/util/env"
|
||||
"k8s.io/kubernetes/pkg/kubectl/resource"
|
||||
"k8s.io/kubernetes/pkg/printers"
|
||||
|
||||
utilerrors "k8s.io/apimachinery/pkg/util/errors"
|
||||
)
|
||||
@@ -91,6 +92,8 @@ var (
|
||||
)
|
||||
|
||||
type EnvOptions struct {
|
||||
PrintFlags *printers.PrintFlags
|
||||
|
||||
Out io.Writer
|
||||
Err io.Writer
|
||||
In io.Reader
|
||||
@@ -100,13 +103,12 @@ type EnvOptions struct {
|
||||
EnvArgs []string
|
||||
Resources []string
|
||||
|
||||
All bool
|
||||
Resolve bool
|
||||
List bool
|
||||
ShortOutput bool
|
||||
Local bool
|
||||
Overwrite bool
|
||||
DryRun bool
|
||||
All bool
|
||||
Resolve bool
|
||||
List bool
|
||||
Local bool
|
||||
Overwrite bool
|
||||
DryRun bool
|
||||
|
||||
ResourceVersion string
|
||||
ContainerSelector string
|
||||
@@ -115,6 +117,8 @@ type EnvOptions struct {
|
||||
From string
|
||||
Prefix string
|
||||
|
||||
PrintObj func(runtime.Object) error
|
||||
|
||||
Builder *resource.Builder
|
||||
Infos []*resource.Info
|
||||
|
||||
@@ -127,6 +131,8 @@ type EnvOptions struct {
|
||||
// pod templates are selected by default and allowing environment to be overwritten
|
||||
func NewEnvOptions(in io.Reader, out, errout io.Writer) *EnvOptions {
|
||||
return &EnvOptions{
|
||||
PrintFlags: printers.NewPrintFlags("env updated"),
|
||||
|
||||
Out: out,
|
||||
Err: errout,
|
||||
In: in,
|
||||
@@ -162,9 +168,9 @@ func NewCmdEnv(f cmdutil.Factory, in io.Reader, out, errout io.Writer) *cobra.Co
|
||||
cmd.Flags().BoolVar(&options.All, "all", options.All, "If true, select all resources in the namespace of the specified resource types")
|
||||
cmd.Flags().BoolVar(&options.Overwrite, "overwrite", options.Overwrite, "If true, allow environment to be overwritten, otherwise reject updates that overwrite existing environment.")
|
||||
|
||||
cmdutil.AddDryRunFlag(cmd)
|
||||
cmdutil.AddPrinterFlags(cmd)
|
||||
options.PrintFlags.AddFlags(cmd)
|
||||
|
||||
cmdutil.AddDryRunFlag(cmd)
|
||||
return cmd
|
||||
}
|
||||
|
||||
@@ -209,7 +215,16 @@ func (o *EnvOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []stri
|
||||
o.Resources = resources
|
||||
o.Cmd = cmd
|
||||
|
||||
o.ShortOutput = cmdutil.GetFlagString(cmd, "output") == "name"
|
||||
o.PrintFlags.Complete(o.DryRun)
|
||||
|
||||
printer, err := o.PrintFlags.ToPrinter()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
o.PrintObj = func(obj runtime.Object) error {
|
||||
return printer.PrintObj(obj, o.Out)
|
||||
}
|
||||
|
||||
if o.List && len(o.Output) > 0 {
|
||||
return cmdutil.UsageErrorf(o.Cmd, "--list and --output may not be specified together")
|
||||
@@ -418,7 +433,7 @@ func (o *EnvOptions) RunEnv(f cmdutil.Factory) error {
|
||||
}
|
||||
|
||||
if o.Local || o.DryRun {
|
||||
if err := cmdutil.PrintObject(o.Cmd, patch.Info.AsVersioned(), o.Out); err != nil {
|
||||
if err := o.PrintObj(patch.Info.AsVersioned()); err != nil {
|
||||
return err
|
||||
}
|
||||
continue
|
||||
@@ -437,14 +452,9 @@ func (o *EnvOptions) RunEnv(f cmdutil.Factory) error {
|
||||
return fmt.Errorf("at least one environment variable must be provided")
|
||||
}
|
||||
|
||||
if len(o.Output) > 0 {
|
||||
if err := cmdutil.PrintObject(o.Cmd, info.AsVersioned(), o.Out); err != nil {
|
||||
return err
|
||||
}
|
||||
continue
|
||||
if err := o.PrintObj(info.AsVersioned()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cmdutil.PrintSuccess(o.ShortOutput, o.Out, info.Object, false, "env updated")
|
||||
}
|
||||
return utilerrors.NewAggregate(allErrs)
|
||||
}
|
||||
|
||||
@@ -26,6 +26,8 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"k8s.io/kubernetes/pkg/printers"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
appsv1 "k8s.io/api/apps/v1"
|
||||
appsv1beta1 "k8s.io/api/apps/v1beta1"
|
||||
@@ -61,16 +63,26 @@ func TestSetEnvLocal(t *testing.T) {
|
||||
tf.Namespace = "test"
|
||||
tf.ClientConfigVal = &restclient.Config{ContentConfig: restclient.ContentConfig{GroupVersion: &schema.GroupVersion{Version: ""}}}
|
||||
|
||||
outputFormat := "name"
|
||||
|
||||
buf := bytes.NewBuffer([]byte{})
|
||||
cmd := NewCmdEnv(tf, os.Stdin, buf, buf)
|
||||
cmd.SetOutput(buf)
|
||||
cmd.Flags().Set("output", "name")
|
||||
cmd.Flags().Set("output", outputFormat)
|
||||
cmd.Flags().Set("local", "true")
|
||||
|
||||
opts := EnvOptions{FilenameOptions: resource.FilenameOptions{
|
||||
Filenames: []string{"../../../../test/e2e/testing-manifests/statefulset/cassandra/controller.yaml"}},
|
||||
opts := EnvOptions{
|
||||
PrintFlags: &printers.PrintFlags{
|
||||
JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(),
|
||||
NamePrintFlags: printers.NewNamePrintFlags("", false),
|
||||
|
||||
OutputFormat: &outputFormat,
|
||||
},
|
||||
FilenameOptions: resource.FilenameOptions{
|
||||
Filenames: []string{"../../../../test/e2e/testing-manifests/statefulset/cassandra/controller.yaml"}},
|
||||
Out: buf,
|
||||
Local: true}
|
||||
Local: true,
|
||||
}
|
||||
err := opts.Complete(tf, cmd, []string{"env=prod"})
|
||||
if err == nil {
|
||||
err = opts.RunEnv(tf)
|
||||
@@ -100,16 +112,26 @@ func TestSetMultiResourcesEnvLocal(t *testing.T) {
|
||||
tf.Namespace = "test"
|
||||
tf.ClientConfigVal = &restclient.Config{ContentConfig: restclient.ContentConfig{GroupVersion: &schema.GroupVersion{Version: ""}}}
|
||||
|
||||
outputFormat := "name"
|
||||
|
||||
buf := bytes.NewBuffer([]byte{})
|
||||
cmd := NewCmdEnv(tf, os.Stdin, buf, buf)
|
||||
cmd.SetOutput(buf)
|
||||
cmd.Flags().Set("output", "name")
|
||||
cmd.Flags().Set("output", outputFormat)
|
||||
cmd.Flags().Set("local", "true")
|
||||
|
||||
opts := EnvOptions{FilenameOptions: resource.FilenameOptions{
|
||||
Filenames: []string{"../../../../test/fixtures/pkg/kubectl/cmd/set/multi-resource-yaml.yaml"}},
|
||||
opts := EnvOptions{
|
||||
PrintFlags: &printers.PrintFlags{
|
||||
JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(),
|
||||
NamePrintFlags: printers.NewNamePrintFlags("", false),
|
||||
|
||||
OutputFormat: &outputFormat,
|
||||
},
|
||||
FilenameOptions: resource.FilenameOptions{
|
||||
Filenames: []string{"../../../../test/fixtures/pkg/kubectl/cmd/set/multi-resource-yaml.yaml"}},
|
||||
Out: buf,
|
||||
Local: true}
|
||||
Local: true,
|
||||
}
|
||||
err := opts.Complete(tf, cmd, []string{"env=prod"})
|
||||
if err == nil {
|
||||
err = opts.RunEnv(tf)
|
||||
@@ -482,11 +504,20 @@ func TestSetEnvRemote(t *testing.T) {
|
||||
}),
|
||||
VersionedAPIPath: path.Join(input.apiPrefix, testapi.Default.GroupVersion().String()),
|
||||
}
|
||||
|
||||
outputFormat := "yaml"
|
||||
|
||||
out := new(bytes.Buffer)
|
||||
cmd := NewCmdEnv(tf, out, out, out)
|
||||
cmd.SetOutput(out)
|
||||
cmd.Flags().Set("output", "yaml")
|
||||
cmd.Flags().Set("output", outputFormat)
|
||||
opts := EnvOptions{
|
||||
PrintFlags: &printers.PrintFlags{
|
||||
JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(),
|
||||
NamePrintFlags: printers.NewNamePrintFlags("", false),
|
||||
|
||||
OutputFormat: &outputFormat,
|
||||
},
|
||||
Out: out,
|
||||
Local: false}
|
||||
err := opts.Complete(tf, cmd, input.args)
|
||||
|
||||
Reference in New Issue
Block a user