github.com/banzaicloud/operator-tools@v0.28.10/pkg/crd/crd.go (about)

     1  // Copyright © 2020 Banzai Cloud
     2  //
     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  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package crd
    16  
    17  import (
    18  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    19  )
    20  
    21  type CRD struct {
    22  	sri ServerResourcesInterface
    23  }
    24  
    25  func NewCRD(sri ServerResourcesInterface) *CRD {
    26  	return &CRD{
    27  		sri: sri,
    28  	}
    29  }
    30  
    31  // ListAPIResources returns all API resources registered for a given API group and version.
    32  func (c *CRD) ListAPIResources(groupVersion metav1.GroupVersion) ([]metav1.APIResource, error) {
    33  	resp, err := c.sri.ServerResourcesForGroupVersion(groupVersion.String())
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  
    38  	return resp.APIResources, nil
    39  }
    40  
    41  // HasAPIResource checks whether a given resource is registered under a given API group and resource or not.
    42  func (c *CRD) HasAPIResource(groupVersion metav1.GroupVersion, name string) (bool, error) {
    43  	resources, err := c.ListAPIResources(groupVersion)
    44  	if err != nil {
    45  		return false, err
    46  	}
    47  
    48  	for _, res := range resources {
    49  		if res.Name == name {
    50  			return true, nil
    51  		}
    52  	}
    53  
    54  	return false, nil
    55  }