mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-28 14:07:14 +00:00
Merge pull request #3805 from mnagy/allow_multiple_files_in_update
Use new resource builder in kubectl update
This commit is contained in:
commit
61c4a04ad2
@ -253,7 +253,7 @@ Usage:
|
|||||||
--client-key="": Path to a client key file for TLS.
|
--client-key="": Path to a client key file for TLS.
|
||||||
--cluster="": The name of the kubeconfig cluster to use
|
--cluster="": The name of the kubeconfig cluster to use
|
||||||
--context="": The name of the kubeconfig context to use
|
--context="": The name of the kubeconfig context to use
|
||||||
-f, --filename="": Filename or URL to file to use to update the resource
|
-f, --filename=[]: Filename, directory, or URL to file to use to update the resource
|
||||||
-h, --help=false: help for update
|
-h, --help=false: help for update
|
||||||
--insecure-skip-tls-verify=false: If true, the server's certificate will not be checked for validity. This will make your HTTPS connections insecure.
|
--insecure-skip-tls-verify=false: If true, the server's certificate will not be checked for validity. This will make your HTTPS connections insecure.
|
||||||
--kubeconfig="": Path to the kubeconfig file to use for CLI requests.
|
--kubeconfig="": Path to the kubeconfig file to use for CLI requests.
|
||||||
|
@ -21,10 +21,14 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/resource"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/resource"
|
||||||
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (f *Factory) NewCmdUpdate(out io.Writer) *cobra.Command {
|
func (f *Factory) NewCmdUpdate(out io.Writer) *cobra.Command {
|
||||||
|
flags := &struct {
|
||||||
|
Filenames util.StringList
|
||||||
|
}{}
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "update -f filename",
|
Use: "update -f filename",
|
||||||
Short: "Update a resource by filename or stdin",
|
Short: "Update a resource by filename or stdin",
|
||||||
@ -42,25 +46,52 @@ Examples:
|
|||||||
$ kubectl update pods my-pod --patch='{ "apiVersion": "v1beta1", "desiredState": { "manifest": [{ "cpu": 100 }]}}'
|
$ kubectl update pods my-pod --patch='{ "apiVersion": "v1beta1", "desiredState": { "manifest": [{ "cpu": 100 }]}}'
|
||||||
<update a pod by downloading it, applying the patch, then updating, requires apiVersion be specified>`,
|
<update a pod by downloading it, applying the patch, then updating, requires apiVersion be specified>`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
filename := GetFlagString(cmd, "filename")
|
schema, err := f.Validator(cmd)
|
||||||
|
checkErr(err)
|
||||||
|
|
||||||
|
cmdNamespace, err := f.DefaultNamespace(cmd)
|
||||||
|
checkErr(err)
|
||||||
|
|
||||||
|
mapper, typer := f.Object(cmd)
|
||||||
|
r := resource.NewBuilder(mapper, typer, ClientMapperForCommand(cmd, f)).
|
||||||
|
ContinueOnError().
|
||||||
|
NamespaceParam(cmdNamespace).RequireNamespace().
|
||||||
|
FilenameParam(flags.Filenames...).
|
||||||
|
Flatten().
|
||||||
|
Do()
|
||||||
|
|
||||||
patch := GetFlagString(cmd, "patch")
|
patch := GetFlagString(cmd, "patch")
|
||||||
if len(filename) == 0 && len(patch) == 0 {
|
if len(flags.Filenames) == 0 && len(patch) == 0 {
|
||||||
usageError(cmd, "Must specify --filename or --patch to update")
|
usageError(cmd, "Must specify --filename or --patch to update")
|
||||||
}
|
}
|
||||||
if len(filename) != 0 && len(patch) != 0 {
|
if len(flags.Filenames) != 0 && len(patch) != 0 {
|
||||||
usageError(cmd, "Can not specify both --filename and --patch")
|
usageError(cmd, "Can not specify both --filename and --patch")
|
||||||
}
|
}
|
||||||
var name string
|
if len(flags.Filenames) > 0 {
|
||||||
if len(filename) > 0 {
|
err := r.Visit(func(info *resource.Info) error {
|
||||||
name = updateWithFile(cmd, f, filename)
|
data, err := info.Mapping.Codec.Encode(info.Object)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := schema.ValidateBytes(data); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := resource.NewHelper(info.Client, info.Mapping).
|
||||||
|
Update(info.Namespace, info.Name, true, data); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fmt.Fprintf(out, "%s\n", info.Name)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
checkErr(err)
|
||||||
} else {
|
} else {
|
||||||
name = updateWithPatch(cmd, args, f, patch)
|
name := updateWithPatch(cmd, args, f, patch)
|
||||||
|
fmt.Fprintf(out, "%s\n", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Fprintf(out, "%s\n", name)
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
cmd.Flags().StringP("filename", "f", "", "Filename or URL to file to use to update the resource")
|
cmd.Flags().VarP(&flags.Filenames, "filename", "f", "Filename, directory, or URL to file to use to update the resource")
|
||||||
cmd.Flags().String("patch", "", "A JSON document to override the existing resource. The resource is downloaded, then patched with the JSON, the updated")
|
cmd.Flags().String("patch", "", "A JSON document to override the existing resource. The resource is downloaded, then patched with the JSON, the updated")
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
@ -87,28 +118,3 @@ func updateWithPatch(cmd *cobra.Command, args []string, f *Factory, patch string
|
|||||||
checkErr(err)
|
checkErr(err)
|
||||||
return name
|
return name
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateWithFile(cmd *cobra.Command, f *Factory, filename string) string {
|
|
||||||
schema, err := f.Validator(cmd)
|
|
||||||
checkErr(err)
|
|
||||||
mapper, typer := f.Object(cmd)
|
|
||||||
|
|
||||||
clientConfig, err := f.ClientConfig(cmd)
|
|
||||||
checkErr(err)
|
|
||||||
cmdApiVersion := clientConfig.Version
|
|
||||||
|
|
||||||
mapping, namespace, name, data := ResourceFromFile(filename, typer, mapper, schema, cmdApiVersion)
|
|
||||||
|
|
||||||
client, err := f.RESTClient(cmd, mapping)
|
|
||||||
checkErr(err)
|
|
||||||
|
|
||||||
cmdNamespace, err := f.DefaultNamespace(cmd)
|
|
||||||
checkErr(err)
|
|
||||||
err = CompareNamespace(cmdNamespace, namespace)
|
|
||||||
checkErr(err)
|
|
||||||
|
|
||||||
err = resource.NewHelper(client, mapping).Update(namespace, name, true, data)
|
|
||||||
checkErr(err)
|
|
||||||
|
|
||||||
return name
|
|
||||||
}
|
|
||||||
|
145
pkg/kubectl/cmd/update_test.go
Normal file
145
pkg/kubectl/cmd/update_test.go
Normal file
@ -0,0 +1,145 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2015 Google Inc. All rights reserved.
|
||||||
|
|
||||||
|
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 cmd_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||||
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
|
||||||
|
)
|
||||||
|
|
||||||
|
func rcTestData() *api.ReplicationControllerList {
|
||||||
|
rc := &api.ReplicationControllerList{
|
||||||
|
ListMeta: api.ListMeta{
|
||||||
|
ResourceVersion: "17",
|
||||||
|
},
|
||||||
|
Items: []api.ReplicationController{
|
||||||
|
{
|
||||||
|
ObjectMeta: api.ObjectMeta{Name: "qux", Namespace: "test", ResourceVersion: "13"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
return rc
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUpdateObject(t *testing.T) {
|
||||||
|
pods, _ := testData()
|
||||||
|
|
||||||
|
f, tf, codec := NewAPIFactory()
|
||||||
|
tf.Printer = &testPrinter{}
|
||||||
|
tf.Client = &client.FakeRESTClient{
|
||||||
|
Codec: codec,
|
||||||
|
Client: client.HTTPClientFunc(func(req *http.Request) (*http.Response, error) {
|
||||||
|
switch p, m := req.URL.Path, req.Method; {
|
||||||
|
case p == "/ns/test/pods/redis-master" && m == "GET":
|
||||||
|
return &http.Response{StatusCode: 200, Body: objBody(codec, &pods.Items[0])}, nil
|
||||||
|
case p == "/ns/test/pods/redis-master" && m == "PUT":
|
||||||
|
return &http.Response{StatusCode: 200, Body: objBody(codec, &pods.Items[0])}, nil
|
||||||
|
default:
|
||||||
|
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
tf.Namespace = "test"
|
||||||
|
buf := bytes.NewBuffer([]byte{})
|
||||||
|
|
||||||
|
cmd := f.NewCmdUpdate(buf)
|
||||||
|
cmd.Flags().Set("filename", "../../../examples/guestbook/redis-master.json")
|
||||||
|
cmd.Run(cmd, []string{})
|
||||||
|
|
||||||
|
// uses the name from the file, not the response
|
||||||
|
if buf.String() != "redis-master\n" {
|
||||||
|
t.Errorf("unexpected output: %s", buf.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUpdateMultipleObject(t *testing.T) {
|
||||||
|
pods, svc := testData()
|
||||||
|
|
||||||
|
f, tf, codec := NewAPIFactory()
|
||||||
|
tf.Printer = &testPrinter{}
|
||||||
|
tf.Client = &client.FakeRESTClient{
|
||||||
|
Codec: codec,
|
||||||
|
Client: client.HTTPClientFunc(func(req *http.Request) (*http.Response, error) {
|
||||||
|
switch p, m := req.URL.Path, req.Method; {
|
||||||
|
case p == "/ns/test/pods/redis-master" && m == "GET":
|
||||||
|
return &http.Response{StatusCode: 200, Body: objBody(codec, &pods.Items[0])}, nil
|
||||||
|
case p == "/ns/test/pods/redis-master" && m == "PUT":
|
||||||
|
return &http.Response{StatusCode: 200, Body: objBody(codec, &pods.Items[0])}, nil
|
||||||
|
|
||||||
|
case p == "/ns/test/services/frontend" && m == "GET":
|
||||||
|
return &http.Response{StatusCode: 200, Body: objBody(codec, &svc.Items[0])}, nil
|
||||||
|
case p == "/ns/test/services/frontend" && m == "PUT":
|
||||||
|
return &http.Response{StatusCode: 200, Body: objBody(codec, &svc.Items[0])}, nil
|
||||||
|
default:
|
||||||
|
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
tf.Namespace = "test"
|
||||||
|
buf := bytes.NewBuffer([]byte{})
|
||||||
|
|
||||||
|
cmd := f.NewCmdUpdate(buf)
|
||||||
|
cmd.Flags().Set("filename", "../../../examples/guestbook/redis-master.json")
|
||||||
|
cmd.Flags().Set("filename", "../../../examples/guestbook/frontend-service.json")
|
||||||
|
cmd.Run(cmd, []string{})
|
||||||
|
|
||||||
|
if buf.String() != "redis-master\nfrontend\n" {
|
||||||
|
t.Errorf("unexpected output: %s", buf.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUpdateDirectory(t *testing.T) {
|
||||||
|
pods, svc := testData()
|
||||||
|
rc := rcTestData()
|
||||||
|
|
||||||
|
f, tf, codec := NewAPIFactory()
|
||||||
|
tf.Printer = &testPrinter{}
|
||||||
|
tf.Client = &client.FakeRESTClient{
|
||||||
|
Codec: codec,
|
||||||
|
Client: client.HTTPClientFunc(func(req *http.Request) (*http.Response, error) {
|
||||||
|
switch p, m := req.URL.Path, req.Method; {
|
||||||
|
case strings.HasPrefix(p, "/ns/test/pods/") && (m == "GET" || m == "PUT"):
|
||||||
|
return &http.Response{StatusCode: 200, Body: objBody(codec, &pods.Items[0])}, nil
|
||||||
|
case strings.HasPrefix(p, "/ns/test/services/") && (m == "GET" || m == "PUT"):
|
||||||
|
return &http.Response{StatusCode: 200, Body: objBody(codec, &svc.Items[0])}, nil
|
||||||
|
case strings.HasPrefix(p, "/ns/test/replicationcontrollers/") && (m == "GET" || m == "PUT"):
|
||||||
|
return &http.Response{StatusCode: 200, Body: objBody(codec, &rc.Items[0])}, nil
|
||||||
|
default:
|
||||||
|
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
tf.Namespace = "test"
|
||||||
|
buf := bytes.NewBuffer([]byte{})
|
||||||
|
|
||||||
|
cmd := f.NewCmdUpdate(buf)
|
||||||
|
cmd.Flags().Set("filename", "../../../examples/guestbook")
|
||||||
|
cmd.Flags().Set("namespace", "test")
|
||||||
|
cmd.Run(cmd, []string{})
|
||||||
|
|
||||||
|
if buf.String() != "frontend-controller\nfrontend\nredis-master\nredis-master\nredis-slave-controller\nredisslave\n" {
|
||||||
|
t.Errorf("unexpected output: %s", buf.String())
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user