...

Source file src/k8s.io/cli-runtime/pkg/resource/crd_finder.go

Documentation: k8s.io/cli-runtime/pkg/resource

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package resource
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"reflect"
    23  
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    26  	"k8s.io/apimachinery/pkg/runtime/schema"
    27  	"k8s.io/client-go/dynamic"
    28  )
    29  
    30  // CRDGetter is a function that can download the list of GVK for all
    31  // CRDs.
    32  type CRDGetter func() ([]schema.GroupKind, error)
    33  
    34  func CRDFromDynamic(client dynamic.Interface) CRDGetter {
    35  	return func() ([]schema.GroupKind, error) {
    36  		list, err := client.Resource(schema.GroupVersionResource{
    37  			Group:    "apiextensions.k8s.io",
    38  			Version:  "v1",
    39  			Resource: "customresourcedefinitions",
    40  		}).List(context.TODO(), metav1.ListOptions{})
    41  		if err != nil {
    42  			return nil, fmt.Errorf("failed to list CRDs: %v", err)
    43  		}
    44  		if list == nil {
    45  			return nil, nil
    46  		}
    47  
    48  		gks := []schema.GroupKind{}
    49  
    50  		// We need to parse the list to get the gvk, I guess that's fine.
    51  		for _, crd := range (*list).Items {
    52  			// Look for group, version, and kind
    53  			group, _, _ := unstructured.NestedString(crd.Object, "spec", "group")
    54  			kind, _, _ := unstructured.NestedString(crd.Object, "spec", "names", "kind")
    55  
    56  			gks = append(gks, schema.GroupKind{
    57  				Group: group,
    58  				Kind:  kind,
    59  			})
    60  		}
    61  
    62  		return gks, nil
    63  	}
    64  }
    65  
    66  // CRDFinder keeps a cache of known CRDs and finds a given GVK in the
    67  // list.
    68  type CRDFinder interface {
    69  	HasCRD(gvk schema.GroupKind) (bool, error)
    70  }
    71  
    72  func NewCRDFinder(getter CRDGetter) CRDFinder {
    73  	return &crdFinder{
    74  		getter: getter,
    75  	}
    76  }
    77  
    78  type crdFinder struct {
    79  	getter CRDGetter
    80  	cache  *[]schema.GroupKind
    81  }
    82  
    83  func (f *crdFinder) cacheCRDs() error {
    84  	if f.cache != nil {
    85  		return nil
    86  	}
    87  
    88  	list, err := f.getter()
    89  	if err != nil {
    90  		return err
    91  	}
    92  	f.cache = &list
    93  	return nil
    94  }
    95  
    96  func (f *crdFinder) findCRD(gvk schema.GroupKind) bool {
    97  	for _, crd := range *f.cache {
    98  		if reflect.DeepEqual(gvk, crd) {
    99  			return true
   100  		}
   101  	}
   102  	return false
   103  }
   104  
   105  func (f *crdFinder) HasCRD(gvk schema.GroupKind) (bool, error) {
   106  	if err := f.cacheCRDs(); err != nil {
   107  		return false, err
   108  	}
   109  	return f.findCRD(gvk), nil
   110  }
   111  

View as plain text