unstructured.go 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. Copyright 2016 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 discovery
  14. import (
  15. "fmt"
  16. "k8s.io/apimachinery/pkg/runtime"
  17. "k8s.io/apimachinery/pkg/runtime/schema"
  18. )
  19. // UnstructuredObjectTyper provides a runtime.ObjectTyper implmentation for
  20. // runtime.Unstructured object based on discovery information.
  21. type UnstructuredObjectTyper struct {
  22. registered map[schema.GroupVersionKind]bool
  23. }
  24. // NewUnstructuredObjectTyper returns a runtime.ObjectTyper for
  25. // unstructred objects based on discovery information.
  26. func NewUnstructuredObjectTyper(groupResources []*APIGroupResources) *UnstructuredObjectTyper {
  27. dot := &UnstructuredObjectTyper{registered: make(map[schema.GroupVersionKind]bool)}
  28. for _, group := range groupResources {
  29. for _, discoveryVersion := range group.Group.Versions {
  30. resources, ok := group.VersionedResources[discoveryVersion.Version]
  31. if !ok {
  32. continue
  33. }
  34. gv := schema.GroupVersion{Group: group.Group.Name, Version: discoveryVersion.Version}
  35. for _, resource := range resources {
  36. dot.registered[gv.WithKind(resource.Kind)] = true
  37. }
  38. }
  39. }
  40. return dot
  41. }
  42. // ObjectKind returns the group,version,kind of the provided object, or an error
  43. // if the object in not runtime.Unstructured or has no group,version,kind
  44. // information.
  45. func (d *UnstructuredObjectTyper) ObjectKind(obj runtime.Object) (schema.GroupVersionKind, error) {
  46. if _, ok := obj.(runtime.Unstructured); !ok {
  47. return schema.GroupVersionKind{}, fmt.Errorf("type %T is invalid for dynamic object typer", obj)
  48. }
  49. return obj.GetObjectKind().GroupVersionKind(), nil
  50. }
  51. // ObjectKinds returns a slice of one element with the group,version,kind of the
  52. // provided object, or an error if the object is not runtime.Unstructured or
  53. // has no group,version,kind information. unversionedType will always be false
  54. // because runtime.Unstructured object should always have group,version,kind
  55. // information set.
  56. func (d *UnstructuredObjectTyper) ObjectKinds(obj runtime.Object) (gvks []schema.GroupVersionKind, unversionedType bool, err error) {
  57. gvk, err := d.ObjectKind(obj)
  58. if err != nil {
  59. return nil, false, err
  60. }
  61. return []schema.GroupVersionKind{gvk}, false, nil
  62. }
  63. // Recognizes returns true if the provided group,version,kind was in the
  64. // discovery information.
  65. func (d *UnstructuredObjectTyper) Recognizes(gvk schema.GroupVersionKind) bool {
  66. return d.registered[gvk]
  67. }
  68. // IsUnversioned returns false always because runtime.Unstructured objects
  69. // should always have group,version,kind information set. ok will be true if the
  70. // object's group,version,kind is api.Registry.
  71. func (d *UnstructuredObjectTyper) IsUnversioned(obj runtime.Object) (unversioned bool, ok bool) {
  72. gvk, err := d.ObjectKind(obj)
  73. if err != nil {
  74. return false, false
  75. }
  76. return false, d.registered[gvk]
  77. }
  78. var _ runtime.ObjectTyper = &UnstructuredObjectTyper{}