2015-06-25 22:56:30 +00:00
/ *
Copyright 2014 The Kubernetes Authors 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 (
2015-08-06 09:24:02 +00:00
"fmt"
2015-06-25 22:56:30 +00:00
"io"
"github.com/spf13/cobra"
2015-09-30 15:58:42 +00:00
2015-08-05 22:03:47 +00:00
"k8s.io/kubernetes/pkg/api"
2015-08-06 09:24:02 +00:00
"k8s.io/kubernetes/pkg/kubectl"
2015-08-05 22:03:47 +00:00
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/resource"
2015-09-30 15:58:42 +00:00
"k8s.io/kubernetes/pkg/util/yaml"
2015-06-25 22:56:30 +00:00
)
2015-08-14 18:46:43 +00:00
// PatchOptions 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 PatchOptions struct {
Filenames [ ] string
}
2015-06-25 22:56:30 +00:00
const (
patch_long = ` Update field ( s ) of a resource using strategic merge patch
2015-07-13 20:46:51 +00:00
JSON and YAML formats are accepted .
2015-11-17 01:13:49 +00:00
Please refer to the models in https : //htmlpreview.github.io/?https://github.com/kubernetes/kubernetes/blob/HEAD/docs/api-reference/v1/definitions.html to find if a field is mutable.`
2015-06-25 22:56:30 +00:00
patch_example = `
2015-08-12 16:50:09 +00:00
# Partially update a node using strategic merge patch
2015-07-13 20:46:51 +00:00
kubectl patch node k8s - node - 1 - p ' { "spec" : { "unschedulable" : true } } '
2015-08-06 09:24:02 +00:00
# Partially update a node identified by the type and name specified in "node.json" using strategic merge patch
kubectl patch - f node . json - p ' { "spec" : { "unschedulable" : true } } '
2015-08-12 16:50:09 +00:00
# Update a container ' s image ; spec . containers [ * ] . name is required because it ' s a merge key
2015-07-13 20:46:51 +00:00
kubectl patch pod valid - pod - p ' { "spec" : { "containers" : [ { "name" : "kubernetes-serve-hostname" , "image" : "new image" } ] } } ' `
2015-06-25 22:56:30 +00:00
)
func NewCmdPatch ( f * cmdutil . Factory , out io . Writer ) * cobra . Command {
2015-08-14 18:46:43 +00:00
options := & PatchOptions { }
2015-06-25 22:56:30 +00:00
cmd := & cobra . Command {
2015-08-06 09:24:02 +00:00
Use : "patch (-f FILENAME | TYPE NAME) -p PATCH" ,
2015-11-10 19:55:20 +00:00
Short : "Update field(s) of a resource using strategic merge patch." ,
2015-06-25 22:56:30 +00:00
Long : patch_long ,
Example : patch_example ,
Run : func ( cmd * cobra . Command , args [ ] string ) {
2015-07-01 22:47:43 +00:00
cmdutil . CheckErr ( cmdutil . ValidateOutputArgs ( cmd ) )
shortOutput := cmdutil . GetFlagString ( cmd , "output" ) == "name"
2015-08-14 18:46:43 +00:00
err := RunPatch ( f , out , cmd , args , shortOutput , options )
2015-07-31 23:43:39 +00:00
cmdutil . CheckErr ( err )
2015-06-25 22:56:30 +00:00
} ,
}
2015-07-13 16:36:12 +00:00
cmd . Flags ( ) . StringP ( "patch" , "p" , "" , "The patch to be applied to the resource JSON file." )
2015-06-25 22:56:30 +00:00
cmd . MarkFlagRequired ( "patch" )
2015-07-01 22:47:43 +00:00
cmdutil . AddOutputFlagsForMutation ( cmd )
2015-08-06 09:24:02 +00:00
usage := "Filename, directory, or URL to a file identifying the resource to update"
2015-08-14 18:46:43 +00:00
kubectl . AddJsonFilenameFlag ( cmd , & options . Filenames , usage )
2015-06-25 22:56:30 +00:00
return cmd
}
2015-08-14 18:46:43 +00:00
func RunPatch ( f * cmdutil . Factory , out io . Writer , cmd * cobra . Command , args [ ] string , shortOutput bool , options * PatchOptions ) error {
2015-08-06 09:24:02 +00:00
cmdNamespace , enforceNamespace , err := f . DefaultNamespace ( )
2015-06-25 22:56:30 +00:00
if err != nil {
return err
}
patch := cmdutil . GetFlagString ( cmd , "patch" )
if len ( patch ) == 0 {
return cmdutil . UsageError ( cmd , "Must specify -p to patch" )
}
2015-09-30 15:58:42 +00:00
patchBytes , err := yaml . ToJSON ( [ ] byte ( patch ) )
if err != nil {
return fmt . Errorf ( "unable to parse %q: %v" , patch , err )
}
2015-06-25 22:56:30 +00:00
mapper , typer := f . Object ( )
r := resource . NewBuilder ( mapper , typer , f . ClientMapperForCommand ( ) ) .
ContinueOnError ( ) .
NamespaceParam ( cmdNamespace ) . DefaultNamespace ( ) .
2015-08-14 18:46:43 +00:00
FilenameParam ( enforceNamespace , options . Filenames ... ) .
2015-06-25 22:56:30 +00:00
ResourceTypeOrNameArgs ( false , args ... ) .
Flatten ( ) .
Do ( )
err = r . Err ( )
if err != nil {
return err
}
2015-08-06 09:24:02 +00:00
infos , err := r . Infos ( )
2015-06-25 22:56:30 +00:00
if err != nil {
return err
}
2015-08-06 09:24:02 +00:00
if len ( infos ) > 1 {
return fmt . Errorf ( "multiple resources provided" )
2015-06-25 22:56:30 +00:00
}
2015-08-06 09:24:02 +00:00
info := infos [ 0 ]
name , namespace := info . Name , info . Namespace
mapping := info . ResourceMapping ( )
client , err := f . RESTClient ( mapping )
2015-06-25 22:56:30 +00:00
if err != nil {
return err
}
helper := resource . NewHelper ( client , mapping )
2015-09-30 15:58:42 +00:00
_ , err = helper . Patch ( namespace , name , api . StrategicMergePatchType , patchBytes )
2015-06-25 22:56:30 +00:00
if err != nil {
return err
}
2015-07-01 22:47:43 +00:00
cmdutil . PrintSuccess ( mapper , shortOutput , out , "" , name , "patched" )
2015-06-25 22:56:30 +00:00
return nil
}