2022-11-24 19:57:01 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-04-14 09:00:52 +00:00
|
|
|
"time"
|
2022-11-24 19:57:01 +00:00
|
|
|
|
|
|
|
"github.com/go-logr/logr"
|
2023-04-14 09:00:52 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/client/clientset/versioned"
|
|
|
|
apiserverclient "github.com/kyverno/kyverno/pkg/clients/apiserver"
|
|
|
|
"github.com/kyverno/kyverno/pkg/clients/dclient"
|
|
|
|
dynamicclient "github.com/kyverno/kyverno/pkg/clients/dynamic"
|
2023-04-11 12:05:20 +00:00
|
|
|
kubeclient "github.com/kyverno/kyverno/pkg/clients/kube"
|
2023-04-14 09:00:52 +00:00
|
|
|
kyvernoclient "github.com/kyverno/kyverno/pkg/clients/kyverno"
|
|
|
|
metadataclient "github.com/kyverno/kyverno/pkg/clients/metadata"
|
2023-04-11 12:05:20 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/config"
|
2023-04-13 11:29:40 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/jmespath"
|
2022-11-29 09:16:07 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/metrics"
|
2023-04-11 13:32:17 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/registryclient"
|
2023-04-14 09:00:52 +00:00
|
|
|
apiserver "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
|
|
|
|
"k8s.io/client-go/dynamic"
|
2023-04-11 12:05:20 +00:00
|
|
|
"k8s.io/client-go/kubernetes"
|
2023-04-14 09:00:52 +00:00
|
|
|
"k8s.io/client-go/metadata"
|
2022-11-24 19:57:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func shutdown(logger logr.Logger, sdowns ...context.CancelFunc) context.CancelFunc {
|
|
|
|
return func() {
|
|
|
|
for i := range sdowns {
|
2023-01-12 04:47:40 +00:00
|
|
|
if sdowns[i] != nil {
|
|
|
|
logger.Info("shutting down...")
|
|
|
|
defer sdowns[i]()
|
|
|
|
}
|
2022-11-24 19:57:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-11 12:05:20 +00:00
|
|
|
type SetupResult struct {
|
|
|
|
Logger logr.Logger
|
|
|
|
Configuration config.Configuration
|
|
|
|
MetricsConfiguration config.MetricsConfiguration
|
|
|
|
MetricsManager metrics.MetricsConfigManager
|
2023-04-13 11:29:40 +00:00
|
|
|
Jp jmespath.Interface
|
2023-04-11 12:05:20 +00:00
|
|
|
KubeClient kubernetes.Interface
|
2023-04-11 17:44:49 +00:00
|
|
|
LeaderElectionClient kubernetes.Interface
|
2023-04-11 13:32:17 +00:00
|
|
|
RegistryClient registryclient.Client
|
2023-04-14 09:00:52 +00:00
|
|
|
KyvernoClient versioned.Interface
|
|
|
|
DynamicClient dynamic.Interface
|
|
|
|
ApiServerClient apiserver.Interface
|
|
|
|
MetadataClient metadata.Interface
|
|
|
|
KyvernoDynamicClient dclient.Interface
|
2023-04-11 12:05:20 +00:00
|
|
|
}
|
|
|
|
|
2023-04-11 13:32:17 +00:00
|
|
|
func Setup(config Configuration, name string, skipResourceFilters bool) (context.Context, SetupResult, context.CancelFunc) {
|
2023-04-11 17:44:49 +00:00
|
|
|
logger := setupLogger()
|
|
|
|
showVersion(logger)
|
|
|
|
sdownMaxProcs := setupMaxProcs(logger)
|
|
|
|
setupProfiling(logger)
|
|
|
|
ctx, sdownSignals := setupSignals(logger)
|
|
|
|
client := kubeclient.From(createKubernetesClient(logger), kubeclient.WithTracing())
|
2023-04-11 12:05:20 +00:00
|
|
|
metricsConfiguration := startMetricsConfigController(ctx, logger, client)
|
|
|
|
metricsManager, sdownMetrics := SetupMetrics(ctx, logger, metricsConfiguration, client)
|
|
|
|
client = client.WithMetrics(metricsManager, metrics.KubeClient)
|
|
|
|
configuration := startConfigController(ctx, logger, client, skipResourceFilters)
|
2023-01-26 09:39:52 +00:00
|
|
|
sdownTracing := SetupTracing(logger, name, client)
|
2023-04-11 14:37:46 +00:00
|
|
|
setupCosign(logger)
|
2023-04-11 13:32:17 +00:00
|
|
|
var registryClient registryclient.Client
|
|
|
|
if config.UsesRegistryClient() {
|
|
|
|
registryClient = setupRegistryClient(ctx, logger, client)
|
|
|
|
}
|
2023-04-11 17:44:49 +00:00
|
|
|
var leaderElectionClient kubernetes.Interface
|
|
|
|
if config.UsesLeaderElection() {
|
|
|
|
leaderElectionClient = createKubernetesClient(logger, kubeclient.WithMetrics(metricsManager, metrics.KubeClient), kubeclient.WithTracing())
|
|
|
|
}
|
2023-04-14 09:00:52 +00:00
|
|
|
var kyvernoClient versioned.Interface
|
|
|
|
if config.UsesKyvernoClient() {
|
|
|
|
kyvernoClient = createKyvernoClient(logger, kyvernoclient.WithMetrics(metricsManager, metrics.KyvernoClient), kyvernoclient.WithTracing())
|
|
|
|
}
|
|
|
|
var dynamicClient dynamic.Interface
|
|
|
|
if config.UsesDynamicClient() {
|
|
|
|
dynamicClient = createDynamicClient(logger, dynamicclient.WithMetrics(metricsManager, metrics.DynamicClient), dynamicclient.WithTracing())
|
|
|
|
}
|
|
|
|
var apiServerClient apiserver.Interface
|
|
|
|
if config.UsesApiServerClient() {
|
|
|
|
apiServerClient = createApiServerClient(logger, apiserverclient.WithMetrics(metricsManager, metrics.ApiServerClient), apiserverclient.WithTracing())
|
|
|
|
}
|
|
|
|
var dClient dclient.Interface
|
|
|
|
if config.UsesKyvernoDynamicClient() {
|
|
|
|
dClient = createKyvernoDynamicClient(logger, ctx, dynamicClient, client, 15*time.Minute)
|
|
|
|
}
|
|
|
|
var metadataClient metadata.Interface
|
|
|
|
if config.UsesMetadataClient() {
|
|
|
|
metadataClient = createMetadataClient(logger, metadataclient.WithMetrics(metricsManager, metrics.MetadataClient), metadataclient.WithTracing())
|
|
|
|
}
|
2023-04-11 12:05:20 +00:00
|
|
|
return ctx,
|
|
|
|
SetupResult{
|
|
|
|
Logger: logger,
|
|
|
|
Configuration: configuration,
|
|
|
|
MetricsConfiguration: metricsConfiguration,
|
|
|
|
MetricsManager: metricsManager,
|
2023-04-13 11:29:40 +00:00
|
|
|
Jp: jmespath.New(configuration),
|
2023-04-11 12:05:20 +00:00
|
|
|
KubeClient: client,
|
2023-04-11 17:44:49 +00:00
|
|
|
LeaderElectionClient: leaderElectionClient,
|
2023-04-11 13:32:17 +00:00
|
|
|
RegistryClient: registryClient,
|
2023-04-14 09:00:52 +00:00
|
|
|
KyvernoClient: kyvernoClient,
|
|
|
|
DynamicClient: dynamicClient,
|
|
|
|
ApiServerClient: apiServerClient,
|
|
|
|
MetadataClient: metadataClient,
|
|
|
|
KyvernoDynamicClient: dClient,
|
2023-04-11 12:05:20 +00:00
|
|
|
},
|
|
|
|
shutdown(logger.WithName("shutdown"), sdownMaxProcs, sdownMetrics, sdownTracing, sdownSignals)
|
2022-11-24 19:57:01 +00:00
|
|
|
}
|