2022-05-17 16:40:51 +02:00
|
|
|
package dclient
|
2019-05-15 07:30:22 -07:00
|
|
|
|
|
|
|
import (
|
2020-10-15 17:54:58 -07:00
|
|
|
"context"
|
2022-08-23 18:52:54 +02:00
|
|
|
"errors"
|
2019-05-15 07:30:22 -07:00
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2022-05-03 07:30:04 +02:00
|
|
|
kubeutils "github.com/kyverno/kyverno/pkg/utils/kube"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2019-05-15 07:30:22 -07:00
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
2022-05-03 07:30:04 +02:00
|
|
|
"k8s.io/apimachinery/pkg/types"
|
2019-05-22 00:16:22 -07:00
|
|
|
"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"
|
2022-05-03 07:30:04 +02:00
|
|
|
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
|
2019-05-15 07:30:22 -07:00
|
|
|
"k8s.io/client-go/rest"
|
|
|
|
)
|
|
|
|
|
2022-05-03 07:30:04 +02:00
|
|
|
type Interface interface {
|
2022-12-21 18:12:26 +01:00
|
|
|
// GetKubeClient provides typed kube client
|
|
|
|
GetKubeClient() kubernetes.Interface
|
2022-05-03 07:30:04 +02:00
|
|
|
// GetEventsInterface provides typed interface for events
|
2022-11-22 14:37:27 +01:00
|
|
|
GetEventsInterface() corev1.EventInterface
|
2022-05-03 07:30:04 +02:00
|
|
|
// GetDynamicInterface fetches underlying dynamic interface
|
|
|
|
GetDynamicInterface() dynamic.Interface
|
|
|
|
// Discovery return the discovery client implementation
|
|
|
|
Discovery() IDiscovery
|
|
|
|
// SetDiscovery sets the discovery client implementation
|
|
|
|
SetDiscovery(discoveryClient IDiscovery)
|
2022-11-22 14:37:27 +01:00
|
|
|
// RawAbsPath performs a raw call to the kubernetes API
|
2022-11-29 14:59:40 +01:00
|
|
|
RawAbsPath(ctx context.Context, path string) ([]byte, error)
|
2022-05-03 07:30:04 +02:00
|
|
|
// GetResource returns the resource in unstructured/json format
|
2022-11-29 14:59:40 +01:00
|
|
|
GetResource(ctx context.Context, apiVersion string, kind string, namespace string, name string, subresources ...string) (*unstructured.Unstructured, error)
|
2022-05-03 07:30:04 +02:00
|
|
|
// PatchResource patches the resource
|
2022-11-29 14:59:40 +01:00
|
|
|
PatchResource(ctx context.Context, apiVersion string, kind string, namespace string, name string, patch []byte) (*unstructured.Unstructured, error)
|
2022-05-03 07:30:04 +02:00
|
|
|
// ListResource returns the list of resources in unstructured/json format
|
|
|
|
// Access items using []Items
|
2022-11-29 14:59:40 +01:00
|
|
|
ListResource(ctx context.Context, apiVersion string, kind string, namespace string, lselector *metav1.LabelSelector) (*unstructured.UnstructuredList, error)
|
2022-05-03 07:30:04 +02:00
|
|
|
// DeleteResource deletes the specified resource
|
2022-11-29 14:59:40 +01:00
|
|
|
DeleteResource(ctx context.Context, apiVersion string, kind string, namespace string, name string, dryRun bool) error
|
2022-05-03 07:30:04 +02:00
|
|
|
// CreateResource creates object for the specified resource/namespace
|
2022-11-29 14:59:40 +01:00
|
|
|
CreateResource(ctx context.Context, apiVersion string, kind string, namespace string, obj interface{}, dryRun bool) (*unstructured.Unstructured, error)
|
2022-05-03 07:30:04 +02:00
|
|
|
// UpdateResource updates object for the specified resource/namespace
|
2022-12-09 22:15:23 +05:30
|
|
|
UpdateResource(ctx context.Context, apiVersion string, kind string, namespace string, obj interface{}, dryRun bool, subresources ...string) (*unstructured.Unstructured, error)
|
2022-05-03 07:30:04 +02:00
|
|
|
// UpdateStatusResource updates the resource "status" subresource
|
2022-11-29 14:59:40 +01:00
|
|
|
UpdateStatusResource(ctx context.Context, apiVersion string, kind string, namespace string, obj interface{}, dryRun bool) (*unstructured.Unstructured, error)
|
2022-05-03 07:30:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Client enables interaction with k8 resource
|
|
|
|
type client struct {
|
2022-11-22 14:37:27 +01:00
|
|
|
dyn dynamic.Interface
|
|
|
|
disco IDiscovery
|
|
|
|
rest rest.Interface
|
|
|
|
kube kubernetes.Interface
|
2019-05-15 07:30:22 -07:00
|
|
|
}
|
|
|
|
|
2022-05-03 07:30:04 +02:00
|
|
|
// NewClient creates new instance of client
|
2022-11-22 14:37:27 +01:00
|
|
|
func NewClient(
|
|
|
|
ctx context.Context,
|
|
|
|
dyn dynamic.Interface,
|
|
|
|
kube kubernetes.Interface,
|
|
|
|
resync time.Duration,
|
|
|
|
) (Interface, error) {
|
|
|
|
disco := kube.Discovery()
|
2022-05-03 07:30:04 +02:00
|
|
|
client := client{
|
2022-11-22 14:37:27 +01:00
|
|
|
dyn: dyn,
|
|
|
|
kube: kube,
|
|
|
|
rest: disco.RESTClient(),
|
2022-08-31 14:03:47 +08:00
|
|
|
}
|
2019-06-11 14:35:26 -07:00
|
|
|
// Set discovery client
|
2022-12-09 22:15:23 +05:30
|
|
|
discoveryClient := &serverResources{
|
2022-11-22 14:37:27 +01:00
|
|
|
cachedClient: memory.NewMemCacheClient(disco),
|
2021-02-01 12:59:13 -08:00
|
|
|
}
|
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
|
2022-09-30 10:12:21 +02:00
|
|
|
go discoveryClient.Poll(ctx, resync)
|
2019-06-11 14:35:26 -07:00
|
|
|
client.SetDiscovery(discoveryClient)
|
|
|
|
return &client, nil
|
2019-05-15 07:30:22 -07:00
|
|
|
}
|
|
|
|
|
2022-05-03 07:30:04 +02:00
|
|
|
// NewDynamicSharedInformerFactory returns a new instance of DynamicSharedInformerFactory
|
|
|
|
func (c *client) NewDynamicSharedInformerFactory(defaultResync time.Duration) dynamicinformer.DynamicSharedInformerFactory {
|
2022-11-22 14:37:27 +01:00
|
|
|
return dynamicinformer.NewDynamicSharedInformerFactory(c.dyn, defaultResync)
|
2020-01-07 15:13:57 -08:00
|
|
|
}
|
|
|
|
|
2022-12-21 18:12:26 +01:00
|
|
|
// GetKubeClient provides typed kube client
|
|
|
|
func (c *client) GetKubeClient() kubernetes.Interface {
|
|
|
|
return c.kube
|
|
|
|
}
|
|
|
|
|
2022-05-03 07:30:04 +02:00
|
|
|
// GetEventsInterface provides typed interface for events
|
2022-11-22 14:37:27 +01:00
|
|
|
func (c *client) GetEventsInterface() corev1.EventInterface {
|
|
|
|
return c.kube.CoreV1().Events(metav1.NamespaceAll)
|
2019-05-15 07:30:22 -07:00
|
|
|
}
|
|
|
|
|
2022-05-03 07:30:04 +02:00
|
|
|
func (c *client) getInterface(apiVersion string, kind string) dynamic.NamespaceableResourceInterface {
|
2022-11-22 14:37:27 +01:00
|
|
|
return c.dyn.Resource(c.getGroupVersionMapper(apiVersion, kind))
|
2019-05-15 07:30:22 -07:00
|
|
|
}
|
|
|
|
|
2022-05-03 07:30:04 +02:00
|
|
|
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
|
2022-05-03 07:30:04 +02:00
|
|
|
func (c *client) getGroupVersionMapper(apiVersion string, kind string) schema.GroupVersionResource {
|
2020-08-07 09:47:33 +05:30
|
|
|
if apiVersion == "" {
|
2023-03-10 08:15:48 +01:00
|
|
|
if kind == "" {
|
|
|
|
return schema.GroupVersionResource{}
|
|
|
|
}
|
|
|
|
apiVersion, kind = kubeutils.GetKindFromGVK(kind)
|
2020-08-07 09:47:33 +05:30
|
|
|
}
|
2023-03-10 08:15:48 +01:00
|
|
|
gv, err := schema.ParseGroupVersion(apiVersion)
|
|
|
|
if err != nil {
|
|
|
|
return schema.GroupVersionResource{}
|
|
|
|
}
|
|
|
|
gvr, err := c.disco.GetGVRFromGVK(gv.WithKind(kind))
|
|
|
|
if err != nil {
|
|
|
|
return schema.GroupVersionResource{}
|
|
|
|
}
|
|
|
|
return gvr
|
2019-05-15 07:30:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetResource returns the resource in unstructured/json format
|
2022-11-29 14:59:40 +01:00
|
|
|
func (c *client) GetResource(ctx context.Context, apiVersion string, kind string, namespace string, name string, subresources ...string) (*unstructured.Unstructured, error) {
|
|
|
|
return c.getResourceInterface(apiVersion, kind, namespace).Get(ctx, name, metav1.GetOptions{}, subresources...)
|
2019-05-15 07:30:22 -07:00
|
|
|
}
|
|
|
|
|
2022-11-22 14:37:27 +01:00
|
|
|
// RawAbsPath performs a raw call to the kubernetes API
|
2022-11-29 14:59:40 +01:00
|
|
|
func (c *client) RawAbsPath(ctx context.Context, path string) ([]byte, error) {
|
2022-11-22 14:37:27 +01:00
|
|
|
if c.rest == nil {
|
2022-08-23 18:52:54 +02:00
|
|
|
return nil, errors.New("rest client not supported")
|
|
|
|
}
|
2022-11-29 14:59:40 +01:00
|
|
|
return c.rest.Get().RequestURI(path).DoRaw(ctx)
|
2022-08-23 18:52:54 +02:00
|
|
|
}
|
|
|
|
|
2022-05-03 07:30:04 +02:00
|
|
|
// PatchResource patches the resource
|
2022-11-29 14:59:40 +01:00
|
|
|
func (c *client) PatchResource(ctx context.Context, apiVersion string, kind string, namespace string, name string, patch []byte) (*unstructured.Unstructured, error) {
|
|
|
|
return c.getResourceInterface(apiVersion, kind, namespace).Patch(ctx, name, types.JSONPatchType, patch, metav1.PatchOptions{})
|
2019-07-17 15:04:02 -07:00
|
|
|
}
|
|
|
|
|
2020-04-02 12:19:32 +05:30
|
|
|
// GetDynamicInterface fetches underlying dynamic interface
|
2022-05-03 07:30:04 +02:00
|
|
|
func (c *client) GetDynamicInterface() dynamic.Interface {
|
2022-11-22 14:37:27 +01:00
|
|
|
return c.dyn
|
2020-04-02 12:19:32 +05:30
|
|
|
}
|
|
|
|
|
2019-05-15 07:30:22 -07:00
|
|
|
// ListResource returns the list of resources in unstructured/json format
|
|
|
|
// Access items using []Items
|
2022-11-29 14:59:40 +01:00
|
|
|
func (c *client) ListResource(ctx context.Context, apiVersion string, kind string, namespace string, lselector *metav1.LabelSelector) (*unstructured.UnstructuredList, error) {
|
2022-05-03 07:30:04 +02:00
|
|
|
options := metav1.ListOptions{}
|
2019-06-25 22:53:18 -07:00
|
|
|
if lselector != nil {
|
2022-05-03 07:30:04 +02:00
|
|
|
options = metav1.ListOptions{LabelSelector: metav1.FormatLabelSelector(lselector)}
|
2019-06-25 22:53:18 -07:00
|
|
|
}
|
2022-11-29 14:59:40 +01:00
|
|
|
return c.getResourceInterface(apiVersion, kind, namespace).List(ctx, options)
|
2019-05-15 07:30:22 -07:00
|
|
|
}
|
|
|
|
|
2019-12-05 11:52:13 -08:00
|
|
|
// DeleteResource deletes the specified resource
|
2022-11-29 14:59:40 +01:00
|
|
|
func (c *client) DeleteResource(ctx context.Context, apiVersion string, kind string, namespace string, name string, dryRun bool) error {
|
2022-05-03 07:30:04 +02:00
|
|
|
options := metav1.DeleteOptions{}
|
2019-06-18 13:52:12 -07:00
|
|
|
if dryRun {
|
2022-05-03 07:30:04 +02:00
|
|
|
options = metav1.DeleteOptions{DryRun: []string{metav1.DryRunAll}}
|
2019-06-18 13:52:12 -07:00
|
|
|
}
|
2022-11-29 14:59:40 +01:00
|
|
|
return c.getResourceInterface(apiVersion, kind, namespace).Delete(ctx, 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
|
2022-11-29 14:59:40 +01:00
|
|
|
func (c *client) CreateResource(ctx context.Context, apiVersion string, kind string, namespace string, obj interface{}, dryRun bool) (*unstructured.Unstructured, error) {
|
2022-05-03 07:30:04 +02:00
|
|
|
options := metav1.CreateOptions{}
|
2019-06-18 13:52:12 -07:00
|
|
|
if dryRun {
|
2022-05-03 07:30:04 +02:00
|
|
|
options = metav1.CreateOptions{DryRun: []string{metav1.DryRunAll}}
|
2019-06-18 13:52:12 -07:00
|
|
|
}
|
2019-05-15 07:30:22 -07:00
|
|
|
// convert typed to unstructured obj
|
2023-01-03 13:02:15 +01:00
|
|
|
if unstructuredObj, err := kubeutils.ObjToUnstructured(obj); err == nil && unstructuredObj != nil {
|
2022-11-29 14:59:40 +01:00
|
|
|
return c.getResourceInterface(apiVersion, kind, namespace).Create(ctx, 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
|
2022-12-09 22:15:23 +05:30
|
|
|
func (c *client) UpdateResource(ctx context.Context, apiVersion string, kind string, namespace string, obj interface{}, dryRun bool, subresources ...string) (*unstructured.Unstructured, error) {
|
2022-05-03 07:30:04 +02:00
|
|
|
options := metav1.UpdateOptions{}
|
2019-06-18 13:52:12 -07:00
|
|
|
if dryRun {
|
2022-05-03 07:30:04 +02:00
|
|
|
options = metav1.UpdateOptions{DryRun: []string{metav1.DryRunAll}}
|
2019-06-18 13:52:12 -07:00
|
|
|
}
|
2019-05-15 07:30:22 -07:00
|
|
|
// convert typed to unstructured obj
|
2023-01-03 13:02:15 +01:00
|
|
|
if unstructuredObj, err := kubeutils.ObjToUnstructured(obj); err == nil && unstructuredObj != nil {
|
2022-12-09 22:15:23 +05:30
|
|
|
return c.getResourceInterface(apiVersion, kind, namespace).Update(ctx, unstructuredObj, options, subresources...)
|
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
|
2022-11-29 14:59:40 +01:00
|
|
|
func (c *client) UpdateStatusResource(ctx context.Context, apiVersion string, kind string, namespace string, obj interface{}, dryRun bool) (*unstructured.Unstructured, error) {
|
2022-05-03 07:30:04 +02:00
|
|
|
options := metav1.UpdateOptions{}
|
2019-06-18 13:52:12 -07:00
|
|
|
if dryRun {
|
2022-05-03 07:30:04 +02:00
|
|
|
options = metav1.UpdateOptions{DryRun: []string{metav1.DryRunAll}}
|
2019-06-18 13:52:12 -07:00
|
|
|
}
|
2019-05-15 12:29:09 -07:00
|
|
|
// convert typed to unstructured obj
|
2023-01-03 13:02:15 +01:00
|
|
|
if unstructuredObj, err := kubeutils.ObjToUnstructured(obj); err == nil && unstructuredObj != nil {
|
2022-11-29 14:59:40 +01:00
|
|
|
return c.getResourceInterface(apiVersion, kind, namespace).UpdateStatus(ctx, 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
|
|
|
}
|
|
|
|
|
2022-05-03 07:30:04 +02:00
|
|
|
// Discovery return the discovery client implementation
|
|
|
|
func (c *client) Discovery() IDiscovery {
|
2022-11-22 14:37:27 +01:00
|
|
|
return c.disco
|
2019-06-11 14:35:26 -07:00
|
|
|
}
|
|
|
|
|
2020-01-24 12:05:53 -08:00
|
|
|
// SetDiscovery sets the discovery client implementation
|
2022-05-03 07:30:04 +02:00
|
|
|
func (c *client) SetDiscovery(discoveryClient IDiscovery) {
|
2022-11-22 14:37:27 +01:00
|
|
|
c.disco = discoveryClient
|
2022-08-31 14:03:47 +08:00
|
|
|
}
|