mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-05 07:26:55 +00:00
fix import cylce after merge + seperate webhookconfig client
This commit is contained in:
parent
b180284003
commit
d8c315e339
6 changed files with 53 additions and 38 deletions
17
main.go
17
main.go
|
@ -14,8 +14,9 @@ import (
|
||||||
"github.com/nirmata/kyverno/pkg/policy"
|
"github.com/nirmata/kyverno/pkg/policy"
|
||||||
"github.com/nirmata/kyverno/pkg/policyviolation"
|
"github.com/nirmata/kyverno/pkg/policyviolation"
|
||||||
"github.com/nirmata/kyverno/pkg/utils"
|
"github.com/nirmata/kyverno/pkg/utils"
|
||||||
|
"github.com/nirmata/kyverno/pkg/webhookconfig"
|
||||||
"github.com/nirmata/kyverno/pkg/webhooks"
|
"github.com/nirmata/kyverno/pkg/webhooks"
|
||||||
kubeinformer "k8s.io/client-go/informers"
|
kubeinformers "k8s.io/client-go/informers"
|
||||||
"k8s.io/sample-controller/pkg/signals"
|
"k8s.io/sample-controller/pkg/signals"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -65,6 +66,12 @@ func main() {
|
||||||
glog.Fatalf("Error creating kubernetes client: %v\n", err)
|
glog.Fatalf("Error creating kubernetes client: %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WERBHOOK REGISTRATION CLIENT
|
||||||
|
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
|
// KYVERNO CRD INFORMER
|
||||||
// watches CRD resources:
|
// watches CRD resources:
|
||||||
// - Policy
|
// - Policy
|
||||||
|
@ -72,7 +79,7 @@ func main() {
|
||||||
// - cache resync time: 10 seconds
|
// - cache resync time: 10 seconds
|
||||||
pInformer := kyvernoinformer.NewSharedInformerFactoryWithOptions(pclient, 10*time.Second)
|
pInformer := kyvernoinformer.NewSharedInformerFactoryWithOptions(pclient, 10*time.Second)
|
||||||
|
|
||||||
// KUBERNETES RESOURCES INFORMER
|
// KUBERNETES RESOURCES INFORMER
|
||||||
// watches namespace resource
|
// watches namespace resource
|
||||||
// - cache resync time: 10 seconds
|
// - cache resync time: 10 seconds
|
||||||
kubeInformer := kubeinformers.NewSharedInformerFactoryWithOptions(kubeClient, 10*time.Second)
|
kubeInformer := kubeinformers.NewSharedInformerFactoryWithOptions(kubeClient, 10*time.Second)
|
||||||
|
@ -109,12 +116,6 @@ func main() {
|
||||||
glog.Fatalf("Failed to initialize TLS key/certificate pair: %v\n", err)
|
glog.Fatalf("Failed to initialize TLS key/certificate pair: %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// WERBHOOK REGISTRATION CLIENT
|
|
||||||
webhookRegistrationClient, err := webhooks.NewWebhookRegistrationClient(clientConfig, client, serverIP, int32(webhookTimeout))
|
|
||||||
if err != nil {
|
|
||||||
glog.Fatalf("Unable to register admission webhooks on cluster: %v\n", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// WEBHOOK REGISTRATION
|
// WEBHOOK REGISTRATION
|
||||||
// - validationwebhookconfiguration (Policy)
|
// - validationwebhookconfiguration (Policy)
|
||||||
// - mutatingwebhookconfiguration (All resources)
|
// - mutatingwebhookconfiguration (All resources)
|
||||||
|
|
|
@ -4,19 +4,14 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/golang/glog"
|
||||||
|
kyverno "github.com/nirmata/kyverno/pkg/api/kyverno/v1alpha1"
|
||||||
|
kyvernolister "github.com/nirmata/kyverno/pkg/client/listers/kyverno/v1alpha1"
|
||||||
client "github.com/nirmata/kyverno/pkg/dclient"
|
client "github.com/nirmata/kyverno/pkg/dclient"
|
||||||
"github.com/nirmata/kyverno/pkg/engine"
|
"github.com/nirmata/kyverno/pkg/engine"
|
||||||
"github.com/nirmata/kyverno/pkg/policy"
|
|
||||||
|
|
||||||
"github.com/golang/glog"
|
|
||||||
|
|
||||||
kyverno "github.com/nirmata/kyverno/pkg/api/kyverno/v1alpha1"
|
|
||||||
|
|
||||||
kyvernolister "github.com/nirmata/kyverno/pkg/client/listers/kyverno/v1alpha1"
|
|
||||||
"github.com/nirmata/kyverno/pkg/info"
|
"github.com/nirmata/kyverno/pkg/info"
|
||||||
policyctr "github.com/nirmata/kyverno/pkg/policy"
|
"github.com/nirmata/kyverno/pkg/policy"
|
||||||
corev1 "k8s.io/api/core/v1"
|
corev1 "k8s.io/api/core/v1"
|
||||||
|
|
||||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||||
"k8s.io/apimachinery/pkg/labels"
|
"k8s.io/apimachinery/pkg/labels"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
|
@ -144,10 +139,9 @@ func listpolicies(ns unstructured.Unstructured, pLister kyvernolister.PolicyList
|
||||||
return filteredpolicies
|
return filteredpolicies
|
||||||
}
|
}
|
||||||
|
|
||||||
func applyPolicy(client *client.Client, resource unstructured.Unstructured, policy kyverno.Policy, policyStatus policy.PolicyStatusInterface) info.PolicyInfo {
|
func applyPolicy(client *client.Client, resource unstructured.Unstructured, p kyverno.Policy, policyStatus policy.PolicyStatusInterface) info.PolicyInfo {
|
||||||
var ps policyctr.PolicyStat
|
var ps policy.PolicyStat
|
||||||
gatherStat := func(policyName string, er engine.EngineResponse) {
|
gatherStat := func(policyName string, er engine.EngineResponse) {
|
||||||
// ps := policyctr.PolicyStat{}
|
|
||||||
ps.PolicyName = policyName
|
ps.PolicyName = policyName
|
||||||
ps.Stats.GenerationExecutionTime = er.ExecutionTime
|
ps.Stats.GenerationExecutionTime = er.ExecutionTime
|
||||||
ps.Stats.RulesAppliedCount = er.RulesAppliedCount
|
ps.Stats.RulesAppliedCount = er.RulesAppliedCount
|
||||||
|
@ -159,15 +153,15 @@ func applyPolicy(client *client.Client, resource unstructured.Unstructured, poli
|
||||||
}
|
}
|
||||||
|
|
||||||
startTime := time.Now()
|
startTime := time.Now()
|
||||||
glog.V(4).Infof("Started apply policy %s on resource %s/%s/%s (%v)", policy.Name, resource.GetKind(), resource.GetNamespace(), resource.GetName(), startTime)
|
glog.V(4).Infof("Started apply policy %s on resource %s/%s/%s (%v)", p.Name, resource.GetKind(), resource.GetNamespace(), resource.GetName(), startTime)
|
||||||
defer func() {
|
defer func() {
|
||||||
glog.V(4).Infof("Finished applying %s on resource %s/%s/%s (%v)", policy.Name, resource.GetKind(), resource.GetNamespace(), resource.GetName(), time.Since(startTime))
|
glog.V(4).Infof("Finished applying %s on resource %s/%s/%s (%v)", p.Name, resource.GetKind(), resource.GetNamespace(), resource.GetName(), time.Since(startTime))
|
||||||
}()
|
}()
|
||||||
policyInfo := info.NewPolicyInfo(policy.Name, resource.GetKind(), resource.GetName(), resource.GetNamespace(), policy.Spec.ValidationFailureAction)
|
policyInfo := info.NewPolicyInfo(p.Name, resource.GetKind(), resource.GetName(), resource.GetNamespace(), p.Spec.ValidationFailureAction)
|
||||||
engineResponse := engine.Generate(client, policy, resource)
|
engineResponse := engine.Generate(client, p, resource)
|
||||||
policyInfo.AddRuleInfos(engineResponse.RuleInfos)
|
policyInfo.AddRuleInfos(engineResponse.RuleInfos)
|
||||||
// gather stats
|
// gather stats
|
||||||
gatherStat(policy.Name, engineResponse)
|
gatherStat(p.Name, engineResponse)
|
||||||
//send stats
|
//send stats
|
||||||
sendStat(false)
|
sendStat(false)
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,7 @@ import (
|
||||||
client "github.com/nirmata/kyverno/pkg/dclient"
|
client "github.com/nirmata/kyverno/pkg/dclient"
|
||||||
"github.com/nirmata/kyverno/pkg/event"
|
"github.com/nirmata/kyverno/pkg/event"
|
||||||
"github.com/nirmata/kyverno/pkg/utils"
|
"github.com/nirmata/kyverno/pkg/utils"
|
||||||
"github.com/nirmata/kyverno/pkg/webhooks"
|
"github.com/nirmata/kyverno/pkg/webhookconfig"
|
||||||
v1 "k8s.io/api/core/v1"
|
v1 "k8s.io/api/core/v1"
|
||||||
"k8s.io/apimachinery/pkg/api/errors"
|
"k8s.io/apimachinery/pkg/api/errors"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
@ -71,7 +71,7 @@ type PolicyController struct {
|
||||||
// mutationwebhookLister can list/get mutatingwebhookconfigurations
|
// mutationwebhookLister can list/get mutatingwebhookconfigurations
|
||||||
mutationwebhookLister webhooklister.MutatingWebhookConfigurationLister
|
mutationwebhookLister webhooklister.MutatingWebhookConfigurationLister
|
||||||
// WebhookRegistrationClient
|
// WebhookRegistrationClient
|
||||||
webhookRegistrationClient *webhooks.WebhookRegistrationClient
|
webhookRegistrationClient *webhookconfig.WebhookRegistrationClient
|
||||||
// Resource manager, manages the mapping for already processed resource
|
// Resource manager, manages the mapping for already processed resource
|
||||||
rm resourceManager
|
rm resourceManager
|
||||||
// filter the resources defined in the list
|
// filter the resources defined in the list
|
||||||
|
@ -82,7 +82,7 @@ type PolicyController struct {
|
||||||
|
|
||||||
// NewPolicyController create a new PolicyController
|
// NewPolicyController create a new PolicyController
|
||||||
func NewPolicyController(kyvernoClient *kyvernoclient.Clientset, client *client.Client, pInformer kyvernoinformer.PolicyInformer, pvInformer kyvernoinformer.PolicyViolationInformer,
|
func NewPolicyController(kyvernoClient *kyvernoclient.Clientset, client *client.Client, pInformer kyvernoinformer.PolicyInformer, pvInformer kyvernoinformer.PolicyViolationInformer,
|
||||||
eventGen event.Interface, webhookInformer webhookinformer.MutatingWebhookConfigurationInformer, webhookRegistrationClient *webhooks.WebhookRegistrationClient) (*PolicyController, error) {
|
eventGen event.Interface, webhookInformer webhookinformer.MutatingWebhookConfigurationInformer, webhookRegistrationClient *webhookconfig.WebhookRegistrationClient) (*PolicyController, error) {
|
||||||
// Event broad caster
|
// Event broad caster
|
||||||
eventBroadcaster := record.NewBroadcaster()
|
eventBroadcaster := record.NewBroadcaster()
|
||||||
eventBroadcaster.StartLogging(glog.Infof)
|
eventBroadcaster.StartLogging(glog.Infof)
|
||||||
|
@ -405,11 +405,11 @@ func (pc *PolicyController) syncPolicy(key string) error {
|
||||||
policy, err := pc.pLister.Get(key)
|
policy, err := pc.pLister.Get(key)
|
||||||
if errors.IsNotFound(err) {
|
if errors.IsNotFound(err) {
|
||||||
glog.V(2).Infof("Policy %v has been deleted", key)
|
glog.V(2).Infof("Policy %v has been deleted", key)
|
||||||
|
|
||||||
// remove the recorded stats for the policy
|
// remove the recorded stats for the policy
|
||||||
pc.statusAggregator.RemovePolicyStats(key)
|
pc.statusAggregator.RemovePolicyStats(key)
|
||||||
// remove webhook configurations if there are not policies
|
// remove webhook configurations if there are not policies
|
||||||
if err := pc.handleWebhookRegistration(true, nil); err != nil {
|
if err := pc.handleWebhookRegistration(true, nil); err != nil {
|
||||||
glog.Errorln(err)
|
glog.Errorln(err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -466,14 +466,14 @@ func (pc *PolicyController) handleWebhookRegistration(delete bool, policy *kyver
|
||||||
if policies == nil {
|
if policies == nil {
|
||||||
glog.V(3).Infoln("No policy found in the cluster, deregistering webhook")
|
glog.V(3).Infoln("No policy found in the cluster, deregistering webhook")
|
||||||
pc.webhookRegistrationClient.DeregisterMutatingWebhook()
|
pc.webhookRegistrationClient.DeregisterMutatingWebhook()
|
||||||
} else if !webhooks.HasMutateOrValidatePolicies(policies) {
|
} else if !HasMutateOrValidatePolicies(policies) {
|
||||||
glog.V(3).Infoln("No muatate/validate policy found in the cluster, deregistering webhook")
|
glog.V(3).Infoln("No muatate/validate policy found in the cluster, deregistering webhook")
|
||||||
pc.webhookRegistrationClient.DeregisterMutatingWebhook()
|
pc.webhookRegistrationClient.DeregisterMutatingWebhook()
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if webhookList == nil && webhooks.HasMutateOrValidate(*policy) {
|
if webhookList == nil && HasMutateOrValidate(*policy) {
|
||||||
glog.V(3).Infoln("Found policy without mutatingwebhook, registering webhook")
|
glog.V(3).Infoln("Found policy without mutatingwebhook, registering webhook")
|
||||||
pc.webhookRegistrationClient.RegisterMutatingWebhook()
|
pc.webhookRegistrationClient.RegisterMutatingWebhook()
|
||||||
}
|
}
|
||||||
|
@ -934,3 +934,22 @@ func joinPatches(patches ...[]byte) []byte {
|
||||||
result = append(result, []byte("\n]")...)
|
result = append(result, []byte("\n]")...)
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func HasMutateOrValidatePolicies(policies []*kyverno.Policy) bool {
|
||||||
|
for _, policy := range policies {
|
||||||
|
if HasMutateOrValidate(*policy) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func HasMutateOrValidate(policy kyverno.Policy) bool {
|
||||||
|
for _, rule := range policy.Spec.Rules {
|
||||||
|
if !reflect.DeepEqual(rule.Mutation, kyverno.Mutation{}) || !reflect.DeepEqual(rule.Validation, kyverno.Validation{}) {
|
||||||
|
glog.Infoln(rule.Name)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package webhooks
|
package webhookconfig
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
|
@ -1,4 +1,4 @@
|
||||||
package webhooks
|
package webhookconfig
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
|
@ -22,6 +22,7 @@ import (
|
||||||
"github.com/nirmata/kyverno/pkg/policy"
|
"github.com/nirmata/kyverno/pkg/policy"
|
||||||
tlsutils "github.com/nirmata/kyverno/pkg/tls"
|
tlsutils "github.com/nirmata/kyverno/pkg/tls"
|
||||||
"github.com/nirmata/kyverno/pkg/utils"
|
"github.com/nirmata/kyverno/pkg/utils"
|
||||||
|
"github.com/nirmata/kyverno/pkg/webhookconfig"
|
||||||
v1beta1 "k8s.io/api/admission/v1beta1"
|
v1beta1 "k8s.io/api/admission/v1beta1"
|
||||||
"k8s.io/client-go/tools/cache"
|
"k8s.io/client-go/tools/cache"
|
||||||
)
|
)
|
||||||
|
@ -37,7 +38,7 @@ type WebhookServer struct {
|
||||||
pListerSynced cache.InformerSynced
|
pListerSynced cache.InformerSynced
|
||||||
pvListerSynced cache.InformerSynced
|
pvListerSynced cache.InformerSynced
|
||||||
eventGen event.Interface
|
eventGen event.Interface
|
||||||
webhookRegistrationClient *WebhookRegistrationClient
|
webhookRegistrationClient *webhookconfig.WebhookRegistrationClient
|
||||||
// API to send policy stats for aggregation
|
// API to send policy stats for aggregation
|
||||||
policyStatus policy.PolicyStatusInterface
|
policyStatus policy.PolicyStatusInterface
|
||||||
filterK8Resources []utils.K8Resource
|
filterK8Resources []utils.K8Resource
|
||||||
|
@ -52,7 +53,7 @@ func NewWebhookServer(
|
||||||
pInformer kyvernoinformer.PolicyInformer,
|
pInformer kyvernoinformer.PolicyInformer,
|
||||||
pvInormer kyvernoinformer.PolicyViolationInformer,
|
pvInormer kyvernoinformer.PolicyViolationInformer,
|
||||||
eventGen event.Interface,
|
eventGen event.Interface,
|
||||||
webhookRegistrationClient *WebhookRegistrationClient,
|
webhookRegistrationClient *webhookconfig.WebhookRegistrationClient,
|
||||||
policyStatus policy.PolicyStatusInterface,
|
policyStatus policy.PolicyStatusInterface,
|
||||||
filterK8Resources string) (*WebhookServer, error) {
|
filterK8Resources string) (*WebhookServer, error) {
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue