apply.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package cmd
  14. import (
  15. "fmt"
  16. "io"
  17. "time"
  18. "github.com/jonboulle/clockwork"
  19. "github.com/renstrom/dedent"
  20. "github.com/spf13/cobra"
  21. "k8s.io/kubernetes/pkg/api"
  22. "k8s.io/kubernetes/pkg/api/errors"
  23. "k8s.io/kubernetes/pkg/api/meta"
  24. "k8s.io/kubernetes/pkg/kubectl"
  25. cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
  26. "k8s.io/kubernetes/pkg/kubectl/resource"
  27. "k8s.io/kubernetes/pkg/runtime"
  28. "k8s.io/kubernetes/pkg/util/strategicpatch"
  29. )
  30. // ApplyOptions stores cmd.Flag values for apply. As new fields are added,
  31. // add them here instead of referencing the cmd.Flags()
  32. type ApplyOptions struct {
  33. Filenames []string
  34. Recursive bool
  35. }
  36. const (
  37. // maxPatchRetry is the maximum number of conflicts retry for during a patch operation before returning failure
  38. maxPatchRetry = 5
  39. // backOffPeriod is the period to back off when apply patch resutls in error.
  40. backOffPeriod = 1 * time.Second
  41. // how many times we can retry before back off
  42. triesBeforeBackOff = 1
  43. )
  44. var (
  45. apply_long = dedent.Dedent(`
  46. Apply a configuration to a resource by filename or stdin.
  47. This resource will be created if it doesn't exist yet.
  48. To use 'apply', always create the resource initially with either 'apply' or 'create --save-config'.
  49. JSON and YAML formats are accepted.`)
  50. apply_example = dedent.Dedent(`
  51. # Apply the configuration in pod.json to a pod.
  52. kubectl apply -f ./pod.json
  53. # Apply the JSON passed into stdin to a pod.
  54. cat pod.json | kubectl apply -f -`)
  55. )
  56. func NewCmdApply(f *cmdutil.Factory, out io.Writer) *cobra.Command {
  57. options := &ApplyOptions{}
  58. cmd := &cobra.Command{
  59. Use: "apply -f FILENAME",
  60. Short: "Apply a configuration to a resource by filename or stdin",
  61. Long: apply_long,
  62. Example: apply_example,
  63. Run: func(cmd *cobra.Command, args []string) {
  64. cmdutil.CheckErr(validateArgs(cmd, args))
  65. cmdutil.CheckErr(cmdutil.ValidateOutputArgs(cmd))
  66. cmdutil.CheckErr(RunApply(f, cmd, out, options))
  67. },
  68. }
  69. usage := "Filename, directory, or URL to file that contains the configuration to apply"
  70. kubectl.AddJsonFilenameFlag(cmd, &options.Filenames, usage)
  71. cmd.MarkFlagRequired("filename")
  72. cmd.Flags().Bool("overwrite", true, "Automatically resolve conflicts between the modified and live configuration by using values from the modified configuration")
  73. cmdutil.AddValidateFlags(cmd)
  74. cmdutil.AddRecursiveFlag(cmd, &options.Recursive)
  75. cmdutil.AddOutputFlagsForMutation(cmd)
  76. cmdutil.AddRecordFlag(cmd)
  77. cmdutil.AddInclude3rdPartyFlags(cmd)
  78. return cmd
  79. }
  80. func validateArgs(cmd *cobra.Command, args []string) error {
  81. if len(args) != 0 {
  82. return cmdutil.UsageError(cmd, "Unexpected args: %v", args)
  83. }
  84. return nil
  85. }
  86. func RunApply(f *cmdutil.Factory, cmd *cobra.Command, out io.Writer, options *ApplyOptions) error {
  87. shortOutput := cmdutil.GetFlagString(cmd, "output") == "name"
  88. schema, err := f.Validator(cmdutil.GetFlagBool(cmd, "validate"), cmdutil.GetFlagString(cmd, "schema-cache-dir"))
  89. if err != nil {
  90. return err
  91. }
  92. cmdNamespace, enforceNamespace, err := f.DefaultNamespace()
  93. if err != nil {
  94. return err
  95. }
  96. mapper, typer := f.Object(cmdutil.GetIncludeThirdPartyAPIs(cmd))
  97. r := resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.ClientForMapping), f.Decoder(true)).
  98. Schema(schema).
  99. ContinueOnError().
  100. NamespaceParam(cmdNamespace).DefaultNamespace().
  101. FilenameParam(enforceNamespace, options.Recursive, options.Filenames...).
  102. Flatten().
  103. Do()
  104. err = r.Err()
  105. if err != nil {
  106. return err
  107. }
  108. encoder := f.JSONEncoder()
  109. decoder := f.Decoder(false)
  110. count := 0
  111. err = r.Visit(func(info *resource.Info, err error) error {
  112. // In this method, info.Object contains the object retrieved from the server
  113. // and info.VersionedObject contains the object decoded from the input source.
  114. if err != nil {
  115. return err
  116. }
  117. // Get the modified configuration of the object. Embed the result
  118. // as an annotation in the modified configuration, so that it will appear
  119. // in the patch sent to the server.
  120. modified, err := kubectl.GetModifiedConfiguration(info, true, encoder)
  121. if err != nil {
  122. return cmdutil.AddSourceToErr(fmt.Sprintf("retrieving modified configuration from:\n%v\nfor:", info), info.Source, err)
  123. }
  124. if err := info.Get(); err != nil {
  125. if !errors.IsNotFound(err) {
  126. return cmdutil.AddSourceToErr(fmt.Sprintf("retrieving current configuration of:\n%v\nfrom server for:", info), info.Source, err)
  127. }
  128. // Create the resource if it doesn't exist
  129. // First, update the annotation used by kubectl apply
  130. if err := kubectl.CreateApplyAnnotation(info, encoder); err != nil {
  131. return cmdutil.AddSourceToErr("creating", info.Source, err)
  132. }
  133. if cmdutil.ShouldRecord(cmd, info) {
  134. if err := cmdutil.RecordChangeCause(info.Object, f.Command()); err != nil {
  135. return cmdutil.AddSourceToErr("creating", info.Source, err)
  136. }
  137. }
  138. // Then create the resource and skip the three-way merge
  139. if err := createAndRefresh(info); err != nil {
  140. return cmdutil.AddSourceToErr("creating", info.Source, err)
  141. }
  142. count++
  143. cmdutil.PrintSuccess(mapper, shortOutput, out, info.Mapping.Resource, info.Name, "created")
  144. return nil
  145. }
  146. overwrite := cmdutil.GetFlagBool(cmd, "overwrite")
  147. helper := resource.NewHelper(info.Client, info.Mapping)
  148. patcher := NewPatcher(encoder, decoder, info.Mapping, helper, overwrite)
  149. patchBytes, err := patcher.patch(info.Object, modified, info.Source, info.Namespace, info.Name)
  150. if err != nil {
  151. return cmdutil.AddSourceToErr(fmt.Sprintf("applying patch:\n%s\nto:\n%v\nfor:", patchBytes, info), info.Source, err)
  152. }
  153. if cmdutil.ShouldRecord(cmd, info) {
  154. patch, err := cmdutil.ChangeResourcePatch(info, f.Command())
  155. if err != nil {
  156. return err
  157. }
  158. _, err = helper.Patch(info.Namespace, info.Name, api.StrategicMergePatchType, patch)
  159. if err != nil {
  160. return cmdutil.AddSourceToErr(fmt.Sprintf("applying patch:\n%s\nto:\n%v\nfor:", patch, info), info.Source, err)
  161. }
  162. }
  163. count++
  164. cmdutil.PrintSuccess(mapper, shortOutput, out, info.Mapping.Resource, info.Name, "configured")
  165. return nil
  166. })
  167. if err != nil {
  168. return err
  169. }
  170. if count == 0 {
  171. return fmt.Errorf("no objects passed to apply")
  172. }
  173. return nil
  174. }
  175. type patcher struct {
  176. encoder runtime.Encoder
  177. decoder runtime.Decoder
  178. mapping *meta.RESTMapping
  179. helper *resource.Helper
  180. overwrite bool
  181. backOff clockwork.Clock
  182. }
  183. func NewPatcher(encoder runtime.Encoder, decoder runtime.Decoder, mapping *meta.RESTMapping, helper *resource.Helper, overwrite bool) *patcher {
  184. return &patcher{
  185. encoder: encoder,
  186. decoder: decoder,
  187. mapping: mapping,
  188. helper: helper,
  189. overwrite: overwrite,
  190. backOff: clockwork.NewRealClock(),
  191. }
  192. }
  193. func (p *patcher) patchSimple(obj runtime.Object, modified []byte, source, namespace, name string) ([]byte, error) {
  194. // Serialize the current configuration of the object from the server.
  195. current, err := runtime.Encode(p.encoder, obj)
  196. if err != nil {
  197. return nil, cmdutil.AddSourceToErr(fmt.Sprintf("serializing current configuration from:\n%v\nfor:", obj), source, err)
  198. }
  199. // Retrieve the original configuration of the object from the annotation.
  200. original, err := kubectl.GetOriginalConfiguration(p.mapping, obj)
  201. if err != nil {
  202. return nil, cmdutil.AddSourceToErr(fmt.Sprintf("retrieving original configuration from:\n%v\nfor:", obj), source, err)
  203. }
  204. // Create the versioned struct from the original from the server for
  205. // strategic patch.
  206. // TODO: Move all structs in apply to use raw data. Can be done once
  207. // builder has a RawResult method which delivers raw data instead of
  208. // internal objects.
  209. versionedObject, _, err := p.decoder.Decode(current, nil, nil)
  210. if err != nil {
  211. return nil, cmdutil.AddSourceToErr(fmt.Sprintf("converting encoded server-side object back to versioned struct:\n%v\nfor:", obj), source, err)
  212. }
  213. // Compute a three way strategic merge patch to send to server.
  214. patch, err := strategicpatch.CreateThreeWayMergePatch(original, modified, current, versionedObject, p.overwrite)
  215. if err != nil {
  216. format := "creating patch with:\noriginal:\n%s\nmodified:\n%s\ncurrent:\n%s\nfor:"
  217. return nil, cmdutil.AddSourceToErr(fmt.Sprintf(format, original, modified, current), source, err)
  218. }
  219. _, err = p.helper.Patch(namespace, name, api.StrategicMergePatchType, patch)
  220. return patch, err
  221. }
  222. func (p *patcher) patch(current runtime.Object, modified []byte, source, namespace, name string) ([]byte, error) {
  223. var getErr error
  224. patchBytes, err := p.patchSimple(current, modified, source, namespace, name)
  225. for i := 1; i <= maxPatchRetry && errors.IsConflict(err); i++ {
  226. if i > triesBeforeBackOff {
  227. p.backOff.Sleep(backOffPeriod)
  228. }
  229. current, getErr = p.helper.Get(namespace, name, false)
  230. if getErr != nil {
  231. return nil, getErr
  232. }
  233. patchBytes, err = p.patchSimple(current, modified, source, namespace, name)
  234. }
  235. return patchBytes, err
  236. }