mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-22 10:27:56 +00:00
Enable watch on node changes.
This commit is contained in:
@@ -20,6 +20,8 @@ import (
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/health"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
|
||||
|
||||
"github.com/golang/glog"
|
||||
)
|
||||
@@ -86,3 +88,7 @@ func (r *HealthyRegistry) ListMinions(ctx api.Context) (currentMinions *api.Node
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (r *HealthyRegistry) WatchMinions(ctx api.Context, label, field labels.Selector, resourceVersion string) (watch.Interface, error) {
|
||||
return r.delegate.WatchMinions(ctx, label, field, resourceVersion)
|
||||
}
|
||||
|
@@ -16,7 +16,11 @@ limitations under the License.
|
||||
|
||||
package minion
|
||||
|
||||
import "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
import (
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
|
||||
)
|
||||
|
||||
// MinionRegistry is an interface for things that know how to store minions.
|
||||
type Registry interface {
|
||||
@@ -25,4 +29,5 @@ type Registry interface {
|
||||
UpdateMinion(ctx api.Context, minion *api.Node) error
|
||||
GetMinion(ctx api.Context, minionID string) (*api.Node, error)
|
||||
DeleteMinion(ctx api.Context, minionID string) error
|
||||
WatchMinions(ctx api.Context, label, field labels.Selector, resourceVersion string) (watch.Interface, error)
|
||||
}
|
||||
|
@@ -29,14 +29,15 @@ import (
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/master/ports"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
|
||||
)
|
||||
|
||||
// REST implements the RESTStorage interface, backed by a MinionRegistry.
|
||||
// REST adapts minion into apiserver's RESTStorage model.
|
||||
type REST struct {
|
||||
registry Registry
|
||||
}
|
||||
|
||||
// NewREST returns a new REST.
|
||||
// NewREST returns a new apiserver.RESTStorage implementation for minion.
|
||||
func NewREST(m Registry) *REST {
|
||||
return &REST{
|
||||
registry: m,
|
||||
@@ -46,6 +47,7 @@ func NewREST(m Registry) *REST {
|
||||
var ErrDoesNotExist = errors.New("The requested resource does not exist.")
|
||||
var ErrNotHealty = errors.New("The requested minion is not healthy.")
|
||||
|
||||
// Create satisfies the RESTStorage interface.
|
||||
func (rs *REST) Create(ctx api.Context, obj runtime.Object) (<-chan apiserver.RESTResult, error) {
|
||||
minion, ok := obj.(*api.Node)
|
||||
if !ok {
|
||||
@@ -67,6 +69,7 @@ func (rs *REST) Create(ctx api.Context, obj runtime.Object) (<-chan apiserver.RE
|
||||
}), nil
|
||||
}
|
||||
|
||||
// Delete satisfies the RESTStorage interface.
|
||||
func (rs *REST) Delete(ctx api.Context, id string) (<-chan apiserver.RESTResult, error) {
|
||||
minion, err := rs.registry.GetMinion(ctx, id)
|
||||
if minion == nil {
|
||||
@@ -80,6 +83,7 @@ func (rs *REST) Delete(ctx api.Context, id string) (<-chan apiserver.RESTResult,
|
||||
}), nil
|
||||
}
|
||||
|
||||
// Get satisfies the RESTStorage interface.
|
||||
func (rs *REST) Get(ctx api.Context, id string) (runtime.Object, error) {
|
||||
minion, err := rs.registry.GetMinion(ctx, id)
|
||||
if minion == nil {
|
||||
@@ -88,6 +92,7 @@ func (rs *REST) Get(ctx api.Context, id string) (runtime.Object, error) {
|
||||
return minion, err
|
||||
}
|
||||
|
||||
// List satisfies the RESTStorage interface.
|
||||
func (rs *REST) List(ctx api.Context, label, field labels.Selector) (runtime.Object, error) {
|
||||
return rs.registry.ListMinions(ctx)
|
||||
}
|
||||
@@ -96,6 +101,7 @@ func (rs *REST) New() runtime.Object {
|
||||
return &api.Node{}
|
||||
}
|
||||
|
||||
// Update satisfies the RESTStorage interface.
|
||||
func (rs *REST) Update(ctx api.Context, obj runtime.Object) (<-chan apiserver.RESTResult, error) {
|
||||
minion, ok := obj.(*api.Node)
|
||||
if !ok {
|
||||
@@ -121,8 +127,10 @@ func (rs *REST) Update(ctx api.Context, obj runtime.Object) (<-chan apiserver.RE
|
||||
}), nil
|
||||
}
|
||||
|
||||
func (rs *REST) toApiMinion(name string) *api.Node {
|
||||
return &api.Node{ObjectMeta: api.ObjectMeta{Name: name}}
|
||||
// Watch returns Minions events via a watch.Interface.
|
||||
// It implements apiserver.ResourceWatcher.
|
||||
func (rs *REST) Watch(ctx api.Context, label, field labels.Selector, resourceVersion string) (watch.Interface, error) {
|
||||
return rs.registry.WatchMinions(ctx, label, field, resourceVersion)
|
||||
}
|
||||
|
||||
// ResourceLocation returns a URL to which one can send traffic for the specified minion.
|
||||
|
@@ -25,7 +25,7 @@ import (
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/registrytest"
|
||||
)
|
||||
|
||||
func TestMinionREST(t *testing.T) {
|
||||
func TestMinionRegistryREST(t *testing.T) {
|
||||
ms := NewREST(registrytest.NewMinionRegistry([]string{"foo", "bar"}, api.NodeResources{}))
|
||||
ctx := api.NewContext()
|
||||
if obj, err := ms.Get(ctx, "foo"); err != nil || obj.(*api.Node).Name != "foo" {
|
||||
@@ -87,7 +87,7 @@ func TestMinionREST(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMinionStorageWithHealthCheck(t *testing.T) {
|
||||
func TestMinionRegistryHealthCheck(t *testing.T) {
|
||||
minionRegistry := registrytest.NewMinionRegistry([]string{}, api.NodeResources{})
|
||||
minionHealthRegistry := HealthyRegistry{
|
||||
delegate: minionRegistry,
|
||||
@@ -119,7 +119,7 @@ func contains(nodes *api.NodeList, nodeID string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func TestMinionStorageInvalidUpdate(t *testing.T) {
|
||||
func TestMinionRegistryInvalidUpdate(t *testing.T) {
|
||||
storage := NewREST(registrytest.NewMinionRegistry([]string{"foo", "bar"}, api.NodeResources{}))
|
||||
ctx := api.NewContext()
|
||||
obj, err := storage.Get(ctx, "foo")
|
||||
@@ -136,7 +136,7 @@ func TestMinionStorageInvalidUpdate(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMinionStorageValidUpdate(t *testing.T) {
|
||||
func TestMinionRegistryValidUpdate(t *testing.T) {
|
||||
storage := NewREST(registrytest.NewMinionRegistry([]string{"foo", "bar"}, api.NodeResources{}))
|
||||
ctx := api.NewContext()
|
||||
obj, err := storage.Get(ctx, "foo")
|
||||
@@ -156,7 +156,7 @@ func TestMinionStorageValidUpdate(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMinionStorageValidatesCreate(t *testing.T) {
|
||||
func TestMinionRegistryValidatesCreate(t *testing.T) {
|
||||
storage := NewREST(registrytest.NewMinionRegistry([]string{"foo", "bar"}, api.NodeResources{}))
|
||||
ctx := api.NewContext()
|
||||
validSelector := map[string]string{"a": "b"}
|
||||
|
Reference in New Issue
Block a user