2014-08-03 07:00:42 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
|
|
|
|
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 cache
|
|
|
|
|
|
|
|
import (
|
2014-09-20 01:09:40 +00:00
|
|
|
"errors"
|
2014-09-16 01:10:10 +00:00
|
|
|
"fmt"
|
2014-08-03 07:00:42 +00:00
|
|
|
"reflect"
|
|
|
|
"time"
|
|
|
|
|
2014-09-23 00:37:12 +00:00
|
|
|
apierrs "github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
|
2014-10-23 02:28:06 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta"
|
2014-09-02 17:55:27 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
2014-08-03 07:00:42 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
|
|
|
|
"github.com/golang/glog"
|
|
|
|
)
|
|
|
|
|
2014-09-16 01:10:10 +00:00
|
|
|
// ListerWatcher is any object that knows how to perform an initial list and start a watch on a resource.
|
|
|
|
type ListerWatcher interface {
|
|
|
|
// List should return a list type object; the Items field will be extracted, and the
|
|
|
|
// ResourceVersion field will be used to start the watch in the right place.
|
|
|
|
List() (runtime.Object, error)
|
|
|
|
// Watch should begin a watch at the specified version.
|
2014-10-07 20:51:28 +00:00
|
|
|
Watch(resourceVersion string) (watch.Interface, error)
|
2014-09-16 01:10:10 +00:00
|
|
|
}
|
|
|
|
|
2014-08-03 22:36:36 +00:00
|
|
|
// Reflector watches a specified resource and causes all changes to be reflected in the given store.
|
|
|
|
type Reflector struct {
|
2014-08-14 22:42:05 +00:00
|
|
|
// The type of object we expect to place in the store.
|
2014-08-03 07:00:42 +00:00
|
|
|
expectedType reflect.Type
|
2014-08-14 22:42:05 +00:00
|
|
|
// The destination to sync up with the watch source
|
|
|
|
store Store
|
2014-09-16 01:10:10 +00:00
|
|
|
// listerWatcher is used to perform lists and watches.
|
|
|
|
listerWatcher ListerWatcher
|
2014-08-18 21:47:20 +00:00
|
|
|
// period controls timing between one watch ending and
|
2014-08-14 22:42:05 +00:00
|
|
|
// the beginning of the next one.
|
2014-08-18 21:47:20 +00:00
|
|
|
period time.Duration
|
2014-08-03 07:00:42 +00:00
|
|
|
}
|
|
|
|
|
2014-09-02 10:00:28 +00:00
|
|
|
// NewReflector creates a new Reflector object which will keep the given store up to
|
2014-08-03 22:36:36 +00:00
|
|
|
// date with the server's contents for the given resource. Reflector promises to
|
2014-08-03 07:00:42 +00:00
|
|
|
// only put things in the store that have the type of expectedType.
|
2014-09-16 01:10:10 +00:00
|
|
|
func NewReflector(lw ListerWatcher, expectedType interface{}, store Store) *Reflector {
|
|
|
|
r := &Reflector{
|
|
|
|
listerWatcher: lw,
|
|
|
|
store: store,
|
|
|
|
expectedType: reflect.TypeOf(expectedType),
|
|
|
|
period: time.Second,
|
2014-08-03 07:00:42 +00:00
|
|
|
}
|
2014-09-16 01:10:10 +00:00
|
|
|
return r
|
2014-08-03 07:00:42 +00:00
|
|
|
}
|
|
|
|
|
2014-08-14 22:42:05 +00:00
|
|
|
// Run starts a watch and handles watch events. Will restart the watch if it is closed.
|
|
|
|
// Run starts a goroutine and returns immediately.
|
2014-09-16 01:10:10 +00:00
|
|
|
func (r *Reflector) Run() {
|
|
|
|
go util.Forever(func() { r.listAndWatch() }, r.period)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Reflector) listAndWatch() {
|
2014-10-07 20:51:28 +00:00
|
|
|
var resourceVersion string
|
2014-09-16 01:10:10 +00:00
|
|
|
|
|
|
|
list, err := r.listerWatcher.List()
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("Failed to list %v: %v", r.expectedType, err)
|
|
|
|
return
|
|
|
|
}
|
2014-10-23 02:54:34 +00:00
|
|
|
jsonBase, err := meta.FindAccessor(list)
|
2014-09-16 01:10:10 +00:00
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("Unable to understand list result %#v", list)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
resourceVersion = jsonBase.ResourceVersion()
|
|
|
|
items, err := runtime.ExtractList(list)
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("Unable to understand list result %#v (%v)", list, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = r.syncWith(items)
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("Unable to sync list result: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
w, err := r.listerWatcher.Watch(resourceVersion)
|
2014-08-03 22:36:36 +00:00
|
|
|
if err != nil {
|
2014-09-16 01:10:10 +00:00
|
|
|
glog.Errorf("failed to watch %v: %v", r.expectedType, err)
|
2014-08-03 22:36:36 +00:00
|
|
|
return
|
|
|
|
}
|
2014-09-20 01:09:40 +00:00
|
|
|
if err := r.watchHandler(w, &resourceVersion); err != nil {
|
2014-09-23 00:37:12 +00:00
|
|
|
glog.Errorf("watch of %v ended with error: %v", r.expectedType, err)
|
2014-09-20 01:09:40 +00:00
|
|
|
return
|
|
|
|
}
|
2014-09-16 01:10:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// syncWith replaces the store's items with the given list.
|
|
|
|
func (r *Reflector) syncWith(items []runtime.Object) error {
|
|
|
|
found := map[string]interface{}{}
|
|
|
|
for _, item := range items {
|
2014-10-23 02:54:34 +00:00
|
|
|
jsonBase, err := meta.FindAccessor(item)
|
2014-09-16 01:10:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unexpected item in list: %v", err)
|
|
|
|
}
|
2014-10-23 02:54:34 +00:00
|
|
|
found[jsonBase.Name()] = item
|
2014-09-16 01:10:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
r.store.Replace(found)
|
|
|
|
return nil
|
2014-08-03 07:00:42 +00:00
|
|
|
}
|
|
|
|
|
2014-08-14 22:42:05 +00:00
|
|
|
// watchHandler watches w and keeps *resourceVersion up to date.
|
2014-10-07 20:51:28 +00:00
|
|
|
func (r *Reflector) watchHandler(w watch.Interface, resourceVersion *string) error {
|
2014-09-20 01:09:40 +00:00
|
|
|
start := time.Now()
|
|
|
|
eventCount := 0
|
2014-08-03 07:00:42 +00:00
|
|
|
for {
|
|
|
|
event, ok := <-w.ResultChan()
|
|
|
|
if !ok {
|
2014-09-20 01:09:40 +00:00
|
|
|
break
|
2014-08-03 07:00:42 +00:00
|
|
|
}
|
2014-09-23 00:37:12 +00:00
|
|
|
if event.Type == watch.Error {
|
|
|
|
return apierrs.FromObject(event.Object)
|
|
|
|
}
|
2014-09-16 01:10:10 +00:00
|
|
|
if e, a := r.expectedType, reflect.TypeOf(event.Object); e != a {
|
2014-08-03 07:00:42 +00:00
|
|
|
glog.Errorf("expected type %v, but watch event object had type %v", e, a)
|
|
|
|
continue
|
|
|
|
}
|
2014-10-23 02:54:34 +00:00
|
|
|
jsonBase, err := meta.FindAccessor(event.Object)
|
2014-08-03 07:00:42 +00:00
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("unable to understand watch event %#v", event)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch event.Type {
|
|
|
|
case watch.Added:
|
2014-10-23 02:54:34 +00:00
|
|
|
r.store.Add(jsonBase.Name(), event.Object)
|
2014-08-03 07:00:42 +00:00
|
|
|
case watch.Modified:
|
2014-10-23 02:54:34 +00:00
|
|
|
r.store.Update(jsonBase.Name(), event.Object)
|
2014-08-03 07:00:42 +00:00
|
|
|
case watch.Deleted:
|
2014-08-18 21:47:20 +00:00
|
|
|
// TODO: Will any consumers need access to the "last known
|
|
|
|
// state", which is passed in event.Object? If so, may need
|
|
|
|
// to change this.
|
2014-10-23 02:54:34 +00:00
|
|
|
r.store.Delete(jsonBase.Name())
|
2014-08-03 07:00:42 +00:00
|
|
|
default:
|
|
|
|
glog.Errorf("unable to understand watch event %#v", event)
|
|
|
|
}
|
2014-10-07 20:51:28 +00:00
|
|
|
*resourceVersion = jsonBase.ResourceVersion()
|
2014-09-20 01:09:40 +00:00
|
|
|
eventCount++
|
2014-08-03 07:00:42 +00:00
|
|
|
}
|
2014-09-20 01:09:40 +00:00
|
|
|
|
|
|
|
watchDuration := time.Now().Sub(start)
|
|
|
|
if watchDuration < 1*time.Second && eventCount == 0 {
|
|
|
|
glog.Errorf("unexpected watch close - watch lasted less than a second and no items received")
|
|
|
|
return errors.New("very short watch")
|
|
|
|
}
|
2014-09-18 10:46:14 +00:00
|
|
|
glog.V(4).Infof("watch close - %v total items received", eventCount)
|
2014-09-20 01:09:40 +00:00
|
|
|
return nil
|
2014-08-03 07:00:42 +00:00
|
|
|
}
|