2014-08-15 21:14:22 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors.
|
2014-08-15 21:14:22 +00:00
|
|
|
|
|
|
|
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 (
|
2017-02-25 08:50:35 +00:00
|
|
|
"fmt"
|
2014-08-15 21:14:22 +00:00
|
|
|
"time"
|
|
|
|
|
2017-01-22 03:36:02 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2017-01-19 14:50:16 +00:00
|
|
|
"k8s.io/apimachinery/pkg/fields"
|
2017-02-25 08:50:35 +00:00
|
|
|
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
|
|
|
"k8s.io/apimachinery/pkg/util/wait"
|
2017-01-24 14:11:51 +00:00
|
|
|
"k8s.io/client-go/tools/cache"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2014-08-15 21:14:22 +00:00
|
|
|
)
|
|
|
|
|
2015-10-02 13:52:01 +00:00
|
|
|
// NewSourceAPI creates config source that watches for changes to the services and endpoints.
|
2016-03-05 01:40:46 +00:00
|
|
|
func NewSourceAPI(c cache.Getter, period time.Duration, servicesChan chan<- ServiceUpdate, endpointsChan chan<- EndpointsUpdate) {
|
2017-02-25 08:50:35 +00:00
|
|
|
stopCh := wait.NeverStop
|
|
|
|
|
2017-01-22 03:36:02 +00:00
|
|
|
servicesLW := cache.NewListWatchFromClient(c, "services", metav1.NamespaceAll, fields.Everything())
|
2017-02-25 08:50:35 +00:00
|
|
|
serviceController := NewServiceController(servicesLW, period, servicesChan)
|
|
|
|
go serviceController.Run(stopCh)
|
2014-08-15 21:14:22 +00:00
|
|
|
|
2017-01-22 03:36:02 +00:00
|
|
|
endpointsLW := cache.NewListWatchFromClient(c, "endpoints", metav1.NamespaceAll, fields.Everything())
|
2017-02-25 08:50:35 +00:00
|
|
|
endpointsController := NewEndpointsController(endpointsLW, period, endpointsChan)
|
|
|
|
go endpointsController.Run(stopCh)
|
|
|
|
|
|
|
|
if !cache.WaitForCacheSync(stopCh, serviceController.HasSynced, endpointsController.HasSynced) {
|
|
|
|
utilruntime.HandleError(fmt.Errorf("source controllers not synced"))
|
|
|
|
}
|
2015-01-11 05:13:32 +00:00
|
|
|
}
|
|
|
|
|
2017-02-25 08:50:35 +00:00
|
|
|
func sendAddService(servicesChan chan<- ServiceUpdate) func(obj interface{}) {
|
|
|
|
return func(obj interface{}) {
|
|
|
|
service, ok := obj.(*api.Service)
|
|
|
|
if !ok {
|
|
|
|
utilruntime.HandleError(fmt.Errorf("cannot convert to *api.Service: %v", obj))
|
|
|
|
return
|
2014-08-29 02:31:41 +00:00
|
|
|
}
|
2017-02-25 08:50:35 +00:00
|
|
|
servicesChan <- ServiceUpdate{Op: ADD, Service: service}
|
2016-03-05 01:40:46 +00:00
|
|
|
}
|
2017-02-25 08:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func sendUpdateService(servicesChan chan<- ServiceUpdate) func(oldObj, newObj interface{}) {
|
|
|
|
return func(_, newObj interface{}) {
|
|
|
|
service, ok := newObj.(*api.Service)
|
|
|
|
if !ok {
|
|
|
|
utilruntime.HandleError(fmt.Errorf("cannot convert to *api.Service: %v", newObj))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
servicesChan <- ServiceUpdate{Op: UPDATE, Service: service}
|
2016-03-05 01:40:46 +00:00
|
|
|
}
|
2017-02-25 08:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func sendDeleteService(servicesChan chan<- ServiceUpdate) func(obj interface{}) {
|
|
|
|
return func(obj interface{}) {
|
|
|
|
var service *api.Service
|
|
|
|
switch t := obj.(type) {
|
|
|
|
case *api.Service:
|
|
|
|
service = t
|
|
|
|
case cache.DeletedFinalStateUnknown:
|
|
|
|
var ok bool
|
|
|
|
service, ok = t.Obj.(*api.Service)
|
|
|
|
if !ok {
|
|
|
|
utilruntime.HandleError(fmt.Errorf("cannot convert to *api.Service: %v", t.Obj))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
utilruntime.HandleError(fmt.Errorf("cannot convert to *api.Service: %v", t))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
servicesChan <- ServiceUpdate{Op: REMOVE, Service: service}
|
2014-08-29 02:31:41 +00:00
|
|
|
}
|
2014-08-15 21:14:22 +00:00
|
|
|
}
|
|
|
|
|
2017-02-25 08:50:35 +00:00
|
|
|
func sendAddEndpoints(endpointsChan chan<- EndpointsUpdate) func(obj interface{}) {
|
|
|
|
return func(obj interface{}) {
|
|
|
|
endpoints, ok := obj.(*api.Endpoints)
|
|
|
|
if !ok {
|
|
|
|
utilruntime.HandleError(fmt.Errorf("cannot convert to *api.Endpoints: %v", obj))
|
|
|
|
return
|
2015-01-06 19:36:03 +00:00
|
|
|
}
|
2017-02-25 08:50:35 +00:00
|
|
|
endpointsChan <- EndpointsUpdate{Op: ADD, Endpoints: endpoints}
|
2016-03-05 01:40:46 +00:00
|
|
|
}
|
2017-02-25 08:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func sendUpdateEndpoints(endpointsChan chan<- EndpointsUpdate) func(oldObj, newObj interface{}) {
|
|
|
|
return func(_, newObj interface{}) {
|
|
|
|
endpoints, ok := newObj.(*api.Endpoints)
|
|
|
|
if !ok {
|
|
|
|
utilruntime.HandleError(fmt.Errorf("cannot convert to *api.Endpoints: %v", newObj))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
endpointsChan <- EndpointsUpdate{Op: UPDATE, Endpoints: endpoints}
|
2016-03-05 01:40:46 +00:00
|
|
|
}
|
2017-02-25 08:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func sendDeleteEndpoints(endpointsChan chan<- EndpointsUpdate) func(obj interface{}) {
|
|
|
|
return func(obj interface{}) {
|
|
|
|
var endpoints *api.Endpoints
|
|
|
|
switch t := obj.(type) {
|
|
|
|
case *api.Endpoints:
|
|
|
|
endpoints = t
|
|
|
|
case cache.DeletedFinalStateUnknown:
|
|
|
|
var ok bool
|
|
|
|
endpoints, ok = t.Obj.(*api.Endpoints)
|
|
|
|
if !ok {
|
|
|
|
utilruntime.HandleError(fmt.Errorf("cannot convert to *api.Endpoints: %v", t.Obj))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
utilruntime.HandleError(fmt.Errorf("cannot convert to *api.Endpoints: %v", obj))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
endpointsChan <- EndpointsUpdate{Op: REMOVE, Endpoints: endpoints}
|
2014-08-15 21:14:22 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-25 08:50:35 +00:00
|
|
|
|
|
|
|
// NewServiceController creates a controller that is watching services and sending
|
|
|
|
// updates into ServiceUpdate channel.
|
|
|
|
func NewServiceController(lw cache.ListerWatcher, period time.Duration, ch chan<- ServiceUpdate) cache.Controller {
|
|
|
|
_, serviceController := cache.NewInformer(
|
|
|
|
lw,
|
|
|
|
&api.Service{},
|
|
|
|
period,
|
|
|
|
cache.ResourceEventHandlerFuncs{
|
|
|
|
AddFunc: sendAddService(ch),
|
|
|
|
UpdateFunc: sendUpdateService(ch),
|
|
|
|
DeleteFunc: sendDeleteService(ch),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
return serviceController
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewEndpointsController creates a controller that is watching endpoints and sending
|
|
|
|
// updates into EndpointsUpdate channel.
|
|
|
|
func NewEndpointsController(lw cache.ListerWatcher, period time.Duration, ch chan<- EndpointsUpdate) cache.Controller {
|
|
|
|
_, endpointsController := cache.NewInformer(
|
|
|
|
lw,
|
|
|
|
&api.Endpoints{},
|
|
|
|
period,
|
|
|
|
cache.ResourceEventHandlerFuncs{
|
|
|
|
AddFunc: sendAddEndpoints(ch),
|
|
|
|
UpdateFunc: sendUpdateEndpoints(ch),
|
|
|
|
DeleteFunc: sendDeleteEndpoints(ch),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
return endpointsController
|
|
|
|
}
|