1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-14 11:57:48 +00:00
kyverno/pkg/clients/dynamic/client.go
Charles-Edouard Brétéché ecbdaae292
feat: add logging support to instrumented clients (#5438)
* feat: add discovery support in instrumented clients

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* fix tracing

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* feat: add logging support to instrumented clients

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
Signed-off-by: Charles-Edouard Brétéché <charled.breteche@gmail.com>
2022-11-22 15:12:30 +00:00

108 lines
3.1 KiB
Go

package clientset
import (
"github.com/go-logr/logr"
"github.com/kyverno/kyverno/pkg/clients/dynamic/resource"
"github.com/kyverno/kyverno/pkg/metrics"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
)
type namespaceableInterface interface {
Namespace(string) dynamic.ResourceInterface
}
func WrapWithMetrics(inner dynamic.Interface, metrics metrics.MetricsConfigManager, clientType metrics.ClientType) dynamic.Interface {
return &withMetrics{inner, metrics, clientType}
}
func WrapWithTracing(inner dynamic.Interface) dynamic.Interface {
return &withTracing{inner}
}
func WrapWithLogging(inner dynamic.Interface, logger logr.Logger) dynamic.Interface {
return &withLogging{inner, logger}
}
type withMetrics struct {
inner dynamic.Interface
metrics metrics.MetricsConfigManager
clientType metrics.ClientType
}
type withMetricsNamespaceable struct {
inner namespaceableInterface
metrics metrics.MetricsConfigManager
resource string
clientType metrics.ClientType
}
func (c *withMetricsNamespaceable) Namespace(namespace string) dynamic.ResourceInterface {
recorder := metrics.NamespacedClientQueryRecorder(c.metrics, namespace, c.resource, c.clientType)
return resource.WithMetrics(c.inner.Namespace(namespace), recorder)
}
func (c *withMetrics) Resource(gvr schema.GroupVersionResource) dynamic.NamespaceableResourceInterface {
recorder := metrics.ClusteredClientQueryRecorder(c.metrics, gvr.Resource, c.clientType)
inner := c.inner.Resource(gvr)
return struct {
dynamic.ResourceInterface
namespaceableInterface
}{
resource.WithMetrics(inner, recorder),
&withMetricsNamespaceable{inner, c.metrics, gvr.Resource, c.clientType},
}
}
type withTracing struct {
inner dynamic.Interface
}
type withTracingNamespaceable struct {
inner namespaceableInterface
client string
kind string
}
func (c *withTracingNamespaceable) Namespace(namespace string) dynamic.ResourceInterface {
return resource.WithTracing(c.inner.Namespace(namespace), c.client, c.kind)
}
func (c *withTracing) Resource(gvr schema.GroupVersionResource) dynamic.NamespaceableResourceInterface {
inner := c.inner.Resource(gvr)
client := gvr.GroupResource().String()
kind := gvr.Resource
return struct {
dynamic.ResourceInterface
namespaceableInterface
}{
resource.WithTracing(inner, client, kind),
&withTracingNamespaceable{inner, client, kind},
}
}
type withLogging struct {
inner dynamic.Interface
logger logr.Logger
}
type withLoggingNamespaceable struct {
inner namespaceableInterface
logger logr.Logger
}
func (c *withLoggingNamespaceable) Namespace(namespace string) dynamic.ResourceInterface {
return resource.WithLogging(c.inner.Namespace(namespace), c.logger)
}
func (c *withLogging) Resource(gvr schema.GroupVersionResource) dynamic.NamespaceableResourceInterface {
logger := c.logger.WithValues("group", gvr.Group, "version", gvr.Version, "resource", gvr.Resource)
inner := c.inner.Resource(gvr)
return struct {
dynamic.ResourceInterface
namespaceableInterface
}{
resource.WithLogging(inner, logger),
&withLoggingNamespaceable{inner, logger},
}
}