crdfinder.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 util
  14. import (
  15. "fmt"
  16. "reflect"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
  19. "k8s.io/apimachinery/pkg/runtime/schema"
  20. "k8s.io/client-go/dynamic"
  21. )
  22. // CRDGetter is a function that can download the list of GVK for all
  23. // CRDs.
  24. type CRDGetter func() ([]schema.GroupKind, error)
  25. func CRDFromDynamic(client dynamic.Interface) CRDGetter {
  26. return func() ([]schema.GroupKind, error) {
  27. list, err := client.Resource(schema.GroupVersionResource{
  28. Group: "apiextensions.k8s.io",
  29. Version: "v1beta1",
  30. Resource: "customresourcedefinitions",
  31. }).List(metav1.ListOptions{})
  32. if err != nil {
  33. return nil, fmt.Errorf("failed to list CRDs: %v", err)
  34. }
  35. if list == nil {
  36. return nil, nil
  37. }
  38. gks := []schema.GroupKind{}
  39. // We need to parse the list to get the gvk, I guess that's fine.
  40. for _, crd := range (*list).Items {
  41. // Look for group, version, and kind
  42. group, _, _ := unstructured.NestedString(crd.Object, "spec", "group")
  43. kind, _, _ := unstructured.NestedString(crd.Object, "spec", "names", "kind")
  44. gks = append(gks, schema.GroupKind{
  45. Group: group,
  46. Kind: kind,
  47. })
  48. }
  49. return gks, nil
  50. }
  51. }
  52. // CRDFinder keeps a cache of known CRDs and finds a given GVK in the
  53. // list.
  54. type CRDFinder interface {
  55. HasCRD(gvk schema.GroupKind) (bool, error)
  56. }
  57. func NewCRDFinder(getter CRDGetter) CRDFinder {
  58. return &crdFinder{
  59. getter: getter,
  60. }
  61. }
  62. type crdFinder struct {
  63. getter CRDGetter
  64. cache *[]schema.GroupKind
  65. }
  66. func (f *crdFinder) cacheCRDs() error {
  67. if f.cache != nil {
  68. return nil
  69. }
  70. list, err := f.getter()
  71. if err != nil {
  72. return err
  73. }
  74. f.cache = &list
  75. return nil
  76. }
  77. func (f *crdFinder) findCRD(gvk schema.GroupKind) bool {
  78. for _, crd := range *f.cache {
  79. if reflect.DeepEqual(gvk, crd) {
  80. return true
  81. }
  82. }
  83. return false
  84. }
  85. func (f *crdFinder) HasCRD(gvk schema.GroupKind) (bool, error) {
  86. if err := f.cacheCRDs(); err != nil {
  87. return false, err
  88. }
  89. return f.findCRD(gvk), nil
  90. }