1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-11 02:16:53 +00:00
kyverno/pkg/webhooks/webhookManager.go

50 lines
1.2 KiB
Go
Raw Normal View History

package webhooks
import (
"github.com/golang/glog"
kyverno "github.com/nirmata/kyverno/pkg/api/kyverno/v1alpha1"
2019-08-08 13:09:40 -07:00
v1beta1 "k8s.io/api/admission/v1beta1"
"k8s.io/apimachinery/pkg/labels"
)
2019-08-08 13:09:40 -07:00
type policyType int
const (
none policyType = iota
mutate
validate
all
)
func (ws *WebhookServer) manageWebhookConfigurations(policy kyverno.Policy, op v1beta1.Operation) {
2019-08-08 13:09:40 -07:00
switch op {
case v1beta1.Create:
ws.registerWebhookConfigurations(policy)
case v1beta1.Delete:
ws.deregisterWebhookConfigurations(policy)
}
}
func (ws *WebhookServer) registerWebhookConfigurations(policy kyverno.Policy) error {
if !ws.webhookRegistrationClient.MutationRegistered.IsSet() {
if err := ws.webhookRegistrationClient.RegisterMutatingWebhook(); err != nil {
return err
}
glog.Infof("Mutating webhook registered")
}
return nil
}
2019-08-08 13:09:40 -07:00
func (ws *WebhookServer) deregisterWebhookConfigurations(policy kyverno.Policy) error {
policies, _ := ws.pLister.List(labels.NewSelector())
2019-08-08 13:09:40 -07:00
// deregister webhook if no policy found in cluster
if len(policies) == 1 {
ws.webhookRegistrationClient.deregisterMutatingWebhook()
glog.Infoln("Mutating webhook deregistered")
2019-08-08 13:09:40 -07:00
}
return nil
}