2019-05-15 07:30:22 -07:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2020-10-15 17:54:58 -07:00
|
|
|
"context"
|
2019-05-15 07:30:22 -07:00
|
|
|
"fmt"
|
2019-05-31 17:59:36 -07:00
|
|
|
"strings"
|
2019-05-15 07:30:22 -07:00
|
|
|
"time"
|
|
|
|
|
2020-03-17 11:05:20 -07:00
|
|
|
"github.com/go-logr/logr"
|
2021-02-07 20:26:56 -08:00
|
|
|
openapiv2 "github.com/googleapis/gnostic/openapiv2"
|
2019-06-25 22:53:18 -07:00
|
|
|
helperv1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2019-05-15 07:30:22 -07:00
|
|
|
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
2019-07-17 15:04:02 -07:00
|
|
|
patchTypes "k8s.io/apimachinery/pkg/types"
|
2020-02-14 18:12:28 -08:00
|
|
|
"k8s.io/apimachinery/pkg/version"
|
2019-05-22 00:16:22 -07:00
|
|
|
"k8s.io/client-go/discovery"
|
|
|
|
"k8s.io/client-go/discovery/cached/memory"
|
2019-05-15 07:30:22 -07:00
|
|
|
"k8s.io/client-go/dynamic"
|
2020-01-07 15:13:57 -08:00
|
|
|
"k8s.io/client-go/dynamic/dynamicinformer"
|
2019-05-22 00:16:22 -07:00
|
|
|
"k8s.io/client-go/kubernetes"
|
2019-05-15 07:30:22 -07:00
|
|
|
csrtype "k8s.io/client-go/kubernetes/typed/certificates/v1beta1"
|
|
|
|
event "k8s.io/client-go/kubernetes/typed/core/v1"
|
|
|
|
"k8s.io/client-go/rest"
|
|
|
|
)
|
|
|
|
|
2019-06-05 17:43:59 -07:00
|
|
|
//Client enables interaction with k8 resource
|
2019-05-15 07:30:22 -07:00
|
|
|
type Client struct {
|
2019-06-11 14:35:26 -07:00
|
|
|
client dynamic.Interface
|
2020-03-17 11:05:20 -07:00
|
|
|
log logr.Logger
|
2019-06-11 14:35:26 -07:00
|
|
|
clientConfig *rest.Config
|
|
|
|
kclient kubernetes.Interface
|
2019-06-20 16:52:09 -07:00
|
|
|
DiscoveryClient IDiscovery
|
2019-05-15 07:30:22 -07:00
|
|
|
}
|
|
|
|
|
2019-06-05 17:43:59 -07:00
|
|
|
//NewClient creates new instance of client
|
2020-03-17 11:05:20 -07:00
|
|
|
func NewClient(config *rest.Config, resync time.Duration, stopCh <-chan struct{}, log logr.Logger) (*Client, error) {
|
|
|
|
|
2019-06-11 14:35:26 -07:00
|
|
|
dclient, err := dynamic.NewForConfig(config)
|
2019-05-15 07:30:22 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-05-22 00:16:22 -07:00
|
|
|
kclient, err := kubernetes.NewForConfig(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-06-11 14:35:26 -07:00
|
|
|
client := Client{
|
|
|
|
client: dclient,
|
2019-05-15 07:30:22 -07:00
|
|
|
clientConfig: config,
|
2019-05-22 00:16:22 -07:00
|
|
|
kclient: kclient,
|
2020-05-14 12:32:11 -07:00
|
|
|
log: log.WithName("dclient"),
|
2019-06-11 14:35:26 -07:00
|
|
|
}
|
2021-02-01 12:59:13 -08:00
|
|
|
|
2019-06-11 14:35:26 -07:00
|
|
|
// Set discovery client
|
2021-02-01 12:59:13 -08:00
|
|
|
discoveryClient := &ServerPreferredResources{
|
|
|
|
cachedClient: memory.NewMemCacheClient(kclient.Discovery()),
|
|
|
|
log: client.log,
|
|
|
|
}
|
|
|
|
|
2019-12-16 12:55:44 -08:00
|
|
|
// client will invalidate registered resources cache every x seconds,
|
|
|
|
// As there is no way to identify if the registered resource is available or not
|
|
|
|
// we will be invalidating the local cache, so the next request get a fresh cache
|
|
|
|
// If a resource is removed then and cache is not invalidate yet, we will not detect the removal
|
|
|
|
// but the re-sync shall re-evaluate
|
|
|
|
go discoveryClient.Poll(resync, stopCh)
|
|
|
|
|
2019-06-11 14:35:26 -07:00
|
|
|
client.SetDiscovery(discoveryClient)
|
|
|
|
return &client, nil
|
2019-05-15 07:30:22 -07:00
|
|
|
}
|
|
|
|
|
2020-01-24 12:05:53 -08:00
|
|
|
//NewDynamicSharedInformerFactory returns a new instance of DynamicSharedInformerFactory
|
2020-01-07 15:13:57 -08:00
|
|
|
func (c *Client) NewDynamicSharedInformerFactory(defaultResync time.Duration) dynamicinformer.DynamicSharedInformerFactory {
|
|
|
|
return dynamicinformer.NewDynamicSharedInformerFactory(c.client, defaultResync)
|
|
|
|
}
|
|
|
|
|
2019-06-05 17:43:59 -07:00
|
|
|
//GetEventsInterface provides typed interface for events
|
2019-05-15 07:30:22 -07:00
|
|
|
func (c *Client) GetEventsInterface() (event.EventInterface, error) {
|
2019-05-22 00:16:22 -07:00
|
|
|
return c.kclient.CoreV1().Events(""), nil
|
2019-05-15 07:30:22 -07:00
|
|
|
}
|
|
|
|
|
2019-06-05 17:43:59 -07:00
|
|
|
//GetCSRInterface provides type interface for CSR
|
2019-05-15 07:30:22 -07:00
|
|
|
func (c *Client) GetCSRInterface() (csrtype.CertificateSigningRequestInterface, error) {
|
2019-05-22 00:16:22 -07:00
|
|
|
return c.kclient.CertificatesV1beta1().CertificateSigningRequests(), nil
|
2019-05-15 07:30:22 -07:00
|
|
|
}
|
|
|
|
|
2020-08-07 09:47:33 +05:30
|
|
|
func (c *Client) getInterface(apiVersion string, kind string) dynamic.NamespaceableResourceInterface {
|
|
|
|
return c.client.Resource(c.getGroupVersionMapper(apiVersion, kind))
|
2019-05-15 07:30:22 -07:00
|
|
|
}
|
|
|
|
|
2020-08-07 09:47:33 +05:30
|
|
|
func (c *Client) getResourceInterface(apiVersion string, kind string, namespace string) dynamic.ResourceInterface {
|
2019-07-08 15:34:21 -07:00
|
|
|
// Get the resource interface from kind
|
2020-08-07 09:47:33 +05:30
|
|
|
namespaceableInterface := c.getInterface(apiVersion, kind)
|
2019-05-15 07:30:22 -07:00
|
|
|
// Get the namespacable interface
|
|
|
|
var resourceInteface dynamic.ResourceInterface
|
|
|
|
if namespace != "" {
|
|
|
|
resourceInteface = namespaceableInterface.Namespace(namespace)
|
|
|
|
} else {
|
|
|
|
resourceInteface = namespaceableInterface
|
|
|
|
}
|
|
|
|
return resourceInteface
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep this a stateful as the resource list will be based on the kubernetes version we connect to
|
2020-08-07 09:47:33 +05:30
|
|
|
func (c *Client) getGroupVersionMapper(apiVersion string, kind string) schema.GroupVersionResource {
|
|
|
|
if apiVersion == "" {
|
2020-12-03 19:19:36 -08:00
|
|
|
gvr, _ := c.DiscoveryClient.GetGVRFromKind(kind)
|
|
|
|
return gvr
|
2020-08-07 09:47:33 +05:30
|
|
|
}
|
|
|
|
|
2021-02-01 12:59:13 -08:00
|
|
|
return c.DiscoveryClient.GetGVRFromAPIVersionKind(apiVersion, kind)
|
2019-05-15 07:30:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetResource returns the resource in unstructured/json format
|
2020-08-07 09:47:33 +05:30
|
|
|
func (c *Client) GetResource(apiVersion string, kind string, namespace string, name string, subresources ...string) (*unstructured.Unstructured, error) {
|
2020-10-15 17:54:58 -07:00
|
|
|
return c.getResourceInterface(apiVersion, kind, namespace).Get(context.TODO(), name, meta.GetOptions{}, subresources...)
|
2019-05-15 07:30:22 -07:00
|
|
|
}
|
|
|
|
|
2019-12-05 11:52:13 -08:00
|
|
|
//PatchResource patches the resource
|
2020-08-07 09:47:33 +05:30
|
|
|
func (c *Client) PatchResource(apiVersion string, kind string, namespace string, name string, patch []byte) (*unstructured.Unstructured, error) {
|
2020-10-15 17:54:58 -07:00
|
|
|
return c.getResourceInterface(apiVersion, kind, namespace).Patch(context.TODO(), name, patchTypes.JSONPatchType, patch, meta.PatchOptions{})
|
2019-07-17 15:04:02 -07:00
|
|
|
}
|
|
|
|
|
2020-04-02 12:19:32 +05:30
|
|
|
// GetDynamicInterface fetches underlying dynamic interface
|
|
|
|
func (c *Client) GetDynamicInterface() dynamic.Interface {
|
|
|
|
return c.client
|
|
|
|
}
|
|
|
|
|
2019-05-15 07:30:22 -07:00
|
|
|
// ListResource returns the list of resources in unstructured/json format
|
|
|
|
// Access items using []Items
|
2020-08-07 09:47:33 +05:30
|
|
|
func (c *Client) ListResource(apiVersion string, kind string, namespace string, lselector *meta.LabelSelector) (*unstructured.UnstructuredList, error) {
|
2019-06-25 22:53:18 -07:00
|
|
|
options := meta.ListOptions{}
|
|
|
|
if lselector != nil {
|
|
|
|
options = meta.ListOptions{LabelSelector: helperv1.FormatLabelSelector(lselector)}
|
|
|
|
}
|
2021-01-24 11:34:02 -08:00
|
|
|
|
2020-10-15 17:54:58 -07:00
|
|
|
return c.getResourceInterface(apiVersion, kind, namespace).List(context.TODO(), options)
|
2019-05-15 07:30:22 -07:00
|
|
|
}
|
|
|
|
|
2019-12-05 11:52:13 -08:00
|
|
|
// DeleteResource deletes the specified resource
|
2020-08-07 09:47:33 +05:30
|
|
|
func (c *Client) DeleteResource(apiVersion string, kind string, namespace string, name string, dryRun bool) error {
|
2019-06-18 13:52:12 -07:00
|
|
|
options := meta.DeleteOptions{}
|
|
|
|
if dryRun {
|
|
|
|
options = meta.DeleteOptions{DryRun: []string{meta.DryRunAll}}
|
|
|
|
}
|
2020-10-15 17:54:58 -07:00
|
|
|
return c.getResourceInterface(apiVersion, kind, namespace).Delete(context.TODO(), name, options)
|
2019-05-15 07:30:22 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-22 00:16:22 -07:00
|
|
|
// CreateResource creates object for the specified resource/namespace
|
2020-08-07 09:47:33 +05:30
|
|
|
func (c *Client) CreateResource(apiVersion string, kind string, namespace string, obj interface{}, dryRun bool) (*unstructured.Unstructured, error) {
|
2019-06-18 13:52:12 -07:00
|
|
|
options := meta.CreateOptions{}
|
|
|
|
if dryRun {
|
|
|
|
options = meta.CreateOptions{DryRun: []string{meta.DryRunAll}}
|
|
|
|
}
|
2019-05-15 07:30:22 -07:00
|
|
|
// convert typed to unstructured obj
|
|
|
|
if unstructuredObj := convertToUnstructured(obj); unstructuredObj != nil {
|
2020-10-15 17:54:58 -07:00
|
|
|
return c.getResourceInterface(apiVersion, kind, namespace).Create(context.TODO(), unstructuredObj, options)
|
2019-05-15 07:30:22 -07:00
|
|
|
}
|
2021-10-12 23:29:20 +02:00
|
|
|
return nil, fmt.Errorf("unable to create resource ")
|
2019-05-15 07:30:22 -07:00
|
|
|
}
|
|
|
|
|
2019-05-22 00:16:22 -07:00
|
|
|
// UpdateResource updates object for the specified resource/namespace
|
2020-08-07 09:47:33 +05:30
|
|
|
func (c *Client) UpdateResource(apiVersion string, kind string, namespace string, obj interface{}, dryRun bool) (*unstructured.Unstructured, error) {
|
2019-06-18 13:52:12 -07:00
|
|
|
options := meta.UpdateOptions{}
|
|
|
|
if dryRun {
|
|
|
|
options = meta.UpdateOptions{DryRun: []string{meta.DryRunAll}}
|
|
|
|
}
|
2019-05-15 07:30:22 -07:00
|
|
|
// convert typed to unstructured obj
|
|
|
|
if unstructuredObj := convertToUnstructured(obj); unstructuredObj != nil {
|
2020-10-15 17:54:58 -07:00
|
|
|
return c.getResourceInterface(apiVersion, kind, namespace).Update(context.TODO(), unstructuredObj, options)
|
2019-05-15 07:30:22 -07:00
|
|
|
}
|
2021-10-12 23:29:20 +02:00
|
|
|
return nil, fmt.Errorf("unable to update resource ")
|
2019-05-15 07:30:22 -07:00
|
|
|
}
|
|
|
|
|
2019-05-15 12:29:09 -07:00
|
|
|
// UpdateStatusResource updates the resource "status" subresource
|
2020-08-07 09:47:33 +05:30
|
|
|
func (c *Client) UpdateStatusResource(apiVersion string, kind string, namespace string, obj interface{}, dryRun bool) (*unstructured.Unstructured, error) {
|
2019-06-18 13:52:12 -07:00
|
|
|
options := meta.UpdateOptions{}
|
|
|
|
if dryRun {
|
|
|
|
options = meta.UpdateOptions{DryRun: []string{meta.DryRunAll}}
|
|
|
|
}
|
2019-05-15 12:29:09 -07:00
|
|
|
// convert typed to unstructured obj
|
|
|
|
if unstructuredObj := convertToUnstructured(obj); unstructuredObj != nil {
|
2020-10-15 17:54:58 -07:00
|
|
|
return c.getResourceInterface(apiVersion, kind, namespace).UpdateStatus(context.TODO(), unstructuredObj, options)
|
2019-05-15 12:29:09 -07:00
|
|
|
}
|
2021-10-12 23:29:20 +02:00
|
|
|
return nil, fmt.Errorf("unable to update resource ")
|
2019-05-15 12:29:09 -07:00
|
|
|
}
|
|
|
|
|
2019-05-15 07:30:22 -07:00
|
|
|
func convertToUnstructured(obj interface{}) *unstructured.Unstructured {
|
2019-05-21 18:50:51 -07:00
|
|
|
unstructuredObj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&obj)
|
2019-05-15 07:30:22 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &unstructured.Unstructured{Object: unstructuredObj}
|
|
|
|
}
|
|
|
|
|
2020-01-24 12:05:53 -08:00
|
|
|
//IDiscovery provides interface to mange Kind and GVR mapping
|
2019-06-11 14:35:26 -07:00
|
|
|
type IDiscovery interface {
|
2020-08-07 09:47:33 +05:30
|
|
|
FindResource(apiVersion string, kind string) (*meta.APIResource, schema.GroupVersionResource, error)
|
2020-12-03 19:19:36 -08:00
|
|
|
GetGVRFromKind(kind string) (schema.GroupVersionResource, error)
|
2020-08-07 09:47:33 +05:30
|
|
|
GetGVRFromAPIVersionKind(apiVersion string, kind string) schema.GroupVersionResource
|
2020-02-14 18:12:28 -08:00
|
|
|
GetServerVersion() (*version.Info, error)
|
2021-02-07 20:26:56 -08:00
|
|
|
OpenAPISchema() (*openapiv2.Document, error)
|
2021-01-29 17:38:23 -08:00
|
|
|
DiscoveryCache() discovery.CachedDiscoveryInterface
|
2019-06-11 14:35:26 -07:00
|
|
|
}
|
|
|
|
|
2020-01-24 12:05:53 -08:00
|
|
|
// SetDiscovery sets the discovery client implementation
|
2019-06-11 14:35:26 -07:00
|
|
|
func (c *Client) SetDiscovery(discoveryClient IDiscovery) {
|
2019-06-20 16:52:09 -07:00
|
|
|
c.DiscoveryClient = discoveryClient
|
2019-06-11 14:35:26 -07:00
|
|
|
}
|
|
|
|
|
2020-01-24 12:05:53 -08:00
|
|
|
//ServerPreferredResources stores the cachedClient instance for discovery client
|
2019-06-11 14:35:26 -07:00
|
|
|
type ServerPreferredResources struct {
|
|
|
|
cachedClient discovery.CachedDiscoveryInterface
|
2020-03-17 11:05:20 -07:00
|
|
|
log logr.Logger
|
2019-06-11 14:35:26 -07:00
|
|
|
}
|
|
|
|
|
2021-01-29 17:38:23 -08:00
|
|
|
// DiscoveryCache gets the discovery client cache
|
|
|
|
func (c ServerPreferredResources) DiscoveryCache() discovery.CachedDiscoveryInterface {
|
|
|
|
return c.cachedClient
|
|
|
|
}
|
|
|
|
|
2019-12-16 12:55:44 -08:00
|
|
|
//Poll will keep invalidate the local cache
|
|
|
|
func (c ServerPreferredResources) Poll(resync time.Duration, stopCh <-chan struct{}) {
|
2020-03-17 11:05:20 -07:00
|
|
|
logger := c.log.WithName("Poll")
|
2019-12-16 12:55:44 -08:00
|
|
|
// start a ticker
|
|
|
|
ticker := time.NewTicker(resync)
|
|
|
|
defer func() { ticker.Stop() }()
|
2020-11-13 14:51:28 +05:30
|
|
|
logger.V(4).Info("starting registered resources sync", "period", resync)
|
2019-12-16 12:55:44 -08:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-stopCh:
|
2020-03-17 11:05:20 -07:00
|
|
|
logger.Info("stopping registered resources sync")
|
2019-12-16 12:55:44 -08:00
|
|
|
return
|
|
|
|
case <-ticker.C:
|
|
|
|
// set cache as stale
|
2020-03-17 11:05:20 -07:00
|
|
|
logger.V(6).Info("invalidating local client cache for registered resources")
|
2019-12-16 12:55:44 -08:00
|
|
|
c.cachedClient.Invalidate()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-17 09:54:13 -07:00
|
|
|
// OpenAPISchema returns the API server OpenAPI schema document
|
2021-02-07 20:26:56 -08:00
|
|
|
func (c ServerPreferredResources) OpenAPISchema() (*openapiv2.Document, error) {
|
2020-03-04 18:56:59 +05:30
|
|
|
return c.cachedClient.OpenAPISchema()
|
|
|
|
}
|
|
|
|
|
2020-05-17 09:54:13 -07:00
|
|
|
// GetGVRFromKind get the Group Version Resource from kind
|
2020-12-03 19:19:36 -08:00
|
|
|
func (c ServerPreferredResources) GetGVRFromKind(kind string) (schema.GroupVersionResource, error) {
|
2020-12-01 12:30:08 -08:00
|
|
|
if kind == "" {
|
2020-12-03 19:19:36 -08:00
|
|
|
return schema.GroupVersionResource{}, nil
|
2020-12-01 12:30:08 -08:00
|
|
|
}
|
|
|
|
|
2020-08-07 09:47:33 +05:30
|
|
|
_, gvr, err := c.FindResource("", kind)
|
2020-05-17 09:54:13 -07:00
|
|
|
if err != nil {
|
2020-05-17 14:37:05 -07:00
|
|
|
c.log.Info("schema not found", "kind", kind)
|
2020-12-03 19:19:36 -08:00
|
|
|
return schema.GroupVersionResource{}, err
|
2019-05-22 00:16:22 -07:00
|
|
|
}
|
2020-05-17 09:54:13 -07:00
|
|
|
|
2020-12-03 19:19:36 -08:00
|
|
|
return gvr, nil
|
2019-05-15 11:24:27 -07:00
|
|
|
}
|
2019-05-31 17:59:36 -07:00
|
|
|
|
2020-08-07 09:47:33 +05:30
|
|
|
// GetGVRFromAPIVersionKind get the Group Version Resource from APIVersion and kind
|
|
|
|
func (c ServerPreferredResources) GetGVRFromAPIVersionKind(apiVersion string, kind string) schema.GroupVersionResource {
|
|
|
|
_, gvr, err := c.FindResource(apiVersion, kind)
|
|
|
|
if err != nil {
|
2020-12-01 12:30:08 -08:00
|
|
|
c.log.Info("schema not found", "kind", kind, "apiVersion", apiVersion, "error : ", err)
|
2020-08-07 09:47:33 +05:30
|
|
|
return schema.GroupVersionResource{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return gvr
|
|
|
|
}
|
|
|
|
|
2020-05-17 09:54:13 -07:00
|
|
|
// GetServerVersion returns the server version of the cluster
|
2020-02-14 18:12:28 -08:00
|
|
|
func (c ServerPreferredResources) GetServerVersion() (*version.Info, error) {
|
|
|
|
return c.cachedClient.ServerVersion()
|
|
|
|
}
|
|
|
|
|
2020-05-17 09:54:13 -07:00
|
|
|
// FindResource finds an API resource that matches 'kind'. If the resource is not
|
|
|
|
// found and the Cache is not fresh, the cache is invalidated and a retry is attempted
|
2020-08-07 09:47:33 +05:30
|
|
|
func (c ServerPreferredResources) FindResource(apiVersion string, kind string) (*meta.APIResource, schema.GroupVersionResource, error) {
|
|
|
|
r, gvr, err := c.findResource(apiVersion, kind)
|
2020-05-17 09:54:13 -07:00
|
|
|
if err == nil {
|
|
|
|
return r, gvr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !c.cachedClient.Fresh() {
|
|
|
|
c.cachedClient.Invalidate()
|
2020-08-07 09:47:33 +05:30
|
|
|
if r, gvr, err = c.findResource(apiVersion, kind); err == nil {
|
2020-05-17 09:54:13 -07:00
|
|
|
return r, gvr, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, schema.GroupVersionResource{}, err
|
|
|
|
}
|
|
|
|
|
2020-08-07 09:47:33 +05:30
|
|
|
func (c ServerPreferredResources) findResource(apiVersion string, kind string) (*meta.APIResource, schema.GroupVersionResource, error) {
|
2021-01-24 11:34:02 -08:00
|
|
|
var serverResources []*meta.APIResourceList
|
2020-08-07 09:47:33 +05:30
|
|
|
var err error
|
|
|
|
if apiVersion == "" {
|
2021-01-24 11:34:02 -08:00
|
|
|
serverResources, err = c.cachedClient.ServerPreferredResources()
|
2020-08-07 09:47:33 +05:30
|
|
|
} else {
|
2021-01-24 11:34:02 -08:00
|
|
|
_, serverResources, err = c.cachedClient.ServerGroupsAndResources()
|
2020-08-07 09:47:33 +05:30
|
|
|
}
|
|
|
|
|
2019-05-31 17:59:36 -07:00
|
|
|
if err != nil {
|
2021-01-24 11:34:02 -08:00
|
|
|
if discovery.IsGroupDiscoveryFailedError(err) {
|
|
|
|
logDiscoveryErrors(err, c)
|
|
|
|
} else if isMetricsServerUnavailable(kind, err) {
|
|
|
|
c.log.V(3).Info("failed to find preferred resource version", "error", err.Error())
|
|
|
|
} else {
|
|
|
|
c.log.Error(err, "failed to find preferred resource version")
|
|
|
|
return nil, schema.GroupVersionResource{}, err
|
|
|
|
}
|
2019-05-31 17:59:36 -07:00
|
|
|
}
|
2020-05-17 09:54:13 -07:00
|
|
|
|
2021-01-24 11:34:02 -08:00
|
|
|
for _, serverResource := range serverResources {
|
|
|
|
if apiVersion != "" && serverResource.GroupVersion != apiVersion {
|
2020-08-07 09:47:33 +05:30
|
|
|
continue
|
|
|
|
}
|
2021-01-24 11:34:02 -08:00
|
|
|
|
|
|
|
for _, resource := range serverResource.APIResources {
|
2021-02-01 12:59:13 -08:00
|
|
|
if strings.Contains(resource.Name, "/") {
|
|
|
|
// skip the sub-resources like deployment/status
|
|
|
|
continue
|
|
|
|
}
|
2019-07-08 15:34:21 -07:00
|
|
|
|
2021-02-01 12:59:13 -08:00
|
|
|
// match kind or names (e.g. Namespace, namespaces, namespace)
|
|
|
|
// to allow matching API paths (e.g. /api/v1/namespaces).
|
|
|
|
if resource.Kind == kind || resource.Name == kind || resource.SingularName == kind {
|
2021-01-24 11:34:02 -08:00
|
|
|
gv, err := schema.ParseGroupVersion(serverResource.GroupVersion)
|
2019-05-31 17:59:36 -07:00
|
|
|
if err != nil {
|
2021-01-24 11:34:02 -08:00
|
|
|
c.log.Error(err, "failed to parse groupVersion", "groupVersion", serverResource.GroupVersion)
|
2020-05-17 09:54:13 -07:00
|
|
|
return nil, schema.GroupVersionResource{}, err
|
2019-05-31 17:59:36 -07:00
|
|
|
}
|
2020-05-17 09:54:13 -07:00
|
|
|
|
|
|
|
return &resource, gv.WithResource(resource.Name), nil
|
2019-05-31 17:59:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-17 09:54:13 -07:00
|
|
|
|
2020-08-07 09:47:33 +05:30
|
|
|
return nil, schema.GroupVersionResource{}, fmt.Errorf("kind '%s' not found in apiVersion '%s'", kind, apiVersion)
|
2019-05-31 17:59:36 -07:00
|
|
|
}
|
2021-01-24 11:34:02 -08:00
|
|
|
|
|
|
|
func logDiscoveryErrors(err error, c ServerPreferredResources) {
|
|
|
|
discoveryError := err.(*discovery.ErrGroupDiscoveryFailed)
|
|
|
|
for gv, e := range discoveryError.Groups {
|
2021-07-21 13:39:07 +05:30
|
|
|
if gv.Group == "custom.metrics.k8s.io" || gv.Group == "metrics.k8s.io" || gv.Group == "external.metrics.k8s.io" {
|
2021-01-24 11:34:02 -08:00
|
|
|
// These error occur when Prometheus is installed as an external metrics server
|
|
|
|
// See: https://github.com/kyverno/kyverno/issues/1490
|
|
|
|
c.log.V(3).Info("failed to retrieve metrics API group", "gv", gv)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
c.log.Error(e, "failed to retrieve API group", "gv", gv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func isMetricsServerUnavailable(kind string, err error) bool {
|
|
|
|
// error message is defined at:
|
|
|
|
// https://github.com/kubernetes/apimachinery/blob/2456ebdaba229616fab2161a615148884b46644b/pkg/api/errors/errors.go#L432
|
|
|
|
return strings.HasPrefix(kind, "metrics.k8s.io/") &&
|
|
|
|
strings.Contains(err.Error(), "the server is currently unable to handle the request")
|
|
|
|
}
|