mapper.go 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. Copyright 2019 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 runtime
  14. import (
  15. "sync"
  16. "k8s.io/apimachinery/pkg/runtime/schema"
  17. )
  18. type equivalentResourceRegistry struct {
  19. // keyFunc computes a key for the specified resource (this allows honoring colocated resources across API groups).
  20. // if null, or if "" is returned, resource.String() is used as the key
  21. keyFunc func(resource schema.GroupResource) string
  22. // resources maps key -> subresource -> equivalent resources (subresource is not included in the returned resources).
  23. // main resources are stored with subresource="".
  24. resources map[string]map[string][]schema.GroupVersionResource
  25. // kinds maps resource -> subresource -> kind
  26. kinds map[schema.GroupVersionResource]map[string]schema.GroupVersionKind
  27. // keys caches the computed key for each GroupResource
  28. keys map[schema.GroupResource]string
  29. mutex sync.RWMutex
  30. }
  31. var _ EquivalentResourceMapper = (*equivalentResourceRegistry)(nil)
  32. var _ EquivalentResourceRegistry = (*equivalentResourceRegistry)(nil)
  33. // NewEquivalentResourceRegistry creates a resource registry that considers all versions of a GroupResource to be equivalent.
  34. func NewEquivalentResourceRegistry() EquivalentResourceRegistry {
  35. return &equivalentResourceRegistry{}
  36. }
  37. // NewEquivalentResourceRegistryWithIdentity creates a resource mapper with a custom identity function.
  38. // If "" is returned by the function, GroupResource#String is used as the identity.
  39. // GroupResources with the same identity string are considered equivalent.
  40. func NewEquivalentResourceRegistryWithIdentity(keyFunc func(schema.GroupResource) string) EquivalentResourceRegistry {
  41. return &equivalentResourceRegistry{keyFunc: keyFunc}
  42. }
  43. func (r *equivalentResourceRegistry) EquivalentResourcesFor(resource schema.GroupVersionResource, subresource string) []schema.GroupVersionResource {
  44. r.mutex.RLock()
  45. defer r.mutex.RUnlock()
  46. return r.resources[r.keys[resource.GroupResource()]][subresource]
  47. }
  48. func (r *equivalentResourceRegistry) KindFor(resource schema.GroupVersionResource, subresource string) schema.GroupVersionKind {
  49. r.mutex.RLock()
  50. defer r.mutex.RUnlock()
  51. return r.kinds[resource][subresource]
  52. }
  53. func (r *equivalentResourceRegistry) RegisterKindFor(resource schema.GroupVersionResource, subresource string, kind schema.GroupVersionKind) {
  54. r.mutex.Lock()
  55. defer r.mutex.Unlock()
  56. if r.kinds == nil {
  57. r.kinds = map[schema.GroupVersionResource]map[string]schema.GroupVersionKind{}
  58. }
  59. if r.kinds[resource] == nil {
  60. r.kinds[resource] = map[string]schema.GroupVersionKind{}
  61. }
  62. r.kinds[resource][subresource] = kind
  63. // get the shared key of the parent resource
  64. key := ""
  65. gr := resource.GroupResource()
  66. if r.keyFunc != nil {
  67. key = r.keyFunc(gr)
  68. }
  69. if key == "" {
  70. key = gr.String()
  71. }
  72. if r.keys == nil {
  73. r.keys = map[schema.GroupResource]string{}
  74. }
  75. r.keys[gr] = key
  76. if r.resources == nil {
  77. r.resources = map[string]map[string][]schema.GroupVersionResource{}
  78. }
  79. if r.resources[key] == nil {
  80. r.resources[key] = map[string][]schema.GroupVersionResource{}
  81. }
  82. r.resources[key][subresource] = append(r.resources[key][subresource], resource)
  83. }