1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-06 16:06:56 +00:00
kyverno/main.go

151 lines
5.5 KiB
Go
Raw Normal View History

package main
import (
"flag"
2019-08-14 18:40:33 -07:00
"time"
2019-05-31 17:59:36 -07:00
"github.com/golang/glog"
2019-08-19 10:00:39 -07:00
kyvernoclient "github.com/nirmata/kyverno/pkg/client/clientset/versioned"
kyvernoinformer "github.com/nirmata/kyverno/pkg/client/informers/externalversions"
2019-05-31 17:59:36 -07:00
"github.com/nirmata/kyverno/pkg/config"
client "github.com/nirmata/kyverno/pkg/dclient"
2019-05-21 11:00:09 -07:00
event "github.com/nirmata/kyverno/pkg/event"
2019-08-14 14:56:53 -07:00
"github.com/nirmata/kyverno/pkg/namespace"
"github.com/nirmata/kyverno/pkg/policy"
2019-08-08 15:36:19 -07:00
"github.com/nirmata/kyverno/pkg/policyviolation"
2019-05-21 11:00:09 -07:00
"github.com/nirmata/kyverno/pkg/webhooks"
"k8s.io/client-go/informers"
2019-05-10 00:05:21 -07:00
"k8s.io/sample-controller/pkg/signals"
)
var (
2019-07-31 17:43:46 -07:00
kubeconfig string
serverIP string
filterK8Resources string
2019-08-02 11:18:02 -07:00
cpu bool
memory bool
2019-08-07 12:32:44 -07:00
webhookTimeout int
)
const defaultReSyncTime = 10 * time.Second
func main() {
2019-05-31 17:59:36 -07:00
defer glog.Flush()
printVersionInfo()
2019-08-02 11:18:02 -07:00
prof = enableProfiling(cpu, memory)
// CLIENT CONFIG
clientConfig, err := createClientConfig(kubeconfig)
if err != nil {
2019-05-31 17:59:36 -07:00
glog.Fatalf("Error building kubeconfig: %v\n", err)
}
// KYVENO CRD CLIENT
// access CRD resources
// - Policy
// - PolicyViolation
2019-08-19 10:00:39 -07:00
pclient, err := kyvernoclient.NewForConfig(clientConfig)
if err != nil {
2019-05-31 17:59:36 -07:00
glog.Fatalf("Error creating client: %v\n", err)
}
2019-08-09 19:12:50 -07:00
// DYNAMIC CLIENT
// - client for all registered resources
client, err := client.NewClient(clientConfig)
if err != nil {
2019-08-09 19:12:50 -07:00
glog.Fatalf("Error creating client: %v\n", err)
}
// KYVERNO CRD INFORMER
// watches CRD resources:
// - Policy
// - PolicyVolation
2019-08-14 18:40:33 -07:00
// - cache resync time: 10 seconds
pInformer := kyvernoinformer.NewSharedInformerFactoryWithOptions(pclient, defaultReSyncTime)
2019-08-13 13:15:04 -07:00
// EVENT GENERATOR
// - generate event with retry
egen := event.NewEventGenerator(client, pInformer.Kyverno().V1alpha1().Policies())
// mutatingWebhookConfiguration Informer
kubeInformer := informers.NewSharedInformerFactory(client.Kclient, defaultReSyncTime)
mutatingWebhookConfigurationLister := kubeInformer.Admissionregistration().V1beta1().MutatingWebhookConfigurations().Lister()
tlsPair, err := initTLSPemPair(clientConfig, client)
if err != nil {
glog.Fatalf("Failed to initialize TLS key/certificate pair: %v\n", err)
}
// WEBHOOK REGISTRATION
// -- validationwebhookconfiguration (Policy)
// -- mutatingwebhookconfiguration (All resources)
webhookRegistrationClient, err := webhooks.NewWebhookRegistrationClient(clientConfig, client, serverIP, int32(webhookTimeout))
if err != nil {
glog.Fatalf("Unable to register admission webhooks on cluster: %v\n", err)
}
if err = webhookRegistrationClient.Register(); err != nil {
glog.Fatalf("Failed registering Admission Webhooks: %v\n", err)
}
// POLICY CONTROLLER
// - reconciliation policy and policy violation
2019-08-14 10:01:47 -07:00
// - process policy on existing resources
// - status: violation count
2019-08-14 10:01:47 -07:00
pc, err := policy.NewPolicyController(pclient, client, pInformer.Kyverno().V1alpha1().Policies(), pInformer.Kyverno().V1alpha1().PolicyViolations(), egen, mutatingWebhookConfigurationLister, webhookRegistrationClient)
if err != nil {
glog.Fatalf("error creating policy controller: %v\n", err)
}
// POLICY VIOLATION CONTROLLER
// status: lastUpdatTime
2019-08-08 15:36:19 -07:00
pvc, err := policyviolation.NewPolicyViolationController(client, pclient, pInformer.Kyverno().V1alpha1().Policies(), pInformer.Kyverno().V1alpha1().PolicyViolations())
if err != nil {
glog.Fatalf("error creating policy violation controller: %v\n", err)
}
2019-08-14 14:56:53 -07:00
// GENERATE CONTROLLER
// - watches for Namespace resource and generates resource based on the policy generate rule
nsc := namespace.NewNamespaceController(pclient, client, kubeInformer.Core().V1().Namespaces(), pInformer.Kyverno().V1alpha1().Policies(), pInformer.Kyverno().V1alpha1().PolicyViolations(), egen)
server, err := webhooks.NewWebhookServer(pclient, client, tlsPair, pInformer.Kyverno().V1alpha1().Policies(), pInformer.Kyverno().V1alpha1().PolicyViolations(), egen, webhookRegistrationClient, filterK8Resources)
if err != nil {
glog.Fatalf("Unable to create webhook server: %v\n", err)
}
stopCh := signals.SetupSignalHandler()
2019-07-19 16:17:10 -07:00
if err = webhookRegistrationClient.Register(); err != nil {
glog.Fatalf("Failed registering Admission Webhooks: %v\n", err)
}
pInformer.Start(stopCh)
2019-08-14 14:56:53 -07:00
kubeInformer.Start(stopCh)
go pc.Run(1, stopCh)
2019-08-08 15:36:19 -07:00
go pvc.Run(1, stopCh)
2019-08-09 13:41:56 -07:00
go egen.Run(1, stopCh)
2019-08-14 14:56:53 -07:00
go nsc.Run(1, stopCh)
//TODO add WG for the go routines?
server.RunAsync()
2019-08-02 11:18:02 -07:00
<-stopCh
2019-08-02 11:18:02 -07:00
disableProfiling(prof)
server.Stop()
}
func init() {
2019-08-02 11:18:02 -07:00
// profiling feature gate
// cpu and memory profiling cannot be enabled at same time
// if both cpu and memory are enabled
// by default is to profile cpu
flag.BoolVar(&cpu, "cpu", false, "cpu profilling feature gate, default to false || cpu and memory profiling cannot be enabled at the same time")
flag.BoolVar(&memory, "memory", false, "memory profilling feature gate, default to false || cpu and memory profiling cannot be enabled at the same time")
2019-08-07 12:32:44 -07:00
flag.IntVar(&webhookTimeout, "webhooktimeout", 2, "timeout for webhook configurations")
flag.StringVar(&kubeconfig, "kubeconfig", "", "Path to a kubeconfig. Only required if out-of-cluster.")
flag.StringVar(&serverIP, "serverIP", "", "IP address where Kyverno controller runs. Only required if out-of-cluster.")
2019-07-31 17:43:46 -07:00
flag.StringVar(&filterK8Resources, "filterK8Resources", "", "k8 resource in format [kind,namespace,name] where policy is not evaluated by the admission webhook. example --filterKind \"[Deployment, kyverno, kyverno]\" --filterKind \"[Deployment, kyverno, kyverno],[Events, *, *]\"")
2019-05-31 17:59:36 -07:00
config.LogDefaultFlags()
flag.Parse()
2019-06-05 17:43:59 -07:00
}