managedfieldsupdater.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. "time"
  16. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  17. "k8s.io/apimachinery/pkg/runtime"
  18. "sigs.k8s.io/structured-merge-diff/v4/fieldpath"
  19. )
  20. type managedFieldsUpdater struct {
  21. fieldManager Manager
  22. }
  23. var _ Manager = &managedFieldsUpdater{}
  24. // NewManagedFieldsUpdater is responsible for updating the managedfields
  25. // in the object, updating the time of the operation as necessary. For
  26. // updates, it uses a hard-coded manager to detect if things have
  27. // changed, and swaps back the correct manager after the operation is
  28. // done.
  29. func NewManagedFieldsUpdater(fieldManager Manager) Manager {
  30. return &managedFieldsUpdater{
  31. fieldManager: fieldManager,
  32. }
  33. }
  34. // Update implements Manager.
  35. func (f *managedFieldsUpdater) Update(liveObj, newObj runtime.Object, managed Managed, manager string) (runtime.Object, Managed, error) {
  36. self := "current-operation"
  37. object, managed, err := f.fieldManager.Update(liveObj, newObj, managed, self)
  38. if err != nil {
  39. return object, managed, err
  40. }
  41. // If the current operation took any fields from anything, it means the object changed,
  42. // so update the timestamp of the managedFieldsEntry and merge with any previous updates from the same manager
  43. if vs, ok := managed.Fields()[self]; ok {
  44. delete(managed.Fields(), self)
  45. if previous, ok := managed.Fields()[manager]; ok {
  46. managed.Fields()[manager] = fieldpath.NewVersionedSet(vs.Set().Union(previous.Set()), vs.APIVersion(), vs.Applied())
  47. } else {
  48. managed.Fields()[manager] = vs
  49. }
  50. managed.Times()[manager] = &metav1.Time{Time: time.Now().UTC()}
  51. }
  52. return object, managed, nil
  53. }
  54. // Apply implements Manager.
  55. func (f *managedFieldsUpdater) Apply(liveObj, appliedObj runtime.Object, managed Managed, fieldManager string, force bool) (runtime.Object, Managed, error) {
  56. object, managed, err := f.fieldManager.Apply(liveObj, appliedObj, managed, fieldManager, force)
  57. if err != nil {
  58. return object, managed, err
  59. }
  60. if object != nil {
  61. managed.Times()[fieldManager] = &metav1.Time{Time: time.Now().UTC()}
  62. } else {
  63. object = liveObj.DeepCopyObject()
  64. RemoveObjectManagedFields(object)
  65. }
  66. return object, managed, nil
  67. }