apply.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 kubectl
  14. import (
  15. "encoding/json"
  16. "k8s.io/kubernetes/pkg/api/annotations"
  17. "k8s.io/kubernetes/pkg/api/meta"
  18. "k8s.io/kubernetes/pkg/kubectl/resource"
  19. "k8s.io/kubernetes/pkg/runtime"
  20. )
  21. type debugError interface {
  22. DebugError() (msg string, args []interface{})
  23. }
  24. // GetOriginalConfiguration retrieves the original configuration of the object
  25. // from the annotation, or nil if no annotation was found.
  26. func GetOriginalConfiguration(mapping *meta.RESTMapping, obj runtime.Object) ([]byte, error) {
  27. annots, err := mapping.MetadataAccessor.Annotations(obj)
  28. if err != nil {
  29. return nil, err
  30. }
  31. if annots == nil {
  32. return nil, nil
  33. }
  34. original, ok := annots[annotations.LastAppliedConfigAnnotation]
  35. if !ok {
  36. return nil, nil
  37. }
  38. return []byte(original), nil
  39. }
  40. // SetOriginalConfiguration sets the original configuration of the object
  41. // as the annotation on the object for later use in computing a three way patch.
  42. func SetOriginalConfiguration(info *resource.Info, original []byte) error {
  43. if len(original) < 1 {
  44. return nil
  45. }
  46. accessor := info.Mapping.MetadataAccessor
  47. annots, err := accessor.Annotations(info.Object)
  48. if err != nil {
  49. return err
  50. }
  51. if annots == nil {
  52. annots = map[string]string{}
  53. }
  54. annots[annotations.LastAppliedConfigAnnotation] = string(original)
  55. if err := info.Mapping.MetadataAccessor.SetAnnotations(info.Object, annots); err != nil {
  56. return err
  57. }
  58. return nil
  59. }
  60. // GetModifiedConfiguration retrieves the modified configuration of the object.
  61. // If annotate is true, it embeds the result as an anotation in the modified
  62. // configuration. If an object was read from the command input, it will use that
  63. // version of the object. Otherwise, it will use the version from the server.
  64. func GetModifiedConfiguration(info *resource.Info, annotate bool, codec runtime.Encoder) ([]byte, error) {
  65. // First serialize the object without the annotation to prevent recursion,
  66. // then add that serialization to it as the annotation and serialize it again.
  67. var modified []byte
  68. if info.VersionedObject != nil {
  69. // If an object was read from input, use that version.
  70. accessor, err := meta.Accessor(info.VersionedObject)
  71. if err != nil {
  72. return nil, err
  73. }
  74. // Get the current annotations from the object.
  75. annots := accessor.GetAnnotations()
  76. if annots == nil {
  77. annots = map[string]string{}
  78. }
  79. original := annots[annotations.LastAppliedConfigAnnotation]
  80. delete(annots, annotations.LastAppliedConfigAnnotation)
  81. accessor.SetAnnotations(annots)
  82. // TODO: this needs to be abstracted - there should be no assumption that versioned object
  83. // can be marshalled to JSON.
  84. modified, err = json.Marshal(info.VersionedObject)
  85. if err != nil {
  86. return nil, err
  87. }
  88. if annotate {
  89. annots[annotations.LastAppliedConfigAnnotation] = string(modified)
  90. accessor.SetAnnotations(annots)
  91. // TODO: this needs to be abstracted - there should be no assumption that versioned object
  92. // can be marshalled to JSON.
  93. modified, err = json.Marshal(info.VersionedObject)
  94. if err != nil {
  95. return nil, err
  96. }
  97. }
  98. // Restore the object to its original condition.
  99. annots[annotations.LastAppliedConfigAnnotation] = original
  100. accessor.SetAnnotations(annots)
  101. } else {
  102. // Otherwise, use the server side version of the object.
  103. accessor := info.Mapping.MetadataAccessor
  104. // Get the current annotations from the object.
  105. annots, err := accessor.Annotations(info.Object)
  106. if err != nil {
  107. return nil, err
  108. }
  109. if annots == nil {
  110. annots = map[string]string{}
  111. }
  112. original := annots[annotations.LastAppliedConfigAnnotation]
  113. delete(annots, annotations.LastAppliedConfigAnnotation)
  114. if err := accessor.SetAnnotations(info.Object, annots); err != nil {
  115. return nil, err
  116. }
  117. modified, err = runtime.Encode(codec, info.Object)
  118. if err != nil {
  119. return nil, err
  120. }
  121. if annotate {
  122. annots[annotations.LastAppliedConfigAnnotation] = string(modified)
  123. if err := info.Mapping.MetadataAccessor.SetAnnotations(info.Object, annots); err != nil {
  124. return nil, err
  125. }
  126. modified, err = runtime.Encode(codec, info.Object)
  127. if err != nil {
  128. return nil, err
  129. }
  130. }
  131. // Restore the object to its original condition.
  132. annots[annotations.LastAppliedConfigAnnotation] = original
  133. if err := info.Mapping.MetadataAccessor.SetAnnotations(info.Object, annots); err != nil {
  134. return nil, err
  135. }
  136. }
  137. return modified, nil
  138. }
  139. // UpdateApplyAnnotation calls CreateApplyAnnotation if the last applied
  140. // configuration annotation is already present. Otherwise, it does nothing.
  141. func UpdateApplyAnnotation(info *resource.Info, codec runtime.Encoder) error {
  142. if original, err := GetOriginalConfiguration(info.Mapping, info.Object); err != nil || len(original) <= 0 {
  143. return err
  144. }
  145. return CreateApplyAnnotation(info, codec)
  146. }
  147. // CreateApplyAnnotation gets the modified configuration of the object,
  148. // without embedding it again, and then sets it on the object as the annotation.
  149. func CreateApplyAnnotation(info *resource.Info, codec runtime.Encoder) error {
  150. modified, err := GetModifiedConfiguration(info, false, codec)
  151. if err != nil {
  152. return err
  153. }
  154. return SetOriginalConfiguration(info, modified)
  155. }
  156. // Create the annotation used by kubectl apply only when createAnnotation is true
  157. // Otherwise, only update the annotation when it already exists
  158. func CreateOrUpdateAnnotation(createAnnotation bool, info *resource.Info, codec runtime.Encoder) error {
  159. if createAnnotation {
  160. return CreateApplyAnnotation(info, codec)
  161. }
  162. return UpdateApplyAnnotation(info, codec)
  163. }