update set selector to use resource builder flags

pull/8/head
David Eads 2018-05-29 11:18:50 -04:00
parent 9c5bdd4b5c
commit 4b836d77d5
4 changed files with 82 additions and 92 deletions

View File

@ -68,6 +68,7 @@ go_test(
"//pkg/kubectl/cmd/testing: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/genericclioptions/resource:go_default_library",
"//pkg/kubectl/scheme:go_default_library",
"//vendor/github.com/spf13/cobra:go_default_library",

View File

@ -23,7 +23,6 @@ import (
"github.com/spf13/cobra"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
@ -40,26 +39,23 @@ import (
// SelectorOptions is the start of the data required to perform the operation. As new fields are added, add them here instead of
// referencing the cmd.Flags()
type SetSelectorOptions struct {
fileOptions resource.FilenameOptions
PrintFlags *genericclioptions.PrintFlags
RecordFlags *genericclioptions.RecordFlags
local bool
dryrun bool
all bool
output string
// Bound
ResourceBuilderFlags *genericclioptions.ResourceBuilderFlags
PrintFlags *genericclioptions.PrintFlags
RecordFlags *genericclioptions.RecordFlags
dryrun bool
// set by args
resources []string
selector *metav1.LabelSelector
ClientForMapping func(mapping *meta.RESTMapping) (resource.RESTClient, error)
PrintObj printers.ResourcePrinterFunc
Recorder genericclioptions.Recorder
builder *resource.Builder
// computed
WriteToServer bool
PrintObj printers.ResourcePrinterFunc
Recorder genericclioptions.Recorder
ResourceFinder genericclioptions.ResourceFinder
// set at initialization
genericclioptions.IOStreams
}
@ -79,6 +75,12 @@ var (
func NewSelectorOptions(streams genericclioptions.IOStreams) *SetSelectorOptions {
return &SetSelectorOptions{
ResourceBuilderFlags: genericclioptions.NewResourceBuilderFlags().
WithScheme(scheme.Scheme).
WithAll(false).
WithLocal(false).
WithUninitialized(false).
WithLatest(),
PrintFlags: genericclioptions.NewPrintFlags("selector updated").WithTypeSetter(scheme.Scheme),
RecordFlags: genericclioptions.NewRecordFlags(),
@ -105,16 +107,12 @@ func NewCmdSelector(f cmdutil.Factory, streams genericclioptions.IOStreams) *cob
},
}
o.ResourceBuilderFlags.AddFlags(cmd.Flags())
o.PrintFlags.AddFlags(cmd)
o.RecordFlags.AddFlags(cmd)
cmd.Flags().BoolVar(&o.all, "all", o.all, "Select all resources, including uninitialized ones, in the namespace of the specified resource types")
cmd.Flags().BoolVar(&o.local, "local", o.local, "If true, set selector will NOT contact api-server but run locally.")
cmd.Flags().String("resource-version", "", "If non-empty, the selectors update will only succeed if this is the current resource-version for the object. Only valid when specifying a single resource.")
usage := "the resource to update the selectors"
cmdutil.AddFilenameOptionFlags(cmd, &o.fileOptions, usage)
cmdutil.AddDryRunFlag(cmd)
cmdutil.AddIncludeUninitializedFlag(cmd)
return cmd
}
@ -130,40 +128,14 @@ func (o *SetSelectorOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, arg
}
o.dryrun = cmdutil.GetDryRunFlag(cmd)
o.output = cmdutil.GetFlagString(cmd, "output")
cmdNamespace, enforceNamespace, err := f.ToRawKubeConfigLoader().Namespace()
if err != nil {
return err
}
o.resources, o.selector, err = getResourcesAndSelector(args)
if err != nil {
return err
}
includeUninitialized := cmdutil.ShouldIncludeUninitialized(cmd, false)
o.builder = f.NewBuilder().
WithScheme(scheme.Scheme, scheme.Scheme.PrioritizedVersionsAllGroups()...).
LocalParam(o.local).
ContinueOnError().
NamespaceParam(cmdNamespace).DefaultNamespace().
FilenameParam(enforceNamespace, &o.fileOptions).
IncludeUninitialized(includeUninitialized).
Flatten()
if !o.local {
o.builder.
ResourceTypeOrNameArgs(o.all, o.resources...).
Latest()
} else {
// if a --local flag was provided, and a resource was specified in the form
// <resource>/<name>, fail immediately as --local cannot query the api server
// for the specified resource.
if len(o.resources) > 0 {
return resource.LocalResourceError
}
}
o.ResourceFinder = o.ResourceBuilderFlags.ToBuilder(f, o.resources)
o.WriteToServer = !(*o.ResourceBuilderFlags.Local || o.dryrun)
if o.dryrun {
o.PrintFlags.Complete("%s (dry run)")
@ -174,17 +146,11 @@ func (o *SetSelectorOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, arg
}
o.PrintObj = printer.PrintObj
o.ClientForMapping = func(mapping *meta.RESTMapping) (resource.RESTClient, error) {
return f.ClientForMapping(mapping)
}
return err
}
// Validate basic inputs
func (o *SetSelectorOptions) Validate() error {
if len(o.resources) < 1 && cmdutil.IsFilenameSliceEmpty(o.fileOptions.Filenames) {
return fmt.Errorf("one or more resources must be specified as <resource> <name> or <resource>/<name>")
}
if o.selector == nil {
return fmt.Errorf("one selector is required")
}
@ -193,11 +159,7 @@ func (o *SetSelectorOptions) Validate() error {
// RunSelector executes the command.
func (o *SetSelectorOptions) RunSelector() error {
r := o.builder.Do()
err := r.Err()
if err != nil {
return err
}
r := o.ResourceFinder.Do()
return r.Visit(func(info *resource.Info, err error) error {
patch := &Patch{Info: info}
@ -218,7 +180,7 @@ func (o *SetSelectorOptions) RunSelector() error {
if patch.Err != nil {
return patch.Err
}
if o.local || o.dryrun {
if !o.WriteToServer {
return o.PrintObj(info.Object, o.Out)
}

View File

@ -17,7 +17,6 @@ limitations under the License.
package set
import (
"net/http"
"reflect"
"strings"
"testing"
@ -28,13 +27,9 @@ import (
extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/rest/fake"
cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
"k8s.io/kubernetes/pkg/kubectl/scheme"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/resource"
)
func TestUpdateSelectorForObjectTypes(t *testing.T) {
@ -317,27 +312,30 @@ func TestGetResourcesAndSelector(t *testing.T) {
}
func TestSelectorTest(t *testing.T) {
tf := cmdtesting.NewTestFactory().WithNamespace("test")
defer tf.Cleanup()
tf.Client = &fake.RESTClient{
GroupVersion: schema.GroupVersion{Version: ""},
NegotiatedSerializer: serializer.DirectCodecFactory{CodecFactory: scheme.Codecs},
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
t.Fatalf("unexpected request: %s %#v\n%#v", req.Method, req.URL, req)
return nil, nil
}),
info := &resource.Info{
Object: &v1.Service{
TypeMeta: metav1.TypeMeta{APIVersion: "v1", Kind: "Service"},
ObjectMeta: metav1.ObjectMeta{Namespace: "some-ns", Name: "cassandra"},
},
}
tf.ClientConfigVal = &restclient.Config{ContentConfig: restclient.ContentConfig{GroupVersion: &schema.GroupVersion{Version: ""}}}
streams, _, buf, _ := genericclioptions.NewTestIOStreams()
cmd := NewCmdSelector(tf, streams)
cmd.Flags().Set("output", "name")
cmd.Flags().Set("local", "true")
cmd.Flags().Set("filename", "../../../../test/e2e/testing-manifests/statefulset/cassandra/service.yaml")
labelToSet, err := metav1.ParseToLabelSelector("environment=qa")
if err != nil {
t.Fatal(err)
}
cmd.Run(cmd, []string{"environment=qa"})
iostreams, _, buf, _ := genericclioptions.NewTestIOStreams()
o := &SetSelectorOptions{
selector: labelToSet,
ResourceFinder: genericclioptions.NewSimpleFakeResourceFinder(info),
Recorder: genericclioptions.NoopRecorder{},
PrintObj: (&printers.NamePrinter{}).PrintObj,
IOStreams: iostreams,
}
if err := o.RunSelector(); err != nil {
t.Fatal(err)
}
if !strings.Contains(buf.String(), "service/cassandra") {
t.Errorf("did not set selector: %s", buf.String())
}

View File

@ -27,14 +27,16 @@ import (
type ResourceBuilderFlags struct {
FileNameFlags *FileNameFlags
LabelSelector *string
FieldSelector *string
AllNamespaces *bool
All *bool
Local *bool
LabelSelector *string
FieldSelector *string
AllNamespaces *bool
All *bool
Local *bool
IncludeUninitialized *bool
Scheme *runtime.Scheme
Latest bool
Scheme *runtime.Scheme
Latest bool
StopOnFirstError bool
}
// NewResourceBuilderFlags returns a default ResourceBuilderFlags
@ -85,6 +87,12 @@ func (o *ResourceBuilderFlags) WithLocal(defaultVal bool) *ResourceBuilderFlags
return o
}
// WithUninitialized is using an alpha feature and may be dropped
func (o *ResourceBuilderFlags) WithUninitialized(defaultVal bool) *ResourceBuilderFlags {
o.IncludeUninitialized = &defaultVal
return o
}
func (o *ResourceBuilderFlags) WithScheme(scheme *runtime.Scheme) *ResourceBuilderFlags {
o.Scheme = scheme
return o
@ -95,6 +103,11 @@ func (o *ResourceBuilderFlags) WithLatest() *ResourceBuilderFlags {
return o
}
func (o *ResourceBuilderFlags) StopOnError() *ResourceBuilderFlags {
o.StopOnFirstError = true
return o
}
// AddFlags registers flags for finding resources
func (o *ResourceBuilderFlags) AddFlags(flagset *pflag.FlagSet) {
o.FileNameFlags.AddFlags(flagset)
@ -114,6 +127,9 @@ func (o *ResourceBuilderFlags) AddFlags(flagset *pflag.FlagSet) {
if o.Local != nil {
flagset.BoolVar(o.Local, "local", *o.Local, "If true, annotation will NOT contact api-server but run locally.")
}
if o.IncludeUninitialized != nil {
flagset.BoolVar(o.IncludeUninitialized, "include-uninitialized", *o.IncludeUninitialized, `If true, the kubectl command applies to uninitialized objects. If explicitly set to false, this flag overrides other flags that make the kubectl commands apply to uninitialized objects, e.g., "--all". Objects with empty metadata.initializers are regarded as initialized.`)
}
}
// ToBuilder gives you back a resource finder to visit resources that are located
@ -153,8 +169,21 @@ func (o *ResourceBuilderFlags) ToBuilder(restClientGetter RESTClientGetter, reso
if o.Latest {
builder.Latest()
}
} else {
builder.Local()
if len(resources) > 0 {
builder.AddError(resource.LocalResourceError)
}
}
if o.IncludeUninitialized != nil {
builder.IncludeUninitialized(*o.IncludeUninitialized)
}
if !o.StopOnFirstError {
builder.ContinueOnError()
}
return &ResourceFindBuilderWrapper{