mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-28 14:07:14 +00:00
update set selector to use resource builder flags
This commit is contained in:
parent
9c5bdd4b5c
commit
4b836d77d5
@ -68,6 +68,7 @@ go_test(
|
|||||||
"//pkg/kubectl/cmd/testing:go_default_library",
|
"//pkg/kubectl/cmd/testing:go_default_library",
|
||||||
"//pkg/kubectl/cmd/util:go_default_library",
|
"//pkg/kubectl/cmd/util:go_default_library",
|
||||||
"//pkg/kubectl/genericclioptions: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/genericclioptions/resource:go_default_library",
|
||||||
"//pkg/kubectl/scheme:go_default_library",
|
"//pkg/kubectl/scheme:go_default_library",
|
||||||
"//vendor/github.com/spf13/cobra:go_default_library",
|
"//vendor/github.com/spf13/cobra:go_default_library",
|
||||||
|
@ -23,7 +23,6 @@ import (
|
|||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"k8s.io/api/core/v1"
|
"k8s.io/api/core/v1"
|
||||||
"k8s.io/apimachinery/pkg/api/meta"
|
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/types"
|
"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
|
// 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()
|
// referencing the cmd.Flags()
|
||||||
type SetSelectorOptions struct {
|
type SetSelectorOptions struct {
|
||||||
fileOptions resource.FilenameOptions
|
// Bound
|
||||||
|
ResourceBuilderFlags *genericclioptions.ResourceBuilderFlags
|
||||||
PrintFlags *genericclioptions.PrintFlags
|
PrintFlags *genericclioptions.PrintFlags
|
||||||
RecordFlags *genericclioptions.RecordFlags
|
RecordFlags *genericclioptions.RecordFlags
|
||||||
|
dryrun bool
|
||||||
local bool
|
|
||||||
dryrun bool
|
|
||||||
all bool
|
|
||||||
output string
|
|
||||||
|
|
||||||
|
// set by args
|
||||||
resources []string
|
resources []string
|
||||||
selector *metav1.LabelSelector
|
selector *metav1.LabelSelector
|
||||||
|
|
||||||
ClientForMapping func(mapping *meta.RESTMapping) (resource.RESTClient, error)
|
// computed
|
||||||
|
WriteToServer bool
|
||||||
PrintObj printers.ResourcePrinterFunc
|
PrintObj printers.ResourcePrinterFunc
|
||||||
Recorder genericclioptions.Recorder
|
Recorder genericclioptions.Recorder
|
||||||
|
ResourceFinder genericclioptions.ResourceFinder
|
||||||
builder *resource.Builder
|
|
||||||
|
|
||||||
|
// set at initialization
|
||||||
genericclioptions.IOStreams
|
genericclioptions.IOStreams
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -79,6 +75,12 @@ var (
|
|||||||
|
|
||||||
func NewSelectorOptions(streams genericclioptions.IOStreams) *SetSelectorOptions {
|
func NewSelectorOptions(streams genericclioptions.IOStreams) *SetSelectorOptions {
|
||||||
return &SetSelectorOptions{
|
return &SetSelectorOptions{
|
||||||
|
ResourceBuilderFlags: genericclioptions.NewResourceBuilderFlags().
|
||||||
|
WithScheme(scheme.Scheme).
|
||||||
|
WithAll(false).
|
||||||
|
WithLocal(false).
|
||||||
|
WithUninitialized(false).
|
||||||
|
WithLatest(),
|
||||||
PrintFlags: genericclioptions.NewPrintFlags("selector updated").WithTypeSetter(scheme.Scheme),
|
PrintFlags: genericclioptions.NewPrintFlags("selector updated").WithTypeSetter(scheme.Scheme),
|
||||||
RecordFlags: genericclioptions.NewRecordFlags(),
|
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.PrintFlags.AddFlags(cmd)
|
||||||
o.RecordFlags.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.")
|
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.AddDryRunFlag(cmd)
|
||||||
cmdutil.AddIncludeUninitializedFlag(cmd)
|
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
@ -130,40 +128,14 @@ func (o *SetSelectorOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, arg
|
|||||||
}
|
}
|
||||||
|
|
||||||
o.dryrun = cmdutil.GetDryRunFlag(cmd)
|
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)
|
o.resources, o.selector, err = getResourcesAndSelector(args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
includeUninitialized := cmdutil.ShouldIncludeUninitialized(cmd, false)
|
o.ResourceFinder = o.ResourceBuilderFlags.ToBuilder(f, o.resources)
|
||||||
o.builder = f.NewBuilder().
|
o.WriteToServer = !(*o.ResourceBuilderFlags.Local || o.dryrun)
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if o.dryrun {
|
if o.dryrun {
|
||||||
o.PrintFlags.Complete("%s (dry run)")
|
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.PrintObj = printer.PrintObj
|
||||||
|
|
||||||
o.ClientForMapping = func(mapping *meta.RESTMapping) (resource.RESTClient, error) {
|
|
||||||
return f.ClientForMapping(mapping)
|
|
||||||
}
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate basic inputs
|
// Validate basic inputs
|
||||||
func (o *SetSelectorOptions) Validate() error {
|
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 {
|
if o.selector == nil {
|
||||||
return fmt.Errorf("one selector is required")
|
return fmt.Errorf("one selector is required")
|
||||||
}
|
}
|
||||||
@ -193,11 +159,7 @@ func (o *SetSelectorOptions) Validate() error {
|
|||||||
|
|
||||||
// RunSelector executes the command.
|
// RunSelector executes the command.
|
||||||
func (o *SetSelectorOptions) RunSelector() error {
|
func (o *SetSelectorOptions) RunSelector() error {
|
||||||
r := o.builder.Do()
|
r := o.ResourceFinder.Do()
|
||||||
err := r.Err()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return r.Visit(func(info *resource.Info, err error) error {
|
return r.Visit(func(info *resource.Info, err error) error {
|
||||||
patch := &Patch{Info: info}
|
patch := &Patch{Info: info}
|
||||||
@ -218,7 +180,7 @@ func (o *SetSelectorOptions) RunSelector() error {
|
|||||||
if patch.Err != nil {
|
if patch.Err != nil {
|
||||||
return patch.Err
|
return patch.Err
|
||||||
}
|
}
|
||||||
if o.local || o.dryrun {
|
if !o.WriteToServer {
|
||||||
return o.PrintObj(info.Object, o.Out)
|
return o.PrintObj(info.Object, o.Out)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,7 +17,6 @@ limitations under the License.
|
|||||||
package set
|
package set
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
@ -28,13 +27,9 @@ import (
|
|||||||
extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
|
extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"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/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) {
|
func TestUpdateSelectorForObjectTypes(t *testing.T) {
|
||||||
@ -317,27 +312,30 @@ func TestGetResourcesAndSelector(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSelectorTest(t *testing.T) {
|
func TestSelectorTest(t *testing.T) {
|
||||||
tf := cmdtesting.NewTestFactory().WithNamespace("test")
|
info := &resource.Info{
|
||||||
defer tf.Cleanup()
|
Object: &v1.Service{
|
||||||
|
TypeMeta: metav1.TypeMeta{APIVersion: "v1", Kind: "Service"},
|
||||||
tf.Client = &fake.RESTClient{
|
ObjectMeta: metav1.ObjectMeta{Namespace: "some-ns", Name: "cassandra"},
|
||||||
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
|
|
||||||
}),
|
|
||||||
}
|
}
|
||||||
tf.ClientConfigVal = &restclient.Config{ContentConfig: restclient.ContentConfig{GroupVersion: &schema.GroupVersion{Version: ""}}}
|
|
||||||
|
|
||||||
streams, _, buf, _ := genericclioptions.NewTestIOStreams()
|
labelToSet, err := metav1.ParseToLabelSelector("environment=qa")
|
||||||
cmd := NewCmdSelector(tf, streams)
|
if err != nil {
|
||||||
cmd.Flags().Set("output", "name")
|
t.Fatal(err)
|
||||||
cmd.Flags().Set("local", "true")
|
}
|
||||||
cmd.Flags().Set("filename", "../../../../test/e2e/testing-manifests/statefulset/cassandra/service.yaml")
|
|
||||||
|
|
||||||
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") {
|
if !strings.Contains(buf.String(), "service/cassandra") {
|
||||||
t.Errorf("did not set selector: %s", buf.String())
|
t.Errorf("did not set selector: %s", buf.String())
|
||||||
}
|
}
|
||||||
|
@ -27,14 +27,16 @@ import (
|
|||||||
type ResourceBuilderFlags struct {
|
type ResourceBuilderFlags struct {
|
||||||
FileNameFlags *FileNameFlags
|
FileNameFlags *FileNameFlags
|
||||||
|
|
||||||
LabelSelector *string
|
LabelSelector *string
|
||||||
FieldSelector *string
|
FieldSelector *string
|
||||||
AllNamespaces *bool
|
AllNamespaces *bool
|
||||||
All *bool
|
All *bool
|
||||||
Local *bool
|
Local *bool
|
||||||
|
IncludeUninitialized *bool
|
||||||
|
|
||||||
Scheme *runtime.Scheme
|
Scheme *runtime.Scheme
|
||||||
Latest bool
|
Latest bool
|
||||||
|
StopOnFirstError bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewResourceBuilderFlags returns a default ResourceBuilderFlags
|
// NewResourceBuilderFlags returns a default ResourceBuilderFlags
|
||||||
@ -85,6 +87,12 @@ func (o *ResourceBuilderFlags) WithLocal(defaultVal bool) *ResourceBuilderFlags
|
|||||||
return o
|
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 {
|
func (o *ResourceBuilderFlags) WithScheme(scheme *runtime.Scheme) *ResourceBuilderFlags {
|
||||||
o.Scheme = scheme
|
o.Scheme = scheme
|
||||||
return o
|
return o
|
||||||
@ -95,6 +103,11 @@ func (o *ResourceBuilderFlags) WithLatest() *ResourceBuilderFlags {
|
|||||||
return o
|
return o
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (o *ResourceBuilderFlags) StopOnError() *ResourceBuilderFlags {
|
||||||
|
o.StopOnFirstError = true
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
|
||||||
// AddFlags registers flags for finding resources
|
// AddFlags registers flags for finding resources
|
||||||
func (o *ResourceBuilderFlags) AddFlags(flagset *pflag.FlagSet) {
|
func (o *ResourceBuilderFlags) AddFlags(flagset *pflag.FlagSet) {
|
||||||
o.FileNameFlags.AddFlags(flagset)
|
o.FileNameFlags.AddFlags(flagset)
|
||||||
@ -114,6 +127,9 @@ func (o *ResourceBuilderFlags) AddFlags(flagset *pflag.FlagSet) {
|
|||||||
if o.Local != nil {
|
if o.Local != nil {
|
||||||
flagset.BoolVar(o.Local, "local", *o.Local, "If true, annotation will NOT contact api-server but run locally.")
|
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
|
// 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 {
|
if o.Latest {
|
||||||
builder.Latest()
|
builder.Latest()
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
builder.Local()
|
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{
|
return &ResourceFindBuilderWrapper{
|
||||||
|
Loading…
Reference in New Issue
Block a user