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

262 lines
8.4 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-11-11 11:10:25 -08:00
"github.com/nirmata/kyverno/pkg/policystore"
2019-08-08 15:36:19 -07:00
"github.com/nirmata/kyverno/pkg/policyviolation"
2019-08-14 14:56:53 -07:00
"github.com/nirmata/kyverno/pkg/utils"
"github.com/nirmata/kyverno/pkg/webhookconfig"
2019-05-21 11:00:09 -07:00
"github.com/nirmata/kyverno/pkg/webhooks"
kubeinformers "k8s.io/client-go/informers"
2019-05-10 00:05:21 -07:00
"k8s.io/sample-controller/pkg/signals"
)
var (
2019-10-28 15:23:52 -05:00
kubeconfig string
serverIP string
2019-10-18 17:38:46 -07:00
cpu bool
memory bool
webhookTimeout int
//TODO: this has been added to backward support command line arguments
// will be removed in future and the configuration will be set only via configmaps
2019-07-31 17:43:46 -07:00
filterK8Resources string
)
func main() {
2019-05-31 17:59:36 -07:00
defer glog.Flush()
printVersionInfo()
2019-08-20 23:51:34 -07:00
// profile cpu and memory consuption
2019-08-02 11:18:02 -07:00
prof = enableProfiling(cpu, memory)
// cleanUp Channel
cleanUp := make(chan struct{})
2019-10-18 17:38:46 -07:00
// SIGINT & SIGTERM channel
stopCh := signals.SetupSignalHandler()
// 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-20 23:51:34 -07:00
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)
}
2019-08-22 11:18:13 -07:00
// CRD CHECK
// - verify if the CRD for Policy & PolicyViolation are avialalbe
if !utils.CRDInstalled(client.DiscoveryClient) {
glog.Fatalf("Required CRDs unavailable")
}
2019-08-20 23:56:47 -07:00
// KUBERNETES CLIENT
kubeClient, err := utils.NewKubeClient(clientConfig)
if err != nil {
glog.Fatalf("Error creating kubernetes client: %v\n", err)
}
// WERBHOOK REGISTRATION CLIENT
2019-11-15 15:53:22 -08:00
webhookRegistrationClient, err := webhookconfig.NewWebhookRegistrationClient(
clientConfig,
client,
serverIP,
int32(webhookTimeout))
if err != nil {
glog.Fatalf("Unable to register admission webhooks on cluster: %v\n", err)
}
// KYVERNO CRD INFORMER
// watches CRD resources:
// - Policy
// - PolicyVolation
2019-08-14 18:40:33 -07:00
// - cache resync time: 10 seconds
2019-11-15 15:53:22 -08:00
pInformer := kyvernoinformer.NewSharedInformerFactoryWithOptions(
pclient,
10*time.Second)
2019-08-20 23:51:34 -07:00
// KUBERNETES RESOURCES INFORMER
// watches namespace resource
// - cache resync time: 10 seconds
2019-11-15 15:53:22 -08:00
kubeInformer := kubeinformers.NewSharedInformerFactoryWithOptions(
kubeClient,
10*time.Second)
2019-10-18 17:38:46 -07:00
// Configuration Data
// dyamically load the configuration from configMap
// - resource filters
// if the configMap is update, the configuration will be updated :D
2019-11-15 15:53:22 -08:00
configData := config.NewConfigData(
kubeClient,
kubeInformer.Core().V1().ConfigMaps(),
filterK8Resources)
2019-10-25 18:49:26 -05:00
2019-11-12 14:41:29 -08:00
// Policy meta-data store
2019-11-15 15:53:22 -08:00
policyMetaStore := policystore.NewPolicyStore(pInformer.Kyverno().V1().ClusterPolicies())
2019-11-12 14:41:29 -08:00
2019-08-13 13:15:04 -07:00
// EVENT GENERATOR
2019-08-20 23:51:34 -07:00
// - generate event with retry mechanism
2019-11-15 15:53:22 -08:00
egen := event.NewEventGenerator(
client,
pInformer.Kyverno().V1().ClusterPolicies())
2019-11-12 14:41:29 -08:00
// POLICY VIOLATION GENERATOR
// -- generate policy violation
2019-11-15 15:53:22 -08:00
pvgen := policyviolation.NewPVGenerator(pclient,
client,
pInformer.Kyverno().V1().ClusterPolicyViolations(),
pInformer.Kyverno().V1().NamespacedPolicyViolations())
2019-11-12 14:41:29 -08:00
// POLICY CONTROLLER
// - reconciliation policy and policy violation
2019-08-14 10:01:47 -07:00
// - process policy on existing resources
2019-08-20 17:35:55 -07:00
// - status aggregator: recieves stats when a policy is applied
2019-08-20 23:51:34 -07:00
// & updates the policy status
2019-11-15 15:53:22 -08:00
pc, err := policy.NewPolicyController(pclient,
client,
pInformer.Kyverno().V1().ClusterPolicies(),
pInformer.Kyverno().V1().ClusterPolicyViolations(),
pInformer.Kyverno().V1().NamespacedPolicyViolations(),
egen,
kubeInformer.Admissionregistration().V1beta1().MutatingWebhookConfigurations(),
webhookRegistrationClient,
configData,
pvgen,
policyMetaStore)
if err != nil {
glog.Fatalf("error creating policy controller: %v\n", err)
}
// POLICY VIOLATION CONTROLLER
2019-08-20 23:51:34 -07:00
// policy violation cleanup if the corresponding resource is deleted
// status: lastUpdatTime
2019-11-15 15:53:22 -08:00
pvc, err := policyviolation.NewPolicyViolationController(
client,
pclient,
pInformer.Kyverno().V1().ClusterPolicies(),
pInformer.Kyverno().V1().ClusterPolicyViolations())
2019-08-08 15:36:19 -07:00
if err != nil {
2019-11-12 19:01:48 -08:00
glog.Fatalf("error creating cluster policy violation controller: %v\n", err)
}
2019-11-15 15:53:22 -08:00
nspvc, err := policyviolation.NewNamespacedPolicyViolationController(
client,
pclient,
pInformer.Kyverno().V1().ClusterPolicies(),
pInformer.Kyverno().V1().NamespacedPolicyViolations())
2019-11-12 19:01:48 -08:00
if err != nil {
glog.Fatalf("error creating namespaced 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
2019-11-15 15:53:22 -08:00
nsc := namespace.NewNamespaceController(
pclient,
client,
kubeInformer.Core().V1().Namespaces(),
pInformer.Kyverno().V1().ClusterPolicies(),
pc.GetPolicyStatusAggregator(),
egen,
configData,
pvgen,
policyMetaStore)
2019-08-14 14:56:53 -07:00
2019-08-20 23:56:47 -07:00
// CONFIGURE CERTIFICATES
2019-06-05 17:43:59 -07:00
tlsPair, err := initTLSPemPair(clientConfig, client)
if err != nil {
2019-05-31 17:59:36 -07:00
glog.Fatalf("Failed to initialize TLS key/certificate pair: %v\n", err)
}
2019-08-20 23:51:34 -07:00
// WEBHOOK REGISTRATION
// - validationwebhookconfiguration (Policy)
// - mutatingwebhookconfiguration (All resources)
// webhook confgiuration is also generated dynamically in the policy controller
// based on the policy resources created
if err = webhookRegistrationClient.Register(); err != nil {
glog.Fatalf("Failed registering Admission Webhooks: %v\n", err)
}
2019-08-20 23:51:34 -07:00
// WEBHOOOK
// - https server to provide endpoints called based on rules defined in Mutating & Validation webhook configuration
// - reports the results based on the response from the policy engine:
// -- annotations on resources with update details on mutation JSON patches
// -- generate policy violation resource
// -- generate events on policy and resource
2019-11-15 15:53:22 -08:00
server, err := webhooks.NewWebhookServer(
pclient,
client,
tlsPair,
pInformer.Kyverno().V1().ClusterPolicies(),
kubeInformer.Rbac().V1().RoleBindings(),
kubeInformer.Rbac().V1().ClusterRoleBindings(),
egen,
webhookRegistrationClient,
pc.GetPolicyStatusAggregator(),
configData,
policyMetaStore,
pvgen,
cleanUp)
if err != nil {
glog.Fatalf("Unable to create webhook server: %v\n", err)
}
2019-08-20 23:51:34 -07:00
// Start the components
pInformer.Start(stopCh)
2019-08-14 14:56:53 -07:00
kubeInformer.Start(stopCh)
2019-11-15 15:53:22 -08:00
go configData.Run(stopCh)
go policyMetaStore.Run(stopCh)
go pc.Run(1, stopCh)
2019-08-08 15:36:19 -07:00
go pvc.Run(1, stopCh)
2019-11-12 19:01:48 -08:00
go nspvc.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)
2019-11-12 14:41:29 -08:00
go pvgen.Run(1, stopCh)
// verifys if the admission control is enabled and active
// resync: 60 seconds
// deadline: 60 seconds (send request)
// max deadline: deadline*3 (set the deployment annotation as false)
2019-10-25 16:55:48 -05:00
server.RunAsync(stopCh)
2019-08-02 11:18:02 -07:00
<-stopCh
2019-08-02 11:18:02 -07:00
disableProfiling(prof)
server.Stop()
// resource cleanup
// remove webhook configurations
<-cleanUp
}
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")
//TODO: this has been added to backward support command line arguments
// will be removed in future and the configuration will be set only via configmaps
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-10-29 11:31:45 -07:00
flag.IntVar(&webhookTimeout, "webhooktimeout", 3, "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-05-31 17:59:36 -07:00
config.LogDefaultFlags()
flag.Parse()
2019-06-05 17:43:59 -07:00
}