2022-11-17 16:01:30 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"reflect"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
|
|
|
|
"github.com/kyverno/kyverno/pkg/client/clientset/versioned"
|
2023-02-10 20:54:53 +00:00
|
|
|
apiserver "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
|
2022-11-18 10:07:29 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/sets"
|
2022-11-22 09:10:07 +00:00
|
|
|
"k8s.io/client-go/dynamic"
|
2022-11-17 16:01:30 +00:00
|
|
|
"k8s.io/client-go/kubernetes"
|
2022-11-22 09:10:07 +00:00
|
|
|
"k8s.io/client-go/metadata"
|
2023-02-10 20:54:53 +00:00
|
|
|
aggregator "k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset"
|
2022-11-22 09:10:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
resourceTpl = `
|
|
|
|
package resource
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-11-22 15:12:30 +00:00
|
|
|
"time"
|
|
|
|
"github.com/go-logr/logr"
|
2022-11-22 09:10:07 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/metrics"
|
|
|
|
"github.com/kyverno/kyverno/pkg/tracing"
|
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/codes"
|
2022-12-09 09:09:11 +00:00
|
|
|
"go.opentelemetry.io/otel/trace"
|
2022-11-22 15:12:30 +00:00
|
|
|
"go.uber.org/multierr"
|
2022-11-22 09:10:07 +00:00
|
|
|
{{- range $package := Packages .Target.Type }}
|
|
|
|
{{ Pkg $package }} {{ Quote $package }}
|
|
|
|
{{- end }}
|
|
|
|
)
|
|
|
|
|
2022-11-22 15:12:30 +00:00
|
|
|
func WithLogging(inner {{ GoType .Target.Type }}, logger logr.Logger) {{ GoType .Target.Type }} {
|
|
|
|
return &withLogging{inner, logger}
|
|
|
|
}
|
|
|
|
|
2022-11-22 09:10:07 +00:00
|
|
|
func WithMetrics(inner {{ GoType .Target.Type }}, recorder metrics.Recorder) {{ GoType .Target.Type }} {
|
|
|
|
return &withMetrics{inner, recorder}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithTracing(inner {{ GoType .Target.Type }}, client, kind string) {{ GoType .Target.Type }} {
|
|
|
|
return &withTracing{inner, client, kind}
|
|
|
|
}
|
|
|
|
|
2022-11-22 15:12:30 +00:00
|
|
|
type withLogging struct {
|
|
|
|
inner {{ GoType .Target.Type }}
|
|
|
|
logger logr.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
{{- range $operation := .Target.Operations }}
|
|
|
|
func (c *withLogging) {{ $operation.Method.Name }}(
|
|
|
|
{{- range $i, $arg := Args $operation.Method -}}
|
|
|
|
{{- if $arg.IsVariadic -}}
|
|
|
|
arg{{ $i }} ...{{ GoType $arg.Type.Elem }},
|
|
|
|
{{- else -}}
|
|
|
|
arg{{ $i }} {{ GoType $arg.Type }},
|
|
|
|
{{- end -}}
|
|
|
|
{{- end -}}
|
|
|
|
) (
|
|
|
|
{{- range $return := Returns $operation.Method -}}
|
|
|
|
{{ GoType $return }},
|
|
|
|
{{- end -}}
|
|
|
|
) {
|
|
|
|
start := time.Now()
|
|
|
|
logger := c.logger.WithValues("operation", {{ Quote $operation.Method.Name }})
|
|
|
|
{{ range $i, $ret := Returns $operation.Method }}ret{{ $i }}{{ if not $ret.IsLast -}},{{- end }} {{ end }} := c.inner.{{ $operation.Method.Name }}(
|
|
|
|
{{- range $i, $arg := Args $operation.Method -}}
|
|
|
|
{{- if $arg.IsVariadic -}}
|
|
|
|
arg{{ $i }}...,
|
|
|
|
{{- else -}}
|
|
|
|
arg{{ $i }},
|
|
|
|
{{- end -}}
|
|
|
|
{{- end -}}
|
|
|
|
)
|
|
|
|
{{- if $operation.HasError }}
|
|
|
|
if err := multierr.Combine(
|
|
|
|
{{- range $i, $ret := Returns $operation.Method -}}
|
|
|
|
{{- if $ret.IsError -}}
|
|
|
|
ret{{ $i }},
|
|
|
|
{{- end -}}
|
|
|
|
{{- end -}}
|
|
|
|
); err != nil {
|
|
|
|
logger.Error(err, "{{ $operation.Method.Name }} failed", "duration", time.Since(start))
|
|
|
|
} else {
|
|
|
|
logger.Info("{{ $operation.Method.Name }} done", "duration", time.Since(start))
|
|
|
|
}
|
|
|
|
{{- else }}
|
|
|
|
logger.Info("{{ $operation.Method.Name }} done", "duration", time.Since(start))
|
|
|
|
{{- end }}
|
|
|
|
return {{ range $i, $ret := Returns $operation.Method -}}
|
|
|
|
ret{{ $i }}{{ if not $ret.IsLast -}},{{- end }}
|
|
|
|
{{- end }}
|
|
|
|
}
|
|
|
|
{{- end }}
|
|
|
|
|
2022-11-22 09:10:07 +00:00
|
|
|
type withMetrics struct {
|
|
|
|
inner {{ GoType .Target.Type }}
|
|
|
|
recorder metrics.Recorder
|
|
|
|
}
|
|
|
|
|
|
|
|
{{- range $operation := .Target.Operations }}
|
|
|
|
func (c *withMetrics) {{ $operation.Method.Name }}(
|
|
|
|
{{- range $i, $arg := Args $operation.Method -}}
|
|
|
|
{{- if $arg.IsVariadic -}}
|
|
|
|
arg{{ $i }} ...{{ GoType $arg.Type.Elem }},
|
|
|
|
{{- else -}}
|
|
|
|
arg{{ $i }} {{ GoType $arg.Type }},
|
|
|
|
{{- end -}}
|
|
|
|
{{- end -}}
|
|
|
|
) (
|
|
|
|
{{- range $return := Returns $operation.Method -}}
|
|
|
|
{{ GoType $return }},
|
|
|
|
{{- end -}}
|
|
|
|
) {
|
2022-12-09 09:09:11 +00:00
|
|
|
{{- if $operation.HasContext }}
|
|
|
|
defer c.recorder.RecordWithContext(arg0, {{ Quote (SnakeCase $operation.Method.Name) }})
|
|
|
|
{{- else }}
|
2022-11-22 09:10:07 +00:00
|
|
|
defer c.recorder.Record({{ Quote (SnakeCase $operation.Method.Name) }})
|
2022-12-09 09:09:11 +00:00
|
|
|
{{- end }}
|
2022-11-22 09:10:07 +00:00
|
|
|
return c.inner.{{ $operation.Method.Name }}(
|
|
|
|
{{- range $i, $arg := Args $operation.Method -}}
|
|
|
|
{{- if $arg.IsVariadic -}}
|
|
|
|
arg{{ $i }}...,
|
|
|
|
{{- else -}}
|
|
|
|
arg{{ $i }},
|
|
|
|
{{- end -}}
|
|
|
|
{{- end -}}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
{{- end }}
|
|
|
|
|
|
|
|
type withTracing struct {
|
|
|
|
inner {{ GoType .Target.Type }}
|
|
|
|
client string
|
|
|
|
kind string
|
|
|
|
}
|
|
|
|
|
|
|
|
{{- range $operation := .Target.Operations }}
|
|
|
|
func (c *withTracing) {{ $operation.Method.Name }}(
|
|
|
|
{{- range $i, $arg := Args $operation.Method -}}
|
|
|
|
{{- if $arg.IsVariadic -}}
|
|
|
|
arg{{ $i }} ...{{ GoType $arg.Type.Elem }},
|
|
|
|
{{- else -}}
|
|
|
|
arg{{ $i }} {{ GoType $arg.Type }},
|
|
|
|
{{- end -}}
|
|
|
|
{{- end -}}
|
|
|
|
) (
|
|
|
|
{{- range $return := Returns $operation.Method -}}
|
|
|
|
{{ GoType $return }},
|
|
|
|
{{- end -}}
|
|
|
|
) {
|
2022-12-08 14:52:12 +00:00
|
|
|
{{- if not $operation.HasContext }}
|
|
|
|
return c.inner.{{ $operation.Method.Name }}(
|
|
|
|
{{- range $i, $arg := Args $operation.Method -}}
|
|
|
|
{{- if $arg.IsVariadic -}}
|
|
|
|
arg{{ $i }}...,
|
|
|
|
{{- else -}}
|
|
|
|
arg{{ $i }},
|
|
|
|
{{- end -}}
|
|
|
|
{{- end -}}
|
|
|
|
)
|
|
|
|
{{- else }}
|
2022-12-09 09:09:11 +00:00
|
|
|
var span trace.Span
|
|
|
|
if tracing.IsInSpan(arg0) {
|
2022-12-11 20:39:37 +00:00
|
|
|
arg0, span = tracing.StartChildSpan(
|
2022-12-09 09:09:11 +00:00
|
|
|
arg0,
|
|
|
|
"",
|
|
|
|
fmt.Sprintf("KUBE %s/%s/%s", c.client, c.kind, {{ Quote $operation.Method.Name }}),
|
2022-12-11 20:39:37 +00:00
|
|
|
trace.WithAttributes(
|
|
|
|
tracing.KubeClientGroupKey.String(c.client),
|
|
|
|
tracing.KubeClientKindKey.String(c.kind),
|
|
|
|
tracing.KubeClientOperationKey.String({{ Quote $operation.Method.Name }}),
|
|
|
|
),
|
2022-12-09 09:09:11 +00:00
|
|
|
)
|
|
|
|
defer span.End()
|
|
|
|
}
|
2022-11-22 09:10:07 +00:00
|
|
|
{{ range $i, $ret := Returns $operation.Method }}ret{{ $i }}{{ if not $ret.IsLast -}},{{- end }} {{ end }} := c.inner.{{ $operation.Method.Name }}(
|
|
|
|
{{- range $i, $arg := Args $operation.Method -}}
|
|
|
|
{{- if $arg.IsVariadic -}}
|
|
|
|
arg{{ $i }}...,
|
|
|
|
{{- else -}}
|
|
|
|
arg{{ $i }},
|
|
|
|
{{- end -}}
|
|
|
|
{{- end -}}
|
|
|
|
)
|
2022-12-09 09:09:11 +00:00
|
|
|
if span != nil {
|
|
|
|
{{- if $operation.HasError }}
|
|
|
|
{{- range $i, $ret := Returns $operation.Method }}
|
|
|
|
{{- if $ret.IsError }}
|
|
|
|
tracing.SetSpanStatus(span, ret{{ $i }})
|
|
|
|
{{- end }}
|
|
|
|
{{- end }}
|
|
|
|
{{- end }}
|
|
|
|
}
|
2022-11-22 09:10:07 +00:00
|
|
|
return {{ range $i, $ret := Returns $operation.Method -}}
|
|
|
|
ret{{ $i }}{{ if not $ret.IsLast -}},{{- end }}
|
|
|
|
{{- end }}
|
2022-12-08 14:52:12 +00:00
|
|
|
{{- end }}
|
2022-11-22 09:10:07 +00:00
|
|
|
}
|
|
|
|
{{- end }}
|
|
|
|
`
|
|
|
|
clientTpl = `
|
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2022-11-22 15:12:30 +00:00
|
|
|
"github.com/go-logr/logr"
|
2022-11-22 09:10:07 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/metrics"
|
2022-11-22 15:12:30 +00:00
|
|
|
"k8s.io/client-go/rest"
|
2022-11-22 09:10:07 +00:00
|
|
|
{{- range $package := Packages .Target.Type }}
|
|
|
|
{{ Pkg $package }} {{ Quote $package }}
|
|
|
|
{{- end }}
|
|
|
|
{{- range $method, $resource := .Target.Resources }}
|
|
|
|
{{ ToLower $method.Name }} "github.com/kyverno/kyverno/{{ $.Folder }}/{{ ToLower $method.Name }}"
|
|
|
|
{{- end }}
|
|
|
|
)
|
|
|
|
|
|
|
|
func WithMetrics(inner {{ GoType .Target.Type }}, metrics metrics.MetricsConfigManager, clientType metrics.ClientType) {{ GoType .Target.Type }} {
|
|
|
|
return &withMetrics{inner, metrics, clientType}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithTracing(inner {{ GoType .Target.Type }}, client string) {{ GoType .Target.Type }} {
|
|
|
|
return &withTracing{inner, client}
|
|
|
|
}
|
|
|
|
|
2022-11-22 15:12:30 +00:00
|
|
|
func WithLogging(inner {{ GoType .Target.Type }}, logger logr.Logger) {{ GoType .Target.Type }} {
|
|
|
|
return &withLogging{inner, logger}
|
|
|
|
}
|
|
|
|
|
2022-11-22 09:10:07 +00:00
|
|
|
type withMetrics struct {
|
|
|
|
inner {{ GoType .Target }}
|
|
|
|
metrics metrics.MetricsConfigManager
|
|
|
|
clientType metrics.ClientType
|
|
|
|
}
|
|
|
|
func (c *withMetrics) RESTClient() rest.Interface {
|
|
|
|
return c.inner.RESTClient()
|
|
|
|
}
|
|
|
|
{{- range $method, $resource := .Target.Resources }}
|
|
|
|
func (c *withMetrics) {{ $method.Name }}({{- if $method.IsNamespaced -}}namespace string{{- end -}}) {{ GoType $resource.Type }} {
|
|
|
|
{{- if $method.IsNamespaced }}
|
|
|
|
recorder := metrics.NamespacedClientQueryRecorder(c.metrics, namespace, {{ Quote $resource.Kind }}, c.clientType)
|
|
|
|
{{- else }}
|
|
|
|
recorder := metrics.ClusteredClientQueryRecorder(c.metrics, {{ Quote $resource.Kind }}, c.clientType)
|
|
|
|
{{- end }}
|
|
|
|
return {{ ToLower $method.Name }}.WithMetrics(c.inner.{{ $method.Name }}(
|
|
|
|
{{- if $method.IsNamespaced -}}namespace{{- end -}}
|
|
|
|
), recorder)
|
|
|
|
}
|
|
|
|
{{- end }}
|
|
|
|
|
|
|
|
type withTracing struct {
|
|
|
|
inner {{ GoType .Target }}
|
|
|
|
client string
|
|
|
|
}
|
|
|
|
func (c *withTracing) RESTClient() rest.Interface {
|
|
|
|
return c.inner.RESTClient()
|
|
|
|
}
|
|
|
|
{{- range $method, $resource := .Target.Resources }}
|
|
|
|
func (c *withTracing) {{ $method.Name }}({{- if $method.IsNamespaced -}}namespace string{{- end -}}) {{ GoType $resource.Type }} {
|
|
|
|
return {{ ToLower $method.Name }}.WithTracing(c.inner.{{ $method.Name }}(
|
|
|
|
{{- if $method.IsNamespaced -}}namespace{{- end -}}), c.client, {{ Quote $resource.Kind -}}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
{{- end }}
|
2022-11-22 15:12:30 +00:00
|
|
|
|
|
|
|
type withLogging struct {
|
|
|
|
inner {{ GoType .Target }}
|
|
|
|
logger logr.Logger
|
|
|
|
}
|
|
|
|
func (c *withLogging) RESTClient() rest.Interface {
|
|
|
|
return c.inner.RESTClient()
|
|
|
|
}
|
|
|
|
{{- range $method, $resource := .Target.Resources }}
|
|
|
|
func (c *withLogging) {{ $method.Name }}({{- if $method.IsNamespaced -}}namespace string{{- end -}}) {{ GoType $resource.Type }} {
|
|
|
|
return {{ ToLower $method.Name }}.WithLogging(c.inner.{{ $method.Name }}(
|
|
|
|
{{- if $method.IsNamespaced -}}namespace{{- end -}}), c.logger.WithValues("resource", {{ Quote $method.Name }})
|
|
|
|
{{- if $method.IsNamespaced -}}.WithValues("namespace", namespace){{- end -}}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
{{- end }}
|
2022-11-22 09:10:07 +00:00
|
|
|
`
|
|
|
|
clientsetTpl = `
|
|
|
|
package clientset
|
|
|
|
|
|
|
|
import (
|
2022-11-22 15:12:30 +00:00
|
|
|
"github.com/go-logr/logr"
|
2022-11-22 09:10:07 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/metrics"
|
|
|
|
{{- range $package := Packages .Target.Type }}
|
|
|
|
{{ Pkg $package }} {{ Quote $package }}
|
|
|
|
{{- end }}
|
2022-11-22 14:26:49 +00:00
|
|
|
{{- range $resourceMethod, $resource := .Target.Resources }}
|
|
|
|
{{ ToLower $resourceMethod.Name }} "github.com/kyverno/kyverno/{{ $.Folder }}/{{ ToLower $resourceMethod.Name }}"
|
|
|
|
{{- end }}
|
2022-11-22 09:10:07 +00:00
|
|
|
{{- range $clientMethod, $client := .Target.Clients }}
|
|
|
|
{{ ToLower $clientMethod.Name }} "github.com/kyverno/kyverno/{{ $.Folder }}/{{ ToLower $clientMethod.Name }}"
|
|
|
|
{{- end }}
|
|
|
|
)
|
|
|
|
|
|
|
|
type clientset struct {
|
2022-11-22 14:26:49 +00:00
|
|
|
{{- range $resourceMethod, $resource := .Target.Resources }}
|
|
|
|
{{ ToLower $resourceMethod.Name }} {{ GoType $resource.Type }}
|
|
|
|
{{- end }}
|
2022-11-22 09:10:07 +00:00
|
|
|
{{- range $clientMethod, $client := .Target.Clients }}
|
|
|
|
{{ ToLower $clientMethod.Name }} {{ GoType $client.Type }}
|
|
|
|
{{- end }}
|
|
|
|
}
|
|
|
|
|
2022-11-22 14:26:49 +00:00
|
|
|
{{- range $resourceMethod, $resource := .Target.Resources }}
|
|
|
|
func (c *clientset) {{ $resourceMethod.Name }}() {{ GoType $resource.Type }}{
|
|
|
|
return c.{{ ToLower $resourceMethod.Name }}
|
2022-11-22 09:10:07 +00:00
|
|
|
}
|
2022-11-22 14:26:49 +00:00
|
|
|
{{- end }}
|
2022-11-22 09:10:07 +00:00
|
|
|
|
|
|
|
{{- range $clientMethod, $client := .Target.Clients }}
|
|
|
|
func (c *clientset) {{ $clientMethod.Name }}() {{ GoType $client.Type }}{
|
|
|
|
return c.{{ ToLower $clientMethod.Name }}
|
|
|
|
}
|
|
|
|
{{- end }}
|
|
|
|
|
2022-11-22 14:26:49 +00:00
|
|
|
func WrapWithMetrics(inner {{ GoType .Target }}, m metrics.MetricsConfigManager, clientType metrics.ClientType) {{ GoType .Target }} {
|
2022-11-22 09:10:07 +00:00
|
|
|
return &clientset{
|
2022-11-22 14:26:49 +00:00
|
|
|
{{- range $resourceMethod, $resource := .Target.Resources }}
|
|
|
|
{{ ToLower $resourceMethod.Name }}: {{ ToLower $resourceMethod.Name }}.WithMetrics(inner.{{ $resourceMethod.Name }}(), metrics.ClusteredClientQueryRecorder(m, {{ Quote $resource.Kind }}, clientType)),
|
|
|
|
{{- end }}
|
2022-11-22 09:10:07 +00:00
|
|
|
{{- range $clientMethod, $client := .Target.Clients }}
|
2022-11-22 14:26:49 +00:00
|
|
|
{{ ToLower $clientMethod.Name }}: {{ ToLower $clientMethod.Name }}.WithMetrics(inner.{{ $clientMethod.Name }}(), m, clientType),
|
2022-11-22 09:10:07 +00:00
|
|
|
{{- end }}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WrapWithTracing(inner {{ GoType .Target }}) {{ GoType .Target }} {
|
|
|
|
return &clientset{
|
2022-11-22 14:26:49 +00:00
|
|
|
{{- range $resourceMethod, $resource := .Target.Resources }}
|
|
|
|
{{ ToLower $resourceMethod.Name }}: {{ ToLower $resourceMethod.Name }}.WithTracing(inner.{{ $resourceMethod.Name }}(), {{ Quote $resourceMethod.Name }}, ""),
|
|
|
|
{{- end }}
|
2022-11-22 09:10:07 +00:00
|
|
|
{{- range $clientMethod, $client := .Target.Clients }}
|
|
|
|
{{ ToLower $clientMethod.Name }}: {{ ToLower $clientMethod.Name }}.WithTracing(inner.{{ $clientMethod.Name }}(), {{ Quote $clientMethod.Name }}),
|
|
|
|
{{- end }}
|
|
|
|
}
|
|
|
|
}
|
2022-11-22 15:12:30 +00:00
|
|
|
|
|
|
|
func WrapWithLogging(inner {{ GoType .Target }}, logger logr.Logger) {{ GoType .Target }} {
|
|
|
|
return &clientset{
|
|
|
|
{{- range $resourceMethod, $resource := .Target.Resources }}
|
|
|
|
{{ ToLower $resourceMethod.Name }}: {{ ToLower $resourceMethod.Name }}.WithLogging(inner.{{ $resourceMethod.Name }}(), logger.WithValues("group", {{ Quote $resourceMethod.Name }})),
|
|
|
|
{{- end }}
|
|
|
|
{{- range $clientMethod, $client := .Target.Clients }}
|
|
|
|
{{ ToLower $clientMethod.Name }}: {{ ToLower $clientMethod.Name }}.WithLogging(inner.{{ $clientMethod.Name }}(), logger.WithValues("group", {{ Quote $clientMethod.Name }})),
|
|
|
|
{{- end }}
|
|
|
|
}
|
|
|
|
}
|
2022-11-22 09:10:07 +00:00
|
|
|
`
|
|
|
|
interfaceTpl = `
|
|
|
|
package clientset
|
|
|
|
|
|
|
|
import (
|
2022-11-22 15:12:30 +00:00
|
|
|
"github.com/go-logr/logr"
|
2022-11-22 09:10:07 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/metrics"
|
|
|
|
{{- range $package := Packages .Target.Type }}
|
|
|
|
{{ Pkg $package }} {{ Quote $package }}
|
|
|
|
{{- end }}
|
|
|
|
{{- range $clientMethod, $client := .Target.Clients }}
|
|
|
|
{{ ToLower $clientMethod.Name }} "github.com/kyverno/kyverno/{{ $.Folder }}/{{ ToLower $clientMethod.Name }}"
|
|
|
|
{{- end }}
|
|
|
|
)
|
|
|
|
|
|
|
|
type Interface interface {
|
|
|
|
{{ GoType .Target.Type }}
|
2022-11-22 15:12:30 +00:00
|
|
|
WithMetrics(metrics.MetricsConfigManager, metrics.ClientType) Interface
|
2022-11-22 09:10:07 +00:00
|
|
|
WithTracing() Interface
|
2022-11-22 15:12:30 +00:00
|
|
|
WithLogging(logr.Logger) Interface
|
2022-11-22 09:10:07 +00:00
|
|
|
}
|
|
|
|
|
2022-11-22 15:12:30 +00:00
|
|
|
func From(inner {{ GoType .Target }}, opts ...NewOption) Interface {
|
|
|
|
i := from(inner)
|
|
|
|
for _, opt := range opts {
|
|
|
|
i = opt(i)
|
|
|
|
}
|
|
|
|
return i
|
2022-11-22 09:10:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type NewOption func (Interface) Interface
|
|
|
|
|
2022-11-22 15:12:30 +00:00
|
|
|
func WithMetrics(m metrics.MetricsConfigManager, t metrics.ClientType) NewOption {
|
|
|
|
return func(i Interface) Interface {
|
|
|
|
return i.WithMetrics(m, t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithTracing() NewOption {
|
|
|
|
return func(i Interface) Interface {
|
|
|
|
return i.WithTracing()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithLogging(logger logr.Logger) NewOption {
|
|
|
|
return func(i Interface) Interface {
|
|
|
|
return i.WithLogging(logger)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-22 09:10:07 +00:00
|
|
|
func NewForConfig(c *rest.Config, opts ...NewOption) (Interface, error) {
|
|
|
|
inner, err := {{ Pkg .Target.Type.PkgPath }}.NewForConfig(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return From(inner, opts...), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewForConfigAndClient(c *rest.Config, httpClient *http.Client, opts ...NewOption) (Interface, error) {
|
|
|
|
inner, err := {{ Pkg .Target.Type.PkgPath }}.NewForConfigAndClient(c, httpClient)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return From(inner, opts...), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewForConfigOrDie(c *rest.Config, opts ...NewOption) Interface {
|
|
|
|
return From({{ Pkg .Target.Type.PkgPath }}.NewForConfigOrDie(c), opts...)
|
|
|
|
}
|
|
|
|
|
2022-11-22 15:12:30 +00:00
|
|
|
type wrapper struct {
|
|
|
|
{{ GoType .Target.Type }}
|
2022-11-22 09:10:07 +00:00
|
|
|
}
|
|
|
|
|
2022-11-22 15:12:30 +00:00
|
|
|
func from(inner {{ GoType .Target }}, opts ...NewOption) Interface {
|
|
|
|
return &wrapper{inner}
|
2022-11-22 09:10:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *wrapper) WithMetrics(m metrics.MetricsConfigManager, t metrics.ClientType) Interface {
|
|
|
|
return from(WrapWithMetrics(i, m, t))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *wrapper) WithTracing() Interface {
|
|
|
|
return from(WrapWithTracing(i))
|
|
|
|
}
|
|
|
|
|
2022-11-22 15:12:30 +00:00
|
|
|
func (i *wrapper) WithLogging(logger logr.Logger) Interface {
|
|
|
|
return from(WrapWithLogging(i, logger))
|
2022-11-22 09:10:07 +00:00
|
|
|
}
|
|
|
|
`
|
2022-11-17 16:01:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)")
|
|
|
|
matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])")
|
|
|
|
)
|
|
|
|
|
2022-11-18 10:07:29 +00:00
|
|
|
type arg struct {
|
2022-11-20 13:42:57 +00:00
|
|
|
reflect.Type
|
2022-11-18 10:07:29 +00:00
|
|
|
IsVariadic bool
|
|
|
|
}
|
|
|
|
|
2022-11-20 13:42:57 +00:00
|
|
|
func (a arg) IsError() bool {
|
|
|
|
return goType(a.Type) == "error"
|
|
|
|
}
|
|
|
|
|
|
|
|
type ret struct {
|
|
|
|
reflect.Type
|
|
|
|
IsLast bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r ret) IsError() bool {
|
|
|
|
return goType(r.Type) == "error"
|
|
|
|
}
|
|
|
|
|
2022-11-18 10:07:29 +00:00
|
|
|
type operation struct {
|
2022-11-22 14:26:49 +00:00
|
|
|
reflect.Method
|
2022-11-18 10:07:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o operation) HasContext() bool {
|
|
|
|
return o.Method.Type.NumIn() > 0 && goType(o.Method.Type.In(0)) == "context.Context"
|
|
|
|
}
|
|
|
|
|
2022-11-20 13:42:57 +00:00
|
|
|
func (o operation) HasError() bool {
|
2022-11-22 14:26:49 +00:00
|
|
|
for _, out := range getOuts(o.Method) {
|
|
|
|
if goType(out) == "error" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2022-11-20 13:42:57 +00:00
|
|
|
}
|
|
|
|
|
2022-11-18 10:07:29 +00:00
|
|
|
type resource struct {
|
2022-11-22 09:10:07 +00:00
|
|
|
reflect.Type
|
2022-11-18 10:07:29 +00:00
|
|
|
Operations []operation
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r resource) Kind() string {
|
|
|
|
return strings.ReplaceAll(r.Type.Name(), "Interface", "")
|
|
|
|
}
|
|
|
|
|
2022-11-22 09:10:07 +00:00
|
|
|
type resourceKey reflect.Method
|
|
|
|
|
|
|
|
func (r resourceKey) IsNamespaced() bool {
|
|
|
|
return r.Type.NumIn() == 1
|
|
|
|
}
|
|
|
|
|
2022-11-18 10:07:29 +00:00
|
|
|
type client struct {
|
2022-11-22 09:10:07 +00:00
|
|
|
reflect.Type
|
|
|
|
Resources map[resourceKey]resource
|
2022-11-18 10:07:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type clientset struct {
|
2022-11-22 09:10:07 +00:00
|
|
|
reflect.Type
|
2022-11-22 14:26:49 +00:00
|
|
|
Clients map[reflect.Method]client
|
|
|
|
Resources map[resourceKey]resource
|
2022-11-18 10:07:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getIns(in reflect.Method) []reflect.Type {
|
|
|
|
var out []reflect.Type
|
|
|
|
for i := 0; i < in.Type.NumIn(); i++ {
|
|
|
|
out = append(out, in.Type.In(i))
|
2022-11-17 16:01:30 +00:00
|
|
|
}
|
2022-11-18 10:07:29 +00:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2022-11-20 13:42:57 +00:00
|
|
|
func getOuts(in reflect.Method) []ret {
|
|
|
|
var out []ret
|
2022-11-18 10:07:29 +00:00
|
|
|
for i := 0; i < in.Type.NumOut(); i++ {
|
2022-11-20 13:42:57 +00:00
|
|
|
out = append(out, ret{
|
|
|
|
Type: in.Type.Out(i),
|
|
|
|
IsLast: i == in.Type.NumOut()-1,
|
|
|
|
})
|
2022-11-17 16:01:30 +00:00
|
|
|
}
|
2022-11-18 10:07:29 +00:00
|
|
|
return out
|
2022-11-17 16:01:30 +00:00
|
|
|
}
|
|
|
|
|
2022-11-18 10:07:29 +00:00
|
|
|
func getMethods(in reflect.Type) []reflect.Method {
|
|
|
|
var out []reflect.Method
|
|
|
|
for i := 0; i < in.NumMethod(); i++ {
|
|
|
|
out = append(out, in.Method(i))
|
2022-11-17 16:01:30 +00:00
|
|
|
}
|
2022-11-18 10:07:29 +00:00
|
|
|
return out
|
2022-11-17 16:01:30 +00:00
|
|
|
}
|
|
|
|
|
2022-11-18 10:07:29 +00:00
|
|
|
func packageAlias(in string) string {
|
|
|
|
alias := in
|
|
|
|
alias = strings.ReplaceAll(alias, ".", "_")
|
|
|
|
alias = strings.ReplaceAll(alias, "-", "_")
|
|
|
|
alias = strings.ReplaceAll(alias, "/", "_")
|
|
|
|
return alias
|
|
|
|
}
|
|
|
|
|
|
|
|
func goType(in reflect.Type) string {
|
2022-11-17 16:01:30 +00:00
|
|
|
switch in.Kind() {
|
|
|
|
case reflect.Pointer:
|
2022-11-18 10:07:29 +00:00
|
|
|
return "*" + goType(in.Elem())
|
2022-11-17 16:01:30 +00:00
|
|
|
case reflect.Array:
|
2022-11-18 10:07:29 +00:00
|
|
|
return "[]" + goType(in.Elem())
|
2022-11-17 16:01:30 +00:00
|
|
|
case reflect.Slice:
|
2022-11-18 10:07:29 +00:00
|
|
|
return "[]" + goType(in.Elem())
|
2022-11-17 16:01:30 +00:00
|
|
|
case reflect.Map:
|
2022-11-18 10:07:29 +00:00
|
|
|
return "map[" + goType(in.Key()) + "]" + goType(in.Elem())
|
2022-11-17 16:01:30 +00:00
|
|
|
}
|
2022-11-18 10:07:29 +00:00
|
|
|
pack := packageAlias(in.PkgPath())
|
2022-11-17 16:01:30 +00:00
|
|
|
if pack == "" {
|
|
|
|
return in.Name()
|
|
|
|
}
|
|
|
|
return pack + "." + in.Name()
|
|
|
|
}
|
|
|
|
|
|
|
|
func toSnakeCase(str string) string {
|
|
|
|
snake := matchFirstCap.ReplaceAllString(str, "${1}_${2}")
|
|
|
|
snake = matchAllCap.ReplaceAllString(snake, "${1}_${2}")
|
|
|
|
return strings.ToLower(snake)
|
|
|
|
}
|
|
|
|
|
2022-11-22 09:10:07 +00:00
|
|
|
func parseResource(in reflect.Type) resource {
|
|
|
|
r := resource{
|
|
|
|
Type: in,
|
|
|
|
}
|
|
|
|
for _, operationMethod := range getMethods(in) {
|
|
|
|
o := operation{
|
|
|
|
Method: operationMethod,
|
|
|
|
}
|
|
|
|
r.Operations = append(r.Operations, o)
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseClient(in reflect.Type) client {
|
|
|
|
c := client{
|
|
|
|
Type: in,
|
|
|
|
Resources: map[resourceKey]resource{},
|
|
|
|
}
|
|
|
|
for _, resourceMethod := range getMethods(in) {
|
|
|
|
// resource methods return only the resosurce interface type
|
|
|
|
if resourceMethod.Type.NumOut() == 1 && resourceMethod.Name != "RESTClient" {
|
|
|
|
resourceType := resourceMethod.Type.Out(0)
|
|
|
|
r := resource{
|
|
|
|
Type: resourceType,
|
|
|
|
}
|
|
|
|
for _, operationMethod := range getMethods(resourceType) {
|
|
|
|
o := operation{
|
|
|
|
Method: operationMethod,
|
|
|
|
}
|
|
|
|
r.Operations = append(r.Operations, o)
|
|
|
|
}
|
|
|
|
c.Resources[resourceKey(resourceMethod)] = r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2022-11-22 14:26:49 +00:00
|
|
|
func parseClientset(in reflect.Type) clientset {
|
2022-11-18 10:07:29 +00:00
|
|
|
cs := clientset{
|
2022-11-22 14:26:49 +00:00
|
|
|
Type: in,
|
|
|
|
Clients: map[reflect.Method]client{},
|
|
|
|
Resources: map[resourceKey]resource{},
|
2022-11-18 10:07:29 +00:00
|
|
|
}
|
|
|
|
for _, clientMethod := range getMethods(in) {
|
|
|
|
// client methods return only the client interface type
|
|
|
|
if clientMethod.Type.NumOut() == 1 && clientMethod.Name != "Discovery" {
|
2022-11-22 09:10:07 +00:00
|
|
|
cs.Clients[clientMethod] = parseClient(clientMethod.Type.Out(0))
|
2022-11-22 14:26:49 +00:00
|
|
|
} else if clientMethod.Name == "Discovery" {
|
|
|
|
cs.Resources[resourceKey(clientMethod)] = parseResource(clientMethod.Type.Out(0))
|
2022-11-18 10:07:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return cs
|
|
|
|
}
|
|
|
|
|
2022-11-22 09:10:07 +00:00
|
|
|
func parseImports(in reflect.Type) []string {
|
2022-12-21 22:33:51 +00:00
|
|
|
imports := sets.New(in.PkgPath())
|
2022-11-22 09:10:07 +00:00
|
|
|
for _, m := range getMethods(in) {
|
|
|
|
for _, i := range getIns(m) {
|
|
|
|
if i.Kind() == reflect.Pointer {
|
|
|
|
i = i.Elem()
|
|
|
|
}
|
|
|
|
if i.PkgPath() != "" {
|
|
|
|
imports.Insert(i.PkgPath())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, i := range getOuts(m) {
|
|
|
|
pkg := i.PkgPath()
|
|
|
|
if i.Kind() == reflect.Pointer {
|
2022-11-22 14:26:49 +00:00
|
|
|
pkg = i.Elem().PkgPath()
|
2022-11-22 09:10:07 +00:00
|
|
|
}
|
|
|
|
if pkg != "" {
|
2022-11-22 14:26:49 +00:00
|
|
|
imports.Insert(pkg)
|
2022-11-18 10:07:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-21 22:33:51 +00:00
|
|
|
return sets.List(imports)
|
2022-11-18 10:07:29 +00:00
|
|
|
}
|
|
|
|
|
2022-11-22 09:10:07 +00:00
|
|
|
func executeTemplate(tpl string, data interface{}, folder string, file string) {
|
2022-11-18 10:07:29 +00:00
|
|
|
tmpl := template.New("xxx")
|
|
|
|
tmpl.Funcs(
|
|
|
|
template.FuncMap{
|
|
|
|
"ToLower": func(in string) string {
|
|
|
|
return strings.ToLower(in)
|
|
|
|
},
|
|
|
|
"Quote": func(in string) string {
|
|
|
|
return `"` + in + `"`
|
|
|
|
},
|
|
|
|
"SnakeCase": func(in string) string {
|
|
|
|
return toSnakeCase(in)
|
|
|
|
},
|
|
|
|
"Args": func(in reflect.Method) []arg {
|
|
|
|
var out []arg
|
|
|
|
for i, a := range getIns(in) {
|
|
|
|
out = append(out, arg{
|
|
|
|
Type: a,
|
|
|
|
IsVariadic: in.Type.IsVariadic() && i == in.Type.NumIn()-1,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
},
|
2022-11-20 13:42:57 +00:00
|
|
|
"Returns": func(in reflect.Method) []ret {
|
2022-11-18 10:07:29 +00:00
|
|
|
return getOuts(in)
|
|
|
|
},
|
|
|
|
"Pkg": func(in string) string {
|
|
|
|
return packageAlias(in)
|
|
|
|
},
|
|
|
|
"GoType": func(in reflect.Type) string {
|
|
|
|
return goType(in)
|
|
|
|
},
|
2022-11-22 09:10:07 +00:00
|
|
|
"Packages": func(in reflect.Type) []string {
|
|
|
|
return parseImports(in)
|
|
|
|
},
|
2022-11-18 10:07:29 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
if tmpl, err := tmpl.Parse(tpl); err != nil {
|
|
|
|
panic(err)
|
|
|
|
} else {
|
|
|
|
if err := os.MkdirAll(folder, 0o755); err != nil {
|
|
|
|
panic(fmt.Sprintf("Failed to create directories for %s", folder))
|
|
|
|
}
|
|
|
|
f, err := os.Create(path.Join(folder, file))
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("Failed to create file %s", path.Join(folder, file)))
|
|
|
|
}
|
|
|
|
if err := tmpl.Execute(f, map[string]interface{}{
|
2022-11-22 09:10:07 +00:00
|
|
|
"Folder": folder,
|
|
|
|
"Target": data,
|
2022-11-18 10:07:29 +00:00
|
|
|
}); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-22 09:10:07 +00:00
|
|
|
func generateResource(r resource, folder string) {
|
|
|
|
executeTemplate(resourceTpl, r, folder, "resource.generated.go")
|
2022-11-21 17:16:25 +00:00
|
|
|
}
|
|
|
|
|
2022-11-22 09:10:07 +00:00
|
|
|
func generateClient(c client, folder string) {
|
|
|
|
executeTemplate(clientTpl, c, folder, "client.generated.go")
|
|
|
|
for m, r := range c.Resources {
|
|
|
|
generateResource(r, path.Join(folder, strings.ToLower(m.Name)))
|
2022-11-21 17:16:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-22 09:10:07 +00:00
|
|
|
func generateClientset(cs clientset, folder string) {
|
|
|
|
executeTemplate(clientsetTpl, cs, folder, "clientset.generated.go")
|
|
|
|
for m, c := range cs.Clients {
|
|
|
|
generateClient(c, path.Join(folder, strings.ToLower(m.Name)))
|
2022-11-21 17:16:25 +00:00
|
|
|
}
|
2022-11-22 14:26:49 +00:00
|
|
|
for m, r := range cs.Resources {
|
|
|
|
generateResource(r, path.Join(folder, strings.ToLower(m.Name)))
|
|
|
|
}
|
2022-11-21 17:16:25 +00:00
|
|
|
}
|
|
|
|
|
2022-11-22 09:10:07 +00:00
|
|
|
func generateInterface(cs clientset, folder string) {
|
|
|
|
executeTemplate(interfaceTpl, cs, folder, "interface.generated.go")
|
2022-11-17 16:01:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2022-11-22 14:26:49 +00:00
|
|
|
kube := parseClientset(reflect.TypeOf((*kubernetes.Interface)(nil)).Elem())
|
2022-11-22 09:10:07 +00:00
|
|
|
generateClientset(kube, "pkg/clients/kube")
|
|
|
|
generateInterface(kube, "pkg/clients/kube")
|
2022-11-22 14:26:49 +00:00
|
|
|
kyverno := parseClientset(reflect.TypeOf((*versioned.Interface)(nil)).Elem())
|
2022-11-22 09:10:07 +00:00
|
|
|
generateClientset(kyverno, "pkg/clients/kyverno")
|
|
|
|
generateInterface(kyverno, "pkg/clients/kyverno")
|
2022-11-22 14:26:49 +00:00
|
|
|
dynamicInterface := parseClientset(reflect.TypeOf((*dynamic.Interface)(nil)).Elem())
|
2022-11-22 09:10:07 +00:00
|
|
|
dynamicResource := parseResource(reflect.TypeOf((*dynamic.ResourceInterface)(nil)).Elem())
|
|
|
|
generateResource(dynamicResource, "pkg/clients/dynamic/resource")
|
|
|
|
generateInterface(dynamicInterface, "pkg/clients/dynamic")
|
2022-11-22 14:26:49 +00:00
|
|
|
metadataInterface := parseClientset(reflect.TypeOf((*metadata.Interface)(nil)).Elem())
|
2022-11-22 09:10:07 +00:00
|
|
|
metadataResource := parseResource(reflect.TypeOf((*metadata.ResourceInterface)(nil)).Elem())
|
|
|
|
generateInterface(metadataInterface, "pkg/clients/metadata")
|
|
|
|
generateResource(metadataResource, "pkg/clients/metadata/resource")
|
2023-02-10 20:54:53 +00:00
|
|
|
apiserverInterface := parseClientset(reflect.TypeOf((*apiserver.Interface)(nil)).Elem())
|
|
|
|
generateClientset(apiserverInterface, "pkg/clients/apiserver")
|
|
|
|
generateInterface(apiserverInterface, "pkg/clients/apiserver")
|
|
|
|
aggregatorInterface := parseClientset(reflect.TypeOf((*aggregator.Interface)(nil)).Elem())
|
|
|
|
generateClientset(aggregatorInterface, "pkg/clients/aggregator")
|
|
|
|
generateInterface(aggregatorInterface, "pkg/clients/aggregator")
|
2022-11-17 16:01:30 +00:00
|
|
|
}
|