managers.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. Copyright 2018 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 fieldpath
  14. // APIVersion describes the version of an object or of a fieldset.
  15. type APIVersion string
  16. // VersionedSet associates a version to a set.
  17. type VersionedSet struct {
  18. *Set
  19. APIVersion APIVersion
  20. Applied bool
  21. }
  22. // ManagedFields is a map from manager to VersionedSet (what they own in
  23. // what version).
  24. type ManagedFields map[string]*VersionedSet
  25. // Difference returns a symmetric difference between two Managers. If a
  26. // given user's entry has version X in lhs and version Y in rhs, then
  27. // the return value for that user will be from rhs. If the difference for
  28. // a user is an empty set, that user will not be inserted in the map.
  29. func (lhs ManagedFields) Difference(rhs ManagedFields) ManagedFields {
  30. diff := ManagedFields{}
  31. for manager, left := range lhs {
  32. right, ok := rhs[manager]
  33. if !ok {
  34. if !left.Empty() {
  35. diff[manager] = left
  36. }
  37. continue
  38. }
  39. // If we have sets in both but their version
  40. // differs, we don't even diff and keep the
  41. // entire thing.
  42. if left.APIVersion != right.APIVersion {
  43. diff[manager] = right
  44. continue
  45. }
  46. newSet := left.Difference(right.Set).Union(right.Difference(left.Set))
  47. if !newSet.Empty() {
  48. diff[manager] = &VersionedSet{
  49. Set: newSet,
  50. APIVersion: right.APIVersion,
  51. }
  52. }
  53. }
  54. for manager, set := range rhs {
  55. if _, ok := lhs[manager]; ok {
  56. // Already done
  57. continue
  58. }
  59. if !set.Empty() {
  60. diff[manager] = set
  61. }
  62. }
  63. return diff
  64. }