2014-08-15 21:14:22 +00:00
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors All rights reserved.
|
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 (
|
|
|
|
"time"
|
|
|
|
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2015-08-13 19:01:50 +00:00
|
|
|
client "k8s.io/kubernetes/pkg/client/unversioned"
|
|
|
|
"k8s.io/kubernetes/pkg/client/unversioned/cache"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/fields"
|
2014-08-15 21:14:22 +00:00
|
|
|
)
|
|
|
|
|
2015-06-28 19:28:10 +00:00
|
|
|
// NewSourceAPIserver creates config source that watches for changes to the services and endpoints.
|
|
|
|
func NewSourceAPI(c *client.Client, period time.Duration, servicesChan chan<- ServiceUpdate, endpointsChan chan<- EndpointsUpdate) {
|
|
|
|
servicesLW := cache.NewListWatchFromClient(c, "services", api.NamespaceAll, fields.Everything())
|
|
|
|
endpointsLW := cache.NewListWatchFromClient(c, "endpoints", api.NamespaceAll, fields.Everything())
|
2014-08-15 21:14:22 +00:00
|
|
|
|
2015-06-28 19:28:10 +00:00
|
|
|
newServicesSourceApiFromLW(servicesLW, period, servicesChan)
|
|
|
|
newEndpointsSourceApiFromLW(endpointsLW, period, endpointsChan)
|
2015-01-11 05:13:32 +00:00
|
|
|
}
|
|
|
|
|
2015-06-28 19:28:10 +00:00
|
|
|
func newServicesSourceApiFromLW(servicesLW cache.ListerWatcher, period time.Duration, servicesChan chan<- ServiceUpdate) {
|
|
|
|
servicesPush := func(objs []interface{}) {
|
|
|
|
var services []api.Service
|
|
|
|
for _, o := range objs {
|
|
|
|
services = append(services, *(o.(*api.Service)))
|
2014-08-29 02:31:41 +00:00
|
|
|
}
|
2015-06-28 19:28:10 +00:00
|
|
|
servicesChan <- ServiceUpdate{Op: SET, Services: services}
|
2014-08-29 02:31:41 +00:00
|
|
|
}
|
|
|
|
|
2015-06-28 19:28:10 +00:00
|
|
|
serviceQueue := cache.NewUndeltaStore(servicesPush, cache.MetaNamespaceKeyFunc)
|
|
|
|
cache.NewReflector(servicesLW, &api.Service{}, serviceQueue, period).Run()
|
2014-08-15 21:14:22 +00:00
|
|
|
}
|
|
|
|
|
2015-06-28 19:28:10 +00:00
|
|
|
func newEndpointsSourceApiFromLW(endpointsLW cache.ListerWatcher, period time.Duration, endpointsChan chan<- EndpointsUpdate) {
|
|
|
|
endpointsPush := func(objs []interface{}) {
|
|
|
|
var endpoints []api.Endpoints
|
|
|
|
for _, o := range objs {
|
|
|
|
endpoints = append(endpoints, *(o.(*api.Endpoints)))
|
2015-01-06 19:36:03 +00:00
|
|
|
}
|
2015-06-28 19:28:10 +00:00
|
|
|
endpointsChan <- EndpointsUpdate{Op: SET, Endpoints: endpoints}
|
2014-08-15 21:14:22 +00:00
|
|
|
}
|
|
|
|
|
2015-06-28 19:28:10 +00:00
|
|
|
endpointQueue := cache.NewUndeltaStore(endpointsPush, cache.MetaNamespaceKeyFunc)
|
|
|
|
cache.NewReflector(endpointsLW, &api.Endpoints{}, endpointQueue, period).Run()
|
2014-08-15 21:14:22 +00:00
|
|
|
}
|