2014-10-06 01:24:19 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 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
|
|
|
|
|
|
|
|
import (
|
2014-10-27 02:21:31 +00:00
|
|
|
"fmt"
|
2014-10-06 01:24:19 +00:00
|
|
|
"io"
|
|
|
|
|
2015-02-05 00:14:48 +00:00
|
|
|
cmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
|
2014-12-28 04:49:51 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/resource"
|
2015-01-26 17:52:19 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
2014-10-06 01:24:19 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2014-10-27 02:21:31 +00:00
|
|
|
func (f *Factory) NewCmdUpdate(out io.Writer) *cobra.Command {
|
2015-01-26 17:52:19 +00:00
|
|
|
flags := &struct {
|
|
|
|
Filenames util.StringList
|
|
|
|
}{}
|
2014-10-06 01:24:19 +00:00
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "update -f filename",
|
2015-02-03 17:59:21 +00:00
|
|
|
Short: "Update a resource by filename or stdin.",
|
2014-10-06 01:24:19 +00:00
|
|
|
Long: `Update a resource by filename or stdin.
|
|
|
|
|
|
|
|
JSON and YAML formats are accepted.
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
2015-02-03 17:59:21 +00:00
|
|
|
// Update a pod using the data in pod.json.
|
2015-02-12 23:49:25 +00:00
|
|
|
$ kubectl update -f pod.json
|
2015-02-06 16:57:52 +00:00
|
|
|
|
2015-02-03 17:59:21 +00:00
|
|
|
// Update a pod based on the JSON passed into stdin.
|
2015-02-12 23:49:25 +00:00
|
|
|
$ cat pod.json | kubectl update -f -
|
2015-02-03 17:59:21 +00:00
|
|
|
|
2015-02-12 23:49:25 +00:00
|
|
|
// Update a pod by downloading it, applying the patch, then updating. Requires apiVersion be specified.
|
|
|
|
$ kubectl update pods my-pod --patch='{ "apiVersion": "v1beta1", "desiredState": { "manifest": [{ "cpu": 100 }]}}'`,
|
2014-10-06 01:24:19 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2015-01-26 17:52:19 +00:00
|
|
|
schema, err := f.Validator(cmd)
|
|
|
|
checkErr(err)
|
|
|
|
|
|
|
|
cmdNamespace, err := f.DefaultNamespace(cmd)
|
|
|
|
checkErr(err)
|
|
|
|
|
2015-02-05 00:14:48 +00:00
|
|
|
patch := cmdutil.GetFlagString(cmd, "patch")
|
2015-01-26 17:52:19 +00:00
|
|
|
if len(flags.Filenames) == 0 && len(patch) == 0 {
|
2015-01-15 21:55:53 +00:00
|
|
|
usageError(cmd, "Must specify --filename or --patch to update")
|
|
|
|
}
|
2015-01-26 17:52:19 +00:00
|
|
|
if len(flags.Filenames) != 0 && len(patch) != 0 {
|
2015-01-15 21:55:53 +00:00
|
|
|
usageError(cmd, "Can not specify both --filename and --patch")
|
|
|
|
}
|
2015-02-06 16:57:52 +00:00
|
|
|
|
|
|
|
// TODO: Make patching work with -f, updating with patched JSON input files
|
|
|
|
if len(flags.Filenames) == 0 {
|
2015-01-26 17:52:19 +00:00
|
|
|
name := updateWithPatch(cmd, args, f, patch)
|
|
|
|
fmt.Fprintf(out, "%s\n", name)
|
2015-02-06 16:57:52 +00:00
|
|
|
return
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
2015-01-02 18:08:37 +00:00
|
|
|
|
2015-02-15 06:36:32 +00:00
|
|
|
mapper, typer := f.Object(cmd)
|
|
|
|
r := resource.NewBuilder(mapper, typer, f.ClientMapperForCommand(cmd)).
|
|
|
|
ContinueOnError().
|
|
|
|
NamespaceParam(cmdNamespace).RequireNamespace().
|
|
|
|
FilenameParam(flags.Filenames...).
|
|
|
|
Flatten().
|
|
|
|
Do()
|
|
|
|
checkErr(r.Err())
|
|
|
|
|
2015-02-06 16:57:52 +00:00
|
|
|
err = r.Visit(func(info *resource.Info) error {
|
|
|
|
data, err := info.Mapping.Codec.Encode(info.Object)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := schema.ValidateBytes(data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
obj, err := resource.NewHelper(info.Client, info.Mapping).Update(info.Namespace, info.Name, true, data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
info.Refresh(obj, true)
|
|
|
|
fmt.Fprintf(out, "%s\n", info.Name)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
checkErr(err)
|
|
|
|
|
2014-10-06 01:24:19 +00:00
|
|
|
},
|
|
|
|
}
|
2015-02-03 17:59:21 +00:00
|
|
|
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, patched with the JSON, then updated.")
|
2014-10-06 01:24:19 +00:00
|
|
|
return cmd
|
|
|
|
}
|
2015-01-15 21:55:53 +00:00
|
|
|
|
|
|
|
func updateWithPatch(cmd *cobra.Command, args []string, f *Factory, patch string) string {
|
2015-01-02 18:08:37 +00:00
|
|
|
cmdNamespace, err := f.DefaultNamespace(cmd)
|
|
|
|
checkErr(err)
|
|
|
|
|
2015-01-15 21:55:53 +00:00
|
|
|
mapper, _ := f.Object(cmd)
|
2015-02-05 00:14:48 +00:00
|
|
|
mapping, namespace, name := cmdutil.ResourceFromArgs(cmd, args, mapper, cmdNamespace)
|
2015-01-15 21:55:53 +00:00
|
|
|
client, err := f.RESTClient(cmd, mapping)
|
|
|
|
checkErr(err)
|
|
|
|
|
|
|
|
helper := resource.NewHelper(client, mapping)
|
|
|
|
obj, err := helper.Get(namespace, name)
|
|
|
|
checkErr(err)
|
|
|
|
|
2015-01-31 18:59:08 +00:00
|
|
|
patchedObj, err := cmdutil.Merge(obj, patch, mapping.Kind)
|
|
|
|
checkErr(err)
|
2015-01-15 21:55:53 +00:00
|
|
|
|
2015-01-31 18:59:08 +00:00
|
|
|
data, err := helper.Codec.Encode(patchedObj)
|
2015-01-15 21:55:53 +00:00
|
|
|
checkErr(err)
|
|
|
|
|
2015-01-31 18:59:08 +00:00
|
|
|
_, err = helper.Update(namespace, name, true, data)
|
2015-01-15 21:55:53 +00:00
|
|
|
checkErr(err)
|
|
|
|
return name
|
|
|
|
}
|