2015-01-07 21:12:29 +00:00
|
|
|
/*
|
|
|
|
Copyright 2015 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 (
|
2015-03-10 04:45:35 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
2015-01-07 21:12:29 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
|
2015-03-15 21:51:41 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
|
2015-01-07 21:12:29 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
|
|
|
|
)
|
|
|
|
|
2015-02-14 22:10:57 +00:00
|
|
|
// ListFunc knows how to list resources
|
|
|
|
type ListFunc func() (runtime.Object, error)
|
|
|
|
|
|
|
|
// WatchFunc knows how to watch resources
|
|
|
|
type WatchFunc func(resourceVersion string) (watch.Interface, error)
|
|
|
|
|
2015-01-07 21:12:29 +00:00
|
|
|
// ListWatch knows how to list and watch a set of apiserver resources. It satisfies the ListerWatcher interface.
|
2015-02-14 22:10:57 +00:00
|
|
|
// It is a convenience function for users of NewReflector, etc.
|
|
|
|
// ListFunc and WatchFunc must not be nil
|
2015-01-07 21:12:29 +00:00
|
|
|
type ListWatch struct {
|
2015-02-14 22:10:57 +00:00
|
|
|
ListFunc ListFunc
|
|
|
|
WatchFunc WatchFunc
|
|
|
|
}
|
|
|
|
|
2015-02-27 18:44:44 +00:00
|
|
|
// NewListWatchFromClient creates a new ListWatch from the specified client, resource, namespace and field selector.
|
2015-03-15 21:51:41 +00:00
|
|
|
func NewListWatchFromClient(c *client.Client, resource string, namespace string, fieldSelector fields.Selector) *ListWatch {
|
2015-02-14 22:10:57 +00:00
|
|
|
listFunc := func() (runtime.Object, error) {
|
2015-03-15 21:51:41 +00:00
|
|
|
return c.Get().Namespace(namespace).Resource(resource).FieldsSelectorParam(api.FieldSelectorQueryParam(c.APIVersion()), fieldSelector).Do().Get()
|
2015-02-14 22:10:57 +00:00
|
|
|
}
|
|
|
|
watchFunc := func(resourceVersion string) (watch.Interface, error) {
|
2015-03-15 21:51:41 +00:00
|
|
|
return c.Get().Prefix("watch").Namespace(namespace).Resource(resource).FieldsSelectorParam(api.FieldSelectorQueryParam(c.APIVersion()), fieldSelector).Param("resourceVersion", resourceVersion).Watch()
|
2015-02-14 22:10:57 +00:00
|
|
|
}
|
|
|
|
return &ListWatch{ListFunc: listFunc, WatchFunc: watchFunc}
|
2015-01-07 21:12:29 +00:00
|
|
|
}
|
|
|
|
|
2015-02-14 22:10:57 +00:00
|
|
|
// List a set of apiserver resources
|
2015-01-07 21:12:29 +00:00
|
|
|
func (lw *ListWatch) List() (runtime.Object, error) {
|
2015-02-14 22:10:57 +00:00
|
|
|
return lw.ListFunc()
|
2015-01-07 21:12:29 +00:00
|
|
|
}
|
|
|
|
|
2015-02-14 22:10:57 +00:00
|
|
|
// Watch a set of apiserver resources
|
2015-01-07 21:12:29 +00:00
|
|
|
func (lw *ListWatch) Watch(resourceVersion string) (watch.Interface, error) {
|
2015-02-14 22:10:57 +00:00
|
|
|
return lw.WatchFunc(resourceVersion)
|
2015-01-07 21:12:29 +00:00
|
|
|
}
|