helpers.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. Copyright 2017 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 rbac
  14. import (
  15. "reflect"
  16. "k8s.io/apimachinery/pkg/api/meta"
  17. "k8s.io/apimachinery/pkg/conversion"
  18. "k8s.io/apimachinery/pkg/runtime"
  19. )
  20. // IsOnlyMutatingGCFields checks finalizers and ownerrefs which GC manipulates
  21. // and indicates that only those fields are changing
  22. func IsOnlyMutatingGCFields(obj, old runtime.Object, equalities conversion.Equalities) bool {
  23. if old == nil || reflect.ValueOf(old).IsNil() {
  24. return false
  25. }
  26. // make a copy of the newObj so that we can stomp for comparison
  27. copied := obj.DeepCopyObject()
  28. copiedMeta, err := meta.Accessor(copied)
  29. if err != nil {
  30. return false
  31. }
  32. oldMeta, err := meta.Accessor(old)
  33. if err != nil {
  34. return false
  35. }
  36. copiedMeta.SetOwnerReferences(oldMeta.GetOwnerReferences())
  37. copiedMeta.SetFinalizers(oldMeta.GetFinalizers())
  38. copiedMeta.SetSelfLink(oldMeta.GetSelfLink())
  39. return equalities.DeepEqual(copied, old)
  40. }