1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-04-15 08:46:36 +00:00

Skip updating webhook configs if namespaceSelector is nil (#3237)

* skip updating webhook configs if namespaceSelector is nil

Signed-off-by: ShutingZhao <shuting@nirmata.com>

* address comments

Signed-off-by: ShutingZhao <shuting@nirmata.com>

* address comment for mutating webhook

Signed-off-by: ShutingZhao <shuting@nirmata.com>

* address comments

Signed-off-by: ShutingZhao <shuting@nirmata.com>

* update logs

Signed-off-by: ShutingZhao <shuting@nirmata.com>
This commit is contained in:
shuting 2022-02-16 19:37:09 +08:00 committed by GitHub
parent a970953d51
commit 2eefe3a544
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -3,6 +3,7 @@ package webhookconfig
import (
"encoding/json"
"fmt"
"reflect"
"strings"
"sync"
"time"
@ -238,15 +239,11 @@ func (wrc *Register) UpdateWebhookConfigurations(configHandler config.Interface)
if err := wrc.updateResourceMutatingWebhookConfiguration(nsSelector); err != nil {
logger.Error(err, "unable to update mutatingWebhookConfigurations", "name", getResourceMutatingWebhookConfigName(wrc.serverIP))
go func() { wrc.UpdateWebhookChan <- true }()
} else {
logger.V(4).Info("successfully updated mutatingWebhookConfigurations", "name", getResourceMutatingWebhookConfigName(wrc.serverIP))
}
if err := wrc.updateResourceValidatingWebhookConfiguration(nsSelector); err != nil {
logger.Error(err, "unable to update validatingWebhookConfigurations", "name", getResourceValidatingWebhookConfigName(wrc.serverIP))
go func() { wrc.UpdateWebhookChan <- true }()
} else {
logger.V(4).Info("successfully updated validatingWebhookConfigurations", "name", getResourceValidatingWebhookConfigName(wrc.serverIP))
}
}
}
@ -734,17 +731,33 @@ func (wrc *Register) updateResourceValidatingWebhookConfiguration(nsSelector map
ok bool
)
webhookChanged := false
for i, whu := range webhooksUntyped {
webhook, ok = whu.(map[string]interface{})
if !ok {
return errors.Wrapf(err, "type mismatched, expected map[string]interface{}, got %T", webhooksUntyped[i])
}
currentSelector, _, err := unstructured.NestedMap(webhook, "namespaceSelector")
if err != nil {
return errors.Wrapf(err, "unable to get validatingWebhookConfigurations.webhooks["+fmt.Sprint(i)+"].namespaceSelector")
}
if !reflect.DeepEqual(nsSelector, currentSelector) {
webhookChanged = true
}
if err = unstructured.SetNestedMap(webhook, nsSelector, "namespaceSelector"); err != nil {
return errors.Wrapf(err, "unable to set validatingWebhookConfigurations.webhooks["+fmt.Sprint(i)+"].namespaceSelector")
}
webhooks = append(webhooks, webhook)
}
if !webhookChanged {
wrc.log.V(4).Info("namespaceSelector unchanged, skip updating validatingWebhookConfigurations")
return nil
}
if err = unstructured.SetNestedSlice(resourceValidating.UnstructuredContent(), webhooks, "webhooks"); err != nil {
return errors.Wrapf(err, "unable to set validatingWebhookConfigurations.webhooks")
}
@ -753,6 +766,8 @@ func (wrc *Register) updateResourceValidatingWebhookConfiguration(nsSelector map
return err
}
wrc.log.V(3).Info("successfully updated validatingWebhookConfigurations", "name", getResourceMutatingWebhookConfigName(wrc.serverIP))
return nil
}
@ -784,17 +799,33 @@ func (wrc *Register) updateResourceMutatingWebhookConfiguration(nsSelector map[s
ok bool
)
webhookChanged := false
for i, whu := range webhooksUntyped {
webhook, ok = whu.(map[string]interface{})
if !ok {
return errors.Wrapf(err, "type mismatched, expected map[string]interface{}, got %T", webhooksUntyped[i])
}
currentSelector, _, err := unstructured.NestedMap(webhook, "namespaceSelector")
if err != nil {
return errors.Wrapf(err, "unable to get mutatingWebhookConfigurations.webhooks["+fmt.Sprint(i)+"].namespaceSelector")
}
if !reflect.DeepEqual(nsSelector, currentSelector) {
webhookChanged = true
}
if err = unstructured.SetNestedMap(webhook, nsSelector, "namespaceSelector"); err != nil {
return errors.Wrapf(err, "unable to set mutatingWebhookConfigurations.webhooks["+fmt.Sprint(i)+"].namespaceSelector")
}
webhooks = append(webhooks, webhook)
}
if !webhookChanged {
wrc.log.V(4).Info("namespaceSelector unchanged, skip updating mutatingWebhookConfigurations")
return nil
}
if err = unstructured.SetNestedSlice(resourceMutating.UnstructuredContent(), webhooks, "webhooks"); err != nil {
return errors.Wrapf(err, "unable to set mutatingWebhookConfigurations.webhooks")
}
@ -803,5 +834,7 @@ func (wrc *Register) updateResourceMutatingWebhookConfiguration(nsSelector map[s
return err
}
wrc.log.V(3).Info("successfully updated mutatingWebhookConfigurations", "name", getResourceMutatingWebhookConfigName(wrc.serverIP))
return nil
}