mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 19:56:01 +00:00
fix issue(#52994)kubectl set resource can not update multi resource in local
This commit is contained in:
parent
2ea306a854
commit
dd9ab89e31
@ -428,7 +428,10 @@ func (o *EnvOptions) RunEnv(f cmdutil.Factory) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(o.Output) > 0 {
|
if len(o.Output) > 0 {
|
||||||
return o.PrintObject(o.Cmd, o.Local, o.Mapper, obj, o.Out)
|
if err := o.PrintObject(o.Cmd, o.Local, o.Mapper, obj, o.Out); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
cmdutil.PrintSuccess(o.Mapper, o.ShortOutput, o.Out, info.Mapping.Resource, info.Name, false, "env updated")
|
cmdutil.PrintSuccess(o.Mapper, o.ShortOutput, o.Out, info.Mapping.Resource, info.Name, false, "env updated")
|
||||||
|
@ -69,3 +69,42 @@ func TestSetEnvLocal(t *testing.T) {
|
|||||||
t.Errorf("did not set env: %s", buf.String())
|
t.Errorf("did not set env: %s", buf.String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSetMultiResourcesEnvLocal(t *testing.T) {
|
||||||
|
f, tf, codec, ns := cmdtesting.NewAPIFactory()
|
||||||
|
tf.Client = &fake.RESTClient{
|
||||||
|
GroupVersion: legacyscheme.Registry.GroupOrDie(api.GroupName).GroupVersion,
|
||||||
|
NegotiatedSerializer: ns,
|
||||||
|
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.Namespace = "test"
|
||||||
|
tf.ClientConfig = &restclient.Config{ContentConfig: restclient.ContentConfig{GroupVersion: &legacyscheme.Registry.GroupOrDie(api.GroupName).GroupVersion}}
|
||||||
|
|
||||||
|
buf := bytes.NewBuffer([]byte{})
|
||||||
|
cmd := NewCmdEnv(f, os.Stdin, buf, buf)
|
||||||
|
cmd.SetOutput(buf)
|
||||||
|
cmd.Flags().Set("output", "name")
|
||||||
|
cmd.Flags().Set("local", "true")
|
||||||
|
mapper, typer := f.Object()
|
||||||
|
tf.Printer = &printers.NamePrinter{Decoders: []runtime.Decoder{codec}, Typer: typer, Mapper: mapper}
|
||||||
|
|
||||||
|
opts := EnvOptions{FilenameOptions: resource.FilenameOptions{
|
||||||
|
Filenames: []string{"../../../../test/fixtures/pkg/kubectl/cmd/set/multi-resource-yaml.yaml"}},
|
||||||
|
Out: buf,
|
||||||
|
Local: true}
|
||||||
|
err := opts.Complete(f, cmd, []string{"env=prod"})
|
||||||
|
if err == nil {
|
||||||
|
err = opts.RunEnv(f)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
expectedOut := "replicationcontrollers/first-rc\nreplicationcontrollers/second-rc\n"
|
||||||
|
if buf.String() != expectedOut {
|
||||||
|
t.Errorf("expected out:\n%s\nbut got:\n%s", expectedOut, buf.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -134,3 +134,44 @@ func TestSetImageValidation(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSetMultiResourcesImageLocal(t *testing.T) {
|
||||||
|
f, tf, codec, ns := cmdtesting.NewAPIFactory()
|
||||||
|
tf.Client = &fake.RESTClient{
|
||||||
|
GroupVersion: legacyscheme.Registry.GroupOrDie(api.GroupName).GroupVersion,
|
||||||
|
NegotiatedSerializer: ns,
|
||||||
|
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.Namespace = "test"
|
||||||
|
tf.ClientConfig = &restclient.Config{ContentConfig: restclient.ContentConfig{GroupVersion: &legacyscheme.Registry.GroupOrDie(api.GroupName).GroupVersion}}
|
||||||
|
|
||||||
|
buf := bytes.NewBuffer([]byte{})
|
||||||
|
cmd := NewCmdImage(f, buf, buf)
|
||||||
|
cmd.SetOutput(buf)
|
||||||
|
cmd.Flags().Set("output", "name")
|
||||||
|
cmd.Flags().Set("local", "true")
|
||||||
|
mapper, typer := f.Object()
|
||||||
|
tf.Printer = &printers.NamePrinter{Decoders: []runtime.Decoder{codec}, Typer: typer, Mapper: mapper}
|
||||||
|
|
||||||
|
opts := ImageOptions{FilenameOptions: resource.FilenameOptions{
|
||||||
|
Filenames: []string{"../../../../test/fixtures/pkg/kubectl/cmd/set/multi-resource-yaml.yaml"}},
|
||||||
|
Out: buf,
|
||||||
|
Local: true}
|
||||||
|
err := opts.Complete(f, cmd, []string{"*=thingy"})
|
||||||
|
if err == nil {
|
||||||
|
err = opts.Validate()
|
||||||
|
}
|
||||||
|
if err == nil {
|
||||||
|
err = opts.Run()
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
expectedOut := "replicationcontrollers/first-rc\nreplicationcontrollers/second-rc\n"
|
||||||
|
if buf.String() != expectedOut {
|
||||||
|
t.Errorf("expected out:\n%s\nbut got:\n%s", expectedOut, buf.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -238,7 +238,10 @@ func (o *ResourcesOptions) Run() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if o.Local || cmdutil.GetDryRunFlag(o.Cmd) {
|
if o.Local || cmdutil.GetDryRunFlag(o.Cmd) {
|
||||||
return o.PrintObject(o.Cmd, o.Local, o.Mapper, info.Object, o.Out)
|
if err := o.PrintObject(o.Cmd, o.Local, o.Mapper, info.Object, o.Out); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
obj, err := resource.NewHelper(info.Client, info.Mapping).Patch(info.Namespace, info.Name, types.StrategicMergePatchType, patch.Patch)
|
obj, err := resource.NewHelper(info.Client, info.Mapping).Patch(info.Namespace, info.Name, types.StrategicMergePatchType, patch.Patch)
|
||||||
|
@ -75,3 +75,48 @@ func TestResourcesLocal(t *testing.T) {
|
|||||||
t.Errorf("did not set resources: %s", buf.String())
|
t.Errorf("did not set resources: %s", buf.String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSetMultiResourcesLimitsLocal(t *testing.T) {
|
||||||
|
f, tf, codec, ns := cmdtesting.NewAPIFactory()
|
||||||
|
tf.Client = &fake.RESTClient{
|
||||||
|
GroupVersion: legacyscheme.Registry.GroupOrDie(api.GroupName).GroupVersion,
|
||||||
|
NegotiatedSerializer: ns,
|
||||||
|
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.Namespace = "test"
|
||||||
|
tf.ClientConfig = &restclient.Config{ContentConfig: restclient.ContentConfig{GroupVersion: &legacyscheme.Registry.GroupOrDie(api.GroupName).GroupVersion}}
|
||||||
|
|
||||||
|
buf := bytes.NewBuffer([]byte{})
|
||||||
|
cmd := NewCmdResources(f, buf, buf)
|
||||||
|
cmd.SetOutput(buf)
|
||||||
|
cmd.Flags().Set("output", "name")
|
||||||
|
cmd.Flags().Set("local", "true")
|
||||||
|
mapper, typer := f.Object()
|
||||||
|
tf.Printer = &printers.NamePrinter{Decoders: []runtime.Decoder{codec}, Typer: typer, Mapper: mapper}
|
||||||
|
|
||||||
|
opts := ResourcesOptions{FilenameOptions: resource.FilenameOptions{
|
||||||
|
Filenames: []string{"../../../../test/fixtures/pkg/kubectl/cmd/set/multi-resource-yaml.yaml"}},
|
||||||
|
Out: buf,
|
||||||
|
Local: true,
|
||||||
|
Limits: "cpu=200m,memory=512Mi",
|
||||||
|
Requests: "cpu=200m,memory=512Mi",
|
||||||
|
ContainerSelector: "*"}
|
||||||
|
|
||||||
|
err := opts.Complete(f, cmd, []string{})
|
||||||
|
if err == nil {
|
||||||
|
err = opts.Validate()
|
||||||
|
}
|
||||||
|
if err == nil {
|
||||||
|
err = opts.Run()
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
expectedOut := "replicationcontrollers/first-rc\nreplicationcontrollers/second-rc\n"
|
||||||
|
if buf.String() != expectedOut {
|
||||||
|
t.Errorf("expected out:\n%s\nbut got:\n%s", expectedOut, buf.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -241,7 +241,10 @@ func (o *SubjectOptions) Run(f cmdutil.Factory, fn updateSubjects) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if o.Local || o.DryRun {
|
if o.Local || o.DryRun {
|
||||||
return o.PrintObject(o.Mapper, info.Object, o.Out)
|
if err := o.PrintObject(o.Mapper, info.Object, o.Out); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
obj, err := resource.NewHelper(info.Client, info.Mapping).Patch(info.Namespace, info.Name, types.StrategicMergePatchType, patch.Patch)
|
obj, err := resource.NewHelper(info.Client, info.Mapping).Patch(info.Namespace, info.Name, types.StrategicMergePatchType, patch.Patch)
|
||||||
|
33
test/fixtures/pkg/kubectl/cmd/set/multi-resource-yaml.yaml
vendored
Normal file
33
test/fixtures/pkg/kubectl/cmd/set/multi-resource-yaml.yaml
vendored
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
apiVersion: v1
|
||||||
|
kind: ReplicationController
|
||||||
|
metadata:
|
||||||
|
name: first-rc
|
||||||
|
spec:
|
||||||
|
replicas: 1
|
||||||
|
selector:
|
||||||
|
app: mock
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: mock
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- name: mock-container
|
||||||
|
image: gcr.io/google-containers/pause:2.0
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
kind: ReplicationController
|
||||||
|
metadata:
|
||||||
|
name: second-rc
|
||||||
|
spec:
|
||||||
|
replicas: 1
|
||||||
|
selector:
|
||||||
|
app: mock
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: mock
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- name: mock-container
|
||||||
|
image: gcr.io/google-containers/pause:2.0
|
Loading…
Reference in New Issue
Block a user