controller_ref.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 v1
  14. import (
  15. "k8s.io/apimachinery/pkg/runtime/schema"
  16. )
  17. // IsControlledBy checks if the object has a controllerRef set to the given owner
  18. func IsControlledBy(obj Object, owner Object) bool {
  19. ref := GetControllerOf(obj)
  20. if ref == nil {
  21. return false
  22. }
  23. return ref.UID == owner.GetUID()
  24. }
  25. // GetControllerOf returns a pointer to a copy of the controllerRef if controllee has a controller
  26. func GetControllerOf(controllee Object) *OwnerReference {
  27. for _, ref := range controllee.GetOwnerReferences() {
  28. if ref.Controller != nil && *ref.Controller {
  29. return &ref
  30. }
  31. }
  32. return nil
  33. }
  34. // NewControllerRef creates an OwnerReference pointing to the given owner.
  35. func NewControllerRef(owner Object, gvk schema.GroupVersionKind) *OwnerReference {
  36. blockOwnerDeletion := true
  37. isController := true
  38. return &OwnerReference{
  39. APIVersion: gvk.GroupVersion().String(),
  40. Kind: gvk.Kind,
  41. Name: owner.GetName(),
  42. UID: owner.GetUID(),
  43. BlockOwnerDeletion: &blockOwnerDeletion,
  44. Controller: &isController,
  45. }
  46. }