2019-08-07 18:01:28 -07:00
|
|
|
package webhooks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/golang/glog"
|
|
|
|
v1alpha1 "github.com/nirmata/kyverno/pkg/apis/policy/v1alpha1"
|
2019-08-08 13:09:40 -07:00
|
|
|
v1beta1 "k8s.io/api/admission/v1beta1"
|
|
|
|
"k8s.io/apimachinery/pkg/labels"
|
2019-08-07 18:01:28 -07:00
|
|
|
)
|
|
|
|
|
2019-08-08 13:09:40 -07:00
|
|
|
type policyType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
none policyType = iota
|
|
|
|
mutate
|
|
|
|
validate
|
|
|
|
all
|
|
|
|
)
|
|
|
|
|
|
|
|
func (ws *WebhookServer) manageWebhookConfigurations(policy v1alpha1.Policy, op v1beta1.Operation) {
|
|
|
|
switch op {
|
|
|
|
case v1beta1.Create:
|
|
|
|
ws.registerWebhookConfigurations(policy)
|
|
|
|
case v1beta1.Delete:
|
|
|
|
ws.deregisterWebhookConfigurations(policy)
|
|
|
|
}
|
|
|
|
}
|
2019-08-07 18:01:28 -07:00
|
|
|
|
2019-08-08 13:09:40 -07:00
|
|
|
func (ws *WebhookServer) registerWebhookConfigurations(policy v1alpha1.Policy) error {
|
2019-08-07 18:01:28 -07:00
|
|
|
for _, rule := range policy.Spec.Rules {
|
|
|
|
if rule.Mutation != nil && !ws.webhookRegistrationClient.MutationRegistered.IsSet() {
|
|
|
|
if err := ws.webhookRegistrationClient.RegisterMutatingWebhook(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
glog.Infof("Mutating webhook registered")
|
|
|
|
}
|
|
|
|
|
|
|
|
if rule.Validation != nil && !ws.webhookRegistrationClient.ValidationRegistered.IsSet() {
|
|
|
|
if err := ws.webhookRegistrationClient.RegisterValidatingWebhook(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
glog.Infof("Validating webhook registered")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-08-08 13:09:40 -07:00
|
|
|
|
|
|
|
func (ws *WebhookServer) deregisterWebhookConfigurations(policy v1alpha1.Policy) error {
|
|
|
|
glog.V(3).Infof("Retreiving policy type for %s\n", policy.Name)
|
|
|
|
|
2019-08-08 15:10:10 -07:00
|
|
|
pt := GetPolicyType([]*v1alpha1.Policy{&policy}, "")
|
2019-08-08 13:09:40 -07:00
|
|
|
|
2019-08-08 15:10:10 -07:00
|
|
|
glog.V(3).Infof("Policy to be deleted type==%v\n", pt)
|
2019-08-08 13:09:40 -07:00
|
|
|
|
2019-08-08 15:10:10 -07:00
|
|
|
existPolicyType := ws.getExistingPolicyType(policy.Name)
|
2019-08-08 13:09:40 -07:00
|
|
|
glog.V(3).Infof("Found existing policy type==%v\n", existPolicyType)
|
|
|
|
|
|
|
|
switch existPolicyType {
|
|
|
|
case none:
|
|
|
|
ws.webhookRegistrationClient.deregister()
|
|
|
|
glog.Infoln("All webhook deregistered")
|
|
|
|
case mutate:
|
|
|
|
if pt != mutate {
|
|
|
|
ws.webhookRegistrationClient.deregisterValidatingWebhook()
|
|
|
|
glog.Infoln("Validating webhook deregistered")
|
|
|
|
}
|
|
|
|
case validate:
|
|
|
|
if pt != validate {
|
|
|
|
ws.webhookRegistrationClient.deregisterMutatingWebhook()
|
|
|
|
glog.Infoln("Mutating webhook deregistered")
|
|
|
|
}
|
|
|
|
case all:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-08-08 15:10:10 -07:00
|
|
|
func (ws *WebhookServer) getExistingPolicyType(policyName string) policyType {
|
2019-08-08 13:09:40 -07:00
|
|
|
|
|
|
|
policies, err := ws.policyLister.List(labels.NewSelector())
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("Failed to get policy list")
|
|
|
|
}
|
|
|
|
|
2019-08-08 15:10:10 -07:00
|
|
|
return GetPolicyType(policies, policyName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPolicyType get the type of policies
|
|
|
|
// excludes is the policy name to be skipped
|
|
|
|
func GetPolicyType(policyList []*v1alpha1.Policy, excludes string) policyType {
|
|
|
|
ptype := none
|
|
|
|
|
|
|
|
for _, p := range policyList {
|
|
|
|
if p.Name == excludes {
|
|
|
|
glog.Infof("Skipping policy type check on %s\n", excludes)
|
2019-08-08 13:09:40 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, rule := range p.Spec.Rules {
|
|
|
|
if rule.Mutation != nil {
|
|
|
|
ptype = ptype | mutate
|
|
|
|
}
|
|
|
|
|
|
|
|
if rule.Validation != nil {
|
|
|
|
ptype = ptype | validate
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ptype
|
|
|
|
}
|