1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-31 03:45:17 +00:00
kyverno/pkg/webhookconfig/rwebhookregister.go

144 lines
5.6 KiB
Go
Raw Normal View History

package webhookconfig
import (
"time"
2020-03-17 11:05:20 -07:00
"github.com/go-logr/logr"
checker "github.com/nirmata/kyverno/pkg/checker"
"github.com/tevino/abool"
mconfiginformer "k8s.io/client-go/informers/admissionregistration/v1beta1"
mconfiglister "k8s.io/client-go/listers/admissionregistration/v1beta1"
cache "k8s.io/client-go/tools/cache"
)
//ResourceWebhookRegister manages the resource webhook registration
type ResourceWebhookRegister struct {
// pendingCreation indicates the status of resource webhook creation
pendingCreation *abool.AtomicBool
LastReqTime *checker.LastReqTime
mwebhookconfigSynced cache.InformerSynced
2020-01-11 18:33:11 +05:30
vwebhookconfigSynced cache.InformerSynced
// list/get mutatingwebhookconfigurations
2020-01-11 18:33:11 +05:30
mWebhookConfigLister mconfiglister.MutatingWebhookConfigurationLister
vWebhookConfigLister mconfiglister.ValidatingWebhookConfigurationLister
webhookRegistrationClient *WebhookRegistrationClient
RunValidationInMutatingWebhook string
2020-03-17 11:05:20 -07:00
log logr.Logger
}
// NewResourceWebhookRegister returns a new instance of ResourceWebhookRegister manager
func NewResourceWebhookRegister(
lastReqTime *checker.LastReqTime,
mconfigwebhookinformer mconfiginformer.MutatingWebhookConfigurationInformer,
2020-01-11 18:33:11 +05:30
vconfigwebhookinformer mconfiginformer.ValidatingWebhookConfigurationInformer,
webhookRegistrationClient *WebhookRegistrationClient,
2020-01-11 18:33:11 +05:30
runValidationInMutatingWebhook string,
2020-03-17 11:05:20 -07:00
log logr.Logger,
) *ResourceWebhookRegister {
return &ResourceWebhookRegister{
2020-01-11 18:33:11 +05:30
pendingCreation: abool.New(),
LastReqTime: lastReqTime,
mwebhookconfigSynced: mconfigwebhookinformer.Informer().HasSynced,
mWebhookConfigLister: mconfigwebhookinformer.Lister(),
vwebhookconfigSynced: vconfigwebhookinformer.Informer().HasSynced,
vWebhookConfigLister: vconfigwebhookinformer.Lister(),
webhookRegistrationClient: webhookRegistrationClient,
RunValidationInMutatingWebhook: runValidationInMutatingWebhook,
2020-03-17 11:05:20 -07:00
log: log,
}
}
//RegisterResourceWebhook registers a resource webhook
func (rww *ResourceWebhookRegister) RegisterResourceWebhook() {
2020-03-17 11:05:20 -07:00
logger := rww.log
// drop the request if creation is in processing
if rww.pendingCreation.IsSet() {
2020-03-17 11:05:20 -07:00
logger.V(3).Info("resource webhook configuration is in pending creation, skip the request")
return
}
timeDiff := time.Since(rww.LastReqTime.Time())
if timeDiff < checker.DefaultDeadline {
2020-03-17 11:05:20 -07:00
logger.V(3).Info("verified webhook status, creating webhook configuration")
2020-01-11 18:33:11 +05:30
go func() {
mutatingConfigName := rww.webhookRegistrationClient.GetResourceMutatingWebhookConfigName()
mutatingConfig, _ := rww.mWebhookConfigLister.Get(mutatingConfigName)
if mutatingConfig != nil {
2020-03-17 11:05:20 -07:00
logger.V(4).Info("mutating webhoook configuration already exists")
2020-01-11 18:33:11 +05:30
} else {
rww.pendingCreation.Set()
err1 := rww.webhookRegistrationClient.CreateResourceMutatingWebhookConfiguration()
rww.pendingCreation.UnSet()
if err1 != nil {
2020-03-17 11:05:20 -07:00
logger.Error(err1, "failed to create resource mutating webhook configuration, re-queue creation request")
2020-01-11 18:33:11 +05:30
rww.RegisterResourceWebhook()
return
}
2020-03-17 11:05:20 -07:00
logger.V(3).Info("successfully created mutating webhook configuration for resources")
2020-01-11 18:33:11 +05:30
}
if rww.RunValidationInMutatingWebhook != "true" {
validatingConfigName := rww.webhookRegistrationClient.GetResourceValidatingWebhookConfigName()
validatingConfig, _ := rww.vWebhookConfigLister.Get(validatingConfigName)
if validatingConfig != nil {
2020-03-17 11:05:20 -07:00
logger.V(4).Info("validating webhoook configuration already exists")
2020-01-11 18:33:11 +05:30
} else {
rww.pendingCreation.Set()
err2 := rww.webhookRegistrationClient.CreateResourceValidatingWebhookConfiguration()
rww.pendingCreation.UnSet()
if err2 != nil {
2020-03-17 11:05:20 -07:00
logger.Error(err2, "failed to create resource validating webhook configuration; re-queue creation request")
2020-01-11 18:33:11 +05:30
rww.RegisterResourceWebhook()
return
}
2020-03-17 11:05:20 -07:00
logger.V(3).Info("successfully created validating webhook configuration for resources")
2020-01-11 18:33:11 +05:30
}
}
}()
}
}
//Run starts the ResourceWebhookRegister manager
func (rww *ResourceWebhookRegister) Run(stopCh <-chan struct{}) {
2020-03-17 11:05:20 -07:00
logger := rww.log
// wait for cache to populate first time
2020-01-11 18:33:11 +05:30
if !cache.WaitForCacheSync(stopCh, rww.mwebhookconfigSynced, rww.vwebhookconfigSynced) {
2020-03-17 11:05:20 -07:00
logger.Info("configuration: failed to sync webhook informer cache")
}
}
// RemoveResourceWebhookConfiguration removes the resource webhook configurations
func (rww *ResourceWebhookRegister) RemoveResourceWebhookConfiguration() error {
2020-03-17 11:05:20 -07:00
logger := rww.log
2020-01-11 18:33:11 +05:30
mutatingConfigName := rww.webhookRegistrationClient.GetResourceMutatingWebhookConfigName()
mutatingConfig, err := rww.mWebhookConfigLister.Get(mutatingConfigName)
if err != nil {
2020-03-17 11:05:20 -07:00
logger.Error(err, "failed to list mutating webhook config")
return err
}
2020-01-11 18:33:11 +05:30
if mutatingConfig != nil {
err = rww.webhookRegistrationClient.RemoveResourceMutatingWebhookConfiguration()
if err != nil {
return err
}
2020-03-17 11:05:20 -07:00
logger.V(3).Info("emoved mutating resource webhook configuration")
}
2020-01-11 18:33:11 +05:30
if rww.RunValidationInMutatingWebhook != "true" {
validatingConfigName := rww.webhookRegistrationClient.GetResourceValidatingWebhookConfigName()
validatingConfig, err := rww.vWebhookConfigLister.Get(validatingConfigName)
if err != nil {
2020-03-17 11:05:20 -07:00
logger.Error(err, "failed to list validating webhook config")
2020-01-11 18:33:11 +05:30
return err
}
if validatingConfig != nil {
err = rww.webhookRegistrationClient.RemoveResourceValidatingWebhookConfiguration()
if err != nil {
return err
}
2020-03-17 11:05:20 -07:00
logger.V(3).Info("removed validating resource webhook configuration")
2020-01-11 18:33:11 +05:30
}
}
return nil
}