lastappliedupdater.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. Copyright 2020 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 internal
  14. import (
  15. "fmt"
  16. "k8s.io/apimachinery/pkg/api/meta"
  17. "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
  18. "k8s.io/apimachinery/pkg/runtime"
  19. )
  20. type lastAppliedUpdater struct {
  21. fieldManager Manager
  22. }
  23. var _ Manager = &lastAppliedUpdater{}
  24. // NewLastAppliedUpdater sets the client-side apply annotation up to date with
  25. // server-side apply managed fields
  26. func NewLastAppliedUpdater(fieldManager Manager) Manager {
  27. return &lastAppliedUpdater{
  28. fieldManager: fieldManager,
  29. }
  30. }
  31. // Update implements Manager.
  32. func (f *lastAppliedUpdater) Update(liveObj, newObj runtime.Object, managed Managed, manager string) (runtime.Object, Managed, error) {
  33. return f.fieldManager.Update(liveObj, newObj, managed, manager)
  34. }
  35. // server-side apply managed fields
  36. func (f *lastAppliedUpdater) Apply(liveObj, newObj runtime.Object, managed Managed, manager string, force bool) (runtime.Object, Managed, error) {
  37. liveObj, managed, err := f.fieldManager.Apply(liveObj, newObj, managed, manager, force)
  38. if err != nil {
  39. return liveObj, managed, err
  40. }
  41. // Sync the client-side apply annotation only from kubectl server-side apply.
  42. // To opt-out of this behavior, users may specify a different field manager.
  43. //
  44. // If the client-side apply annotation doesn't exist,
  45. // then continue because we have no annotation to update
  46. if manager == "kubectl" && hasLastApplied(liveObj) {
  47. lastAppliedValue, err := buildLastApplied(newObj)
  48. if err != nil {
  49. return nil, nil, fmt.Errorf("failed to build last-applied annotation: %v", err)
  50. }
  51. err = SetLastApplied(liveObj, lastAppliedValue)
  52. if err != nil {
  53. return nil, nil, fmt.Errorf("failed to set last-applied annotation: %v", err)
  54. }
  55. }
  56. return liveObj, managed, err
  57. }
  58. func hasLastApplied(obj runtime.Object) bool {
  59. var accessor, err = meta.Accessor(obj)
  60. if err != nil {
  61. panic(fmt.Sprintf("couldn't get accessor: %v", err))
  62. }
  63. var annotations = accessor.GetAnnotations()
  64. if annotations == nil {
  65. return false
  66. }
  67. lastApplied, ok := annotations[LastAppliedConfigAnnotation]
  68. return ok && len(lastApplied) > 0
  69. }
  70. func buildLastApplied(obj runtime.Object) (string, error) {
  71. obj = obj.DeepCopyObject()
  72. var accessor, err = meta.Accessor(obj)
  73. if err != nil {
  74. panic(fmt.Sprintf("couldn't get accessor: %v", err))
  75. }
  76. // Remove the annotation from the object before encoding the object
  77. var annotations = accessor.GetAnnotations()
  78. delete(annotations, LastAppliedConfigAnnotation)
  79. accessor.SetAnnotations(annotations)
  80. lastApplied, err := runtime.Encode(unstructured.UnstructuredJSONScheme, obj)
  81. if err != nil {
  82. return "", fmt.Errorf("couldn't encode object into last applied annotation: %v", err)
  83. }
  84. return string(lastApplied), nil
  85. }