mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-08 03:33:56 +00:00
Merge pull request #56478 from hzxuzhonghu/fix-admissionregistration
Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. admissionregistration use shared informer instead of poll **What this PR does / why we need it**: poll with 1s interval influence apiserver's performance **Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*: Fixes #56357 **Special notes for your reviewer**: **Release note**: ```release-note NONE ```
This commit is contained in:
commit
46df831351
8
staging/src/k8s.io/apiserver/Godeps/Godeps.json
generated
8
staging/src/k8s.io/apiserver/Godeps/Godeps.json
generated
@ -1790,6 +1790,10 @@
|
|||||||
"ImportPath": "k8s.io/client-go/informers",
|
"ImportPath": "k8s.io/client-go/informers",
|
||||||
"Rev": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
|
"Rev": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/client-go/informers/admissionregistration/v1beta1",
|
||||||
|
"Rev": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "k8s.io/client-go/kubernetes",
|
"ImportPath": "k8s.io/client-go/kubernetes",
|
||||||
"Rev": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
|
"Rev": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
|
||||||
@ -1814,6 +1818,10 @@
|
|||||||
"ImportPath": "k8s.io/client-go/kubernetes/typed/core/v1",
|
"ImportPath": "k8s.io/client-go/kubernetes/typed/core/v1",
|
||||||
"Rev": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
|
"Rev": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/client-go/listers/admissionregistration/v1beta1",
|
||||||
|
"Rev": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "k8s.io/client-go/listers/core/v1",
|
"ImportPath": "k8s.io/client-go/listers/core/v1",
|
||||||
"Rev": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
|
"Rev": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
|
||||||
|
@ -21,9 +21,12 @@ go_test(
|
|||||||
"//vendor/k8s.io/api/admissionregistration/v1beta1:go_default_library",
|
"//vendor/k8s.io/api/admissionregistration/v1beta1:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
||||||
|
"//vendor/k8s.io/apimachinery/pkg/labels:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/util/wait:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/util/wait:go_default_library",
|
||||||
|
"//vendor/k8s.io/client-go/listers/admissionregistration/v1beta1:go_default_library",
|
||||||
|
"//vendor/k8s.io/client-go/tools/cache:go_default_library",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -42,8 +45,13 @@ go_library(
|
|||||||
"//vendor/k8s.io/api/admissionregistration/v1beta1:go_default_library",
|
"//vendor/k8s.io/api/admissionregistration/v1beta1:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
||||||
|
"//vendor/k8s.io/apimachinery/pkg/labels:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
|
||||||
|
"//vendor/k8s.io/apimachinery/pkg/util/runtime:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/util/wait:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/util/wait:go_default_library",
|
||||||
|
"//vendor/k8s.io/client-go/informers/admissionregistration/v1beta1:go_default_library",
|
||||||
|
"//vendor/k8s.io/client-go/listers/admissionregistration/v1beta1:go_default_library",
|
||||||
|
"//vendor/k8s.io/client-go/tools/cache:go_default_library",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -18,84 +18,70 @@ package configuration
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
|
||||||
"sort"
|
"sort"
|
||||||
|
"sync/atomic"
|
||||||
"github.com/golang/glog"
|
|
||||||
|
|
||||||
"k8s.io/api/admissionregistration/v1beta1"
|
"k8s.io/api/admissionregistration/v1beta1"
|
||||||
"k8s.io/apimachinery/pkg/api/errors"
|
"k8s.io/apimachinery/pkg/labels"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
admissionregistrationinformers "k8s.io/client-go/informers/admissionregistration/v1beta1"
|
||||||
|
admissionregistrationlisters "k8s.io/client-go/listers/admissionregistration/v1beta1"
|
||||||
|
"k8s.io/client-go/tools/cache"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MutatingWebhookConfigurationLister interface {
|
|
||||||
List(opts metav1.ListOptions) (*v1beta1.MutatingWebhookConfigurationList, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
// MutatingWebhookConfigurationManager collects the mutating webhook objects so that they can be called.
|
// MutatingWebhookConfigurationManager collects the mutating webhook objects so that they can be called.
|
||||||
type MutatingWebhookConfigurationManager struct {
|
type MutatingWebhookConfigurationManager struct {
|
||||||
*poller
|
configuration *atomic.Value
|
||||||
|
lister admissionregistrationlisters.MutatingWebhookConfigurationLister
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMutatingWebhookConfigurationManager(c MutatingWebhookConfigurationLister) *MutatingWebhookConfigurationManager {
|
func NewMutatingWebhookConfigurationManager(informer admissionregistrationinformers.MutatingWebhookConfigurationInformer) *MutatingWebhookConfigurationManager {
|
||||||
getFn := func() (runtime.Object, error) {
|
manager := &MutatingWebhookConfigurationManager{
|
||||||
list, err := c.List(metav1.ListOptions{})
|
configuration: &atomic.Value{},
|
||||||
if err != nil {
|
lister: informer.Lister(),
|
||||||
if errors.IsNotFound(err) || errors.IsForbidden(err) {
|
|
||||||
glog.V(5).Infof("MutatingWebhookConfiguration are disabled due to an error: %v", err)
|
|
||||||
return nil, ErrDisabled
|
|
||||||
}
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return mergeMutatingWebhookConfigurations(list), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return &MutatingWebhookConfigurationManager{
|
// Start with an empty list
|
||||||
newPoller(getFn),
|
manager.configuration.Store(&v1beta1.MutatingWebhookConfiguration{})
|
||||||
}
|
|
||||||
|
// On any change, rebuild the config
|
||||||
|
informer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
|
||||||
|
AddFunc: func(_ interface{}) { manager.updateConfiguration() },
|
||||||
|
UpdateFunc: func(_, _ interface{}) { manager.updateConfiguration() },
|
||||||
|
DeleteFunc: func(_ interface{}) { manager.updateConfiguration() },
|
||||||
|
})
|
||||||
|
|
||||||
|
return manager
|
||||||
}
|
}
|
||||||
|
|
||||||
// Webhooks returns the merged MutatingWebhookConfiguration.
|
// Webhooks returns the merged MutatingWebhookConfiguration.
|
||||||
func (im *MutatingWebhookConfigurationManager) Webhooks() (*v1beta1.MutatingWebhookConfiguration, error) {
|
func (m *MutatingWebhookConfigurationManager) Webhooks() *v1beta1.MutatingWebhookConfiguration {
|
||||||
configuration, err := im.poller.configuration()
|
return m.configuration.Load().(*v1beta1.MutatingWebhookConfiguration)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MutatingWebhookConfigurationManager) updateConfiguration() {
|
||||||
|
configurations, err := m.lister.List(labels.Everything())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
utilruntime.HandleError(fmt.Errorf("error updating configuration: %v", err))
|
||||||
|
return
|
||||||
}
|
}
|
||||||
mutatingWebhookConfiguration, ok := configuration.(*v1beta1.MutatingWebhookConfiguration)
|
m.configuration.Store(mergeMutatingWebhookConfigurations(configurations))
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("expected type %v, got type %v", reflect.TypeOf(mutatingWebhookConfiguration), reflect.TypeOf(configuration))
|
|
||||||
}
|
|
||||||
return mutatingWebhookConfiguration, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (im *MutatingWebhookConfigurationManager) Run(stopCh <-chan struct{}) {
|
func mergeMutatingWebhookConfigurations(configurations []*v1beta1.MutatingWebhookConfiguration) *v1beta1.MutatingWebhookConfiguration {
|
||||||
im.poller.Run(stopCh)
|
|
||||||
}
|
|
||||||
|
|
||||||
func mergeMutatingWebhookConfigurations(
|
|
||||||
list *v1beta1.MutatingWebhookConfigurationList,
|
|
||||||
) *v1beta1.MutatingWebhookConfiguration {
|
|
||||||
configurations := append([]v1beta1.MutatingWebhookConfiguration{}, list.Items...)
|
|
||||||
var ret v1beta1.MutatingWebhookConfiguration
|
var ret v1beta1.MutatingWebhookConfiguration
|
||||||
// The internal order of webhooks for each configuration is provided by the user
|
// The internal order of webhooks for each configuration is provided by the user
|
||||||
// but configurations themselves can be in any order. As we are going to run these
|
// but configurations themselves can be in any order. As we are going to run these
|
||||||
// webhooks in serial, they are sorted here to have a deterministic order.
|
// webhooks in serial, they are sorted here to have a deterministic order.
|
||||||
sort.Sort(byName(configurations))
|
sort.SliceStable(configurations, MutatingWebhookConfigurationSorter(configurations).ByName)
|
||||||
for _, c := range configurations {
|
for _, c := range configurations {
|
||||||
ret.Webhooks = append(ret.Webhooks, c.Webhooks...)
|
ret.Webhooks = append(ret.Webhooks, c.Webhooks...)
|
||||||
}
|
}
|
||||||
return &ret
|
return &ret
|
||||||
}
|
}
|
||||||
|
|
||||||
// byName sorts MutatingWebhookConfiguration by name. These objects are all in
|
type MutatingWebhookConfigurationSorter []*v1beta1.MutatingWebhookConfiguration
|
||||||
// cluster namespace (aka no namespace) thus they all have unique names.
|
|
||||||
type byName []v1beta1.MutatingWebhookConfiguration
|
|
||||||
|
|
||||||
func (x byName) Len() int { return len(x) }
|
func (a MutatingWebhookConfigurationSorter) ByName(i, j int) bool {
|
||||||
|
return a[i].Name < a[j].Name
|
||||||
func (x byName) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
|
|
||||||
|
|
||||||
func (x byName) Less(i, j int) bool {
|
|
||||||
return x[i].ObjectMeta.Name < x[j].ObjectMeta.Name
|
|
||||||
}
|
}
|
||||||
|
@ -17,24 +17,107 @@ limitations under the License.
|
|||||||
package configuration
|
package configuration
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"k8s.io/api/admissionregistration/v1beta1"
|
"k8s.io/api/admissionregistration/v1beta1"
|
||||||
"k8s.io/apimachinery/pkg/api/errors"
|
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
"k8s.io/apimachinery/pkg/labels"
|
||||||
|
admissionregistrationlisters "k8s.io/client-go/listers/admissionregistration/v1beta1"
|
||||||
|
"k8s.io/client-go/tools/cache"
|
||||||
)
|
)
|
||||||
|
|
||||||
type disabledMutatingWebhookConfigLister struct{}
|
type fakeMutatingWebhookConfigSharedInformer struct {
|
||||||
|
informer *fakeMutatingWebhookConfigInformer
|
||||||
func (l *disabledMutatingWebhookConfigLister) List(options metav1.ListOptions) (*v1beta1.MutatingWebhookConfigurationList, error) {
|
lister *fakeMutatingWebhookConfigLister
|
||||||
return nil, errors.NewNotFound(schema.GroupResource{Group: "admissionregistration", Resource: "MutatingWebhookConfigurations"}, "")
|
|
||||||
}
|
}
|
||||||
func TestMutatingWebhookConfigDisabled(t *testing.T) {
|
|
||||||
manager := NewMutatingWebhookConfigurationManager(&disabledMutatingWebhookConfigLister{})
|
func (f *fakeMutatingWebhookConfigSharedInformer) Informer() cache.SharedIndexInformer {
|
||||||
manager.sync()
|
return f.informer
|
||||||
_, err := manager.Webhooks()
|
}
|
||||||
if err.Error() != ErrDisabled.Error() {
|
func (f *fakeMutatingWebhookConfigSharedInformer) Lister() admissionregistrationlisters.MutatingWebhookConfigurationLister {
|
||||||
t.Errorf("expected %v, got %v", ErrDisabled, err)
|
return f.lister
|
||||||
|
}
|
||||||
|
|
||||||
|
type fakeMutatingWebhookConfigInformer struct {
|
||||||
|
eventHandler cache.ResourceEventHandler
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeMutatingWebhookConfigInformer) AddEventHandler(handler cache.ResourceEventHandler) {
|
||||||
|
fmt.Println("added handler")
|
||||||
|
f.eventHandler = handler
|
||||||
|
}
|
||||||
|
func (f *fakeMutatingWebhookConfigInformer) AddEventHandlerWithResyncPeriod(handler cache.ResourceEventHandler, resyncPeriod time.Duration) {
|
||||||
|
panic("unsupported")
|
||||||
|
}
|
||||||
|
func (f *fakeMutatingWebhookConfigInformer) GetStore() cache.Store {
|
||||||
|
panic("unsupported")
|
||||||
|
}
|
||||||
|
func (f *fakeMutatingWebhookConfigInformer) GetController() cache.Controller {
|
||||||
|
panic("unsupported")
|
||||||
|
}
|
||||||
|
func (f *fakeMutatingWebhookConfigInformer) Run(stopCh <-chan struct{}) {
|
||||||
|
panic("unsupported")
|
||||||
|
}
|
||||||
|
func (f *fakeMutatingWebhookConfigInformer) HasSynced() bool {
|
||||||
|
panic("unsupported")
|
||||||
|
}
|
||||||
|
func (f *fakeMutatingWebhookConfigInformer) LastSyncResourceVersion() string {
|
||||||
|
panic("unsupported")
|
||||||
|
}
|
||||||
|
func (f *fakeMutatingWebhookConfigInformer) AddIndexers(indexers cache.Indexers) error {
|
||||||
|
panic("unsupported")
|
||||||
|
}
|
||||||
|
func (f *fakeMutatingWebhookConfigInformer) GetIndexer() cache.Indexer { panic("unsupported") }
|
||||||
|
|
||||||
|
type fakeMutatingWebhookConfigLister struct {
|
||||||
|
list []*v1beta1.MutatingWebhookConfiguration
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeMutatingWebhookConfigLister) List(selector labels.Selector) (ret []*v1beta1.MutatingWebhookConfiguration, err error) {
|
||||||
|
return f.list, f.err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeMutatingWebhookConfigLister) Get(name string) (*v1beta1.MutatingWebhookConfiguration, error) {
|
||||||
|
panic("unsupported")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetMutatingWebhookConfig(t *testing.T) {
|
||||||
|
informer := &fakeMutatingWebhookConfigSharedInformer{
|
||||||
|
informer: &fakeMutatingWebhookConfigInformer{},
|
||||||
|
lister: &fakeMutatingWebhookConfigLister{},
|
||||||
|
}
|
||||||
|
|
||||||
|
// no configurations
|
||||||
|
informer.lister.list = nil
|
||||||
|
manager := NewMutatingWebhookConfigurationManager(informer)
|
||||||
|
if configurations := manager.Webhooks(); len(configurations.Webhooks) != 0 {
|
||||||
|
t.Errorf("expected empty webhooks, but got %v", configurations.Webhooks)
|
||||||
|
}
|
||||||
|
|
||||||
|
// list err
|
||||||
|
webhookConfiguration := &v1beta1.MutatingWebhookConfiguration{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{Name: "webhook1"},
|
||||||
|
Webhooks: []v1beta1.Webhook{{Name: "webhook1.1"}},
|
||||||
|
}
|
||||||
|
informer.lister.list = []*v1beta1.MutatingWebhookConfiguration{webhookConfiguration.DeepCopy()}
|
||||||
|
informer.lister.err = fmt.Errorf("mutating webhook configuration list error")
|
||||||
|
informer.informer.eventHandler.OnAdd(webhookConfiguration.DeepCopy())
|
||||||
|
if configurations := manager.Webhooks(); len(configurations.Webhooks) != 0 {
|
||||||
|
t.Errorf("expected empty webhooks, but got %v", configurations.Webhooks)
|
||||||
|
}
|
||||||
|
|
||||||
|
// configuration populated
|
||||||
|
informer.lister.err = nil
|
||||||
|
informer.informer.eventHandler.OnAdd(webhookConfiguration.DeepCopy())
|
||||||
|
configurations := manager.Webhooks()
|
||||||
|
if len(configurations.Webhooks) == 0 {
|
||||||
|
t.Errorf("expected non empty webhooks")
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(configurations.Webhooks, webhookConfiguration.Webhooks) {
|
||||||
|
t.Errorf("Expected\n%#v\ngot\n%#v", webhookConfiguration.Webhooks, configurations.Webhooks)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,67 +18,69 @@ package configuration
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"sort"
|
||||||
|
"sync/atomic"
|
||||||
"github.com/golang/glog"
|
|
||||||
|
|
||||||
"k8s.io/api/admissionregistration/v1beta1"
|
"k8s.io/api/admissionregistration/v1beta1"
|
||||||
"k8s.io/apimachinery/pkg/api/errors"
|
"k8s.io/apimachinery/pkg/labels"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
admissionregistrationinformers "k8s.io/client-go/informers/admissionregistration/v1beta1"
|
||||||
|
admissionregistrationlisters "k8s.io/client-go/listers/admissionregistration/v1beta1"
|
||||||
|
"k8s.io/client-go/tools/cache"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ValidatingWebhookConfigurationLister interface {
|
|
||||||
List(opts metav1.ListOptions) (*v1beta1.ValidatingWebhookConfigurationList, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ValidatingWebhookConfigurationManager collects the validating webhook objects so that they can be called.
|
// ValidatingWebhookConfigurationManager collects the validating webhook objects so that they can be called.
|
||||||
type ValidatingWebhookConfigurationManager struct {
|
type ValidatingWebhookConfigurationManager struct {
|
||||||
*poller
|
configuration *atomic.Value
|
||||||
|
lister admissionregistrationlisters.ValidatingWebhookConfigurationLister
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewValidatingWebhookConfigurationManager(c ValidatingWebhookConfigurationLister) *ValidatingWebhookConfigurationManager {
|
func NewValidatingWebhookConfigurationManager(informer admissionregistrationinformers.ValidatingWebhookConfigurationInformer) *ValidatingWebhookConfigurationManager {
|
||||||
getFn := func() (runtime.Object, error) {
|
manager := &ValidatingWebhookConfigurationManager{
|
||||||
list, err := c.List(metav1.ListOptions{})
|
configuration: &atomic.Value{},
|
||||||
if err != nil {
|
lister: informer.Lister(),
|
||||||
if errors.IsNotFound(err) || errors.IsForbidden(err) {
|
|
||||||
glog.V(5).Infof("ValidatingWebhookConfiguration are disabled due to an error: %v", err)
|
|
||||||
return nil, ErrDisabled
|
|
||||||
}
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return mergeValidatingWebhookConfigurations(list), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return &ValidatingWebhookConfigurationManager{
|
// Start with an empty list
|
||||||
newPoller(getFn),
|
manager.configuration.Store(&v1beta1.ValidatingWebhookConfiguration{})
|
||||||
}
|
|
||||||
|
// On any change, rebuild the config
|
||||||
|
informer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
|
||||||
|
AddFunc: func(_ interface{}) { manager.updateConfiguration() },
|
||||||
|
UpdateFunc: func(_, _ interface{}) { manager.updateConfiguration() },
|
||||||
|
DeleteFunc: func(_ interface{}) { manager.updateConfiguration() },
|
||||||
|
})
|
||||||
|
|
||||||
|
return manager
|
||||||
}
|
}
|
||||||
|
|
||||||
// Webhooks returns the merged ValidatingWebhookConfiguration.
|
// Webhooks returns the merged ValidatingWebhookConfiguration.
|
||||||
func (im *ValidatingWebhookConfigurationManager) Webhooks() (*v1beta1.ValidatingWebhookConfiguration, error) {
|
func (v *ValidatingWebhookConfigurationManager) Webhooks() *v1beta1.ValidatingWebhookConfiguration {
|
||||||
configuration, err := im.poller.configuration()
|
return v.configuration.Load().(*v1beta1.ValidatingWebhookConfiguration)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
validatingWebhookConfiguration, ok := configuration.(*v1beta1.ValidatingWebhookConfiguration)
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("expected type %v, got type %v", reflect.TypeOf(validatingWebhookConfiguration), reflect.TypeOf(configuration))
|
|
||||||
}
|
|
||||||
return validatingWebhookConfiguration, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (im *ValidatingWebhookConfigurationManager) Run(stopCh <-chan struct{}) {
|
func (v *ValidatingWebhookConfigurationManager) updateConfiguration() {
|
||||||
im.poller.Run(stopCh)
|
configurations, err := v.lister.List(labels.Everything())
|
||||||
|
if err != nil {
|
||||||
|
utilruntime.HandleError(fmt.Errorf("error updating configuration: %v", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
v.configuration.Store(mergeValidatingWebhookConfigurations(configurations))
|
||||||
}
|
}
|
||||||
|
|
||||||
func mergeValidatingWebhookConfigurations(
|
func mergeValidatingWebhookConfigurations(
|
||||||
list *v1beta1.ValidatingWebhookConfigurationList,
|
configurations []*v1beta1.ValidatingWebhookConfiguration,
|
||||||
) *v1beta1.ValidatingWebhookConfiguration {
|
) *v1beta1.ValidatingWebhookConfiguration {
|
||||||
configurations := list.Items
|
sort.SliceStable(configurations, ValidatingWebhookConfigurationSorter(configurations).ByName)
|
||||||
var ret v1beta1.ValidatingWebhookConfiguration
|
var ret v1beta1.ValidatingWebhookConfiguration
|
||||||
for _, c := range configurations {
|
for _, c := range configurations {
|
||||||
ret.Webhooks = append(ret.Webhooks, c.Webhooks...)
|
ret.Webhooks = append(ret.Webhooks, c.Webhooks...)
|
||||||
}
|
}
|
||||||
return &ret
|
return &ret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ValidatingWebhookConfigurationSorter []*v1beta1.ValidatingWebhookConfiguration
|
||||||
|
|
||||||
|
func (a ValidatingWebhookConfigurationSorter) ByName(i, j int) bool {
|
||||||
|
return a[i].Name < a[j].Name
|
||||||
|
}
|
||||||
|
@ -17,24 +17,107 @@ limitations under the License.
|
|||||||
package configuration
|
package configuration
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"k8s.io/api/admissionregistration/v1beta1"
|
"k8s.io/api/admissionregistration/v1beta1"
|
||||||
"k8s.io/apimachinery/pkg/api/errors"
|
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
"k8s.io/apimachinery/pkg/labels"
|
||||||
|
admissionregistrationlisters "k8s.io/client-go/listers/admissionregistration/v1beta1"
|
||||||
|
"k8s.io/client-go/tools/cache"
|
||||||
)
|
)
|
||||||
|
|
||||||
type disabledValidatingWebhookConfigLister struct{}
|
type fakeValidatingWebhookConfigSharedInformer struct {
|
||||||
|
informer *fakeValidatingWebhookConfigInformer
|
||||||
func (l *disabledValidatingWebhookConfigLister) List(options metav1.ListOptions) (*v1beta1.ValidatingWebhookConfigurationList, error) {
|
lister *fakeValidatingWebhookConfigLister
|
||||||
return nil, errors.NewNotFound(schema.GroupResource{Group: "admissionregistration", Resource: "ValidatingWebhookConfigurations"}, "")
|
|
||||||
}
|
}
|
||||||
func TestWebhookConfigDisabled(t *testing.T) {
|
|
||||||
manager := NewValidatingWebhookConfigurationManager(&disabledValidatingWebhookConfigLister{})
|
func (f *fakeValidatingWebhookConfigSharedInformer) Informer() cache.SharedIndexInformer {
|
||||||
manager.sync()
|
return f.informer
|
||||||
_, err := manager.Webhooks()
|
}
|
||||||
if err.Error() != ErrDisabled.Error() {
|
func (f *fakeValidatingWebhookConfigSharedInformer) Lister() admissionregistrationlisters.ValidatingWebhookConfigurationLister {
|
||||||
t.Errorf("expected %v, got %v", ErrDisabled, err)
|
return f.lister
|
||||||
|
}
|
||||||
|
|
||||||
|
type fakeValidatingWebhookConfigInformer struct {
|
||||||
|
eventHandler cache.ResourceEventHandler
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeValidatingWebhookConfigInformer) AddEventHandler(handler cache.ResourceEventHandler) {
|
||||||
|
fmt.Println("added handler")
|
||||||
|
f.eventHandler = handler
|
||||||
|
}
|
||||||
|
func (f *fakeValidatingWebhookConfigInformer) AddEventHandlerWithResyncPeriod(handler cache.ResourceEventHandler, resyncPeriod time.Duration) {
|
||||||
|
panic("unsupported")
|
||||||
|
}
|
||||||
|
func (f *fakeValidatingWebhookConfigInformer) GetStore() cache.Store {
|
||||||
|
panic("unsupported")
|
||||||
|
}
|
||||||
|
func (f *fakeValidatingWebhookConfigInformer) GetController() cache.Controller {
|
||||||
|
panic("unsupported")
|
||||||
|
}
|
||||||
|
func (f *fakeValidatingWebhookConfigInformer) Run(stopCh <-chan struct{}) {
|
||||||
|
panic("unsupported")
|
||||||
|
}
|
||||||
|
func (f *fakeValidatingWebhookConfigInformer) HasSynced() bool {
|
||||||
|
panic("unsupported")
|
||||||
|
}
|
||||||
|
func (f *fakeValidatingWebhookConfigInformer) LastSyncResourceVersion() string {
|
||||||
|
panic("unsupported")
|
||||||
|
}
|
||||||
|
func (f *fakeValidatingWebhookConfigInformer) AddIndexers(indexers cache.Indexers) error {
|
||||||
|
panic("unsupported")
|
||||||
|
}
|
||||||
|
func (f *fakeValidatingWebhookConfigInformer) GetIndexer() cache.Indexer { panic("unsupported") }
|
||||||
|
|
||||||
|
type fakeValidatingWebhookConfigLister struct {
|
||||||
|
list []*v1beta1.ValidatingWebhookConfiguration
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeValidatingWebhookConfigLister) List(selector labels.Selector) (ret []*v1beta1.ValidatingWebhookConfiguration, err error) {
|
||||||
|
return f.list, f.err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeValidatingWebhookConfigLister) Get(name string) (*v1beta1.ValidatingWebhookConfiguration, error) {
|
||||||
|
panic("unsupported")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGettValidatingWebhookConfig(t *testing.T) {
|
||||||
|
informer := &fakeValidatingWebhookConfigSharedInformer{
|
||||||
|
informer: &fakeValidatingWebhookConfigInformer{},
|
||||||
|
lister: &fakeValidatingWebhookConfigLister{},
|
||||||
|
}
|
||||||
|
|
||||||
|
// no configurations
|
||||||
|
informer.lister.list = nil
|
||||||
|
manager := NewValidatingWebhookConfigurationManager(informer)
|
||||||
|
if configurations := manager.Webhooks(); len(configurations.Webhooks) != 0 {
|
||||||
|
t.Errorf("expected empty webhooks, but got %v", configurations.Webhooks)
|
||||||
|
}
|
||||||
|
|
||||||
|
// list error
|
||||||
|
webhookConfiguration := &v1beta1.ValidatingWebhookConfiguration{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{Name: "webhook1"},
|
||||||
|
Webhooks: []v1beta1.Webhook{{Name: "webhook1.1"}},
|
||||||
|
}
|
||||||
|
informer.lister.list = []*v1beta1.ValidatingWebhookConfiguration{webhookConfiguration.DeepCopy()}
|
||||||
|
informer.lister.err = fmt.Errorf("validating webhook configuration list error")
|
||||||
|
informer.informer.eventHandler.OnAdd(webhookConfiguration.DeepCopy())
|
||||||
|
if configurations := manager.Webhooks(); len(configurations.Webhooks) != 0 {
|
||||||
|
t.Errorf("expected empty webhooks, but got %v", configurations.Webhooks)
|
||||||
|
}
|
||||||
|
|
||||||
|
// configuration populated
|
||||||
|
informer.lister.err = nil
|
||||||
|
informer.informer.eventHandler.OnAdd(webhookConfiguration.DeepCopy())
|
||||||
|
configurations := manager.Webhooks()
|
||||||
|
if len(configurations.Webhooks) == 0 {
|
||||||
|
t.Errorf("expected non empty webhooks")
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(configurations.Webhooks, webhookConfiguration.Webhooks) {
|
||||||
|
t.Errorf("Expected\n%#v\ngot\n%#v", webhookConfiguration.Webhooks, configurations.Webhooks)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,12 +14,10 @@ go_library(
|
|||||||
"//vendor/k8s.io/api/admission/v1beta1:go_default_library",
|
"//vendor/k8s.io/api/admission/v1beta1:go_default_library",
|
||||||
"//vendor/k8s.io/api/admissionregistration/v1beta1:go_default_library",
|
"//vendor/k8s.io/api/admissionregistration/v1beta1:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
|
||||||
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/runtime/serializer:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/runtime/serializer:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/runtime/serializer/json:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/runtime/serializer/json:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/util/runtime:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/util/runtime:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/util/wait:go_default_library",
|
|
||||||
"//vendor/k8s.io/apiserver/pkg/admission:go_default_library",
|
"//vendor/k8s.io/apiserver/pkg/admission:go_default_library",
|
||||||
"//vendor/k8s.io/apiserver/pkg/admission/configuration:go_default_library",
|
"//vendor/k8s.io/apiserver/pkg/admission/configuration:go_default_library",
|
||||||
"//vendor/k8s.io/apiserver/pkg/admission/initializer:go_default_library",
|
"//vendor/k8s.io/apiserver/pkg/admission/initializer:go_default_library",
|
||||||
|
@ -30,12 +30,10 @@ import (
|
|||||||
admissionv1beta1 "k8s.io/api/admission/v1beta1"
|
admissionv1beta1 "k8s.io/api/admission/v1beta1"
|
||||||
"k8s.io/api/admissionregistration/v1beta1"
|
"k8s.io/api/admissionregistration/v1beta1"
|
||||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/runtime/serializer"
|
"k8s.io/apimachinery/pkg/runtime/serializer"
|
||||||
"k8s.io/apimachinery/pkg/runtime/serializer/json"
|
"k8s.io/apimachinery/pkg/runtime/serializer/json"
|
||||||
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
||||||
"k8s.io/apimachinery/pkg/util/wait"
|
|
||||||
"k8s.io/apiserver/pkg/admission"
|
"k8s.io/apiserver/pkg/admission"
|
||||||
"k8s.io/apiserver/pkg/admission/configuration"
|
"k8s.io/apiserver/pkg/admission/configuration"
|
||||||
genericadmissioninit "k8s.io/apiserver/pkg/admission/initializer"
|
genericadmissioninit "k8s.io/apiserver/pkg/admission/initializer"
|
||||||
@ -69,8 +67,7 @@ func Register(plugins *admission.Plugins) {
|
|||||||
|
|
||||||
// WebhookSource can list dynamic webhook plugins.
|
// WebhookSource can list dynamic webhook plugins.
|
||||||
type WebhookSource interface {
|
type WebhookSource interface {
|
||||||
Run(stopCh <-chan struct{})
|
Webhooks() *v1beta1.MutatingWebhookConfiguration
|
||||||
Webhooks() (*v1beta1.MutatingWebhookConfiguration, error)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewMutatingWebhook returns a generic admission webhook plugin.
|
// NewMutatingWebhook returns a generic admission webhook plugin.
|
||||||
@ -146,14 +143,17 @@ func (a *MutatingWebhook) SetScheme(scheme *runtime.Scheme) {
|
|||||||
// WantsExternalKubeClientSet defines a function which sets external ClientSet for admission plugins that need it
|
// WantsExternalKubeClientSet defines a function which sets external ClientSet for admission plugins that need it
|
||||||
func (a *MutatingWebhook) SetExternalKubeClientSet(client clientset.Interface) {
|
func (a *MutatingWebhook) SetExternalKubeClientSet(client clientset.Interface) {
|
||||||
a.namespaceMatcher.Client = client
|
a.namespaceMatcher.Client = client
|
||||||
a.hookSource = configuration.NewMutatingWebhookConfigurationManager(client.AdmissionregistrationV1beta1().MutatingWebhookConfigurations())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetExternalKubeInformerFactory implements the WantsExternalKubeInformerFactory interface.
|
// SetExternalKubeInformerFactory implements the WantsExternalKubeInformerFactory interface.
|
||||||
func (a *MutatingWebhook) SetExternalKubeInformerFactory(f informers.SharedInformerFactory) {
|
func (a *MutatingWebhook) SetExternalKubeInformerFactory(f informers.SharedInformerFactory) {
|
||||||
namespaceInformer := f.Core().V1().Namespaces()
|
namespaceInformer := f.Core().V1().Namespaces()
|
||||||
a.namespaceMatcher.NamespaceLister = namespaceInformer.Lister()
|
a.namespaceMatcher.NamespaceLister = namespaceInformer.Lister()
|
||||||
a.SetReadyFunc(namespaceInformer.Informer().HasSynced)
|
mutatingWebhookConfigurationsInformer := f.Admissionregistration().V1beta1().MutatingWebhookConfigurations()
|
||||||
|
a.hookSource = configuration.NewMutatingWebhookConfigurationManager(mutatingWebhookConfigurationsInformer)
|
||||||
|
a.SetReadyFunc(func() bool {
|
||||||
|
return namespaceInformer.Informer().HasSynced() && mutatingWebhookConfigurationsInformer.Informer().HasSynced()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// ValidateInitialization implements the InitializationValidator interface.
|
// ValidateInitialization implements the InitializationValidator interface.
|
||||||
@ -176,35 +176,21 @@ func (a *MutatingWebhook) ValidateInitialization() error {
|
|||||||
if a.defaulter == nil {
|
if a.defaulter == nil {
|
||||||
return fmt.Errorf("MutatingWebhook.defaulter is not properly setup")
|
return fmt.Errorf("MutatingWebhook.defaulter is not properly setup")
|
||||||
}
|
}
|
||||||
go a.hookSource.Run(wait.NeverStop)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *MutatingWebhook) loadConfiguration(attr admission.Attributes) (*v1beta1.MutatingWebhookConfiguration, error) {
|
func (a *MutatingWebhook) loadConfiguration(attr admission.Attributes) *v1beta1.MutatingWebhookConfiguration {
|
||||||
hookConfig, err := a.hookSource.Webhooks()
|
hookConfig := a.hookSource.Webhooks()
|
||||||
// if Webhook configuration is disabled, fail open
|
return hookConfig
|
||||||
if err == configuration.ErrDisabled {
|
|
||||||
return &v1beta1.MutatingWebhookConfiguration{}, nil
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
e := apierrors.NewServerTimeout(attr.GetResource().GroupResource(), string(attr.GetOperation()), 1)
|
|
||||||
e.ErrStatus.Message = fmt.Sprintf("Unable to refresh the Webhook configuration: %v", err)
|
|
||||||
e.ErrStatus.Reason = "LoadingConfiguration"
|
|
||||||
e.ErrStatus.Details.Causes = append(e.ErrStatus.Details.Causes, metav1.StatusCause{
|
|
||||||
Type: "MutatingWebhookConfigurationFailure",
|
|
||||||
Message: "An error has occurred while refreshing the MutatingWebhook configuration, no resources can be created/updated/deleted/connected until a refresh succeeds.",
|
|
||||||
})
|
|
||||||
return nil, e
|
|
||||||
}
|
|
||||||
return hookConfig, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Admit makes an admission decision based on the request attributes.
|
// Admit makes an admission decision based on the request attributes.
|
||||||
func (a *MutatingWebhook) Admit(attr admission.Attributes) error {
|
func (a *MutatingWebhook) Admit(attr admission.Attributes) error {
|
||||||
hookConfig, err := a.loadConfiguration(attr)
|
if !a.WaitForReady() {
|
||||||
if err != nil {
|
return admission.NewForbidden(attr, fmt.Errorf("not yet ready to handle request"))
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
hookConfig := a.loadConfiguration(attr)
|
||||||
hooks := hookConfig.Webhooks
|
hooks := hookConfig.Webhooks
|
||||||
ctx := context.TODO()
|
ctx := context.TODO()
|
||||||
|
|
||||||
|
@ -47,16 +47,16 @@ type fakeHookSource struct {
|
|||||||
err error
|
err error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *fakeHookSource) Webhooks() (*registrationv1beta1.MutatingWebhookConfiguration, error) {
|
func (f *fakeHookSource) Webhooks() *registrationv1beta1.MutatingWebhookConfiguration {
|
||||||
if f.err != nil {
|
if f.err != nil {
|
||||||
return nil, f.err
|
return nil
|
||||||
}
|
}
|
||||||
for i, h := range f.hooks {
|
for i, h := range f.hooks {
|
||||||
if h.NamespaceSelector == nil {
|
if h.NamespaceSelector == nil {
|
||||||
f.hooks[i].NamespaceSelector = &metav1.LabelSelector{}
|
f.hooks[i].NamespaceSelector = &metav1.LabelSelector{}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ®istrationv1beta1.MutatingWebhookConfiguration{Webhooks: f.hooks}, nil
|
return ®istrationv1beta1.MutatingWebhookConfiguration{Webhooks: f.hooks}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *fakeHookSource) Run(stopCh <-chan struct{}) {}
|
func (f *fakeHookSource) Run(stopCh <-chan struct{}) {}
|
||||||
|
@ -13,11 +13,9 @@ go_library(
|
|||||||
"//vendor/k8s.io/api/admission/v1beta1:go_default_library",
|
"//vendor/k8s.io/api/admission/v1beta1:go_default_library",
|
||||||
"//vendor/k8s.io/api/admissionregistration/v1beta1:go_default_library",
|
"//vendor/k8s.io/api/admissionregistration/v1beta1:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
|
||||||
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/runtime/serializer:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/runtime/serializer:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/util/runtime:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/util/runtime:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/util/wait:go_default_library",
|
|
||||||
"//vendor/k8s.io/apiserver/pkg/admission:go_default_library",
|
"//vendor/k8s.io/apiserver/pkg/admission:go_default_library",
|
||||||
"//vendor/k8s.io/apiserver/pkg/admission/configuration:go_default_library",
|
"//vendor/k8s.io/apiserver/pkg/admission/configuration:go_default_library",
|
||||||
"//vendor/k8s.io/apiserver/pkg/admission/initializer:go_default_library",
|
"//vendor/k8s.io/apiserver/pkg/admission/initializer:go_default_library",
|
||||||
|
@ -30,11 +30,9 @@ import (
|
|||||||
admissionv1beta1 "k8s.io/api/admission/v1beta1"
|
admissionv1beta1 "k8s.io/api/admission/v1beta1"
|
||||||
"k8s.io/api/admissionregistration/v1beta1"
|
"k8s.io/api/admissionregistration/v1beta1"
|
||||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/runtime/serializer"
|
"k8s.io/apimachinery/pkg/runtime/serializer"
|
||||||
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
||||||
"k8s.io/apimachinery/pkg/util/wait"
|
|
||||||
"k8s.io/apiserver/pkg/admission"
|
"k8s.io/apiserver/pkg/admission"
|
||||||
"k8s.io/apiserver/pkg/admission/configuration"
|
"k8s.io/apiserver/pkg/admission/configuration"
|
||||||
genericadmissioninit "k8s.io/apiserver/pkg/admission/initializer"
|
genericadmissioninit "k8s.io/apiserver/pkg/admission/initializer"
|
||||||
@ -68,8 +66,7 @@ func Register(plugins *admission.Plugins) {
|
|||||||
|
|
||||||
// WebhookSource can list dynamic webhook plugins.
|
// WebhookSource can list dynamic webhook plugins.
|
||||||
type WebhookSource interface {
|
type WebhookSource interface {
|
||||||
Run(stopCh <-chan struct{})
|
Webhooks() *v1beta1.ValidatingWebhookConfiguration
|
||||||
Webhooks() (*v1beta1.ValidatingWebhookConfiguration, error)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewValidatingAdmissionWebhook returns a generic admission webhook plugin.
|
// NewValidatingAdmissionWebhook returns a generic admission webhook plugin.
|
||||||
@ -141,20 +138,23 @@ func (a *ValidatingAdmissionWebhook) SetScheme(scheme *runtime.Scheme) {
|
|||||||
// WantsExternalKubeClientSet defines a function which sets external ClientSet for admission plugins that need it
|
// WantsExternalKubeClientSet defines a function which sets external ClientSet for admission plugins that need it
|
||||||
func (a *ValidatingAdmissionWebhook) SetExternalKubeClientSet(client clientset.Interface) {
|
func (a *ValidatingAdmissionWebhook) SetExternalKubeClientSet(client clientset.Interface) {
|
||||||
a.namespaceMatcher.Client = client
|
a.namespaceMatcher.Client = client
|
||||||
a.hookSource = configuration.NewValidatingWebhookConfigurationManager(client.AdmissionregistrationV1beta1().ValidatingWebhookConfigurations())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetExternalKubeInformerFactory implements the WantsExternalKubeInformerFactory interface.
|
// SetExternalKubeInformerFactory implements the WantsExternalKubeInformerFactory interface.
|
||||||
func (a *ValidatingAdmissionWebhook) SetExternalKubeInformerFactory(f informers.SharedInformerFactory) {
|
func (a *ValidatingAdmissionWebhook) SetExternalKubeInformerFactory(f informers.SharedInformerFactory) {
|
||||||
namespaceInformer := f.Core().V1().Namespaces()
|
namespaceInformer := f.Core().V1().Namespaces()
|
||||||
a.namespaceMatcher.NamespaceLister = namespaceInformer.Lister()
|
a.namespaceMatcher.NamespaceLister = namespaceInformer.Lister()
|
||||||
a.SetReadyFunc(namespaceInformer.Informer().HasSynced)
|
validatingWebhookConfigurationsInformer := f.Admissionregistration().V1beta1().ValidatingWebhookConfigurations()
|
||||||
|
a.hookSource = configuration.NewValidatingWebhookConfigurationManager(validatingWebhookConfigurationsInformer)
|
||||||
|
a.SetReadyFunc(func() bool {
|
||||||
|
return namespaceInformer.Informer().HasSynced() && validatingWebhookConfigurationsInformer.Informer().HasSynced()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// ValidateInitialization implements the InitializationValidator interface.
|
// ValidateInitialization implements the InitializationValidator interface.
|
||||||
func (a *ValidatingAdmissionWebhook) ValidateInitialization() error {
|
func (a *ValidatingAdmissionWebhook) ValidateInitialization() error {
|
||||||
if a.hookSource == nil {
|
if a.hookSource == nil {
|
||||||
return fmt.Errorf("ValidatingAdmissionWebhook admission plugin requires a Kubernetes client to be provided")
|
return fmt.Errorf("ValidatingAdmissionWebhook admission plugin requires a Kubernetes informer to be provided")
|
||||||
}
|
}
|
||||||
if err := a.namespaceMatcher.Validate(); err != nil {
|
if err := a.namespaceMatcher.Validate(); err != nil {
|
||||||
return fmt.Errorf("ValidatingAdmissionWebhook.namespaceMatcher is not properly setup: %v", err)
|
return fmt.Errorf("ValidatingAdmissionWebhook.namespaceMatcher is not properly setup: %v", err)
|
||||||
@ -165,35 +165,19 @@ func (a *ValidatingAdmissionWebhook) ValidateInitialization() error {
|
|||||||
if err := a.convertor.Validate(); err != nil {
|
if err := a.convertor.Validate(); err != nil {
|
||||||
return fmt.Errorf("ValidatingAdmissionWebhook.convertor is not properly setup: %v", err)
|
return fmt.Errorf("ValidatingAdmissionWebhook.convertor is not properly setup: %v", err)
|
||||||
}
|
}
|
||||||
go a.hookSource.Run(wait.NeverStop)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *ValidatingAdmissionWebhook) loadConfiguration(attr admission.Attributes) (*v1beta1.ValidatingWebhookConfiguration, error) {
|
func (a *ValidatingAdmissionWebhook) loadConfiguration(attr admission.Attributes) *v1beta1.ValidatingWebhookConfiguration {
|
||||||
hookConfig, err := a.hookSource.Webhooks()
|
return a.hookSource.Webhooks()
|
||||||
// if Webhook configuration is disabled, fail open
|
|
||||||
if err == configuration.ErrDisabled {
|
|
||||||
return &v1beta1.ValidatingWebhookConfiguration{}, nil
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
e := apierrors.NewServerTimeout(attr.GetResource().GroupResource(), string(attr.GetOperation()), 1)
|
|
||||||
e.ErrStatus.Message = fmt.Sprintf("Unable to refresh the Webhook configuration: %v", err)
|
|
||||||
e.ErrStatus.Reason = "LoadingConfiguration"
|
|
||||||
e.ErrStatus.Details.Causes = append(e.ErrStatus.Details.Causes, metav1.StatusCause{
|
|
||||||
Type: "ValidatingWebhookConfigurationFailure",
|
|
||||||
Message: "An error has occurred while refreshing the ValidatingWebhook configuration, no resources can be created/updated/deleted/connected until a refresh succeeds.",
|
|
||||||
})
|
|
||||||
return nil, e
|
|
||||||
}
|
|
||||||
return hookConfig, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate makes an admission decision based on the request attributes.
|
// Validate makes an admission decision based on the request attributes.
|
||||||
func (a *ValidatingAdmissionWebhook) Validate(attr admission.Attributes) error {
|
func (a *ValidatingAdmissionWebhook) Validate(attr admission.Attributes) error {
|
||||||
hookConfig, err := a.loadConfiguration(attr)
|
if !a.WaitForReady() {
|
||||||
if err != nil {
|
return admission.NewForbidden(attr, fmt.Errorf("not yet ready to handle request"))
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
hookConfig := a.loadConfiguration(attr)
|
||||||
hooks := hookConfig.Webhooks
|
hooks := hookConfig.Webhooks
|
||||||
ctx := context.TODO()
|
ctx := context.TODO()
|
||||||
|
|
||||||
|
@ -47,16 +47,16 @@ type fakeHookSource struct {
|
|||||||
err error
|
err error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *fakeHookSource) Webhooks() (*registrationv1beta1.ValidatingWebhookConfiguration, error) {
|
func (f *fakeHookSource) Webhooks() *registrationv1beta1.ValidatingWebhookConfiguration {
|
||||||
if f.err != nil {
|
if f.err != nil {
|
||||||
return nil, f.err
|
return nil
|
||||||
}
|
}
|
||||||
for i, h := range f.hooks {
|
for i, h := range f.hooks {
|
||||||
if h.NamespaceSelector == nil {
|
if h.NamespaceSelector == nil {
|
||||||
f.hooks[i].NamespaceSelector = &metav1.LabelSelector{}
|
f.hooks[i].NamespaceSelector = &metav1.LabelSelector{}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ®istrationv1beta1.ValidatingWebhookConfiguration{Webhooks: f.hooks}, nil
|
return ®istrationv1beta1.ValidatingWebhookConfiguration{Webhooks: f.hooks}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *fakeHookSource) Run(stopCh <-chan struct{}) {}
|
func (f *fakeHookSource) Run(stopCh <-chan struct{}) {}
|
||||||
|
Loading…
Reference in New Issue
Block a user