2019-01-12 04:58:27 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 The Kubernetes Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
v1 "k8s.io/api/core/v1"
|
2019-12-12 01:27:03 +00:00
|
|
|
discovery "k8s.io/api/discovery/v1beta1"
|
2019-01-12 04:58:27 +00:00
|
|
|
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
|
|
|
coreinformers "k8s.io/client-go/informers/core/v1"
|
2019-12-12 01:27:03 +00:00
|
|
|
discoveryinformers "k8s.io/client-go/informers/discovery/v1beta1"
|
2019-01-12 04:58:27 +00:00
|
|
|
"k8s.io/client-go/tools/cache"
|
2020-08-10 17:43:49 +00:00
|
|
|
"k8s.io/klog/v2"
|
2019-01-12 04:58:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ServiceHandler is an abstract interface of objects which receive
|
|
|
|
// notifications about service object changes.
|
|
|
|
type ServiceHandler interface {
|
|
|
|
// OnServiceAdd is called whenever creation of new service object
|
|
|
|
// is observed.
|
|
|
|
OnServiceAdd(service *v1.Service)
|
|
|
|
// OnServiceUpdate is called whenever modification of an existing
|
|
|
|
// service object is observed.
|
|
|
|
OnServiceUpdate(oldService, service *v1.Service)
|
|
|
|
// OnServiceDelete is called whenever deletion of an existing service
|
|
|
|
// object is observed.
|
|
|
|
OnServiceDelete(service *v1.Service)
|
2019-09-27 21:51:53 +00:00
|
|
|
// OnServiceSynced is called once all the initial event handlers were
|
2019-01-12 04:58:27 +00:00
|
|
|
// called and the state is fully propagated to local cache.
|
|
|
|
OnServiceSynced()
|
|
|
|
}
|
|
|
|
|
|
|
|
// EndpointsHandler is an abstract interface of objects which receive
|
|
|
|
// notifications about endpoints object changes.
|
|
|
|
type EndpointsHandler interface {
|
|
|
|
// OnEndpointsAdd is called whenever creation of new endpoints object
|
|
|
|
// is observed.
|
|
|
|
OnEndpointsAdd(endpoints *v1.Endpoints)
|
|
|
|
// OnEndpointsUpdate is called whenever modification of an existing
|
|
|
|
// endpoints object is observed.
|
|
|
|
OnEndpointsUpdate(oldEndpoints, endpoints *v1.Endpoints)
|
2019-09-27 21:51:53 +00:00
|
|
|
// OnEndpointsDelete is called whenever deletion of an existing endpoints
|
2019-01-12 04:58:27 +00:00
|
|
|
// object is observed.
|
|
|
|
OnEndpointsDelete(endpoints *v1.Endpoints)
|
|
|
|
// OnEndpointsSynced is called once all the initial event handlers were
|
|
|
|
// called and the state is fully propagated to local cache.
|
|
|
|
OnEndpointsSynced()
|
|
|
|
}
|
|
|
|
|
2019-09-27 21:51:53 +00:00
|
|
|
// EndpointSliceHandler is an abstract interface of objects which receive
|
|
|
|
// notifications about endpoint slice object changes.
|
|
|
|
type EndpointSliceHandler interface {
|
|
|
|
// OnEndpointSliceAdd is called whenever creation of new endpoint slice
|
|
|
|
// object is observed.
|
|
|
|
OnEndpointSliceAdd(endpointSlice *discovery.EndpointSlice)
|
|
|
|
// OnEndpointSliceUpdate is called whenever modification of an existing
|
|
|
|
// endpoint slice object is observed.
|
|
|
|
OnEndpointSliceUpdate(oldEndpointSlice, newEndpointSlice *discovery.EndpointSlice)
|
|
|
|
// OnEndpointSliceDelete is called whenever deletion of an existing
|
|
|
|
// endpoint slice object is observed.
|
|
|
|
OnEndpointSliceDelete(endpointSlice *discovery.EndpointSlice)
|
|
|
|
// OnEndpointSlicesSynced is called once all the initial event handlers were
|
|
|
|
// called and the state is fully propagated to local cache.
|
|
|
|
OnEndpointSlicesSynced()
|
|
|
|
}
|
|
|
|
|
|
|
|
// NoopEndpointSliceHandler is a noop handler for proxiers that have not yet
|
|
|
|
// implemented a full EndpointSliceHandler.
|
|
|
|
type NoopEndpointSliceHandler struct{}
|
|
|
|
|
|
|
|
// OnEndpointSliceAdd is a noop handler for EndpointSlice creates.
|
|
|
|
func (*NoopEndpointSliceHandler) OnEndpointSliceAdd(endpointSlice *discovery.EndpointSlice) {}
|
|
|
|
|
|
|
|
// OnEndpointSliceUpdate is a noop handler for EndpointSlice updates.
|
|
|
|
func (*NoopEndpointSliceHandler) OnEndpointSliceUpdate(oldEndpointSlice, newEndpointSlice *discovery.EndpointSlice) {
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnEndpointSliceDelete is a noop handler for EndpointSlice deletes.
|
|
|
|
func (*NoopEndpointSliceHandler) OnEndpointSliceDelete(endpointSlice *discovery.EndpointSlice) {}
|
|
|
|
|
|
|
|
// OnEndpointSlicesSynced is a noop handler for EndpointSlice syncs.
|
|
|
|
func (*NoopEndpointSliceHandler) OnEndpointSlicesSynced() {}
|
|
|
|
|
2020-03-26 21:07:15 +00:00
|
|
|
var _ EndpointSliceHandler = &NoopEndpointSliceHandler{}
|
|
|
|
|
2019-01-12 04:58:27 +00:00
|
|
|
// EndpointsConfig tracks a set of endpoints configurations.
|
|
|
|
type EndpointsConfig struct {
|
|
|
|
listerSynced cache.InformerSynced
|
|
|
|
eventHandlers []EndpointsHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewEndpointsConfig creates a new EndpointsConfig.
|
|
|
|
func NewEndpointsConfig(endpointsInformer coreinformers.EndpointsInformer, resyncPeriod time.Duration) *EndpointsConfig {
|
|
|
|
result := &EndpointsConfig{
|
|
|
|
listerSynced: endpointsInformer.Informer().HasSynced,
|
|
|
|
}
|
|
|
|
|
|
|
|
endpointsInformer.Informer().AddEventHandlerWithResyncPeriod(
|
|
|
|
cache.ResourceEventHandlerFuncs{
|
|
|
|
AddFunc: result.handleAddEndpoints,
|
|
|
|
UpdateFunc: result.handleUpdateEndpoints,
|
|
|
|
DeleteFunc: result.handleDeleteEndpoints,
|
|
|
|
},
|
|
|
|
resyncPeriod,
|
|
|
|
)
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterEventHandler registers a handler which is called on every endpoints change.
|
|
|
|
func (c *EndpointsConfig) RegisterEventHandler(handler EndpointsHandler) {
|
|
|
|
c.eventHandlers = append(c.eventHandlers, handler)
|
|
|
|
}
|
|
|
|
|
2019-08-30 18:33:25 +00:00
|
|
|
// Run waits for cache synced and invokes handlers after syncing.
|
2019-01-12 04:58:27 +00:00
|
|
|
func (c *EndpointsConfig) Run(stopCh <-chan struct{}) {
|
|
|
|
klog.Info("Starting endpoints config controller")
|
|
|
|
|
2019-09-27 21:51:53 +00:00
|
|
|
if !cache.WaitForNamedCacheSync("endpoints config", stopCh, c.listerSynced) {
|
2019-01-12 04:58:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range c.eventHandlers {
|
|
|
|
klog.V(3).Infof("Calling handler.OnEndpointsSynced()")
|
|
|
|
c.eventHandlers[i].OnEndpointsSynced()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *EndpointsConfig) handleAddEndpoints(obj interface{}) {
|
|
|
|
endpoints, ok := obj.(*v1.Endpoints)
|
|
|
|
if !ok {
|
|
|
|
utilruntime.HandleError(fmt.Errorf("unexpected object type: %v", obj))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for i := range c.eventHandlers {
|
|
|
|
klog.V(4).Infof("Calling handler.OnEndpointsAdd")
|
|
|
|
c.eventHandlers[i].OnEndpointsAdd(endpoints)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *EndpointsConfig) handleUpdateEndpoints(oldObj, newObj interface{}) {
|
|
|
|
oldEndpoints, ok := oldObj.(*v1.Endpoints)
|
|
|
|
if !ok {
|
|
|
|
utilruntime.HandleError(fmt.Errorf("unexpected object type: %v", oldObj))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
endpoints, ok := newObj.(*v1.Endpoints)
|
|
|
|
if !ok {
|
|
|
|
utilruntime.HandleError(fmt.Errorf("unexpected object type: %v", newObj))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for i := range c.eventHandlers {
|
|
|
|
klog.V(4).Infof("Calling handler.OnEndpointsUpdate")
|
|
|
|
c.eventHandlers[i].OnEndpointsUpdate(oldEndpoints, endpoints)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *EndpointsConfig) handleDeleteEndpoints(obj interface{}) {
|
|
|
|
endpoints, ok := obj.(*v1.Endpoints)
|
|
|
|
if !ok {
|
|
|
|
tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
|
|
|
|
if !ok {
|
|
|
|
utilruntime.HandleError(fmt.Errorf("unexpected object type: %v", obj))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if endpoints, ok = tombstone.Obj.(*v1.Endpoints); !ok {
|
|
|
|
utilruntime.HandleError(fmt.Errorf("unexpected object type: %v", obj))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for i := range c.eventHandlers {
|
|
|
|
klog.V(4).Infof("Calling handler.OnEndpointsDelete")
|
|
|
|
c.eventHandlers[i].OnEndpointsDelete(endpoints)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-27 21:51:53 +00:00
|
|
|
// EndpointSliceConfig tracks a set of endpoints configurations.
|
|
|
|
type EndpointSliceConfig struct {
|
|
|
|
listerSynced cache.InformerSynced
|
|
|
|
eventHandlers []EndpointSliceHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewEndpointSliceConfig creates a new EndpointSliceConfig.
|
|
|
|
func NewEndpointSliceConfig(endpointSliceInformer discoveryinformers.EndpointSliceInformer, resyncPeriod time.Duration) *EndpointSliceConfig {
|
|
|
|
result := &EndpointSliceConfig{
|
|
|
|
listerSynced: endpointSliceInformer.Informer().HasSynced,
|
|
|
|
}
|
|
|
|
|
|
|
|
endpointSliceInformer.Informer().AddEventHandlerWithResyncPeriod(
|
|
|
|
cache.ResourceEventHandlerFuncs{
|
|
|
|
AddFunc: result.handleAddEndpointSlice,
|
|
|
|
UpdateFunc: result.handleUpdateEndpointSlice,
|
|
|
|
DeleteFunc: result.handleDeleteEndpointSlice,
|
|
|
|
},
|
|
|
|
resyncPeriod,
|
|
|
|
)
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterEventHandler registers a handler which is called on every endpoint slice change.
|
|
|
|
func (c *EndpointSliceConfig) RegisterEventHandler(handler EndpointSliceHandler) {
|
|
|
|
c.eventHandlers = append(c.eventHandlers, handler)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run waits for cache synced and invokes handlers after syncing.
|
|
|
|
func (c *EndpointSliceConfig) Run(stopCh <-chan struct{}) {
|
|
|
|
klog.Info("Starting endpoint slice config controller")
|
|
|
|
|
|
|
|
if !cache.WaitForNamedCacheSync("endpoint slice config", stopCh, c.listerSynced) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, h := range c.eventHandlers {
|
|
|
|
klog.V(3).Infof("Calling handler.OnEndpointSlicesSynced()")
|
|
|
|
h.OnEndpointSlicesSynced()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *EndpointSliceConfig) handleAddEndpointSlice(obj interface{}) {
|
|
|
|
endpointSlice, ok := obj.(*discovery.EndpointSlice)
|
|
|
|
if !ok {
|
|
|
|
utilruntime.HandleError(fmt.Errorf("unexpected object type: %T", obj))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, h := range c.eventHandlers {
|
2020-03-26 21:07:15 +00:00
|
|
|
klog.V(4).Infof("Calling handler.OnEndpointSliceAdd %+v", endpointSlice)
|
2019-09-27 21:51:53 +00:00
|
|
|
h.OnEndpointSliceAdd(endpointSlice)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *EndpointSliceConfig) handleUpdateEndpointSlice(oldObj, newObj interface{}) {
|
|
|
|
oldEndpointSlice, ok := oldObj.(*discovery.EndpointSlice)
|
|
|
|
if !ok {
|
|
|
|
utilruntime.HandleError(fmt.Errorf("unexpected object type: %T", newObj))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
newEndpointSlice, ok := newObj.(*discovery.EndpointSlice)
|
|
|
|
if !ok {
|
|
|
|
utilruntime.HandleError(fmt.Errorf("unexpected object type: %T", newObj))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, h := range c.eventHandlers {
|
|
|
|
klog.V(4).Infof("Calling handler.OnEndpointSliceUpdate")
|
|
|
|
h.OnEndpointSliceUpdate(oldEndpointSlice, newEndpointSlice)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *EndpointSliceConfig) handleDeleteEndpointSlice(obj interface{}) {
|
|
|
|
endpointSlice, ok := obj.(*discovery.EndpointSlice)
|
|
|
|
if !ok {
|
|
|
|
tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
|
|
|
|
if !ok {
|
|
|
|
utilruntime.HandleError(fmt.Errorf("unexpected object type: %T", obj))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if endpointSlice, ok = tombstone.Obj.(*discovery.EndpointSlice); !ok {
|
|
|
|
utilruntime.HandleError(fmt.Errorf("unexpected object type: %T", obj))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, h := range c.eventHandlers {
|
|
|
|
klog.V(4).Infof("Calling handler.OnEndpointsDelete")
|
|
|
|
h.OnEndpointSliceDelete(endpointSlice)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-12 04:58:27 +00:00
|
|
|
// ServiceConfig tracks a set of service configurations.
|
|
|
|
type ServiceConfig struct {
|
|
|
|
listerSynced cache.InformerSynced
|
|
|
|
eventHandlers []ServiceHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewServiceConfig creates a new ServiceConfig.
|
|
|
|
func NewServiceConfig(serviceInformer coreinformers.ServiceInformer, resyncPeriod time.Duration) *ServiceConfig {
|
|
|
|
result := &ServiceConfig{
|
|
|
|
listerSynced: serviceInformer.Informer().HasSynced,
|
|
|
|
}
|
|
|
|
|
|
|
|
serviceInformer.Informer().AddEventHandlerWithResyncPeriod(
|
|
|
|
cache.ResourceEventHandlerFuncs{
|
|
|
|
AddFunc: result.handleAddService,
|
|
|
|
UpdateFunc: result.handleUpdateService,
|
|
|
|
DeleteFunc: result.handleDeleteService,
|
|
|
|
},
|
|
|
|
resyncPeriod,
|
|
|
|
)
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterEventHandler registers a handler which is called on every service change.
|
|
|
|
func (c *ServiceConfig) RegisterEventHandler(handler ServiceHandler) {
|
|
|
|
c.eventHandlers = append(c.eventHandlers, handler)
|
|
|
|
}
|
|
|
|
|
2019-08-30 18:33:25 +00:00
|
|
|
// Run waits for cache synced and invokes handlers after syncing.
|
2019-01-12 04:58:27 +00:00
|
|
|
func (c *ServiceConfig) Run(stopCh <-chan struct{}) {
|
|
|
|
klog.Info("Starting service config controller")
|
|
|
|
|
2019-09-27 21:51:53 +00:00
|
|
|
if !cache.WaitForNamedCacheSync("service config", stopCh, c.listerSynced) {
|
2019-01-12 04:58:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range c.eventHandlers {
|
|
|
|
klog.V(3).Info("Calling handler.OnServiceSynced()")
|
|
|
|
c.eventHandlers[i].OnServiceSynced()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ServiceConfig) handleAddService(obj interface{}) {
|
|
|
|
service, ok := obj.(*v1.Service)
|
|
|
|
if !ok {
|
|
|
|
utilruntime.HandleError(fmt.Errorf("unexpected object type: %v", obj))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for i := range c.eventHandlers {
|
|
|
|
klog.V(4).Info("Calling handler.OnServiceAdd")
|
|
|
|
c.eventHandlers[i].OnServiceAdd(service)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ServiceConfig) handleUpdateService(oldObj, newObj interface{}) {
|
|
|
|
oldService, ok := oldObj.(*v1.Service)
|
|
|
|
if !ok {
|
|
|
|
utilruntime.HandleError(fmt.Errorf("unexpected object type: %v", oldObj))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
service, ok := newObj.(*v1.Service)
|
|
|
|
if !ok {
|
|
|
|
utilruntime.HandleError(fmt.Errorf("unexpected object type: %v", newObj))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for i := range c.eventHandlers {
|
|
|
|
klog.V(4).Info("Calling handler.OnServiceUpdate")
|
|
|
|
c.eventHandlers[i].OnServiceUpdate(oldService, service)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ServiceConfig) handleDeleteService(obj interface{}) {
|
|
|
|
service, ok := obj.(*v1.Service)
|
|
|
|
if !ok {
|
|
|
|
tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
|
|
|
|
if !ok {
|
|
|
|
utilruntime.HandleError(fmt.Errorf("unexpected object type: %v", obj))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if service, ok = tombstone.Obj.(*v1.Service); !ok {
|
|
|
|
utilruntime.HandleError(fmt.Errorf("unexpected object type: %v", obj))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for i := range c.eventHandlers {
|
|
|
|
klog.V(4).Info("Calling handler.OnServiceDelete")
|
|
|
|
c.eventHandlers[i].OnServiceDelete(service)
|
|
|
|
}
|
|
|
|
}
|
2019-12-12 01:27:03 +00:00
|
|
|
|
|
|
|
// NodeHandler is an abstract interface of objects which receive
|
|
|
|
// notifications about node object changes.
|
|
|
|
type NodeHandler interface {
|
|
|
|
// OnNodeAdd is called whenever creation of new node object
|
|
|
|
// is observed.
|
|
|
|
OnNodeAdd(node *v1.Node)
|
|
|
|
// OnNodeUpdate is called whenever modification of an existing
|
|
|
|
// node object is observed.
|
|
|
|
OnNodeUpdate(oldNode, node *v1.Node)
|
2020-08-10 17:43:49 +00:00
|
|
|
// OnNodeDelete is called whenever deletion of an existing node
|
2019-12-12 01:27:03 +00:00
|
|
|
// object is observed.
|
|
|
|
OnNodeDelete(node *v1.Node)
|
|
|
|
// OnNodeSynced is called once all the initial event handlers were
|
|
|
|
// called and the state is fully propagated to local cache.
|
|
|
|
OnNodeSynced()
|
|
|
|
}
|
|
|
|
|
|
|
|
// NoopNodeHandler is a noop handler for proxiers that have not yet
|
|
|
|
// implemented a full NodeHandler.
|
|
|
|
type NoopNodeHandler struct{}
|
|
|
|
|
|
|
|
// OnNodeAdd is a noop handler for Node creates.
|
|
|
|
func (*NoopNodeHandler) OnNodeAdd(node *v1.Node) {}
|
|
|
|
|
|
|
|
// OnNodeUpdate is a noop handler for Node updates.
|
|
|
|
func (*NoopNodeHandler) OnNodeUpdate(oldNode, node *v1.Node) {}
|
|
|
|
|
|
|
|
// OnNodeDelete is a noop handler for Node deletes.
|
|
|
|
func (*NoopNodeHandler) OnNodeDelete(node *v1.Node) {}
|
|
|
|
|
|
|
|
// OnNodeSynced is a noop handler for Node syncs.
|
|
|
|
func (*NoopNodeHandler) OnNodeSynced() {}
|
|
|
|
|
2020-03-26 21:07:15 +00:00
|
|
|
var _ NodeHandler = &NoopNodeHandler{}
|
|
|
|
|
2019-12-12 01:27:03 +00:00
|
|
|
// NodeConfig tracks a set of node configurations.
|
|
|
|
// It accepts "set", "add" and "remove" operations of node via channels, and invokes registered handlers on change.
|
|
|
|
type NodeConfig struct {
|
|
|
|
listerSynced cache.InformerSynced
|
|
|
|
eventHandlers []NodeHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewNodeConfig creates a new NodeConfig.
|
|
|
|
func NewNodeConfig(nodeInformer coreinformers.NodeInformer, resyncPeriod time.Duration) *NodeConfig {
|
|
|
|
result := &NodeConfig{
|
|
|
|
listerSynced: nodeInformer.Informer().HasSynced,
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeInformer.Informer().AddEventHandlerWithResyncPeriod(
|
|
|
|
cache.ResourceEventHandlerFuncs{
|
|
|
|
AddFunc: result.handleAddNode,
|
|
|
|
UpdateFunc: result.handleUpdateNode,
|
|
|
|
DeleteFunc: result.handleDeleteNode,
|
|
|
|
},
|
|
|
|
resyncPeriod,
|
|
|
|
)
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterEventHandler registers a handler which is called on every node change.
|
|
|
|
func (c *NodeConfig) RegisterEventHandler(handler NodeHandler) {
|
|
|
|
c.eventHandlers = append(c.eventHandlers, handler)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run starts the goroutine responsible for calling registered handlers.
|
|
|
|
func (c *NodeConfig) Run(stopCh <-chan struct{}) {
|
|
|
|
klog.Info("Starting node config controller")
|
|
|
|
|
|
|
|
if !cache.WaitForNamedCacheSync("node config", stopCh, c.listerSynced) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range c.eventHandlers {
|
|
|
|
klog.V(3).Infof("Calling handler.OnNodeSynced()")
|
|
|
|
c.eventHandlers[i].OnNodeSynced()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *NodeConfig) handleAddNode(obj interface{}) {
|
|
|
|
node, ok := obj.(*v1.Node)
|
|
|
|
if !ok {
|
|
|
|
utilruntime.HandleError(fmt.Errorf("unexpected object type: %v", obj))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for i := range c.eventHandlers {
|
|
|
|
klog.V(4).Infof("Calling handler.OnNodeAdd")
|
|
|
|
c.eventHandlers[i].OnNodeAdd(node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *NodeConfig) handleUpdateNode(oldObj, newObj interface{}) {
|
|
|
|
oldNode, ok := oldObj.(*v1.Node)
|
|
|
|
if !ok {
|
|
|
|
utilruntime.HandleError(fmt.Errorf("unexpected object type: %v", oldObj))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
node, ok := newObj.(*v1.Node)
|
|
|
|
if !ok {
|
|
|
|
utilruntime.HandleError(fmt.Errorf("unexpected object type: %v", newObj))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for i := range c.eventHandlers {
|
|
|
|
klog.V(5).Infof("Calling handler.OnNodeUpdate")
|
|
|
|
c.eventHandlers[i].OnNodeUpdate(oldNode, node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *NodeConfig) handleDeleteNode(obj interface{}) {
|
|
|
|
node, ok := obj.(*v1.Node)
|
|
|
|
if !ok {
|
|
|
|
tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
|
|
|
|
if !ok {
|
|
|
|
utilruntime.HandleError(fmt.Errorf("unexpected object type: %v", obj))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if node, ok = tombstone.Obj.(*v1.Node); !ok {
|
|
|
|
utilruntime.HandleError(fmt.Errorf("unexpected object type: %v", obj))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for i := range c.eventHandlers {
|
|
|
|
klog.V(4).Infof("Calling handler.OnNodeDelete")
|
|
|
|
c.eventHandlers[i].OnNodeDelete(node)
|
|
|
|
}
|
|
|
|
}
|