2019-08-27 14:52:56 -07:00
|
|
|
package webhookconfig
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-11-26 16:07:06 -08:00
|
|
|
"sync"
|
2019-08-27 14:52:56 -07:00
|
|
|
|
2020-10-07 11:12:31 -07:00
|
|
|
"github.com/kyverno/kyverno/pkg/config"
|
2021-10-05 00:15:09 -07:00
|
|
|
admregapi "k8s.io/api/admissionregistration/v1"
|
2021-04-12 20:29:51 -07:00
|
|
|
errorsapi "k8s.io/apimachinery/pkg/api/errors"
|
2019-08-27 14:52:56 -07:00
|
|
|
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
)
|
|
|
|
|
2021-10-05 00:15:09 -07:00
|
|
|
func (wrc *Register) defaultResourceWebhookRule() admregapi.Rule {
|
|
|
|
if wrc.autoUpdateWebhooks {
|
|
|
|
return admregapi.Rule{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return admregapi.Rule{
|
|
|
|
Resources: []string{"*/*"},
|
|
|
|
APIGroups: []string{"*"},
|
|
|
|
APIVersions: []string{"*"},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-14 13:01:40 -07:00
|
|
|
func (wrc *Register) constructDefaultDebugMutatingWebhookConfig(caData []byte) *admregapi.MutatingWebhookConfiguration {
|
2020-03-17 11:05:20 -07:00
|
|
|
logger := wrc.log
|
2019-08-27 14:52:56 -07:00
|
|
|
url := fmt.Sprintf("https://%s%s", wrc.serverIP, config.MutatingWebhookServicePath)
|
2020-05-17 14:37:05 -07:00
|
|
|
logger.V(4).Info("Debug MutatingWebhookConfig registered", "url", url)
|
2019-08-27 14:52:56 -07:00
|
|
|
return &admregapi.MutatingWebhookConfiguration{
|
|
|
|
ObjectMeta: v1.ObjectMeta{
|
2019-09-04 13:43:12 -07:00
|
|
|
Name: config.MutatingWebhookConfigurationDebugName,
|
2019-08-27 14:52:56 -07:00
|
|
|
},
|
2020-03-17 11:05:20 -07:00
|
|
|
Webhooks: []admregapi.MutatingWebhook{
|
|
|
|
generateDebugMutatingWebhook(
|
2021-10-05 00:15:09 -07:00
|
|
|
config.MutatingWebhookName+"-ignore",
|
2019-08-27 14:52:56 -07:00
|
|
|
url,
|
|
|
|
caData,
|
|
|
|
true,
|
|
|
|
wrc.timeoutSeconds,
|
2021-10-05 00:15:09 -07:00
|
|
|
wrc.defaultResourceWebhookRule(),
|
2019-09-04 13:43:12 -07:00
|
|
|
[]admregapi.OperationType{admregapi.Create, admregapi.Update},
|
2021-10-05 00:15:09 -07:00
|
|
|
admregapi.Ignore,
|
|
|
|
),
|
|
|
|
generateDebugMutatingWebhook(
|
|
|
|
config.MutatingWebhookName+"-fail",
|
|
|
|
url,
|
|
|
|
caData,
|
|
|
|
true,
|
|
|
|
wrc.timeoutSeconds,
|
|
|
|
wrc.defaultResourceWebhookRule(),
|
|
|
|
[]admregapi.OperationType{admregapi.Create, admregapi.Update},
|
|
|
|
admregapi.Fail,
|
2019-08-27 14:52:56 -07:00
|
|
|
),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-14 13:01:40 -07:00
|
|
|
func (wrc *Register) constructDefaultMutatingWebhookConfig(caData []byte) *admregapi.MutatingWebhookConfiguration {
|
2019-08-27 14:52:56 -07:00
|
|
|
return &admregapi.MutatingWebhookConfiguration{
|
|
|
|
ObjectMeta: v1.ObjectMeta{
|
2019-09-04 13:43:12 -07:00
|
|
|
Name: config.MutatingWebhookConfigurationName,
|
2021-08-14 05:37:40 +05:30
|
|
|
OwnerReferences: []v1.OwnerReference{
|
|
|
|
wrc.constructOwner(),
|
|
|
|
},
|
2019-08-27 14:52:56 -07:00
|
|
|
},
|
2021-10-05 00:15:09 -07:00
|
|
|
Webhooks: []admregapi.MutatingWebhook{
|
|
|
|
generateMutatingWebhook(
|
|
|
|
config.MutatingWebhookName+"-ignore",
|
|
|
|
config.MutatingWebhookServicePath,
|
|
|
|
caData,
|
|
|
|
false,
|
|
|
|
wrc.timeoutSeconds,
|
|
|
|
wrc.defaultResourceWebhookRule(),
|
|
|
|
[]admregapi.OperationType{admregapi.Create, admregapi.Update},
|
|
|
|
admregapi.Ignore,
|
|
|
|
),
|
|
|
|
generateMutatingWebhook(
|
|
|
|
config.MutatingWebhookName+"-fail",
|
|
|
|
config.MutatingWebhookServicePath,
|
|
|
|
caData,
|
|
|
|
false,
|
|
|
|
wrc.timeoutSeconds,
|
|
|
|
wrc.defaultResourceWebhookRule(),
|
|
|
|
[]admregapi.OperationType{admregapi.Create, admregapi.Update},
|
|
|
|
admregapi.Fail,
|
|
|
|
),
|
|
|
|
},
|
2019-08-27 14:52:56 -07:00
|
|
|
}
|
|
|
|
}
|
2019-09-04 13:43:12 -07:00
|
|
|
|
2020-11-26 16:07:06 -08:00
|
|
|
//getResourceMutatingWebhookConfigName returns the webhook configuration name
|
2021-10-05 00:15:09 -07:00
|
|
|
func getResourceMutatingWebhookConfigName(serverIP string) string {
|
|
|
|
if serverIP != "" {
|
2019-11-15 14:01:40 -08:00
|
|
|
return config.MutatingWebhookConfigurationDebugName
|
2019-09-04 13:43:12 -07:00
|
|
|
}
|
2019-11-15 14:01:40 -08:00
|
|
|
return config.MutatingWebhookConfigurationName
|
|
|
|
}
|
|
|
|
|
2020-11-26 16:07:06 -08:00
|
|
|
func (wrc *Register) removeResourceMutatingWebhookConfiguration(wg *sync.WaitGroup) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
2021-10-05 00:15:09 -07:00
|
|
|
configName := getResourceMutatingWebhookConfigName(wrc.serverIP)
|
2020-11-26 16:07:06 -08:00
|
|
|
logger := wrc.log.WithValues("kind", kindMutating, "name", configName)
|
2021-04-12 20:29:51 -07:00
|
|
|
|
|
|
|
if mutateCache, ok := wrc.resCache.GetGVRCache("MutatingWebhookConfiguration"); ok {
|
|
|
|
if _, err := mutateCache.Lister().Get(configName); err != nil && errorsapi.IsNotFound(err) {
|
|
|
|
logger.V(4).Info("webhook not found")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-04 13:43:12 -07:00
|
|
|
// delete webhook configuration
|
2020-11-26 16:07:06 -08:00
|
|
|
err := wrc.client.DeleteResource("", kindMutating, "", configName, false)
|
2021-10-05 00:15:09 -07:00
|
|
|
if errorsapi.IsNotFound(err) {
|
2020-06-30 11:53:27 -07:00
|
|
|
logger.V(4).Info("webhook configuration not found")
|
|
|
|
return
|
2019-09-04 13:43:12 -07:00
|
|
|
}
|
2020-05-17 14:37:05 -07:00
|
|
|
|
2019-09-04 13:43:12 -07:00
|
|
|
if err != nil {
|
2020-06-30 11:53:27 -07:00
|
|
|
logger.Error(err, "failed to delete the mutating webhook configuration")
|
|
|
|
return
|
2019-09-04 13:43:12 -07:00
|
|
|
}
|
2020-05-17 14:37:05 -07:00
|
|
|
|
2020-11-26 16:07:06 -08:00
|
|
|
logger.Info("webhook configuration deleted")
|
2019-09-04 13:43:12 -07:00
|
|
|
}
|
2020-01-11 18:33:11 +05:30
|
|
|
|
2021-06-14 13:01:40 -07:00
|
|
|
func (wrc *Register) constructDefaultDebugValidatingWebhookConfig(caData []byte) *admregapi.ValidatingWebhookConfiguration {
|
2020-01-11 18:33:11 +05:30
|
|
|
url := fmt.Sprintf("https://%s%s", wrc.serverIP, config.ValidatingWebhookServicePath)
|
|
|
|
|
|
|
|
return &admregapi.ValidatingWebhookConfiguration{
|
|
|
|
ObjectMeta: v1.ObjectMeta{
|
|
|
|
Name: config.ValidatingWebhookConfigurationDebugName,
|
|
|
|
},
|
2020-03-17 11:05:20 -07:00
|
|
|
Webhooks: []admregapi.ValidatingWebhook{
|
|
|
|
generateDebugValidatingWebhook(
|
2021-10-05 00:15:09 -07:00
|
|
|
config.ValidatingWebhookName+"-ignore",
|
|
|
|
url,
|
|
|
|
caData,
|
|
|
|
true,
|
|
|
|
wrc.timeoutSeconds,
|
|
|
|
wrc.defaultResourceWebhookRule(),
|
|
|
|
[]admregapi.OperationType{admregapi.Create, admregapi.Update, admregapi.Delete, admregapi.Connect},
|
|
|
|
admregapi.Ignore,
|
|
|
|
),
|
|
|
|
generateDebugValidatingWebhook(
|
|
|
|
config.ValidatingWebhookName+"-fail",
|
2020-01-11 18:33:11 +05:30
|
|
|
url,
|
|
|
|
caData,
|
|
|
|
true,
|
|
|
|
wrc.timeoutSeconds,
|
2021-10-05 00:15:09 -07:00
|
|
|
wrc.defaultResourceWebhookRule(),
|
2021-07-15 00:12:10 +05:30
|
|
|
[]admregapi.OperationType{admregapi.Create, admregapi.Update, admregapi.Delete, admregapi.Connect},
|
2021-10-05 00:15:09 -07:00
|
|
|
admregapi.Fail,
|
2020-01-11 18:33:11 +05:30
|
|
|
),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-14 13:01:40 -07:00
|
|
|
func (wrc *Register) constructDefaultValidatingWebhookConfig(caData []byte) *admregapi.ValidatingWebhookConfiguration {
|
2020-01-11 18:33:11 +05:30
|
|
|
return &admregapi.ValidatingWebhookConfiguration{
|
|
|
|
ObjectMeta: v1.ObjectMeta{
|
|
|
|
Name: config.ValidatingWebhookConfigurationName,
|
2021-08-14 05:37:40 +05:30
|
|
|
OwnerReferences: []v1.OwnerReference{
|
|
|
|
wrc.constructOwner(),
|
|
|
|
},
|
2020-01-11 18:33:11 +05:30
|
|
|
},
|
2020-03-17 11:05:20 -07:00
|
|
|
Webhooks: []admregapi.ValidatingWebhook{
|
|
|
|
generateValidatingWebhook(
|
2021-10-05 00:15:09 -07:00
|
|
|
config.ValidatingWebhookName+"-ignore",
|
|
|
|
config.ValidatingWebhookServicePath,
|
|
|
|
caData,
|
|
|
|
false,
|
|
|
|
wrc.timeoutSeconds,
|
|
|
|
wrc.defaultResourceWebhookRule(),
|
|
|
|
[]admregapi.OperationType{admregapi.Create, admregapi.Update, admregapi.Delete, admregapi.Connect},
|
|
|
|
admregapi.Ignore,
|
|
|
|
),
|
|
|
|
generateValidatingWebhook(
|
|
|
|
config.ValidatingWebhookName+"-fail",
|
2020-01-11 18:33:11 +05:30
|
|
|
config.ValidatingWebhookServicePath,
|
|
|
|
caData,
|
|
|
|
false,
|
|
|
|
wrc.timeoutSeconds,
|
2021-10-05 00:15:09 -07:00
|
|
|
wrc.defaultResourceWebhookRule(),
|
2021-07-15 00:12:10 +05:30
|
|
|
[]admregapi.OperationType{admregapi.Create, admregapi.Update, admregapi.Delete, admregapi.Connect},
|
2021-10-05 00:15:09 -07:00
|
|
|
admregapi.Fail,
|
2020-01-11 18:33:11 +05:30
|
|
|
),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-26 16:07:06 -08:00
|
|
|
// getResourceValidatingWebhookConfigName returns the webhook configuration name
|
2021-10-05 00:15:09 -07:00
|
|
|
func getResourceValidatingWebhookConfigName(serverIP string) string {
|
|
|
|
if serverIP != "" {
|
2020-01-11 18:33:11 +05:30
|
|
|
return config.ValidatingWebhookConfigurationDebugName
|
|
|
|
}
|
2020-05-17 14:37:05 -07:00
|
|
|
|
2020-01-11 18:33:11 +05:30
|
|
|
return config.ValidatingWebhookConfigurationName
|
|
|
|
}
|
|
|
|
|
2020-11-26 16:07:06 -08:00
|
|
|
func (wrc *Register) removeResourceValidatingWebhookConfiguration(wg *sync.WaitGroup) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
2021-10-05 00:15:09 -07:00
|
|
|
configName := getResourceValidatingWebhookConfigName(wrc.serverIP)
|
2020-11-26 16:07:06 -08:00
|
|
|
logger := wrc.log.WithValues("kind", kindValidating, "name", configName)
|
2021-04-12 20:29:51 -07:00
|
|
|
|
|
|
|
if mutateCache, ok := wrc.resCache.GetGVRCache("ValidatingWebhookConfiguration"); ok {
|
|
|
|
if _, err := mutateCache.Lister().Get(configName); err != nil && errorsapi.IsNotFound(err) {
|
|
|
|
logger.V(4).Info("webhook not found")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-26 16:07:06 -08:00
|
|
|
err := wrc.client.DeleteResource("", kindValidating, "", configName, false)
|
2021-10-05 00:15:09 -07:00
|
|
|
if errorsapi.IsNotFound(err) {
|
2020-05-17 14:37:05 -07:00
|
|
|
logger.V(5).Info("webhook configuration not found")
|
2020-06-30 11:53:27 -07:00
|
|
|
return
|
2020-01-11 18:33:11 +05:30
|
|
|
}
|
2020-05-17 14:37:05 -07:00
|
|
|
|
2020-01-11 18:33:11 +05:30
|
|
|
if err != nil {
|
2020-06-30 11:53:27 -07:00
|
|
|
logger.Error(err, "failed to delete the validating webhook configuration")
|
|
|
|
return
|
2020-01-11 18:33:11 +05:30
|
|
|
}
|
2020-05-17 14:37:05 -07:00
|
|
|
|
2020-11-26 16:07:06 -08:00
|
|
|
logger.Info("webhook configuration deleted")
|
2020-01-11 18:33:11 +05:30
|
|
|
}
|